Skip to content

Commit 4b109ed

Browse files
authored
redundant_clone: split iterator checks into redundant_iter_cloned (#15277)
Needed to split the lints crate. changelog: split `redundant_clone` iterator checks into `redundant_iter_cloned` <!-- TRIAGEBOT_START --> <!-- TRIAGEBOT_SUMMARY_START --> ### Summary Notes - [Feature-freeze](#15277 (comment)) by [github-actions[bot]](https://github.com/github-actions[bot]) *Managed by `@rustbot`—see [help](https://forge.rust-lang.org/triagebot/note.html) for details* <!-- TRIAGEBOT_SUMMARY_END --> <!-- TRIAGEBOT_END -->
2 parents 4a8b7ea + 81e8c67 commit 4b109ed

File tree

7 files changed

+44
-17
lines changed

7 files changed

+44
-17
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6553,6 +6553,7 @@ Released 2018-09-13
65536553
[`redundant_feature_names`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_feature_names
65546554
[`redundant_field_names`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_field_names
65556555
[`redundant_guards`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_guards
6556+
[`redundant_iter_cloned`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_iter_cloned
65566557
[`redundant_locals`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_locals
65576558
[`redundant_pattern`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_pattern
65586559
[`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
@@ -453,6 +453,7 @@ pub static LINTS: &[&::declare_clippy_lint::LintInfo] = &[
453453
crate::methods::READONLY_WRITE_LOCK_INFO,
454454
crate::methods::READ_LINE_WITHOUT_TRIM_INFO,
455455
crate::methods::REDUNDANT_AS_STR_INFO,
456+
crate::methods::REDUNDANT_ITER_CLONED_INFO,
456457
crate::methods::REPEAT_ONCE_INFO,
457458
crate::methods::RESULT_FILTER_MAP_INFO,
458459
crate::methods::RESULT_MAP_OR_INTO_OPTION_INFO,

clippy_lints/src/methods/iter_overeager_cloned.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ use rustc_middle::mir::{FakeReadCause, Mutability};
1010
use rustc_middle::ty::{self, BorrowKind};
1111
use rustc_span::{Symbol, sym};
1212

13-
use super::ITER_OVEREAGER_CLONED;
14-
use crate::redundant_clone::REDUNDANT_CLONE;
13+
use super::{ITER_OVEREAGER_CLONED, REDUNDANT_ITER_CLONED};
1514

1615
#[derive(Clone, Copy)]
1716
pub(super) enum Op<'a> {
@@ -96,7 +95,7 @@ pub(super) fn check<'tcx>(
9695
}
9796

9897
let (lint, msg, trailing_clone) = match op {
99-
Op::RmCloned | Op::NeedlessMove(_) => (REDUNDANT_CLONE, "unneeded cloning of iterator items", ""),
98+
Op::RmCloned | Op::NeedlessMove(_) => (REDUNDANT_ITER_CLONED, "unneeded cloning of iterator items", ""),
10099
Op::LaterCloned | Op::FixClosure(_, _) => (
101100
ITER_OVEREAGER_CLONED,
102101
"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
@@ -4614,6 +4614,31 @@ declare_clippy_lint! {
46144614
"hardcoded localhost IP address"
46154615
}
46164616

4617+
declare_clippy_lint! {
4618+
/// ### What it does
4619+
/// Checks for calls to `Iterator::cloned` where the original value could be used
4620+
/// instead.
4621+
///
4622+
/// ### Why is this bad?
4623+
/// It is not always possible for the compiler to eliminate useless allocations and
4624+
/// deallocations generated by redundant `clone()`s.
4625+
///
4626+
/// ### Example
4627+
/// ```no_run
4628+
/// let x = vec![String::new()];
4629+
/// let _ = x.iter().cloned().map(|x| x.len());
4630+
/// ```
4631+
/// Use instead:
4632+
/// ```no_run
4633+
/// let x = vec![String::new()];
4634+
/// let _ = x.iter().map(|x| x.len());
4635+
/// ```
4636+
#[clippy::version = "1.90.0"]
4637+
pub REDUNDANT_ITER_CLONED,
4638+
perf,
4639+
"detects redundant calls to `Iterator::cloned`"
4640+
}
4641+
46174642
#[expect(clippy::struct_excessive_bools)]
46184643
pub struct Methods {
46194644
avoid_breaking_exported_api: bool,
@@ -4794,6 +4819,7 @@ impl_lint_pass!(Methods => [
47944819
IO_OTHER_ERROR,
47954820
SWAP_WITH_TEMPORARY,
47964821
IP_CONSTANT,
4822+
REDUNDANT_ITER_CLONED,
47974823
]);
47984824

47994825
/// 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)