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

add [`unnecessary_vec_drain] lint #9623

Closed
wants to merge 4 commits into from
Closed

Conversation

lana99
Copy link

@lana99 lana99 commented Oct 10, 2022

fixes #9339

detects usage of drain with RangeFull and dropped iterator

vec.drain(..)
vec.drain(0..vec.len())

will suggest to change to

vec.clear()

changelog: New lint: [unnecessary_vec_drain]
#9623

@rust-highfive
Copy link

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @flip1995 (or someone else) soon.

Please see the contribution instructions for more information.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Oct 10, 2022
@bors
Copy link
Contributor

bors commented Oct 16, 2022

☔ The latest upstream changes (presumably #9658) made this pull request unmergeable. Please resolve the merge conflicts.

Copy link
Member

@flip1995 flip1995 left a comment

Choose a reason for hiding this comment

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

Some code cleanup has to be done here.

/// ### Example
/// ```rust
/// let mut vec: Vec<i32> = Vec::new();
/// vec.drain(..);
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
/// vec.drain(..);
/// vec.drain(..);

/// vec.clear();
/// ```
#[clippy::version = "1.66.0"]
pub UNNECESSARY_VEC_DRAIN,
Copy link
Member

Choose a reason for hiding this comment

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

Please rename it to

Suggested change
pub UNNECESSARY_VEC_DRAIN,
pub NEEDLESS_VEC_DRAIN,

impl LateLintPass<'_> for UnnecessaryVecDrain {
fn check_stmt<'tcx>(&mut self, cx: &LateContext<'tcx>, stmt: &'tcx Stmt<'tcx>) {
if let hir::StmtKind::Semi(semi_expr) = &stmt.kind {
if let hir::ExprKind::MethodCall(path, rcvr, method_args,drain_span) = &semi_expr.kind
Copy link
Member

Choose a reason for hiding this comment

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

We abbreviate receiver with recv usually. Would be great if you could do this also in this lint impl.

&& path.ident.name == sym!(drain)
{
let ty = cx.typeck_results().expr_ty(rcvr);
if let [expr_element] = &**method_args
Copy link
Member

Choose a reason for hiding this comment

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

You can pull this into the check above and already match for the single argument in methods_args.

{
let ty = cx.typeck_results().expr_ty(rcvr);
if let [expr_element] = &**method_args
&& is_type_diagnostic_item(cx, ty, sym::Vec)
Copy link
Member

Choose a reason for hiding this comment

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

You're combining if let chains with the if_chain macro. Please use if let chains everywhere.

You can also use irrefutable patterns in if let chains, so this can be all in one chain up until here.

Comment on lines +73 to +76
if let Int(start,_) = lit.node
&& path_seg.ident.name == sym!(len)
&& owner_rcvr == owner_expr
&& start == 0
Copy link
Member

Choose a reason for hiding this comment

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

NIT: The order of these checks are a bit weird. This check should be done right after matching it to ExprKind::Lit.

Comment on lines +12 to +13
LL | vec.drain(0..vec.len());
| ^^^^^^^^^^^^^^^^^^^ help: consider calling `clear()`: `vec.clear()`
Copy link
Member

Choose a reason for hiding this comment

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

This suggestion won't work. The code after applying this suggestion would be vec.vec.clear(). You'll have to fix the span your using when producing this suggestion to also include vec.

@flip1995 flip1995 added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Oct 23, 2022
@bluthej
Copy link
Contributor

bluthej commented Mar 27, 2023

This can be closed since #10528 closed issue #9339

@blyxyas
Copy link
Member

blyxyas commented Oct 7, 2023

Closing this, as #9339 was fixed by #10528. @lana99 Thanks for trying! ❤️ ฅ^•ﻌ•^ฅ

@blyxyas blyxyas closed this Oct 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

new lint: vec.drain(..) instead of vec.clear()
7 participants