Skip to content

Commit f08a783

Browse files
committed
redundant_clone: split iterator checks into redundant_iter_cloned lint
1 parent dcc7906 commit f08a783

File tree

7 files changed

+44
-16
lines changed

7 files changed

+44
-16
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6304,6 +6304,7 @@ Released 2018-09-13
63046304
[`redundant_feature_names`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_feature_names
63056305
[`redundant_field_names`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_field_names
63066306
[`redundant_guards`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_guards
6307+
[`redundant_iter_cloned`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_iter_cloned
63076308
[`redundant_locals`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_locals
63086309
[`redundant_pattern`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_pattern
63096310
[`redundant_pattern_matching`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_pattern_matching

clippy_lints/src/declared_lints.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,7 @@ pub static LINTS: &[&::declare_clippy_lint::LintInfo] = &[
449449
crate::methods::READONLY_WRITE_LOCK_INFO,
450450
crate::methods::READ_LINE_WITHOUT_TRIM_INFO,
451451
crate::methods::REDUNDANT_AS_STR_INFO,
452+
crate::methods::REDUNDANT_ITER_CLONED_INFO,
452453
crate::methods::REPEAT_ONCE_INFO,
453454
crate::methods::RESULT_FILTER_MAP_INFO,
454455
crate::methods::RESULT_MAP_OR_INTO_OPTION_INFO,

clippy_lints/src/methods/iter_overeager_cloned.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_middle::ty::{self, BorrowKind};
1111
use rustc_span::{Symbol, sym};
1212

1313
use super::ITER_OVEREAGER_CLONED;
14-
use crate::redundant_clone::REDUNDANT_CLONE;
14+
use super::REDUNDANT_ITER_CLONED;
1515

1616
#[derive(Clone, Copy)]
1717
pub(super) enum Op<'a> {
@@ -96,7 +96,7 @@ pub(super) fn check<'tcx>(
9696
}
9797

9898
let (lint, msg, trailing_clone) = match op {
99-
Op::RmCloned | Op::NeedlessMove(_) => (REDUNDANT_CLONE, "unneeded cloning of iterator items", ""),
99+
Op::RmCloned | Op::NeedlessMove(_) => (REDUNDANT_ITER_CLONED, "unneeded cloning of iterator items", ""),
100100
Op::LaterCloned | Op::FixClosure(_, _) => (
101101
ITER_OVEREAGER_CLONED,
102102
"unnecessarily eager cloning of iterator items",

clippy_lints/src/methods/mod.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4565,6 +4565,31 @@ declare_clippy_lint! {
45654565
"hardcoded localhost IP address"
45664566
}
45674567

4568+
declare_clippy_lint! {
4569+
/// ### What it does
4570+
/// Checks for calls to `Iterator::cloned` where the original value could be used
4571+
/// instead.
4572+
///
4573+
/// ### Why is this bad?
4574+
/// It is not always possible for the compiler to eliminate useless allocations and
4575+
/// deallocations generated by redundant `clone()`s.
4576+
///
4577+
/// ### Example
4578+
/// ```no_run
4579+
/// let x = vec![String::new()];
4580+
/// let _ = x.iter().cloned().map(|x| x.len());
4581+
/// ```
4582+
/// Use instead:
4583+
/// ```no_run
4584+
/// let x = vec![String::new()];
4585+
/// let _ = x.iter().map(|x| x.len());
4586+
/// ```
4587+
#[clippy::version = "1.90.0"]
4588+
pub REDUNDANT_ITER_CLONED,
4589+
perf,
4590+
"detects redundant calls to `Iterator::cloned`"
4591+
}
4592+
45684593
#[expect(clippy::struct_excessive_bools)]
45694594
pub struct Methods {
45704595
avoid_breaking_exported_api: bool,
@@ -4744,6 +4769,7 @@ impl_lint_pass!(Methods => [
47444769
IO_OTHER_ERROR,
47454770
SWAP_WITH_TEMPORARY,
47464771
IP_CONSTANT,
4772+
REDUNDANT_ITER_CLONED,
47474773
]);
47484774

47494775
/// Extracts a method call name, args, and `Span` of the method name.

tests/ui/iter_overeager_cloned.fixed

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![warn(clippy::iter_overeager_cloned, clippy::redundant_clone, clippy::filter_next)]
1+
#![warn(clippy::iter_overeager_cloned, clippy::redundant_iter_cloned, clippy::filter_next)]
22
#![allow(
33
dead_code,
44
clippy::let_unit_value,
@@ -16,7 +16,7 @@ fn main() {
1616
//~^ iter_overeager_cloned
1717

1818
let _: usize = vec.iter().filter(|x| x == &"2").count();
19-
//~^ redundant_clone
19+
//~^ redundant_iter_cloned
2020

2121
let _: Vec<_> = vec.iter().take(2).cloned().collect();
2222
//~^ iter_overeager_cloned
@@ -77,19 +77,19 @@ fn main() {
7777
}
7878

7979
let _ = vec.iter().map(|x| x.len());
80-
//~^ redundant_clone
80+
//~^ redundant_iter_cloned
8181

8282
// This would fail if changed.
8383
let _ = vec.iter().cloned().map(|x| x + "2");
8484

8585
let _ = vec.iter().for_each(|x| assert!(!x.is_empty()));
86-
//~^ redundant_clone
86+
//~^ redundant_iter_cloned
8787

8888
let _ = vec.iter().all(|x| x.len() == 1);
89-
//~^ redundant_clone
89+
//~^ redundant_iter_cloned
9090

9191
let _ = vec.iter().any(|x| x.len() == 1);
92-
//~^ redundant_clone
92+
//~^ redundant_iter_cloned
9393

9494
// Should probably stay as it is.
9595
let _ = [0, 1, 2, 3, 4].iter().cloned().take(10);

tests/ui/iter_overeager_cloned.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![warn(clippy::iter_overeager_cloned, clippy::redundant_clone, clippy::filter_next)]
1+
#![warn(clippy::iter_overeager_cloned, clippy::redundant_iter_cloned, clippy::filter_next)]
22
#![allow(
33
dead_code,
44
clippy::let_unit_value,
@@ -16,7 +16,7 @@ fn main() {
1616
//~^ iter_overeager_cloned
1717

1818
let _: usize = vec.iter().filter(|x| x == &"2").cloned().count();
19-
//~^ redundant_clone
19+
//~^ redundant_iter_cloned
2020

2121
let _: Vec<_> = vec.iter().cloned().take(2).collect();
2222
//~^ iter_overeager_cloned
@@ -78,19 +78,19 @@ fn main() {
7878
}
7979

8080
let _ = vec.iter().cloned().map(|x| x.len());
81-
//~^ redundant_clone
81+
//~^ redundant_iter_cloned
8282

8383
// This would fail if changed.
8484
let _ = vec.iter().cloned().map(|x| x + "2");
8585

8686
let _ = vec.iter().cloned().for_each(|x| assert!(!x.is_empty()));
87-
//~^ redundant_clone
87+
//~^ redundant_iter_cloned
8888

8989
let _ = vec.iter().cloned().all(|x| x.len() == 1);
90-
//~^ redundant_clone
90+
//~^ redundant_iter_cloned
9191

9292
let _ = vec.iter().cloned().any(|x| x.len() == 1);
93-
//~^ redundant_clone
93+
//~^ redundant_iter_cloned
9494

9595
// Should probably stay as it is.
9696
let _ = [0, 1, 2, 3, 4].iter().cloned().take(10);

tests/ui/iter_overeager_cloned.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ LL | let _: usize = vec.iter().filter(|x| x == &"2").cloned().count();
2525
| |
2626
| help: try: `.count()`
2727
|
28-
= note: `-D clippy::redundant-clone` implied by `-D warnings`
29-
= help: to override `-D warnings` add `#[allow(clippy::redundant_clone)]`
28+
= note: `-D clippy::redundant-iter-cloned` implied by `-D warnings`
29+
= help: to override `-D warnings` add `#[allow(clippy::redundant_iter_cloned)]`
3030

3131
error: unnecessarily eager cloning of iterator items
3232
--> tests/ui/iter_overeager_cloned.rs:21:21

0 commit comments

Comments
 (0)