Skip to content

redundant_clone: split iterator checks into redundant_iter_cloned #15277

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6304,6 +6304,7 @@ Released 2018-09-13
[`redundant_feature_names`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_feature_names
[`redundant_field_names`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_field_names
[`redundant_guards`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_guards
[`redundant_iter_cloned`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_iter_cloned
[`redundant_locals`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_locals
[`redundant_pattern`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_pattern
[`redundant_pattern_matching`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_pattern_matching
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/declared_lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,7 @@ pub static LINTS: &[&::declare_clippy_lint::LintInfo] = &[
crate::methods::READONLY_WRITE_LOCK_INFO,
crate::methods::READ_LINE_WITHOUT_TRIM_INFO,
crate::methods::REDUNDANT_AS_STR_INFO,
crate::methods::REDUNDANT_ITER_CLONED_INFO,
crate::methods::REPEAT_ONCE_INFO,
crate::methods::RESULT_FILTER_MAP_INFO,
crate::methods::RESULT_MAP_OR_INTO_OPTION_INFO,
Expand Down
5 changes: 2 additions & 3 deletions clippy_lints/src/methods/iter_overeager_cloned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ use rustc_middle::mir::{FakeReadCause, Mutability};
use rustc_middle::ty::{self, BorrowKind};
use rustc_span::{Symbol, sym};

use super::ITER_OVEREAGER_CLONED;
use crate::redundant_clone::REDUNDANT_CLONE;
use super::{ITER_OVEREAGER_CLONED, REDUNDANT_ITER_CLONED};

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

let (lint, msg, trailing_clone) = match op {
Op::RmCloned | Op::NeedlessMove(_) => (REDUNDANT_CLONE, "unneeded cloning of iterator items", ""),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmmm we may need to perform some tomfoolery here, checking for which lint is active, and if only redundant_clone is, still lint it with a deprecation notice for a couple versions (If redundant_iter_cloned is active, we can directly report that and avoid redundant_clone altogether)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

redundant_clone is in the nursery right now since it's main part is very broken so I don't think we need to do anything special.

Op::RmCloned | Op::NeedlessMove(_) => (REDUNDANT_ITER_CLONED, "unneeded cloning of iterator items", ""),
Op::LaterCloned | Op::FixClosure(_, _) => (
ITER_OVEREAGER_CLONED,
"unnecessarily eager cloning of iterator items",
Expand Down
26 changes: 26 additions & 0 deletions clippy_lints/src/methods/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4565,6 +4565,31 @@ declare_clippy_lint! {
"hardcoded localhost IP address"
}

declare_clippy_lint! {
/// ### What it does
/// Checks for calls to `Iterator::cloned` where the original value could be used
/// instead.
///
/// ### Why is this bad?
/// It is not always possible for the compiler to eliminate useless allocations and
/// deallocations generated by redundant `clone()`s.
///
/// ### Example
/// ```no_run
/// let x = vec![String::new()];
/// let _ = x.iter().cloned().map(|x| x.len());
/// ```
/// Use instead:
/// ```no_run
/// let x = vec![String::new()];
/// let _ = x.iter().map(|x| x.len());
/// ```
#[clippy::version = "1.90.0"]
pub REDUNDANT_ITER_CLONED,
perf,
"detects redundant calls to `Iterator::cloned`"
}

#[expect(clippy::struct_excessive_bools)]
pub struct Methods {
avoid_breaking_exported_api: bool,
Expand Down Expand Up @@ -4744,6 +4769,7 @@ impl_lint_pass!(Methods => [
IO_OTHER_ERROR,
SWAP_WITH_TEMPORARY,
IP_CONSTANT,
REDUNDANT_ITER_CLONED,
]);

/// Extracts a method call name, args, and `Span` of the method name.
Expand Down
12 changes: 6 additions & 6 deletions tests/ui/iter_overeager_cloned.fixed
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![warn(clippy::iter_overeager_cloned, clippy::redundant_clone, clippy::filter_next)]
#![warn(clippy::iter_overeager_cloned, clippy::redundant_iter_cloned, clippy::filter_next)]
#![allow(
dead_code,
clippy::let_unit_value,
Expand All @@ -16,7 +16,7 @@ fn main() {
//~^ iter_overeager_cloned

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

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

let _ = vec.iter().map(|x| x.len());
//~^ redundant_clone
//~^ redundant_iter_cloned

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

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

let _ = vec.iter().all(|x| x.len() == 1);
//~^ redundant_clone
//~^ redundant_iter_cloned

let _ = vec.iter().any(|x| x.len() == 1);
//~^ redundant_clone
//~^ redundant_iter_cloned

// Should probably stay as it is.
let _ = [0, 1, 2, 3, 4].iter().cloned().take(10);
Expand Down
12 changes: 6 additions & 6 deletions tests/ui/iter_overeager_cloned.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![warn(clippy::iter_overeager_cloned, clippy::redundant_clone, clippy::filter_next)]
#![warn(clippy::iter_overeager_cloned, clippy::redundant_iter_cloned, clippy::filter_next)]
#![allow(
dead_code,
clippy::let_unit_value,
Expand All @@ -16,7 +16,7 @@ fn main() {
//~^ iter_overeager_cloned

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

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

let _ = vec.iter().cloned().map(|x| x.len());
//~^ redundant_clone
//~^ redundant_iter_cloned

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

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

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

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

// Should probably stay as it is.
let _ = [0, 1, 2, 3, 4].iter().cloned().take(10);
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/iter_overeager_cloned.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ LL | let _: usize = vec.iter().filter(|x| x == &"2").cloned().count();
| |
| help: try: `.count()`
|
= note: `-D clippy::redundant-clone` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::redundant_clone)]`
= note: `-D clippy::redundant-iter-cloned` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::redundant_iter_cloned)]`

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