Skip to content

Commit eccb6a6

Browse files
committed
sugg on expr 1
1 parent 87ee2c5 commit eccb6a6

File tree

4 files changed

+108
-7
lines changed

4 files changed

+108
-7
lines changed

clippy_lints/src/mut_mut.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
use clippy_utils::diagnostics::{span_lint, span_lint_hir};
1+
use clippy_utils::diagnostics::{span_lint, span_lint_hir, span_lint_hir_and_then};
22
use clippy_utils::higher;
3+
use clippy_utils::sugg::Sugg;
4+
use rustc_errors::Applicability;
35
use rustc_hir::{self as hir, AmbigArg, intravisit};
46
use rustc_lint::{LateContext, LateLintPass, LintContext};
57
use rustc_middle::ty;
@@ -80,12 +82,17 @@ impl<'tcx> intravisit::Visitor<'tcx> for MutVisitor<'_, 'tcx> {
8082
} else if let ty::Ref(_, ty, hir::Mutability::Mut) = self.cx.typeck_results().expr_ty(e).kind()
8183
&& ty.peel_refs().is_sized(self.cx.tcx, self.cx.typing_env())
8284
{
83-
span_lint_hir(
85+
let mut applicability = Applicability::MaybeIncorrect;
86+
let sugg = Sugg::hir_with_applicability(self.cx, e, "..", &mut applicability).mut_addr_deref();
87+
span_lint_hir_and_then(
8488
self.cx,
8589
MUT_MUT,
8690
expr.hir_id,
8791
expr.span,
88-
"this expression mutably borrows a mutable reference. Consider reborrowing",
92+
"this expression mutably borrows a mutable reference",
93+
|diag| {
94+
diag.span_suggestion(expr.span, "reborrow instead", sugg, applicability);
95+
},
8996
);
9097
}
9198
}

tests/ui/mut_mut.fixed

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
//@aux-build:proc_macros.rs
2+
3+
#![warn(clippy::mut_mut)]
4+
#![allow(unused)]
5+
#![allow(
6+
clippy::no_effect,
7+
clippy::uninlined_format_args,
8+
clippy::unnecessary_operation,
9+
clippy::needless_pass_by_ref_mut
10+
)]
11+
12+
extern crate proc_macros;
13+
use proc_macros::{external, inline_macros};
14+
15+
fn fun(x: &mut &mut u32) {
16+
//~^ mut_mut
17+
}
18+
19+
fn less_fun(x: *mut *mut u32) {
20+
let y = x;
21+
}
22+
23+
macro_rules! mut_ptr {
24+
($p:expr) => {
25+
&mut $p
26+
};
27+
}
28+
29+
#[allow(unused_mut, unused_variables)]
30+
#[inline_macros]
31+
fn main() {
32+
let mut x = &mut &mut 1u32;
33+
//~^ mut_mut
34+
{
35+
let mut y = &mut *x;
36+
//~^ mut_mut
37+
}
38+
39+
{
40+
let y: &mut &mut u32 = &mut &mut 2;
41+
//~^ mut_mut
42+
//~| mut_mut
43+
}
44+
45+
{
46+
let y: &mut &mut &mut u32 = &mut &mut &mut 2;
47+
//~^ mut_mut
48+
//~| mut_mut
49+
//~| mut_mut
50+
}
51+
52+
let mut z = inline!(&mut $(&mut 3u32));
53+
//~^ mut_mut
54+
}
55+
56+
fn issue939() {
57+
let array = [5, 6, 7, 8, 9];
58+
let mut args = array.iter().skip(2);
59+
for &arg in &mut args {
60+
println!("{}", arg);
61+
}
62+
63+
let args = &mut args;
64+
for arg in args {
65+
println!(":{}", arg);
66+
}
67+
}
68+
69+
fn issue6922() {
70+
// do not lint from an external macro
71+
external!(let mut_mut_ty: &mut &mut u32 = &mut &mut 1u32;);
72+
}
73+
74+
mod issue9035 {
75+
use std::fmt::Display;
76+
77+
struct Foo<'a> {
78+
inner: &'a mut dyn Display,
79+
}
80+
81+
impl Foo<'_> {
82+
fn foo(&mut self) {
83+
let hlp = &mut self.inner;
84+
bar(hlp);
85+
}
86+
}
87+
88+
fn bar(_: &mut impl Display) {}
89+
}
90+
91+
fn allow_works() {
92+
#[allow(clippy::mut_mut)]
93+
let _ = &mut &mut 1;
94+
}

tests/ui/mut_mut.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ LL | let mut z = inline!(&mut $(&mut 3u32));
2121
|
2222
= note: this error originates in the macro `__inline_mac_fn_main` (in Nightly builds, run with -Z macro-backtrace for more info)
2323

24-
error: this expression mutably borrows a mutable reference. Consider reborrowing
24+
error: this expression mutably borrows a mutable reference
2525
--> tests/ui/mut_mut.rs:35:21
2626
|
2727
LL | let mut y = &mut x;
28-
| ^^^^^^
28+
| ^^^^^^ help: reborrow instead: `&mut *x`
2929

3030
error: generally you want to avoid `&mut &mut _` if possible
3131
--> tests/ui/mut_mut.rs:40:32

tests/ui/mut_mut_unfixable.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ error: generally you want to avoid `&mut &mut _` if possible
1313
LL | let mut x = &mut &mut 1u32;
1414
| ^^^^^^^^^^^^^^
1515

16-
error: this expression mutably borrows a mutable reference. Consider reborrowing
16+
error: this expression mutably borrows a mutable reference
1717
--> tests/ui/mut_mut_unfixable.rs:18:21
1818
|
1919
LL | let mut y = &mut x;
20-
| ^^^^^^
20+
| ^^^^^^ help: reborrow instead: `&mut *x`
2121

2222
error: generally you want to avoid `&mut &mut _` if possible
2323
--> tests/ui/mut_mut_unfixable.rs:24:17

0 commit comments

Comments
 (0)