Skip to content

Commit ee0598e

Browse files
committed
Auto merge of rust-lang#6571 - ThibsG:BoxedLocalTrait, r=phansch
Fix FP for `boxed_local` lint in default trait fn impl Fix FP on default trait function implementation on `boxed_local` lint. Maybe I checked too much when looking if `self` is carrying `Self` in its bound type. I can't find a good test case for this, so it could be too much conservative. Let me know if you think only detecting `self` parameter is enough. Fixes: rust-lang#4804 changelog: none
2 parents 68bcd20 + 8a6fea4 commit ee0598e

File tree

3 files changed

+62
-5
lines changed

3 files changed

+62
-5
lines changed

clippy_lints/src/escape.rs

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
use rustc_hir::intravisit;
2-
use rustc_hir::{self, Body, FnDecl, HirId, HirIdSet, ItemKind, Node};
2+
use rustc_hir::{self, AssocItemKind, Body, FnDecl, HirId, HirIdSet, ItemKind, Node};
33
use rustc_infer::infer::TyCtxtInferExt;
44
use rustc_lint::{LateContext, LateLintPass};
5-
use rustc_middle::ty::{self, Ty};
5+
use rustc_middle::ty::{self, TraitRef, Ty};
66
use rustc_session::{declare_tool_lint, impl_lint_pass};
77
use rustc_span::source_map::Span;
8+
use rustc_span::symbol::kw;
89
use rustc_target::abi::LayoutOf;
910
use rustc_target::spec::abi::Abi;
1011
use rustc_typeck::expr_use_visitor::{ConsumeMode, Delegate, ExprUseVisitor, PlaceBase, PlaceWithHirId};
1112

12-
use crate::utils::span_lint;
13+
use crate::utils::{contains_ty, span_lint};
1314

1415
#[derive(Copy, Clone)]
1516
pub struct BoxedLocal {
@@ -51,6 +52,7 @@ fn is_non_trait_box(ty: Ty<'_>) -> bool {
5152
struct EscapeDelegate<'a, 'tcx> {
5253
cx: &'a LateContext<'tcx>,
5354
set: HirIdSet,
55+
trait_self_ty: Option<Ty<'a>>,
5456
too_large_for_stack: u64,
5557
}
5658

@@ -72,19 +74,34 @@ impl<'tcx> LateLintPass<'tcx> for BoxedLocal {
7274
}
7375
}
7476

75-
// If the method is an impl for a trait, don't warn.
7677
let parent_id = cx.tcx.hir().get_parent_item(hir_id);
7778
let parent_node = cx.tcx.hir().find(parent_id);
7879

80+
let mut trait_self_ty = None;
7981
if let Some(Node::Item(item)) = parent_node {
82+
// If the method is an impl for a trait, don't warn.
8083
if let ItemKind::Impl { of_trait: Some(_), .. } = item.kind {
8184
return;
8285
}
86+
87+
// find `self` ty for this trait if relevant
88+
if let ItemKind::Trait(_, _, _, _, items) = item.kind {
89+
for trait_item in items {
90+
if trait_item.id.hir_id == hir_id {
91+
// be sure we have `self` parameter in this function
92+
if let AssocItemKind::Fn { has_self: true } = trait_item.kind {
93+
trait_self_ty =
94+
Some(TraitRef::identity(cx.tcx, trait_item.id.hir_id.owner.to_def_id()).self_ty());
95+
}
96+
}
97+
}
98+
}
8399
}
84100

85101
let mut v = EscapeDelegate {
86102
cx,
87103
set: HirIdSet::default(),
104+
trait_self_ty,
88105
too_large_for_stack: self.too_large_for_stack,
89106
};
90107

@@ -153,6 +170,14 @@ impl<'a, 'tcx> Delegate<'tcx> for EscapeDelegate<'a, 'tcx> {
153170
return;
154171
}
155172

173+
// skip if there is a `self` parameter binding to a type
174+
// that contains `Self` (i.e.: `self: Box<Self>`), see #4804
175+
if let Some(trait_self_ty) = self.trait_self_ty {
176+
if map.name(cmt.hir_id) == kw::SelfLower && contains_ty(cmt.place.ty(), trait_self_ty) {
177+
return;
178+
}
179+
}
180+
156181
if is_non_trait_box(cmt.place.ty()) && !self.is_large_box(cmt.place.ty()) {
157182
self.set.insert(cmt.hir_id);
158183
}

tests/ui/escape_analysis.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,3 +182,23 @@ pub extern "C" fn do_not_warn_me(_c_pointer: Box<String>) -> () {}
182182

183183
#[rustfmt::skip] // Forces rustfmt to not add ABI
184184
pub extern fn do_not_warn_me_no_abi(_c_pointer: Box<String>) -> () {}
185+
186+
// Issue #4804 - default implementation in trait
187+
mod issue4804 {
188+
trait DefaultTraitImplTest {
189+
// don't warn on `self`
190+
fn default_impl(self: Box<Self>) -> u32 {
191+
5
192+
}
193+
194+
// warn on `x: Box<u32>`
195+
fn default_impl_x(self: Box<Self>, x: Box<u32>) -> u32 {
196+
4
197+
}
198+
}
199+
200+
trait WarnTrait {
201+
// warn on `x: Box<u32>`
202+
fn foo(x: Box<u32>) {}
203+
}
204+
}

tests/ui/escape_analysis.stderr

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,17 @@ error: local variable doesn't need to be boxed here
1212
LL | pub fn new(_needs_name: Box<PeekableSeekable<&()>>) -> () {}
1313
| ^^^^^^^^^^^
1414

15-
error: aborting due to 2 previous errors
15+
error: local variable doesn't need to be boxed here
16+
--> $DIR/escape_analysis.rs:195:44
17+
|
18+
LL | fn default_impl_x(self: Box<Self>, x: Box<u32>) -> u32 {
19+
| ^
20+
21+
error: local variable doesn't need to be boxed here
22+
--> $DIR/escape_analysis.rs:202:16
23+
|
24+
LL | fn foo(x: Box<u32>) {}
25+
| ^
26+
27+
error: aborting due to 4 previous errors
1628

0 commit comments

Comments
 (0)