Skip to content

deref_addrof expands macros before creating suggestions #6234

Closed
@jyn514

Description

@jyn514

I tried this code:

macro_rules! m {
    ($visitor: expr) => (*&$visitor)
}

pub struct S;
impl S {
    pub fn f(&self) -> &Self {
        m!(self)
    }
}

I expected to see this happen: Clippy suggests turning *&$visitor to $visitor

Instead, this happened: Clippy suggests turning *&$visitor to self:

The following errors were reported:
error[E0424]: expected value, found module `self`
 --> src/lib.rs:2:26
  |
2 |       ($visitor: expr) => (self)
  |                            ^^^^ `self` value is a keyword only available in methods with a `self` parameter
...
7 | /     pub fn f(&self) -> &Self {
8 | |         m!(self)
  | |         -------- in this macro invocation
9 | |     }
  | |_____- this function has a `self` parameter, but a macro invocation can only access identifiers it receives from parameters
  |
  = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to previous error

For more information about this error, try `rustc --explain E0424`.
Original diagnostics will follow.

warning: immediately dereferencing a reference
 --> src/lib.rs:2:26
  |
2 |     ($visitor: expr) => (*&$visitor)
  |                          ^^^^^^^^^^ help: try this: `self`
...
8 |         m!(self)
  |         -------- in this macro invocation
  |
  = note: `#[warn(clippy::deref_addrof)]` on by default
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#deref_addrof
  = note: this warning originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

Meta

  • cargo clippy -V: clippy 0.0.212 (ffa2e7a 2020-10-24)
  • rustc -Vv:
rustc 1.49.0-nightly (ffa2e7ae8 2020-10-24)
binary: rustc
commit-hash: ffa2e7ae8fbf9badc035740db949b9dae271c29f
commit-date: 2020-10-24
host: x86_64-unknown-linux-gnu
release: 1.49.0-nightly
LLVM version: 11.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when appliedL-suggestionLint: Improving, adding or fixing lint suggestions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions