Inconsistent warning for macros that contain unexpected_cfgs · Issue #18461 · rust-lang/rust-analyzer (original) (raw)

Hi, I recently encountered an inconsistent behavior of rust-analyzer when I have a macro which expands to something with unexpected_cfgs.

I would like to understand if this is expected, because it feels like a bug to me.

Here is a minimal example:

Say I have a library mylib, and mylib/lib.rs has

pub fn my_lib_func() {
    println!("my nice little library")
}

#[macro_export]
macro_rules! my_lib_macro {
    () => {
        #[cfg(feature = "my_feature")]
        $crate::my_lib_func()
    };
}

#[test]
fn my_unit_test() {
    // rust-analyzer warning here
    my_lib_macro!();
}

mylib has no feature declared, so I got an unexpected_cfgs warning in the place where I called my_lib_macro!():

unexpected `cfg` condition value: `my_feature`
   no expected values for `feature`
   consider adding `my_feature` as a feature in `Cargo.toml`
   see <https://doc.rust-lang.org/nightly/rustc/check-cfg/cargo-specifics.html> for more information about checking conditional configuration
   `#[warn(unexpected_cfgs)]` on by default [unexpected_cfgs]

This is great, exactly what I would expect.

However, if I use mylib in other packages, say I have another crate, myapp, that depends on mylib.
In myapp/main.rs, I have:

use mylib::my_lib_macro;

fn main() {
    my_lib_macro!();
}

Rust-analyzer does not report anything in main(), which confuses me why that is the case.

Hopefully the example makes sense, happy to provide more information if needed.
And thanks for the help :).