Skip to content

Commit 6ae89c4

Browse files
authored
Merge pull request #3321 from 0ndorio/fix/1123_false_positive_on_boxed_local
Avoid linting `boxed_local` on trait implementations.
2 parents eb683e6 + 2d8b4f3 commit 6ae89c4

File tree

3 files changed

+56
-7
lines changed

3 files changed

+56
-7
lines changed

clippy_lints/src/escape.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ impl LintPass for Pass {
6565
}
6666

6767
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
68+
6869
fn check_fn(
6970
&mut self,
7071
cx: &LateContext<'a, 'tcx>,
@@ -74,13 +75,23 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
7475
_: Span,
7576
node_id: NodeId,
7677
) {
77-
let fn_def_id = cx.tcx.hir.local_def_id(node_id);
78+
// If the method is an impl for a trait, don't warn
79+
let parent_id = cx.tcx.hir.get_parent(node_id);
80+
let parent_node = cx.tcx.hir.find(parent_id);
81+
82+
if let Some(Node::Item(item)) = parent_node {
83+
if let ItemKind::Impl(_, _, _, _, Some(..), _, _) = item.node {
84+
return;
85+
}
86+
}
87+
7888
let mut v = EscapeDelegate {
7989
cx,
8090
set: NodeSet(),
8191
too_large_for_stack: self.too_large_for_stack,
8292
};
8393

94+
let fn_def_id = cx.tcx.hir.local_def_id(node_id);
8495
let region_scope_tree = &cx.tcx.region_scope_tree(fn_def_id);
8596
ExprUseVisitor::new(&mut v, cx.tcx, cx.param_env, region_scope_tree, cx.tables, None).consume_body(body);
8697

tests/ui/escape_analysis.rs

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@
77
// option. This file may not be copied, modified, or distributed
88
// except according to those terms.
99

10-
1110
#![feature(box_syntax)]
1211

13-
#![allow(warnings, clippy)]
14-
15-
#![warn(boxed_local)]
12+
#![allow(clippy::borrowed_box, clippy::needless_pass_by_value, clippy::unused_unit)]
13+
#![warn(clippy::boxed_local)]
1614

1715
#[derive(Clone)]
1816
struct A;
@@ -70,8 +68,7 @@ fn warn_pass() {
7068
}
7169

7270
fn nowarn_return() -> Box<A> {
73-
let fx = box A;
74-
fx // moved out, "escapes"
71+
box A // moved out, "escapes"
7572
}
7673

7774
fn nowarn_move() {
@@ -139,3 +136,28 @@ pub struct PeekableSeekable<I: Foo> {
139136

140137
pub fn new(_needs_name: Box<PeekableSeekable<&()>>) -> () {
141138
}
139+
140+
/// Regression for #916, #1123
141+
///
142+
/// This shouldn't warn for `boxed_local`as the implementation of a trait
143+
/// can't change much about the trait definition.
144+
trait BoxedAction {
145+
fn do_sth(self: Box<Self>);
146+
}
147+
148+
impl BoxedAction for u64 {
149+
fn do_sth(self: Box<Self>) {
150+
println!("{}", *self)
151+
}
152+
}
153+
154+
/// Regression for #1478
155+
///
156+
/// This shouldn't warn for `boxed_local`as self itself is a box type.
157+
trait MyTrait {
158+
fn do_sth(self);
159+
}
160+
161+
impl<T> MyTrait for Box<T> {
162+
fn do_sth(self) {}
163+
}

tests/ui/escape_analysis.stderr

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error: local variable doesn't need to be boxed here
2+
--> $DIR/escape_analysis.rs:45:13
3+
|
4+
45 | fn warn_arg(x: Box<A>) {
5+
| ^
6+
|
7+
= note: `-D clippy::boxed-local` implied by `-D warnings`
8+
9+
error: local variable doesn't need to be boxed here
10+
--> $DIR/escape_analysis.rs:137:12
11+
|
12+
137 | pub fn new(_needs_name: Box<PeekableSeekable<&()>>) -> () {
13+
| ^^^^^^^^^^^
14+
15+
error: aborting due to 2 previous errors
16+

0 commit comments

Comments
 (0)