Description
openedon Nov 3, 2024
Problem
Hi, I recently encountered an inconsistent behavior of cargo check
when I have a macro which expands to something with unexpected_cfgs
.
In short, I have a macro definition that expand to something with #[cfg(feature = "my_feature")]
. When my_feature
is not declared in Cargo.toml
of my crate, I expected unexpected_cfgs
to be reported whenever I use this macro.
However, whenever I use this macro in a downstream project (which also doesn't declare my_feature
), no warning reported.
I originally thought this was a rust-analyzer problem, but after the discussion in rust-lang/rust-analyzer#18461, it seems this is more relevant to cargo.
I would like to understand if this is the expected behavior, because it feels like a bug to me.
Steps
- Say I have a library
mylib
, andmylib/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!();
}
- Now running
cargo check --tests
, I got the expected warning:
--> mylib/src/lib.rs:8:15
|
8 | #[cfg(feature = "my_feature")]
| ^^^^^^^^^^^^^^^^^^^^^^ help: remove the condition
...
15 | my_lib_macro!();
| --------------- in this macro invocation
|
= note: no expected values for `feature`
= help: consider adding `my_feature` as a feature in `Cargo.toml`
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg/cargo-specifics.html> for more information about checking conditional configuration
= note: `#[warn(unexpected_cfgs)]` on by default
= note: this warning originates in the macro `my_lib_macro` (in Nightly builds, run with -Z macro-backtrace for more info)
- However, if I use
mylib
in other packages, say I have another crate,myapp
, that depends onmylib
.
Inmyapp/main.rs
, I have:
use mylib::my_lib_macro;
fn main() {
my_lib_macro!();
}
- Running
cargo check
onmyapp
reports nothing.
Possible Solution(s)
No response
Notes
No response
Version
cargo 1.82.0 (8f40fc59f 2024-08-21)
release: 1.82.0
commit-hash: 8f40fc59fb0c8df91c97405785197f3c630304ea
commit-date: 2024-08-21
host: x86_64-apple-darwin
libgit2: 1.8.1 (sys:0.19.0 vendored)
libcurl: 8.6.0 (sys:0.4.74+curl-8.9.0 system ssl:(SecureTransport) LibreSSL/3.3.6)
ssl: OpenSSL 1.1.1w 11 Sep 2023
os: Mac OS 14.5.0 [64-bit]