Skip to content

Commit 7ca8ff0

Browse files
committed
Also lint Box::new in the unused_allocation lint
Previously, only (box foo).something() would be linted, wher something takes &self or &mut self. Now, we also lint for Box::new(foo).something().
1 parent e030eb1 commit 7ca8ff0

File tree

5 files changed

+32
-10
lines changed

5 files changed

+32
-10
lines changed

compiler/rustc_lint/src/unused.rs

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,18 +1149,17 @@ declare_lint! {
11491149
/// ### Example
11501150
///
11511151
/// ```rust
1152-
/// #![feature(box_syntax)]
11531152
/// fn main() {
1154-
/// let a = (box [1, 2, 3]).len();
1153+
/// let a = (Box::new([1, 2, 3])).len();
11551154
/// }
11561155
/// ```
11571156
///
11581157
/// {{produces}}
11591158
///
11601159
/// ### Explanation
11611160
///
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`)
11641163
/// should be used instead to avoid the allocation.
11651164
pub(super) UNUSED_ALLOCATION,
11661165
Warn,
@@ -1171,14 +1170,35 @@ declare_lint_pass!(UnusedAllocation => [UNUSED_ALLOCATION]);
11711170

11721171
impl<'tcx> LateLintPass<'tcx> for UnusedAllocation {
11731172
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+
}
11761196
_ => return,
1177-
}
1197+
};
11781198

11791199
for adj in cx.typeck_results().expr_adjustments(e) {
11801200
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| {
11821202
lint.build(match m {
11831203
adjustment::AutoBorrowMutability::Not => fluent::lint::unused_allocation,
11841204
adjustment::AutoBorrowMutability::Mut { .. } => {

src/test/ui/iterators/into-iter-on-arrays-lint.fixed

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// run-rustfix
33
// rustfix-only-machine-applicable
44

5-
#[allow(unused_must_use)]
5+
#[allow(unused_must_use, unused_allocation)]
66
fn main() {
77
let small = [1, 2];
88
let big = [0u8; 33];

src/test/ui/iterators/into-iter-on-arrays-lint.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// run-rustfix
33
// rustfix-only-machine-applicable
44

5-
#[allow(unused_must_use)]
5+
#[allow(unused_must_use, unused_allocation)]
66
fn main() {
77
let small = [1, 2];
88
let big = [0u8; 33];

src/test/ui/self/arbitrary_self_types_trait.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// run-pass
2+
#![allow(unused_allocation)]
23

34
use std::rc::Rc;
45

src/test/ui/structs-enums/align-struct.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// run-pass
22
#![allow(dead_code)]
3+
#![allow(unused_allocation)]
34

45
use std::mem;
56

0 commit comments

Comments
 (0)