Skip to content

[unnecessary_mut_passed]: don't lint in macro expansions #11269

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

Merged
merged 1 commit into from
Aug 2, 2023
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
5 changes: 5 additions & 0 deletions clippy_lints/src/mut_reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ declare_lint_pass!(UnnecessaryMutPassed => [UNNECESSARY_MUT_PASSED]);

impl<'tcx> LateLintPass<'tcx> for UnnecessaryMutPassed {
fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
if e.span.from_expansion() {
// Issue #11268
return;
}

match e.kind {
ExprKind::Call(fn_expr, arguments) => {
if let ExprKind::Path(ref path) = fn_expr.kind {
Expand Down
15 changes: 14 additions & 1 deletion tests/ui/mut_reference.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
#![allow(unused_variables)]
#![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 {
Expand Down
8 changes: 4 additions & 4 deletions tests/ui/mut_reference.stderr
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
error: the function `takes_an_immutable_reference` doesn't need a mutable reference
--> $DIR/mut_reference.rs:17:34
--> $DIR/mut_reference.rs:30:34
|
LL | takes_an_immutable_reference(&mut 42);
| ^^^^^^^
|
= note: `-D clippy::unnecessary-mut-passed` implied by `-D warnings`

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

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

error: this argument is a mutable reference, but not used mutably
--> $DIR/mut_reference.rs:11:44
--> $DIR/mut_reference.rs:24:44
|
LL | fn takes_a_mutable_reference(&self, a: &mut i32) {}
| ^^^^^^^^ help: consider changing to: `&i32`
Expand Down