rust-clippy icon indicating copy to clipboard operation
rust-clippy copied to clipboard

Lint path or import used in macro

Open i509VCB opened this issue 2 years ago • 1 comments

What it does

Lint any instances of paths to items or use statements which do not use $crate or global paths (such as ::std::vec::Vec)

Lint Name

macro_use_path

Category

correctness

Advantage

  • Since imports and paths in macros are unhygienic, you should use $crate or global paths.
  • This will make macros used by other crates less likely to fail to compile.

Drawbacks

  • Internal macros may wish to avoid this requirement

Example

pub trait Delegate {}

#[macro_export]
macro_rules! impl_delegate {
    ($ty: ty) => {
        impl Delegate for $ty {}
    }
}

Could be written as:

pub trait Delegate {}

#[macro_export]
macro_rules! impl_delegate {
    impl $crate::Delegate for $ty {}
}

i509VCB avatar Jul 03 '22 02:07 i509VCB

Discussion on zulip about this lint is going to be a challenge given the macro is not enough information (you need to look at the expanded macro to determine the AST.

i509VCB avatar Aug 08 '22 01:08 i509VCB