Skip to content

[feat]: make unnecessary_mut_passed lint auto applicable #14626

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
11 changes: 9 additions & 2 deletions clippy_lints/src/mut_reference.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use clippy_utils::diagnostics::span_lint;
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::source::snippet;
use rustc_errors::Applicability;
use rustc_hir::{BorrowKind, Expr, ExprKind, Mutability};
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::ty::{self, Ty};
Expand Down Expand Up @@ -86,11 +88,16 @@ fn check_arguments<'tcx>(
match parameter.kind() {
ty::Ref(_, _, Mutability::Not) | ty::RawPtr(_, Mutability::Not) => {
if let ExprKind::AddrOf(BorrowKind::Ref, Mutability::Mut, _) = argument.kind {
span_lint(
let snippet = snippet(cx, argument.span, "..");
let trimmed_snippet = &snippet[5..];
span_lint_and_sugg(
Comment on lines +91 to +92
Copy link
Contributor

@samueltardieu samueltardieu Apr 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursory review:

It would probably be best if you use span_lint_and_then(), with .span_suggestion() (or .span_suggestion_verbose(), whether this gives a prettier result or not), and propose to rewrite &mut as &, instead of reconstructing the snippet. You can proceed as follows:

  • The span to replace is from the start of argument to the start of the AddrOf argument.
  • You should also ensure that the context of argument is the same one as the context of the AddrOf argument, to ensure that the &mut has not been built by a macro.
  • Add tests where &mut comes from a macro for example (build a macro refmut and call refmut!(variable)), and where the expression comes from a macro (build a macro identity and call &mut identity!(var)).

Also, in the PR message (changelog) and title, you should use the lint name in lower case.

cx,
UNNECESSARY_MUT_PASSED,
argument.span,
format!("the {fn_kind} `{name}` doesn't need a mutable reference"),
"try",
format!("&{trimmed_snippet}"),
Applicability::MachineApplicable,
);
}
},
Expand Down
6 changes: 3 additions & 3 deletions tests/ui/mut_reference.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error: the function `takes_an_immutable_reference` doesn't need a mutable refere
--> tests/ui/mut_reference.rs:30:34
|
LL | takes_an_immutable_reference(&mut 42);
| ^^^^^^^
| ^^^^^^^ help: try: `&42`
|
= note: `-D clippy::unnecessary-mut-passed` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::unnecessary_mut_passed)]`
Expand All @@ -11,13 +11,13 @@ error: the function `as_ptr` doesn't need a mutable reference
--> tests/ui/mut_reference.rs:34:12
|
LL | as_ptr(&mut 42);
| ^^^^^^^
| ^^^^^^^ help: try: `&42`

error: the method `takes_an_immutable_reference` doesn't need a mutable reference
--> tests/ui/mut_reference.rs:39:44
|
LL | my_struct.takes_an_immutable_reference(&mut 42);
| ^^^^^^^
| ^^^^^^^ help: try: `&42`

error: aborting due to 3 previous errors

Loading