Skip to content

Fix ICE for iter_overeager_cloned #8602

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

Merged
merged 1 commit into from
Mar 30, 2022
Merged
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
16 changes: 12 additions & 4 deletions clippy_lints/src/methods/iter_overeager_cloned.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::source::snippet;
use clippy_utils::ty::{get_iterator_item_ty, is_copy};
use clippy_utils::ty::{get_iterator_item_ty, implements_trait, is_copy};
use itertools::Itertools;
use rustc_errors::Applicability;
use rustc_hir as hir;
use rustc_lint::LateContext;
use rustc_middle::ty;
use rustc_span::sym;
use std::ops::Not;

use super::ITER_OVEREAGER_CLONED;
Expand All @@ -20,9 +21,16 @@ pub(super) fn check<'tcx>(
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,
let inner_ty = if_chain! {
if let Some(iterator_trait_id) = cx.tcx.get_diagnostic_item(sym::Iterator);
let recv_ty = cx.typeck_results().expr_ty(recv);
if implements_trait(cx, recv_ty, iterator_trait_id, &[]);
if let Some(inner_ty) = get_iterator_item_ty(cx, cx.typeck_results().expr_ty_adjusted(recv));
then {
inner_ty
} else {
return;
}
};

match inner_ty.kind() {
Expand Down
6 changes: 6 additions & 0 deletions tests/ui/iter_overeager_cloned.fixed
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// run-rustfix
#![warn(clippy::iter_overeager_cloned, clippy::redundant_clone, clippy::filter_next)]
#![allow(dead_code)]

fn main() {
let vec = vec!["1".to_string(), "2".to_string(), "3".to_string()];
Expand Down Expand Up @@ -43,3 +44,8 @@ fn main() {
// Should probably stay as it is.
let _ = [0, 1, 2, 3, 4].iter().cloned().take(10);
}

// #8527
fn cloned_flatten(x: Option<&Option<String>>) -> Option<String> {
x.cloned().flatten()
}
6 changes: 6 additions & 0 deletions tests/ui/iter_overeager_cloned.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// run-rustfix
#![warn(clippy::iter_overeager_cloned, clippy::redundant_clone, clippy::filter_next)]
#![allow(dead_code)]

fn main() {
let vec = vec!["1".to_string(), "2".to_string(), "3".to_string()];
Expand Down Expand Up @@ -45,3 +46,8 @@ fn main() {
// Should probably stay as it is.
let _ = [0, 1, 2, 3, 4].iter().cloned().take(10);
}

// #8527
fn cloned_flatten(x: Option<&Option<String>>) -> Option<String> {
x.cloned().flatten()
}
14 changes: 7 additions & 7 deletions tests/ui/iter_overeager_cloned.stderr
Original file line number Diff line number Diff line change
@@ -1,45 +1,45 @@
error: called `cloned().last()` on an `Iterator`. It may be more efficient to call `last().cloned()` instead
--> $DIR/iter_overeager_cloned.rs:7:29
--> $DIR/iter_overeager_cloned.rs:8: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
--> $DIR/iter_overeager_cloned.rs:10: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
--> $DIR/iter_overeager_cloned.rs:12: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
--> $DIR/iter_overeager_cloned.rs:14: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
--> $DIR/iter_overeager_cloned.rs:16: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
--> $DIR/iter_overeager_cloned.rs:18: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
--> $DIR/iter_overeager_cloned.rs:20:13
|
LL | let _ = [Some(Some("str".to_string())), Some(Some("str".to_string()))]
| _____________^
Expand Down