-
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.
Add doc Add description iter_with_drain dogfood
- Loading branch information
Showing
14 changed files
with
194 additions
and
13 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
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,68 @@ | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use clippy_utils::is_integer_const; | ||
use clippy_utils::ty::is_type_diagnostic_item; | ||
use clippy_utils::{ | ||
higher::{self, Range}, | ||
SpanlessEq, | ||
}; | ||
use rustc_ast::ast::RangeLimits; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::{Expr, ExprKind, QPath}; | ||
use rustc_lint::LateContext; | ||
use rustc_span::symbol::{sym, Symbol}; | ||
use rustc_span::Span; | ||
|
||
use super::ITER_WITH_DRAIN; | ||
|
||
const DRAIN_TYPES: &[Symbol] = &[sym::Vec, sym::VecDeque]; | ||
|
||
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, recv: &Expr<'_>, span: Span, arg: &Expr<'_>) { | ||
let ty = cx.typeck_results().expr_ty(recv).peel_refs(); | ||
if DRAIN_TYPES.iter().any(|&sym| is_type_diagnostic_item(cx, ty, sym)) { | ||
if let Some(range) = higher::Range::hir(arg) { | ||
let left_full = match range { | ||
Range { start: Some(start), .. } if is_integer_const(cx, start, 0) => true, | ||
Range { start: None, .. } => true, | ||
_ => false, | ||
}; | ||
let full = left_full | ||
&& match range { | ||
Range { | ||
end: Some(end), | ||
limits: RangeLimits::HalfOpen, | ||
.. | ||
} => { | ||
// `x.drain(..x.len())` call | ||
if_chain! { | ||
// should we delete the drain_path? | ||
if let ExprKind::MethodCall(_drain_path, drain_args, _) = expr.kind; | ||
if let ExprKind::MethodCall(len_path, len_args, _) = end.kind; | ||
if len_path.ident.name == sym::len && len_args.len() == 1; | ||
if let ExprKind::Path(QPath::Resolved(_, drain_path)) = drain_args[0].kind; | ||
if let ExprKind::Path(QPath::Resolved(_, len_path)) = len_args[0].kind; | ||
if SpanlessEq::new(cx).eq_path_segments(drain_path.segments, len_path.segments); | ||
then { true } | ||
else { false } | ||
} | ||
}, | ||
Range { | ||
end: None, | ||
limits: RangeLimits::HalfOpen, | ||
.. | ||
} => true, | ||
_ => false, | ||
}; | ||
if full { | ||
span_lint_and_sugg( | ||
cx, | ||
ITER_WITH_DRAIN, | ||
span.with_hi(expr.span.hi()), | ||
"use `into_iter` instead of `drain` for iterating all elements by value in a container", | ||
"try this", | ||
"into_iter()".to_string(), | ||
Applicability::MaybeIncorrect, | ||
); | ||
} | ||
} | ||
} | ||
} |
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
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,41 @@ | ||
#![warn(clippy::iter_with_drain)] | ||
use std::collections::{BinaryHeap, HashMap, HashSet, VecDeque}; | ||
|
||
fn full() { | ||
let mut a = vec!["aaa".to_string(), "bbb".to_string()]; | ||
let mut a: BinaryHeap<_> = a.drain(..).collect(); | ||
let mut a: HashSet<_> = a.drain().collect(); | ||
let mut a: VecDeque<_> = a.drain().collect(); | ||
let mut a: Vec<_> = a.drain(..).collect(); | ||
let mut a: HashMap<_, _> = a.drain(..).map(|x| (x.clone(), x)).collect(); | ||
let _: Vec<(String, String)> = a.drain().collect(); | ||
} | ||
|
||
fn closed() { | ||
let mut a = vec!["aaa".to_string(), "bbb".to_string()]; | ||
let mut a: BinaryHeap<_> = a.drain(0..).collect(); | ||
let mut a: HashSet<_> = a.drain().collect(); | ||
let mut a: VecDeque<_> = a.drain().collect(); | ||
let mut a: Vec<_> = a.drain(..a.len()).collect(); | ||
let mut a: HashMap<_, _> = a.drain(0..a.len()).map(|x| (x.clone(), x)).collect(); | ||
let _: Vec<(String, String)> = a.drain().collect(); | ||
} | ||
|
||
fn should_not_help() { | ||
let mut a = vec!["aaa".to_string(), "bbb".to_string()]; | ||
let mut a: BinaryHeap<_> = a.drain(1..).collect(); | ||
let mut a: HashSet<_> = a.drain().collect(); | ||
let mut a: VecDeque<_> = a.drain().collect(); | ||
let mut a: Vec<_> = a.drain(..a.len() - 1).collect(); | ||
let mut a: HashMap<_, _> = a.drain(1..a.len() - 1).map(|x| (x.clone(), x)).collect(); | ||
let _: Vec<(String, String)> = a.drain().collect(); | ||
|
||
let mut b = vec!["aaa".to_string(), "bbb".to_string()]; | ||
let _: Vec<_> = b.drain(0..a.len()).collect(); | ||
} | ||
|
||
fn main() { | ||
full(); | ||
closed(); | ||
should_not_help(); | ||
} |
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,40 @@ | ||
error: use `into_iter` instead of `drain` for iterating all elements by value in a container | ||
--> $DIR/iter_with_drain.rs:6:34 | ||
| | ||
LL | let mut a: BinaryHeap<_> = a.drain(..).collect(); | ||
| ^^^^^^^^^ help: try this: `into_iter()` | ||
| | ||
= note: `-D clippy::iter-with-drain` implied by `-D warnings` | ||
|
||
error: use `into_iter` instead of `drain` for iterating all elements by value in a container | ||
--> $DIR/iter_with_drain.rs:9:27 | ||
| | ||
LL | let mut a: Vec<_> = a.drain(..).collect(); | ||
| ^^^^^^^^^ help: try this: `into_iter()` | ||
|
||
error: use `into_iter` instead of `drain` for iterating all elements by value in a container | ||
--> $DIR/iter_with_drain.rs:10:34 | ||
| | ||
LL | let mut a: HashMap<_, _> = a.drain(..).map(|x| (x.clone(), x)).collect(); | ||
| ^^^^^^^^^ help: try this: `into_iter()` | ||
|
||
error: use `into_iter` instead of `drain` for iterating all elements by value in a container | ||
--> $DIR/iter_with_drain.rs:16:34 | ||
| | ||
LL | let mut a: BinaryHeap<_> = a.drain(0..).collect(); | ||
| ^^^^^^^^^^ help: try this: `into_iter()` | ||
|
||
error: use `into_iter` instead of `drain` for iterating all elements by value in a container | ||
--> $DIR/iter_with_drain.rs:19:27 | ||
| | ||
LL | let mut a: Vec<_> = a.drain(..a.len()).collect(); | ||
| ^^^^^^^^^^^^^^^^ help: try this: `into_iter()` | ||
|
||
error: use `into_iter` instead of `drain` for iterating all elements by value in a container | ||
--> $DIR/iter_with_drain.rs:20:34 | ||
| | ||
LL | let mut a: HashMap<_, _> = a.drain(0..a.len()).map(|x| (x.clone(), x)).collect(); | ||
| ^^^^^^^^^^^^^^^^^ help: try this: `into_iter()` | ||
|
||
error: aborting due to 6 previous errors | ||
|
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
// run-rustfix | ||
|
||
#![allow(unused_parens)] | ||
|
||
#![allow(clippy::iter_with_drain)] | ||
fn f() -> usize { | ||
42 | ||
} | ||
|
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
// run-rustfix | ||
|
||
#![allow(unused_parens)] | ||
|
||
#![allow(clippy::iter_with_drain)] | ||
fn f() -> usize { | ||
42 | ||
} | ||
|