-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
feat(new lint): new lint manual_retain
#8972
Conversation
r? @llogiq (rust-highfive has picked a reviewer for you, use r? to override) |
1f94ae4
to
983c443
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks pretty good. I only have some small-ish nits regarding code style and one suggestion for extending the test. With those out of the way this is good to merge.
clippy_lints/src/use_retain.rs
Outdated
/// ``` | ||
#[clippy::version = "1.63.0"] | ||
pub USE_RETAIN, | ||
style, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this is a question of style
. I'd place it in either performance
(because it helps avoid allocating a new container) or complexity
(because it simplifies the code) with a small preference to the former.
clippy_lints/src/use_retain.rs
Outdated
|
||
impl<'tcx> LateLintPass<'tcx> for UseRetain { | ||
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) { | ||
if_chain! { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can now use "let chains". That is we can if let ... && let ... && foo ...
without any macro. Just replace the second and following if
s by &&
, lose the ;
in the end and the then
and it should work.
clippy_lints/src/use_retain.rs
Outdated
left_expr: &hir::Expr<'_>, | ||
target_expr: &hir::Expr<'_>, | ||
) { | ||
if_chain! { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Feel free to also use let chains here.
clippy_lints/src/use_retain.rs
Outdated
left_expr: &hir::Expr<'_>, | ||
target_expr: &hir::Expr<'_>, | ||
) { | ||
if_chain! { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and here.
clippy_lints/src/use_retain.rs
Outdated
left_expr: &hir::Expr<'_>, | ||
target_expr: &hir::Expr<'_>, | ||
) { | ||
if_chain! { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need I say more? 😄
clippy_lints/src/use_retain.rs
Outdated
} | ||
|
||
fn suggest(cx: &LateContext<'_>, parent_expr: &hir::Expr<'_>, left_expr: &hir::Expr<'_>, filter_expr: &hir::Expr<'_>) { | ||
if_chain! { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another one
clippy_lints/src/use_retain.rs
Outdated
return ACCEPTABLE_METHODS | ||
.iter() | ||
.any(|&method| match_def_path(cx, collect_def_id, method)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return ACCEPTABLE_METHODS | |
.iter() | |
.any(|&method| match_def_path(cx, collect_def_id, method)); | |
ACCEPTABLE_METHODS | |
.iter() | |
.any(|&method| match_def_path(cx, collect_def_id, method)) |
Why wasn't this caught by needless-return
!?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I made the bug ticket, I'll check when I have time.
clippy_lints/src/use_retain.rs
Outdated
return ACCEPTABLE_TYPES | ||
.iter() | ||
.any(|&ty| is_type_diagnostic_item(cx, expr_ty, ty)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return ACCEPTABLE_TYPES | |
.iter() | |
.any(|&ty| is_type_diagnostic_item(cx, expr_ty, ty)); | |
ACCEPTABLE_TYPES | |
.iter() | |
.any(|&ty| is_type_diagnostic_item(cx, expr_ty, ty)) |
same here
tests/ui/use_retain.rs
Outdated
// https://github.com/rust-lang/rust/issues/71503 | ||
let mut heap = BinaryHeap::from([1, 2, 3]); | ||
heap = heap.into_iter().filter(|x| x % 2 == 0).collect(); | ||
heap = heap.iter().filter(|&x| x % 2 == 0).copied().collect(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps also one case with .cloned()
instead of .copied()
(allowing the respective lint of course)?
The name does follow the naming guidelines. It should probably be called something like 'manual_retain' or 'slow retain'. -- I ran this on the top 1000 (more or less) crates of crates.io. I got one warning and the fix worked so that is looking good.
|
yes, I think
Thanks for this too! |
I added some commits for your suggestions and I changed the lint name to manual_retain. In addition to, I noticed that this lint needs to check msrv, so I added this commit. Please let me know if we need |
☔ The latest upstream changes (presumably #8964) made this pull request unmergeable. Please resolve the merge conflicts. |
☔ The latest upstream changes (presumably #8989) made this pull request unmergeable. Please resolve the merge conflicts. |
☔ The latest upstream changes (presumably #9031) made this pull request unmergeable. Please resolve the merge conflicts. |
use_retain
manual_retain
Sorry, I had a lot to do this week, so I didn't get to review stuff. This looks good and can be merged after a rebase. |
Co-authored-by: llogiq <[email protected]>
I rebased latest master. Do We need a git squash?
I don't mind. Thanks again for your reviews :) |
@bors r+ |
📌 Commit 676af45 has been approved by |
☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test |
close #8097
This PR is a new lint implementation.
This lint checks if the
retain
method is available.Thank you in advance.
changelog: add new
[`manual_retain`]
lint