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
Changes from 1 commit
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
29 changes: 23 additions & 6 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 @@ -69,9 +81,14 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BoxedLocal {
return;
}

// Issue 4804 fix: don't warn if the method is a default trait impl
// 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
return;
match fn_decl.implicit_self {
ImplicitSelfKind::Imm
| ImplicitSelfKind::Mut => return,

_ => {}
}
}
}

Expand All @@ -90,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