Fix incorrect suggestion for macro expansion in deref_addrof lint

This commit is contained in:
ThibsG 2020-10-31 19:56:51 +01:00
parent c45255b145
commit 158bf9aa44
4 changed files with 47 additions and 4 deletions

View File

@ -46,13 +46,19 @@ impl EarlyLintPass for DerefAddrOf {
if !in_macro(addrof_target.span);
then {
let mut applicability = Applicability::MachineApplicable;
let sugg = if e.span.from_expansion() {
let snip = snippet_with_applicability(cx, e.span, "_", &mut applicability);
snip.trim_start_matches(|c| c == '&' || c == '*').to_string()
} else {
snippet_with_applicability(cx, addrof_target.span, "_", &mut applicability).to_string()
};
span_lint_and_sugg(
cx,
DEREF_ADDROF,
e.span,
"immediately dereferencing a reference",
"try this",
format!("{}", snippet_with_applicability(cx, addrof_target.span, "_", &mut applicability)),
sugg,
applicability,
);
}

View File

@ -1,4 +1,5 @@
// run-rustfix
#![warn(clippy::deref_addrof)]
fn get_number() -> usize {
10
@ -10,7 +11,6 @@ fn get_reference(n: &usize) -> &usize {
#[allow(clippy::many_single_char_names, clippy::double_parens)]
#[allow(unused_variables, unused_parens)]
#[warn(clippy::deref_addrof)]
fn main() {
let a = 10;
let aref = &a;
@ -37,3 +37,16 @@ fn main() {
let b = *aref;
}
macro_rules! m {
($visitor: expr) => {
$visitor
};
}
pub struct S;
impl S {
pub fn f(&self) -> &Self {
m!(self)
}
}

View File

@ -1,4 +1,5 @@
// run-rustfix
#![warn(clippy::deref_addrof)]
fn get_number() -> usize {
10
@ -10,7 +11,6 @@ fn get_reference(n: &usize) -> &usize {
#[allow(clippy::many_single_char_names, clippy::double_parens)]
#[allow(unused_variables, unused_parens)]
#[warn(clippy::deref_addrof)]
fn main() {
let a = 10;
let aref = &a;
@ -37,3 +37,16 @@ fn main() {
let b = **&aref;
}
macro_rules! m {
($visitor: expr) => {
*&$visitor
};
}
pub struct S;
impl S {
pub fn f(&self) -> &Self {
m!(self)
}
}

View File

@ -48,5 +48,16 @@ error: immediately dereferencing a reference
LL | let b = **&aref;
| ^^^^^^ help: try this: `aref`
error: aborting due to 8 previous errors
error: immediately dereferencing a reference
--> $DIR/deref_addrof.rs:43:9
|
LL | *&$visitor
| ^^^^^^^^^^ help: try this: `$visitor`
...
LL | m!(self)
| -------- in this macro invocation
|
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
error: aborting due to 9 previous errors