Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistent warning for macros that contain unexpected_cfgs #18461

Closed
funemy opened this issue Nov 2, 2024 · 3 comments
Closed

Inconsistent warning for macros that contain unexpected_cfgs #18461

funemy opened this issue Nov 2, 2024 · 3 comments
Labels
C-support Category: support questions

Comments

@funemy
Copy link

funemy commented Nov 2, 2024

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 :).

@funemy funemy added the C-support Category: support questions label Nov 2, 2024
@ChayimFriedman2
Copy link
Contributor

Is the same thing happening with cargo check?

@funemy
Copy link
Author

funemy commented Nov 2, 2024

@ChayimFriedman2 I'm not sure if I did it correctly, but I didn't see any warning with cargo check.

@ChayimFriedman2
Copy link
Contributor

Then rust-analyzer is functioning correctly, the warnings are reported by Cargo (and rustc), if they are missing it's at most a Cargo bug, not a rust-analyzer bug.

I believe this is because there is no way to state the expected features for a binary, so every feature is expected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-support Category: support questions
Projects
None yet
Development

No branches or pull requests

2 participants