@@ -1149,18 +1149,17 @@ declare_lint! {
1149
1149
/// ### Example
1150
1150
///
1151
1151
/// ```rust
1152
- /// #![feature(box_syntax)]
1153
1152
/// fn main() {
1154
- /// let a = (box [1, 2, 3]).len();
1153
+ /// let a = (Box::new( [1, 2, 3]) ).len();
1155
1154
/// }
1156
1155
/// ```
1157
1156
///
1158
1157
/// {{produces}}
1159
1158
///
1160
1159
/// ### Explanation
1161
1160
///
1162
- /// When a `box ` expression is immediately coerced to a reference, then
1163
- /// the allocation is unnecessary, and a reference (using `&` or `&mut`)
1161
+ /// When a `Box::new() ` expression is immediately coerced to a reference,
1162
+ /// then the allocation is unnecessary, and a reference (using `&` or `&mut`)
1164
1163
/// should be used instead to avoid the allocation.
1165
1164
pub ( super ) UNUSED_ALLOCATION ,
1166
1165
Warn ,
@@ -1171,14 +1170,35 @@ declare_lint_pass!(UnusedAllocation => [UNUSED_ALLOCATION]);
1171
1170
1172
1171
impl < ' tcx > LateLintPass < ' tcx > for UnusedAllocation {
1173
1172
fn check_expr ( & mut self , cx : & LateContext < ' _ > , e : & hir:: Expr < ' _ > ) {
1174
- match e. kind {
1175
- hir:: ExprKind :: Box ( _) => { }
1173
+ let span = match e. kind {
1174
+ hir:: ExprKind :: Box ( _) => {
1175
+ // Ideally, we'd underline the `box` part of the expression here,
1176
+ // but at this point we have lost the span of the `box` keyword.
1177
+ // Constructing a span from the inner and the entire expression's
1178
+ // span won't work in many cases, so we just return the entire
1179
+ // span of the `box foo`.
1180
+ e. span
1181
+ }
1182
+ hir:: ExprKind :: Call ( ref callee, _) => {
1183
+ // Look for Box::new(foo)
1184
+ if let hir:: ExprKind :: Path ( ref qpath) = callee. kind &&
1185
+ // `Res::Local` indicates a closure
1186
+ let Res :: Def ( _, def_id) = cx. qpath_res ( qpath, callee. hir_id ) &&
1187
+ let Some ( owned_box_new_def_id) = cx. tcx . lang_items ( ) . owned_box_new ( ) &&
1188
+ def_id == owned_box_new_def_id
1189
+ {
1190
+ // We have a Box::new() call here
1191
+ callee. span
1192
+ } else {
1193
+ return
1194
+ }
1195
+ }
1176
1196
_ => return ,
1177
- }
1197
+ } ;
1178
1198
1179
1199
for adj in cx. typeck_results ( ) . expr_adjustments ( e) {
1180
1200
if let adjustment:: Adjust :: Borrow ( adjustment:: AutoBorrow :: Ref ( _, m) ) = adj. kind {
1181
- cx. struct_span_lint ( UNUSED_ALLOCATION , e . span , |lint| {
1201
+ cx. struct_span_lint ( UNUSED_ALLOCATION , span, |lint| {
1182
1202
lint. build ( match m {
1183
1203
adjustment:: AutoBorrowMutability :: Not => fluent:: lint:: unused_allocation,
1184
1204
adjustment:: AutoBorrowMutability :: Mut { .. } => {
0 commit comments