-
Notifications
You must be signed in to change notification settings - Fork 1.7k
add [`unnecessary_vec_drain] lint #9623
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,96 @@ | ||||||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||||||
use clippy_utils::source::snippet_with_applicability; | ||||||
use clippy_utils::ty::{is_type_diagnostic_item, is_type_lang_item}; | ||||||
use rustc_ast::LitKind::Int; | ||||||
use rustc_errors::Applicability; | ||||||
use rustc_hir as hir; | ||||||
use rustc_hir::{LangItem, Stmt}; | ||||||
use rustc_lint::{LateContext, LateLintPass}; | ||||||
use rustc_session::{declare_lint_pass, declare_tool_lint}; | ||||||
use rustc_span::symbol::sym; | ||||||
|
||||||
declare_clippy_lint! { | ||||||
/// ### What it does | ||||||
/// Checks for usage of `vec.drain(..)` with RangeFull that is not bound to a value `let`. | ||||||
/// | ||||||
/// ### Why is this bad? | ||||||
/// This creates an iterator that is dropped immediately. | ||||||
/// | ||||||
/// `vec.clear()` also shows clearer intention. | ||||||
/// | ||||||
/// ### Example | ||||||
/// ```rust | ||||||
/// let mut vec: Vec<i32> = Vec::new(); | ||||||
/// vec.drain(..); | ||||||
/// ``` | ||||||
/// Use instead: | ||||||
/// ```rust | ||||||
/// let mut vec: Vec<i32> = Vec::new(); | ||||||
/// vec.clear(); | ||||||
/// ``` | ||||||
#[clippy::version = "1.66.0"] | ||||||
pub UNNECESSARY_VEC_DRAIN, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please rename it to
Suggested change
|
||||||
style, | ||||||
"unnecessary `vec.drain(..)`" | ||||||
} | ||||||
declare_lint_pass!(UnnecessaryVecDrain => [UNNECESSARY_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 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We abbreviate receiver with |
||||||
&& path.ident.name == sym!(drain) | ||||||
{ | ||||||
let ty = cx.typeck_results().expr_ty(rcvr); | ||||||
if let [expr_element] = &**method_args | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||||||
&& is_type_diagnostic_item(cx, ty, sym::Vec) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're combining You can also use irrefutable patterns in |
||||||
{ | ||||||
let ty = cx.typeck_results().expr_ty(expr_element); | ||||||
if is_type_lang_item(cx, ty, LangItem::RangeFull) | ||||||
{ | ||||||
let mut applicability = Applicability::MachineApplicable; | ||||||
span_lint_and_sugg( | ||||||
cx, | ||||||
UNNECESSARY_VEC_DRAIN, | ||||||
semi_expr.span, | ||||||
"unnecessary iterator `Drain` is created and dropped immedietly", | ||||||
"consider calling `clear()`", | ||||||
format!("{}.clear()", snippet_with_applicability(cx, rcvr.span, "", &mut applicability)), | ||||||
applicability | ||||||
); | ||||||
} | ||||||
if let hir::ExprKind::Struct(_, expr_fields, _) = &expr_element.kind | ||||||
&& is_type_lang_item(cx, ty, LangItem::Range) | ||||||
{ | ||||||
if_chain! { | ||||||
if let hir::ExprKind::Lit(lit) = &expr_fields[0].expr.kind; | ||||||
if let hir::ExprKind::MethodCall(path_seg, vec_expr, _, _) = expr_fields[1].expr.kind; | ||||||
|
||||||
if let hir::hir_id::HirId{owner: owner_expr,..} = &vec_expr.hir_id; | ||||||
if let hir::hir_id::HirId{owner: owner_rcvr,..} = &rcvr.hir_id; | ||||||
|
||||||
then { | ||||||
if let Int(start,_) = lit.node | ||||||
&& path_seg.ident.name == sym!(len) | ||||||
&& owner_rcvr == owner_expr | ||||||
&& start == 0 | ||||||
Comment on lines
+73
to
+76
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||||||
{ | ||||||
let mut applicability = Applicability::MachineApplicable; | ||||||
span_lint_and_sugg( | ||||||
cx, | ||||||
UNNECESSARY_VEC_DRAIN, | ||||||
*drain_span, | ||||||
"unnecessary iterator `Drain` is created and dropped immediately", | ||||||
"consider calling `clear()`", | ||||||
format!("{}.clear()", snippet_with_applicability(cx, rcvr.span, "", &mut applicability)), | ||||||
applicability | ||||||
); | ||||||
} | ||||||
} | ||||||
} | ||||||
} | ||||||
} | ||||||
} | ||||||
} | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
### What it does | ||
Checks for usage of `vec.drain(..)` with RangeFull that is not bound to a value `let`. | ||
|
||
### Why is this bad? | ||
This creates an iterator that is dropped immediately. | ||
|
||
`vec.clear()` also shows clearer intention. | ||
|
||
### Example | ||
``` | ||
let mut vec: Vec<i32> = Vec::new(); | ||
vec.drain(..); | ||
``` | ||
Use instead: | ||
``` | ||
let mut vec: Vec<i32> = Vec::new(); | ||
vec.clear(); | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#![allow(unused)] | ||
#![warn(clippy::unnecessary_vec_drain)] | ||
|
||
fn main() { | ||
let mut vec: Vec<i32> = Vec::new(); | ||
//Lint | ||
vec.drain(..); | ||
vec.drain(0..vec.len()); | ||
|
||
// Dont Lint | ||
let iter = vec.drain(..); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
error: unnecessary iterator `Drain` is created and dropped immedietly | ||
--> $DIR/unnecessary_vec_drain.rs:7:5 | ||
| | ||
LL | vec.drain(..); | ||
| ^^^^^^^^^^^^^ help: consider calling `clear()`: `vec.clear()` | ||
| | ||
= note: `-D clippy::unnecessary-vec-drain` implied by `-D warnings` | ||
|
||
error: unnecessary iterator `Drain` is created and dropped immediately | ||
--> $DIR/unnecessary_vec_drain.rs:8:9 | ||
| | ||
LL | vec.drain(0..vec.len()); | ||
| ^^^^^^^^^^^^^^^^^^^ help: consider calling `clear()`: `vec.clear()` | ||
Comment on lines
+12
to
+13
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
||
error: aborting due to 2 previous errors | ||
|
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.