Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return early if the method is a default trait impl for boxed_local #4833

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions clippy_lints/src/escape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub struct BoxedLocal {
}

declare_clippy_lint! {
/// **What it does:** Checks for usage of `Box<T>` where an unboxed `T` would
/// **What it does:** Checks for usage of `Box<T>` where an unboxed `T` or `&T` would
/// work fine.
///
/// **Why is this bad?** This is an unnecessary allocation, and bad for
Expand All @@ -30,8 +30,20 @@ declare_clippy_lint! {
/// ```rust
/// # fn foo(bar: usize) {}
/// let x = Box::new(1);
/// foo(*x);
/// println!("{}", *x);
///
/// fn foo(arg: Box<[u32]>) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That will test-fail. Please leave the foo(*x); and rename this to bar

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok

/// // code which doesn't move the box
/// }
/// ```
/// Should be written:
/// ```rust
/// let x = 1;
/// println!("{}", x);
///
/// fn foo(array: &[u32]) {
/// //...
/// }
/// ```
pub BOXED_LOCAL,
perf,
Expand All @@ -55,7 +67,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BoxedLocal {
&mut self,
cx: &LateContext<'a, 'tcx>,
_: visit::FnKind<'tcx>,
_: &'tcx FnDecl,
fn_decl: &'tcx FnDecl,
body: &'tcx Body,
_: Span,
hir_id: HirId,
Expand All @@ -68,6 +80,16 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BoxedLocal {
if let ItemKind::Impl(_, _, _, _, Some(..), _, _) = item.kind {
return;
}

// Issue 4804 fix: don't warn if the method is a default trait impl accepting "self"
if let ItemKind::Trait(_, _, _, _, _) = item.kind {
MentalAllergen marked this conversation as resolved.
Show resolved Hide resolved
match fn_decl.implicit_self {
ImplicitSelfKind::Imm
| ImplicitSelfKind::Mut => return,

_ => {}
}
}
}

let mut v = EscapeDelegate {
Expand All @@ -85,7 +107,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BoxedLocal {
cx,
BOXED_LOCAL,
cx.tcx.hir().span(node),
"local variable doesn't need to be boxed here",
"local variable doesn't need to be boxed here"
);
}
}
Expand Down
9 changes: 9 additions & 0 deletions tests/ui/escape_analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,15 @@ impl<T> MyTrait for Box<T> {
fn do_sth(self) {}
}

/// Fix for #4804
///
/// This shouldn't warn for `boxed_local` as traits don't have a know size in compile time
trait DefaultTraitImplTest {
fn default_impl(self: Box<Self>) -> u32 {
5
}
}
Comment on lines +160 to +164
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this enough to cover this fix? There might be some more niche cases which I hadn't encountered but which should be tested as well.


// Issue #3739 - capture in closures
mod issue_3739 {
use super::A;
Expand Down