Skip to content

Commit 125a9eb

Browse files
committed
sugg on expr 2
1 parent 1b064fe commit 125a9eb

File tree

4 files changed

+26
-21
lines changed

4 files changed

+26
-21
lines changed

clippy_lints/src/mut_mut.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clippy_utils::diagnostics::{span_lint, span_lint_hir, span_lint_hir_and_then};
1+
use clippy_utils::diagnostics::{span_lint, span_lint_hir_and_then};
22
use clippy_utils::higher;
33
use clippy_utils::sugg::Sugg;
44
use rustc_errors::Applicability;
@@ -72,12 +72,17 @@ impl<'tcx> intravisit::Visitor<'tcx> for MutVisitor<'_, 'tcx> {
7272
intravisit::walk_expr(self, body);
7373
} else if let hir::ExprKind::AddrOf(hir::BorrowKind::Ref, hir::Mutability::Mut, e) = expr.kind {
7474
if let hir::ExprKind::AddrOf(hir::BorrowKind::Ref, hir::Mutability::Mut, _) = e.kind {
75-
span_lint_hir(
75+
let mut applicability = Applicability::MaybeIncorrect;
76+
let sugg = Sugg::hir_with_applicability(self.cx, e, "..", &mut applicability);
77+
span_lint_hir_and_then(
7678
self.cx,
7779
MUT_MUT,
7880
expr.hir_id,
7981
expr.span,
80-
"generally you want to avoid `&mut &mut _` if possible",
82+
"an expression of form `&mut &mut _`",
83+
|diag| {
84+
diag.span_suggestion(expr.span, "remove the extra `&mut`", sugg, applicability);
85+
},
8186
);
8287
} else if let ty::Ref(_, ty, hir::Mutability::Mut) = self.cx.typeck_results().expr_ty(e).kind()
8388
&& ty.peel_refs().is_sized(self.cx.tcx, self.cx.typing_env())

tests/ui/mut_mut.fixed

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,27 +29,27 @@ macro_rules! mut_ptr {
2929
#[allow(unused_mut, unused_variables)]
3030
#[inline_macros]
3131
fn main() {
32-
let mut x = &mut &mut 1u32;
32+
let mut x = &mut 1u32;
3333
//~^ mut_mut
3434
{
3535
let mut y = &mut *x;
3636
//~^ mut_mut
3737
}
3838

3939
{
40-
let y: &mut &mut u32 = &mut &mut 2;
40+
let y: &mut &mut u32 = &mut 2;
4141
//~^ mut_mut
4242
//~| mut_mut
4343
}
4444

4545
{
46-
let y: &mut &mut &mut u32 = &mut &mut &mut 2;
46+
let y: &mut &mut &mut u32 = &mut &mut 2;
4747
//~^ mut_mut
4848
//~| mut_mut
4949
//~| mut_mut
5050
}
5151

52-
let mut z = inline!(&mut $(&mut 3u32));
52+
let mut z = inline!(&mut 3u32mut $(&mut 3u32));
5353
//~^ mut_mut
5454
}
5555

tests/ui/mut_mut.stderr

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@ LL | fn fun(x: &mut &mut u32) {
77
= note: `-D clippy::mut-mut` implied by `-D warnings`
88
= help: to override `-D warnings` add `#[allow(clippy::mut_mut)]`
99

10-
error: generally you want to avoid `&mut &mut _` if possible
10+
error: an expression of form `&mut &mut _`
1111
--> tests/ui/mut_mut.rs:32:17
1212
|
1313
LL | let mut x = &mut &mut 1u32;
14-
| ^^^^^^^^^^^^^^
14+
| ^^^^^^^^^^^^^^ help: remove the extra `&mut`: `&mut 1u32`
1515

16-
error: generally you want to avoid `&mut &mut _` if possible
16+
error: an expression of form `&mut &mut _`
1717
--> tests/ui/mut_mut.rs:52:25
1818
|
1919
LL | let mut z = inline!(&mut $(&mut 3u32));
20-
| ^
20+
| ^ help: remove the extra `&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

@@ -27,23 +27,23 @@ error: this expression mutably borrows a mutable reference
2727
LL | let mut y = &mut x;
2828
| ^^^^^^ help: reborrow instead: `&mut *x`
2929

30-
error: generally you want to avoid `&mut &mut _` if possible
30+
error: an expression of form `&mut &mut _`
3131
--> tests/ui/mut_mut.rs:40:32
3232
|
3333
LL | let y: &mut &mut u32 = &mut &mut 2;
34-
| ^^^^^^^^^^^
34+
| ^^^^^^^^^^^ help: remove the extra `&mut`: `&mut 2`
3535

3636
error: generally you want to avoid `&mut &mut _` if possible
3737
--> tests/ui/mut_mut.rs:40:16
3838
|
3939
LL | let y: &mut &mut u32 = &mut &mut 2;
4040
| ^^^^^^^^^^^^^
4141

42-
error: generally you want to avoid `&mut &mut _` if possible
42+
error: an expression of form `&mut &mut _`
4343
--> tests/ui/mut_mut.rs:46:37
4444
|
4545
LL | let y: &mut &mut &mut u32 = &mut &mut &mut 2;
46-
| ^^^^^^^^^^^^^^^^
46+
| ^^^^^^^^^^^^^^^^ help: remove the extra `&mut`: `&mut &mut 2`
4747

4848
error: generally you want to avoid `&mut &mut _` if possible
4949
--> tests/ui/mut_mut.rs:46:16

tests/ui/mut_mut_unfixable.stderr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,29 @@ LL | fn fun(x: &mut &mut u32) -> bool {
77
= note: `-D clippy::mut-mut` implied by `-D warnings`
88
= help: to override `-D warnings` add `#[allow(clippy::mut_mut)]`
99

10-
error: generally you want to avoid `&mut &mut _` if possible
10+
error: an expression of form `&mut &mut _`
1111
--> tests/ui/mut_mut_unfixable.rs:15:17
1212
|
1313
LL | let mut x = &mut &mut 1u32;
14-
| ^^^^^^^^^^^^^^
14+
| ^^^^^^^^^^^^^^ help: remove the extra `&mut`: `&mut 1u32`
1515

1616
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;
2020
| ^^^^^^ help: reborrow instead: `&mut *x`
2121

22-
error: generally you want to avoid `&mut &mut _` if possible
22+
error: an expression of form `&mut &mut _`
2323
--> tests/ui/mut_mut_unfixable.rs:24:17
2424
|
2525
LL | let y = &mut &mut 2;
26-
| ^^^^^^^^^^^
26+
| ^^^^^^^^^^^ help: remove the extra `&mut`: `&mut 2`
2727

28-
error: generally you want to avoid `&mut &mut _` if possible
28+
error: an expression of form `&mut &mut _`
2929
--> tests/ui/mut_mut_unfixable.rs:30:17
3030
|
3131
LL | let y = &mut &mut &mut 2;
32-
| ^^^^^^^^^^^^^^^^
32+
| ^^^^^^^^^^^^^^^^ help: remove the extra `&mut`: `&mut &mut 2`
3333

3434
error: aborting due to 5 previous errors
3535

0 commit comments

Comments
 (0)