Skip to content

Commit 0f72c02

Browse files
committed
make applicability Unspecified if the iterator is Copy
1 parent 73d40e2 commit 0f72c02

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

clippy_lints/src/methods/map_identity.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then};
22
use clippy_utils::source::snippet_with_applicability;
3-
use clippy_utils::ty::is_type_diagnostic_item;
3+
use clippy_utils::ty::{is_copy, is_type_diagnostic_item};
44
use clippy_utils::{is_expr_untyped_identity_function, is_mutable, is_trait_method, path_to_local_with_projections};
55
use rustc_errors::Applicability;
66
use rustc_hir::{self as hir, ExprKind, Node, PatKind};
@@ -28,7 +28,14 @@ pub(super) fn check(
2828
&& let Some(call_span) = expr.span.trim_start(caller.span)
2929
{
3030
let main_sugg = (call_span, String::new());
31-
let mut app = Applicability::MachineApplicable;
31+
let mut app = if is_copy(cx, caller_ty) {
32+
// there is technically a behavioral change here for `Copy` iterators, where
33+
// `iter.map(|x| x).next()` would mutate a temporary copy of the iterator and
34+
// changing it to `iter.next()` mutates iter directly
35+
Applicability::Unspecified
36+
} else {
37+
Applicability::MachineApplicable
38+
};
3239

3340
let needs_to_be_mutable = cx.typeck_results().expr_ty_adjusted(expr).is_mutable_ptr();
3441
if needs_to_be_mutable && !is_mutable(cx, caller) {

0 commit comments

Comments
 (0)