Skip to content

Commit 158bf9a

Browse files
committed
Fix incorrect suggestion for macro expansion in deref_addrof lint
1 parent c45255b commit 158bf9a

File tree

4 files changed

+47
-4
lines changed

4 files changed

+47
-4
lines changed

clippy_lints/src/reference.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,19 @@ impl EarlyLintPass for DerefAddrOf {
4646
if !in_macro(addrof_target.span);
4747
then {
4848
let mut applicability = Applicability::MachineApplicable;
49+
let sugg = if e.span.from_expansion() {
50+
let snip = snippet_with_applicability(cx, e.span, "_", &mut applicability);
51+
snip.trim_start_matches(|c| c == '&' || c == '*').to_string()
52+
} else {
53+
snippet_with_applicability(cx, addrof_target.span, "_", &mut applicability).to_string()
54+
};
4955
span_lint_and_sugg(
5056
cx,
5157
DEREF_ADDROF,
5258
e.span,
5359
"immediately dereferencing a reference",
5460
"try this",
55-
format!("{}", snippet_with_applicability(cx, addrof_target.span, "_", &mut applicability)),
61+
sugg,
5662
applicability,
5763
);
5864
}

tests/ui/deref_addrof.fixed

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// run-rustfix
2+
#![warn(clippy::deref_addrof)]
23

34
fn get_number() -> usize {
45
10
@@ -10,7 +11,6 @@ fn get_reference(n: &usize) -> &usize {
1011

1112
#[allow(clippy::many_single_char_names, clippy::double_parens)]
1213
#[allow(unused_variables, unused_parens)]
13-
#[warn(clippy::deref_addrof)]
1414
fn main() {
1515
let a = 10;
1616
let aref = &a;
@@ -37,3 +37,16 @@ fn main() {
3737

3838
let b = *aref;
3939
}
40+
41+
macro_rules! m {
42+
($visitor: expr) => {
43+
$visitor
44+
};
45+
}
46+
47+
pub struct S;
48+
impl S {
49+
pub fn f(&self) -> &Self {
50+
m!(self)
51+
}
52+
}

tests/ui/deref_addrof.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// run-rustfix
2+
#![warn(clippy::deref_addrof)]
23

34
fn get_number() -> usize {
45
10
@@ -10,7 +11,6 @@ fn get_reference(n: &usize) -> &usize {
1011

1112
#[allow(clippy::many_single_char_names, clippy::double_parens)]
1213
#[allow(unused_variables, unused_parens)]
13-
#[warn(clippy::deref_addrof)]
1414
fn main() {
1515
let a = 10;
1616
let aref = &a;
@@ -37,3 +37,16 @@ fn main() {
3737

3838
let b = **&aref;
3939
}
40+
41+
macro_rules! m {
42+
($visitor: expr) => {
43+
*&$visitor
44+
};
45+
}
46+
47+
pub struct S;
48+
impl S {
49+
pub fn f(&self) -> &Self {
50+
m!(self)
51+
}
52+
}

tests/ui/deref_addrof.stderr

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,16 @@ error: immediately dereferencing a reference
4848
LL | let b = **&aref;
4949
| ^^^^^^ help: try this: `aref`
5050

51-
error: aborting due to 8 previous errors
51+
error: immediately dereferencing a reference
52+
--> $DIR/deref_addrof.rs:43:9
53+
|
54+
LL | *&$visitor
55+
| ^^^^^^^^^^ help: try this: `$visitor`
56+
...
57+
LL | m!(self)
58+
| -------- in this macro invocation
59+
|
60+
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
61+
62+
error: aborting due to 9 previous errors
5263

0 commit comments

Comments
 (0)