Skip to content

Commit a613460

Browse files
committed
Fix #[expect] for needless_borrow, ref_binding_to_ref
1 parent 3e77162 commit a613460

File tree

6 files changed

+75
-42
lines changed

6 files changed

+75
-42
lines changed

clippy_lints/src/dereference.rs

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then};
1+
use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_hir_and_then};
22
use clippy_utils::source::{snippet_with_applicability, snippet_with_context};
33
use clippy_utils::sugg::has_enclosing_paren;
44
use clippy_utils::ty::peel_mid_ty_refs;
@@ -131,6 +131,7 @@ pub struct Dereferencing {
131131
struct StateData {
132132
/// Span of the top level expression
133133
span: Span,
134+
hir_id: HirId,
134135
}
135136

136137
enum State {
@@ -165,6 +166,8 @@ struct RefPat {
165166
app: Applicability,
166167
/// All the replacements which need to be made.
167168
replacements: Vec<(Span, String)>,
169+
/// The [`HirId`] that the lint should be emitted at.
170+
hir_id: HirId,
168171
}
169172

170173
impl<'tcx> LateLintPass<'tcx> for Dereferencing {
@@ -218,7 +221,10 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing {
218221
is_final_ufcs: matches!(expr.kind, ExprKind::Call(..)),
219222
target_mut,
220223
},
221-
StateData { span: expr.span },
224+
StateData {
225+
span: expr.span,
226+
hir_id: expr.hir_id,
227+
},
222228
));
223229
},
224230
RefOp::AddrOf => {
@@ -290,7 +296,10 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing {
290296
required_precedence,
291297
msg,
292298
},
293-
StateData { span: expr.span },
299+
StateData {
300+
span: expr.span,
301+
hir_id: expr.hir_id,
302+
},
294303
));
295304
}
296305
},
@@ -383,6 +392,7 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing {
383392
spans: vec![pat.span],
384393
app,
385394
replacements: vec![(pat.span, snip.into())],
395+
hir_id: pat.hir_id
386396
}),
387397
);
388398
}
@@ -395,13 +405,15 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing {
395405
for pat in self.ref_locals.drain(..).filter_map(|(_, x)| x) {
396406
let replacements = pat.replacements;
397407
let app = pat.app;
398-
span_lint_and_then(
408+
let lint = if pat.always_deref {
409+
NEEDLESS_BORROW
410+
} else {
411+
REF_BINDING_TO_REFERENCE
412+
};
413+
span_lint_hir_and_then(
399414
cx,
400-
if pat.always_deref {
401-
NEEDLESS_BORROW
402-
} else {
403-
REF_BINDING_TO_REFERENCE
404-
},
415+
lint,
416+
pat.hir_id,
405417
pat.spans,
406418
"this pattern creates a reference to a reference",
407419
|diag| {
@@ -638,19 +650,14 @@ fn report<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>, state: State, data: S
638650
} => {
639651
let mut app = Applicability::MachineApplicable;
640652
let snip = snippet_with_context(cx, expr.span, data.span.ctxt(), "..", &mut app).0;
641-
span_lint_and_sugg(
642-
cx,
643-
NEEDLESS_BORROW,
644-
data.span,
645-
msg,
646-
"change this to",
647-
if required_precedence > expr.precedence().order() && !has_enclosing_paren(&snip) {
653+
span_lint_hir_and_then(cx, NEEDLESS_BORROW, data.hir_id, data.span, msg, |diag| {
654+
let sugg = if required_precedence > expr.precedence().order() && !has_enclosing_paren(&snip) {
648655
format!("({})", snip)
649656
} else {
650657
snip.into()
651-
},
652-
app,
653-
);
658+
};
659+
diag.span_suggestion(data.span, "change this to", sugg, app);
660+
});
654661
},
655662
}
656663
}

tests/ui/needless_borrow.fixed

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// run-rustfix
22

3+
#![feature(lint_reasons)]
4+
35
#[warn(clippy::all, clippy::needless_borrow)]
46
#[allow(unused_variables, clippy::unnecessary_mut_passed)]
57
fn main() {
@@ -96,3 +98,9 @@ trait Trait {}
9698
impl<'a> Trait for &'a str {}
9799

98100
fn h(_: &dyn Trait) {}
101+
102+
fn check_expect_suppression() {
103+
let a = 5;
104+
#[expect(clippy::needless_borrow)]
105+
let _ = x(&&a);
106+
}

tests/ui/needless_borrow.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// run-rustfix
22

3+
#![feature(lint_reasons)]
4+
35
#[warn(clippy::all, clippy::needless_borrow)]
46
#[allow(unused_variables, clippy::unnecessary_mut_passed)]
57
fn main() {
@@ -96,3 +98,9 @@ trait Trait {}
9698
impl<'a> Trait for &'a str {}
9799

98100
fn h(_: &dyn Trait) {}
101+
102+
fn check_expect_suppression() {
103+
let a = 5;
104+
#[expect(clippy::needless_borrow)]
105+
let _ = x(&&a);
106+
}

tests/ui/needless_borrow.stderr

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,97 @@
11
error: this expression creates a reference which is immediately dereferenced by the compiler
2-
--> $DIR/needless_borrow.rs:9:15
2+
--> $DIR/needless_borrow.rs:11:15
33
|
44
LL | let _ = x(&&a); // warn
55
| ^^^ help: change this to: `&a`
66
|
77
= note: `-D clippy::needless-borrow` implied by `-D warnings`
88

99
error: this expression creates a reference which is immediately dereferenced by the compiler
10-
--> $DIR/needless_borrow.rs:13:13
10+
--> $DIR/needless_borrow.rs:15:13
1111
|
1212
LL | mut_ref(&mut &mut b); // warn
1313
| ^^^^^^^^^^^ help: change this to: `&mut b`
1414

1515
error: this expression creates a reference which is immediately dereferenced by the compiler
16-
--> $DIR/needless_borrow.rs:25:13
16+
--> $DIR/needless_borrow.rs:27:13
1717
|
1818
LL | &&a
1919
| ^^^ help: change this to: `&a`
2020

2121
error: this expression creates a reference which is immediately dereferenced by the compiler
22-
--> $DIR/needless_borrow.rs:27:15
22+
--> $DIR/needless_borrow.rs:29:15
2323
|
2424
LL | 46 => &&a,
2525
| ^^^ help: change this to: `&a`
2626

2727
error: this expression creates a reference which is immediately dereferenced by the compiler
28-
--> $DIR/needless_borrow.rs:33:27
28+
--> $DIR/needless_borrow.rs:35:27
2929
|
3030
LL | break &ref_a;
3131
| ^^^^^^ help: change this to: `ref_a`
3232

3333
error: this expression creates a reference which is immediately dereferenced by the compiler
34-
--> $DIR/needless_borrow.rs:40:15
34+
--> $DIR/needless_borrow.rs:42:15
3535
|
3636
LL | let _ = x(&&&a);
3737
| ^^^^ help: change this to: `&a`
3838

3939
error: this expression creates a reference which is immediately dereferenced by the compiler
40-
--> $DIR/needless_borrow.rs:41:15
40+
--> $DIR/needless_borrow.rs:43:15
4141
|
4242
LL | let _ = x(&mut &&a);
4343
| ^^^^^^^^ help: change this to: `&a`
4444

4545
error: this expression creates a reference which is immediately dereferenced by the compiler
46-
--> $DIR/needless_borrow.rs:42:15
46+
--> $DIR/needless_borrow.rs:44:15
4747
|
4848
LL | let _ = x(&&&mut b);
4949
| ^^^^^^^^ help: change this to: `&mut b`
5050

5151
error: this expression creates a reference which is immediately dereferenced by the compiler
52-
--> $DIR/needless_borrow.rs:43:15
52+
--> $DIR/needless_borrow.rs:45:15
5353
|
5454
LL | let _ = x(&&ref_a);
5555
| ^^^^^^^ help: change this to: `ref_a`
5656

5757
error: this expression creates a reference which is immediately dereferenced by the compiler
58-
--> $DIR/needless_borrow.rs:46:11
58+
--> $DIR/needless_borrow.rs:48:11
5959
|
6060
LL | x(&b);
6161
| ^^ help: change this to: `b`
6262

6363
error: this expression creates a reference which is immediately dereferenced by the compiler
64-
--> $DIR/needless_borrow.rs:53:13
64+
--> $DIR/needless_borrow.rs:55:13
6565
|
6666
LL | mut_ref(&mut x);
6767
| ^^^^^^ help: change this to: `x`
6868

6969
error: this expression creates a reference which is immediately dereferenced by the compiler
70-
--> $DIR/needless_borrow.rs:54:13
70+
--> $DIR/needless_borrow.rs:56:13
7171
|
7272
LL | mut_ref(&mut &mut x);
7373
| ^^^^^^^^^^^ help: change this to: `x`
7474

7575
error: this expression creates a reference which is immediately dereferenced by the compiler
76-
--> $DIR/needless_borrow.rs:55:23
76+
--> $DIR/needless_borrow.rs:57:23
7777
|
7878
LL | let y: &mut i32 = &mut x;
7979
| ^^^^^^ help: change this to: `x`
8080

8181
error: this expression creates a reference which is immediately dereferenced by the compiler
82-
--> $DIR/needless_borrow.rs:56:23
82+
--> $DIR/needless_borrow.rs:58:23
8383
|
8484
LL | let y: &mut i32 = &mut &mut x;
8585
| ^^^^^^^^^^^ help: change this to: `x`
8686

8787
error: this expression borrows a value the compiler would automatically borrow
88-
--> $DIR/needless_borrow.rs:72:13
88+
--> $DIR/needless_borrow.rs:74:13
8989
|
9090
LL | let _ = (&x).0;
9191
| ^^^^ help: change this to: `x`
9292

9393
error: this expression borrows a value the compiler would automatically borrow
94-
--> $DIR/needless_borrow.rs:74:22
94+
--> $DIR/needless_borrow.rs:76:22
9595
|
9696
LL | let _ = unsafe { (&*x).0 };
9797
| ^^^^^ help: change this to: `(*x)`

tests/ui/ref_binding_to_reference.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// FIXME: run-rustfix waiting on multi-span suggestions
22

3+
#![feature(lint_reasons)]
34
#![warn(clippy::ref_binding_to_reference)]
45
#![allow(clippy::needless_borrowed_reference)]
56

@@ -73,3 +74,12 @@ impl T1 for S {
7374
let _: &&String = x;
7475
}
7576
}
77+
78+
fn check_expect_suppression() {
79+
let x = String::new();
80+
#[expect(clippy::ref_binding_to_reference)]
81+
let _: &&String = match Some(&x) {
82+
Some(ref x) => x,
83+
None => return,
84+
};
85+
}

tests/ui/ref_binding_to_reference.stderr

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: this pattern creates a reference to a reference
2-
--> $DIR/ref_binding_to_reference.rs:30:14
2+
--> $DIR/ref_binding_to_reference.rs:31:14
33
|
44
LL | Some(ref x) => x,
55
| ^^^^^
@@ -11,7 +11,7 @@ LL | Some(x) => &x,
1111
| ~ ~~
1212

1313
error: this pattern creates a reference to a reference
14-
--> $DIR/ref_binding_to_reference.rs:36:14
14+
--> $DIR/ref_binding_to_reference.rs:37:14
1515
|
1616
LL | Some(ref x) => {
1717
| ^^^^^
@@ -25,7 +25,7 @@ LL ~ &x
2525
|
2626

2727
error: this pattern creates a reference to a reference
28-
--> $DIR/ref_binding_to_reference.rs:46:14
28+
--> $DIR/ref_binding_to_reference.rs:47:14
2929
|
3030
LL | Some(ref x) => m2!(x),
3131
| ^^^^^
@@ -36,7 +36,7 @@ LL | Some(x) => m2!(&x),
3636
| ~ ~~
3737

3838
error: this pattern creates a reference to a reference
39-
--> $DIR/ref_binding_to_reference.rs:51:15
39+
--> $DIR/ref_binding_to_reference.rs:52:15
4040
|
4141
LL | let _ = |&ref x: &&String| {
4242
| ^^^^^
@@ -48,7 +48,7 @@ LL ~ let _: &&String = &x;
4848
|
4949

5050
error: this pattern creates a reference to a reference
51-
--> $DIR/ref_binding_to_reference.rs:57:12
51+
--> $DIR/ref_binding_to_reference.rs:58:12
5252
|
5353
LL | fn f2<'a>(&ref x: &&'a String) -> &'a String {
5454
| ^^^^^
@@ -61,7 +61,7 @@ LL ~ x
6161
|
6262

6363
error: this pattern creates a reference to a reference
64-
--> $DIR/ref_binding_to_reference.rs:64:11
64+
--> $DIR/ref_binding_to_reference.rs:65:11
6565
|
6666
LL | fn f(&ref x: &&String) {
6767
| ^^^^^
@@ -73,7 +73,7 @@ LL ~ let _: &&String = &x;
7373
|
7474

7575
error: this pattern creates a reference to a reference
76-
--> $DIR/ref_binding_to_reference.rs:72:11
76+
--> $DIR/ref_binding_to_reference.rs:73:11
7777
|
7878
LL | fn f(&ref x: &&String) {
7979
| ^^^^^

0 commit comments

Comments
 (0)