Skip to content
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

unnecessary-mut-passed: make lint message say if fn is a function or a method #5892

Merged
merged 1 commit into from
Aug 11, 2020
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
13 changes: 10 additions & 3 deletions clippy_lints/src/mut_reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,28 @@ impl<'tcx> LateLintPass<'tcx> for UnnecessaryMutPassed {
arguments,
cx.typeck_results().expr_ty(fn_expr),
&rustc_hir_pretty::to_string(rustc_hir_pretty::NO_ANN, |s| s.print_qpath(path, false)),
"function",
);
}
},
ExprKind::MethodCall(ref path, _, ref arguments, _) => {
let def_id = cx.typeck_results().type_dependent_def_id(e.hir_id).unwrap();
let substs = cx.typeck_results().node_substs(e.hir_id);
let method_type = cx.tcx.type_of(def_id).subst(cx.tcx, substs);
check_arguments(cx, arguments, method_type, &path.ident.as_str())
check_arguments(cx, arguments, method_type, &path.ident.as_str(), "method")
},
_ => (),
}
}
}

fn check_arguments<'tcx>(cx: &LateContext<'tcx>, arguments: &[Expr<'_>], type_definition: Ty<'tcx>, name: &str) {
fn check_arguments<'tcx>(
cx: &LateContext<'tcx>,
arguments: &[Expr<'_>],
type_definition: Ty<'tcx>,
name: &str,
fn_kind: &str,
) {
match type_definition.kind {
ty::FnDef(..) | ty::FnPtr(_) => {
let parameters = type_definition.fn_sig(cx.tcx).skip_binder().inputs();
Expand All @@ -68,7 +75,7 @@ fn check_arguments<'tcx>(cx: &LateContext<'tcx>, arguments: &[Expr<'_>], type_de
cx,
UNNECESSARY_MUT_PASSED,
argument.span,
&format!("The function/method `{}` doesn't need a mutable reference", name),
&format!("the {} `{}` doesn't need a mutable reference", fn_kind, name),
);
}
},
Expand Down
6 changes: 3 additions & 3 deletions tests/ui/mut_reference.stderr
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
error: The function/method `takes_an_immutable_reference` doesn't need a mutable reference
error: the function `takes_an_immutable_reference` doesn't need a mutable reference
--> $DIR/mut_reference.rs:17:34
|
LL | takes_an_immutable_reference(&mut 42);
| ^^^^^^^
|
= note: `-D clippy::unnecessary-mut-passed` implied by `-D warnings`

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

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