Skip to content

Commit 3337f79

Browse files
committed
Auto merge of #5892 - matthiaskrgr:redundant_mut, r=flip1995
unnecessary-mut-passed: make lint message say if fn is a function or a method changelog: refine "unnecessary-mut-passed" lint message
2 parents 09bd400 + b8713e3 commit 3337f79

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

clippy_lints/src/mut_reference.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,28 @@ impl<'tcx> LateLintPass<'tcx> for UnnecessaryMutPassed {
3939
arguments,
4040
cx.typeck_results().expr_ty(fn_expr),
4141
&rustc_hir_pretty::to_string(rustc_hir_pretty::NO_ANN, |s| s.print_qpath(path, false)),
42+
"function",
4243
);
4344
}
4445
},
4546
ExprKind::MethodCall(ref path, _, ref arguments, _) => {
4647
let def_id = cx.typeck_results().type_dependent_def_id(e.hir_id).unwrap();
4748
let substs = cx.typeck_results().node_substs(e.hir_id);
4849
let method_type = cx.tcx.type_of(def_id).subst(cx.tcx, substs);
49-
check_arguments(cx, arguments, method_type, &path.ident.as_str())
50+
check_arguments(cx, arguments, method_type, &path.ident.as_str(), "method")
5051
},
5152
_ => (),
5253
}
5354
}
5455
}
5556

56-
fn check_arguments<'tcx>(cx: &LateContext<'tcx>, arguments: &[Expr<'_>], type_definition: Ty<'tcx>, name: &str) {
57+
fn check_arguments<'tcx>(
58+
cx: &LateContext<'tcx>,
59+
arguments: &[Expr<'_>],
60+
type_definition: Ty<'tcx>,
61+
name: &str,
62+
fn_kind: &str,
63+
) {
5764
match type_definition.kind {
5865
ty::FnDef(..) | ty::FnPtr(_) => {
5966
let parameters = type_definition.fn_sig(cx.tcx).skip_binder().inputs();
@@ -68,7 +75,7 @@ fn check_arguments<'tcx>(cx: &LateContext<'tcx>, arguments: &[Expr<'_>], type_de
6875
cx,
6976
UNNECESSARY_MUT_PASSED,
7077
argument.span,
71-
&format!("The function/method `{}` doesn't need a mutable reference", name),
78+
&format!("the {} `{}` doesn't need a mutable reference", fn_kind, name),
7279
);
7380
}
7481
},

tests/ui/mut_reference.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
error: The function/method `takes_an_immutable_reference` doesn't need a mutable reference
1+
error: the function `takes_an_immutable_reference` doesn't need a mutable reference
22
--> $DIR/mut_reference.rs:17:34
33
|
44
LL | takes_an_immutable_reference(&mut 42);
55
| ^^^^^^^
66
|
77
= note: `-D clippy::unnecessary-mut-passed` implied by `-D warnings`
88

9-
error: The function/method `as_ptr` doesn't need a mutable reference
9+
error: the function `as_ptr` doesn't need a mutable reference
1010
--> $DIR/mut_reference.rs:19:12
1111
|
1212
LL | as_ptr(&mut 42);
1313
| ^^^^^^^
1414

15-
error: The function/method `takes_an_immutable_reference` doesn't need a mutable reference
15+
error: the method `takes_an_immutable_reference` doesn't need a mutable reference
1616
--> $DIR/mut_reference.rs:23:44
1717
|
1818
LL | my_struct.takes_an_immutable_reference(&mut 42);

0 commit comments

Comments
 (0)