Skip to content

unnecessary_mut_passed: add structured suggestion #15438

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 1 commit 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
13 changes: 10 additions & 3 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::sugg::Sugg;
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 @@ -83,13 +85,18 @@ fn check_arguments<'tcx>(
let parameters = type_definition.fn_sig(cx.tcx).skip_binder().inputs();
for (argument, parameter) in iter::zip(arguments, parameters) {
if let ty::Ref(_, _, Mutability::Not) | ty::RawPtr(_, Mutability::Not) = parameter.kind()
&& let ExprKind::AddrOf(BorrowKind::Ref, Mutability::Mut, _) = argument.kind
&& let ExprKind::AddrOf(BorrowKind::Ref, Mutability::Mut, arg) = argument.kind
{
span_lint(
let mut applicability = Applicability::MachineApplicable;
let sugg = Sugg::hir_with_applicability(cx, arg, "_", &mut applicability).addr();
span_lint_and_sugg(
cx,
UNNECESSARY_MUT_PASSED,
argument.span,
format!("the {fn_kind} `{name}` doesn't need a mutable reference"),
"remove the `mut`",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

or maybe this should say "remove this mut"? That sounds a bit more like the regular compiler diagnostics

sugg.to_string(),
applicability,
);
}
}
Expand Down
59 changes: 59 additions & 0 deletions tests/ui/mut_reference.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#![allow(unused_variables, dead_code)]
fn takes_an_immutable_reference(a: &i32) {}
fn takes_a_mutable_reference(a: &mut i32) {}

mod issue11268 {
macro_rules! x {
($f:expr) => {
$f(&mut 1);
};
}

fn f() {
x!(super::takes_an_immutable_reference);
x!(super::takes_a_mutable_reference);
}
}

struct MyStruct;

impl MyStruct {
fn takes_an_immutable_reference(&self, a: &i32) {}

fn takes_a_mutable_reference(&self, a: &mut i32) {}
}

#[warn(clippy::unnecessary_mut_passed)]
fn main() {
// Functions
takes_an_immutable_reference(&42);
//~^ unnecessary_mut_passed

let as_ptr: fn(&i32) = takes_an_immutable_reference;
as_ptr(&42);
//~^ unnecessary_mut_passed

// Methods
let my_struct = MyStruct;
my_struct.takes_an_immutable_reference(&42);
//~^ unnecessary_mut_passed

// No error

// Functions
takes_an_immutable_reference(&42);
let as_ptr: fn(&i32) = takes_an_immutable_reference;
as_ptr(&42);

takes_a_mutable_reference(&mut 42);
let as_ptr: fn(&mut i32) = takes_a_mutable_reference;
as_ptr(&mut 42);

let a = &mut 42;
takes_an_immutable_reference(a);

// Methods
my_struct.takes_an_immutable_reference(&42);
my_struct.takes_a_mutable_reference(&mut 42);
my_struct.takes_an_immutable_reference(a);
}
1 change: 0 additions & 1 deletion tests/ui/mut_reference.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#![allow(unused_variables, dead_code)]
//@no-rustfix
fn takes_an_immutable_reference(a: &i32) {}
fn takes_a_mutable_reference(a: &mut i32) {}

Expand Down
12 changes: 6 additions & 6 deletions tests/ui/mut_reference.stderr
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
error: the function `takes_an_immutable_reference` doesn't need a mutable reference
--> tests/ui/mut_reference.rs:30:34
--> tests/ui/mut_reference.rs:29:34
|
LL | takes_an_immutable_reference(&mut 42);
| ^^^^^^^
| ^^^^^^^ help: remove the `mut`: `&42`
|
= note: `-D clippy::unnecessary-mut-passed` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::unnecessary_mut_passed)]`

error: the function `as_ptr` doesn't need a mutable reference
--> tests/ui/mut_reference.rs:34:12
--> tests/ui/mut_reference.rs:33:12
|
LL | as_ptr(&mut 42);
| ^^^^^^^
| ^^^^^^^ help: remove the `mut`: `&42`

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

error: aborting due to 3 previous errors