forked from rust-lang/rust
-
-
Notifications
You must be signed in to change notification settings - Fork 2
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 rust-lang#8203 - pmnoxx:piotr-next-lint, r=llogiq
New lint: `iter_overeager_cloned` Closes rust-lang#8202 changelog: New lint: [`iter_overeager_cloned`]
- Loading branch information
Showing
9 changed files
with
285 additions
and
15 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,62 @@ | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use clippy_utils::source::snippet; | ||
use clippy_utils::ty::{get_iterator_item_ty, is_copy}; | ||
use itertools::Itertools; | ||
use rustc_errors::Applicability; | ||
use rustc_hir as hir; | ||
use rustc_lint::LateContext; | ||
use rustc_middle::ty; | ||
use std::ops::Not; | ||
|
||
use super::ITER_OVEREAGER_CLONED; | ||
use crate::redundant_clone::REDUNDANT_CLONE; | ||
|
||
/// lint overeager use of `cloned()` for `Iterator`s | ||
pub(super) fn check<'tcx>( | ||
cx: &LateContext<'tcx>, | ||
expr: &'tcx hir::Expr<'_>, | ||
recv: &'tcx hir::Expr<'_>, | ||
name: &str, | ||
map_arg: &[hir::Expr<'_>], | ||
) { | ||
// Check if it's iterator and get type associated with `Item`. | ||
let inner_ty = match get_iterator_item_ty(cx, cx.typeck_results().expr_ty_adjusted(recv)) { | ||
Some(ty) => ty, | ||
_ => return, | ||
}; | ||
|
||
match inner_ty.kind() { | ||
ty::Ref(_, ty, _) if !is_copy(cx, ty) => {}, | ||
_ => return, | ||
}; | ||
|
||
let (lint, preserve_cloned) = match name { | ||
"count" => (REDUNDANT_CLONE, false), | ||
_ => (ITER_OVEREAGER_CLONED, true), | ||
}; | ||
let wildcard_params = map_arg.is_empty().not().then(|| "...").unwrap_or_default(); | ||
let msg = format!( | ||
"called `cloned().{}({})` on an `Iterator`. It may be more efficient to call `{}({}){}` instead", | ||
name, | ||
wildcard_params, | ||
name, | ||
wildcard_params, | ||
preserve_cloned.then(|| ".cloned()").unwrap_or_default(), | ||
); | ||
|
||
span_lint_and_sugg( | ||
cx, | ||
lint, | ||
expr.span, | ||
&msg, | ||
"try this", | ||
format!( | ||
"{}.{}({}){}", | ||
snippet(cx, recv.span, ".."), | ||
name, | ||
map_arg.iter().map(|a| snippet(cx, a.span, "..")).join(", "), | ||
preserve_cloned.then(|| ".cloned()").unwrap_or_default(), | ||
), | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// run-rustfix | ||
#![warn(clippy::iter_overeager_cloned, clippy::redundant_clone, clippy::filter_next)] | ||
|
||
fn main() { | ||
let vec = vec!["1".to_string(), "2".to_string(), "3".to_string()]; | ||
|
||
let _: Option<String> = vec.iter().last().cloned(); | ||
|
||
let _: Option<String> = vec.iter().chain(vec.iter()).next().cloned(); | ||
|
||
let _: usize = vec.iter().filter(|x| x == &"2").count(); | ||
|
||
let _: Vec<_> = vec.iter().take(2).cloned().collect(); | ||
|
||
let _: Vec<_> = vec.iter().skip(2).cloned().collect(); | ||
|
||
let _ = vec.iter().filter(|x| x == &"2").nth(2).cloned(); | ||
|
||
let _ = [Some(Some("str".to_string())), Some(Some("str".to_string()))] | ||
.iter().flatten().cloned(); | ||
|
||
// Not implemented yet | ||
let _ = vec.iter().cloned().filter(|x| x.starts_with('2')); | ||
|
||
// Not implemented yet | ||
let _ = vec.iter().cloned().map(|x| x.len()); | ||
|
||
// This would fail if changed. | ||
let _ = vec.iter().cloned().map(|x| x + "2"); | ||
|
||
// Not implemented yet | ||
let _ = vec.iter().cloned().find(|x| x == "2"); | ||
|
||
// Not implemented yet | ||
let _ = vec.iter().cloned().for_each(|x| assert!(!x.is_empty())); | ||
|
||
// Not implemented yet | ||
let _ = vec.iter().cloned().all(|x| x.len() == 1); | ||
|
||
// Not implemented yet | ||
let _ = vec.iter().cloned().any(|x| x.len() == 1); | ||
|
||
// Should probably stay as it is. | ||
let _ = [0, 1, 2, 3, 4].iter().cloned().take(10); | ||
} |
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,47 @@ | ||
// run-rustfix | ||
#![warn(clippy::iter_overeager_cloned, clippy::redundant_clone, clippy::filter_next)] | ||
|
||
fn main() { | ||
let vec = vec!["1".to_string(), "2".to_string(), "3".to_string()]; | ||
|
||
let _: Option<String> = vec.iter().cloned().last(); | ||
|
||
let _: Option<String> = vec.iter().chain(vec.iter()).cloned().next(); | ||
|
||
let _: usize = vec.iter().filter(|x| x == &"2").cloned().count(); | ||
|
||
let _: Vec<_> = vec.iter().cloned().take(2).collect(); | ||
|
||
let _: Vec<_> = vec.iter().cloned().skip(2).collect(); | ||
|
||
let _ = vec.iter().filter(|x| x == &"2").cloned().nth(2); | ||
|
||
let _ = [Some(Some("str".to_string())), Some(Some("str".to_string()))] | ||
.iter() | ||
.cloned() | ||
.flatten(); | ||
|
||
// Not implemented yet | ||
let _ = vec.iter().cloned().filter(|x| x.starts_with('2')); | ||
|
||
// Not implemented yet | ||
let _ = vec.iter().cloned().map(|x| x.len()); | ||
|
||
// This would fail if changed. | ||
let _ = vec.iter().cloned().map(|x| x + "2"); | ||
|
||
// Not implemented yet | ||
let _ = vec.iter().cloned().find(|x| x == "2"); | ||
|
||
// Not implemented yet | ||
let _ = vec.iter().cloned().for_each(|x| assert!(!x.is_empty())); | ||
|
||
// Not implemented yet | ||
let _ = vec.iter().cloned().all(|x| x.len() == 1); | ||
|
||
// Not implemented yet | ||
let _ = vec.iter().cloned().any(|x| x.len() == 1); | ||
|
||
// Should probably stay as it is. | ||
let _ = [0, 1, 2, 3, 4].iter().cloned().take(10); | ||
} |
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,58 @@ | ||
error: called `cloned().last()` on an `Iterator`. It may be more efficient to call `last().cloned()` instead | ||
--> $DIR/iter_overeager_cloned.rs:7:29 | ||
| | ||
LL | let _: Option<String> = vec.iter().cloned().last(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `vec.iter().last().cloned()` | ||
| | ||
= note: `-D clippy::iter-overeager-cloned` implied by `-D warnings` | ||
|
||
error: called `cloned().next()` on an `Iterator`. It may be more efficient to call `next().cloned()` instead | ||
--> $DIR/iter_overeager_cloned.rs:9:29 | ||
| | ||
LL | let _: Option<String> = vec.iter().chain(vec.iter()).cloned().next(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `vec.iter().chain(vec.iter()).next().cloned()` | ||
|
||
error: called `cloned().count()` on an `Iterator`. It may be more efficient to call `count()` instead | ||
--> $DIR/iter_overeager_cloned.rs:11:20 | ||
| | ||
LL | let _: usize = vec.iter().filter(|x| x == &"2").cloned().count(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `vec.iter().filter(|x| x == &"2").count()` | ||
| | ||
= note: `-D clippy::redundant-clone` implied by `-D warnings` | ||
|
||
error: called `cloned().take(...)` on an `Iterator`. It may be more efficient to call `take(...).cloned()` instead | ||
--> $DIR/iter_overeager_cloned.rs:13:21 | ||
| | ||
LL | let _: Vec<_> = vec.iter().cloned().take(2).collect(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `vec.iter().take(2).cloned()` | ||
|
||
error: called `cloned().skip(...)` on an `Iterator`. It may be more efficient to call `skip(...).cloned()` instead | ||
--> $DIR/iter_overeager_cloned.rs:15:21 | ||
| | ||
LL | let _: Vec<_> = vec.iter().cloned().skip(2).collect(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `vec.iter().skip(2).cloned()` | ||
|
||
error: called `cloned().nth(...)` on an `Iterator`. It may be more efficient to call `nth(...).cloned()` instead | ||
--> $DIR/iter_overeager_cloned.rs:17:13 | ||
| | ||
LL | let _ = vec.iter().filter(|x| x == &"2").cloned().nth(2); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `vec.iter().filter(|x| x == &"2").nth(2).cloned()` | ||
|
||
error: called `cloned().flatten()` on an `Iterator`. It may be more efficient to call `flatten().cloned()` instead | ||
--> $DIR/iter_overeager_cloned.rs:19:13 | ||
| | ||
LL | let _ = [Some(Some("str".to_string())), Some(Some("str".to_string()))] | ||
| _____________^ | ||
LL | | .iter() | ||
LL | | .cloned() | ||
LL | | .flatten(); | ||
| |__________________^ | ||
| | ||
help: try this | ||
| | ||
LL ~ let _ = [Some(Some("str".to_string())), Some(Some("str".to_string()))] | ||
LL ~ .iter().flatten().cloned(); | ||
| | ||
|
||
error: aborting due to 7 previous errors | ||
|