-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #10528 - bluthej:clear-with-drain, r=llogiq
Clear with drain changelog: [`clear_with_drain`]: Add new lint Fixes #9339
- Loading branch information
Showing
9 changed files
with
346 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use clippy_utils::is_range_full; | ||
use clippy_utils::ty::is_type_diagnostic_item; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::{Expr, ExprKind, QPath}; | ||
use rustc_lint::LateContext; | ||
use rustc_span::symbol::sym; | ||
use rustc_span::Span; | ||
|
||
use super::CLEAR_WITH_DRAIN; | ||
|
||
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, recv: &Expr<'_>, span: Span, arg: &Expr<'_>) { | ||
let ty = cx.typeck_results().expr_ty(recv); | ||
if is_type_diagnostic_item(cx, ty, sym::Vec) | ||
&& let ExprKind::Path(QPath::Resolved(None, container_path)) = recv.kind | ||
&& is_range_full(cx, arg, Some(container_path)) | ||
{ | ||
span_lint_and_sugg( | ||
cx, | ||
CLEAR_WITH_DRAIN, | ||
span.with_hi(expr.span.hi()), | ||
"`drain` used to clear a `Vec`", | ||
"try", | ||
"clear()".to_string(), | ||
Applicability::MachineApplicable, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// run-rustfix | ||
#![allow(unused)] | ||
#![warn(clippy::clear_with_drain)] | ||
|
||
fn range() { | ||
let mut v = vec![1, 2, 3]; | ||
let iter = v.drain(0..v.len()); // Yay | ||
|
||
let mut v = vec![1, 2, 3]; | ||
let n = v.drain(0..v.len()).count(); // Yay | ||
|
||
let mut v = vec![1, 2, 3]; | ||
let iter = v.drain(usize::MIN..v.len()); // Yay | ||
let n = iter.count(); | ||
|
||
let mut v = vec![1, 2, 3]; | ||
v.clear(); // Nay | ||
|
||
let mut v = vec![1, 2, 3]; | ||
v.clear(); // Nay | ||
} | ||
|
||
fn range_from() { | ||
let mut v = vec![1, 2, 3]; | ||
let iter = v.drain(0..); // Yay | ||
|
||
let mut v = vec![1, 2, 3]; | ||
let mut iter = v.drain(0..); // Yay | ||
let next = iter.next(); | ||
|
||
let mut v = vec![1, 2, 3]; | ||
let next = v.drain(usize::MIN..).next(); // Yay | ||
|
||
let mut v = vec![1, 2, 3]; | ||
v.clear(); // Nay | ||
|
||
let mut v = vec![1, 2, 3]; | ||
v.clear(); // Nay | ||
} | ||
|
||
fn range_full() { | ||
let mut v = vec![1, 2, 3]; | ||
let iter = v.drain(..); // Yay | ||
|
||
let mut v = vec![1, 2, 3]; | ||
// Yay | ||
for x in v.drain(..) { | ||
let y = format!("x = {x}"); | ||
} | ||
|
||
let mut v = vec![1, 2, 3]; | ||
v.clear(); // Nay | ||
} | ||
|
||
fn range_to() { | ||
let mut v = vec![1, 2, 3]; | ||
let iter = v.drain(..v.len()); // Yay | ||
|
||
let mut v = vec![1, 2, 3]; | ||
let iter = v.drain(..v.len()); // Yay | ||
for x in iter { | ||
let y = format!("x = {x}"); | ||
} | ||
|
||
let mut v = vec![1, 2, 3]; | ||
v.clear(); // Nay | ||
} | ||
|
||
fn partial_drains() { | ||
let mut v = vec![1, 2, 3]; | ||
v.drain(1..); // Yay | ||
let mut v = vec![1, 2, 3]; | ||
v.drain(1..).max(); // Yay | ||
|
||
let mut v = vec![1, 2, 3]; | ||
v.drain(..v.len() - 1); // Yay | ||
let mut v = vec![1, 2, 3]; | ||
v.drain(..v.len() - 1).min(); // Yay | ||
|
||
let mut v = vec![1, 2, 3]; | ||
v.drain(1..v.len() - 1); // Yay | ||
let mut v = vec![1, 2, 3]; | ||
let w: Vec<i8> = v.drain(1..v.len() - 1).collect(); // Yay | ||
} | ||
|
||
fn main() {} |
Oops, something went wrong.