Skip to content

Commit 4ae3029

Browse files
committed
say what exactly requires the variable to be mutable
1 parent 95fc598 commit 4ae3029

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

clippy_lints/src/methods/map_identity.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::ty::is_type_diagnostic_item;
33
use clippy_utils::{is_expr_untyped_identity_function, is_mutable, is_trait_method, path_to_local};
44
use rustc_errors::Applicability;
5-
use rustc_hir::{self as hir, Node, PatKind};
5+
use rustc_hir::{self as hir, ExprKind, Node, PatKind};
66
use rustc_lint::LateContext;
77
use rustc_span::{Span, Symbol, sym};
88

@@ -39,7 +39,13 @@ pub(super) fn check(
3939
}
4040
}
4141

42-
let method_requiring_mut = String::from("random_method"); // TODO
42+
let method_requiring_mut = if let Node::Expr(expr) = cx.tcx.parent_hir_node(expr.hir_id)
43+
&& let ExprKind::MethodCall(method, ..) = expr.kind
44+
{
45+
Some(method.ident)
46+
} else {
47+
None
48+
};
4349

4450
span_lint_and_then(
4551
cx,
@@ -57,10 +63,14 @@ pub(super) fn check(
5763
},
5864
);
5965
if !apply {
60-
diag.span_note(
61-
caller.span,
62-
format!("this must be made mutable to use `{method_requiring_mut}`"),
63-
);
66+
if let Some(method_requiring_mut) = method_requiring_mut {
67+
diag.span_note(
68+
caller.span,
69+
format!("this must be made mutable to use `{method_requiring_mut}`"),
70+
);
71+
} else {
72+
diag.span_note(caller.span, "this must be made mutable".to_string());
73+
}
6474
}
6575
},
6676
);

tests/ui/map_identity_unfixable.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: unnecessary map of the identity function
44
LL | let _ = subindex.0.map(|n| n).next();
55
| ^^^^^^^^^^^ help: remove the call to `map`
66
|
7-
note: this must be made mutable to use `random_method`
7+
note: this must be made mutable to use `next`
88
--> tests/ui/map_identity_unfixable.rs:7:13
99
|
1010
LL | let _ = subindex.0.map(|n| n).next();

0 commit comments

Comments
 (0)