I tried this code:
macro_rules! foo {
() => {
cfg!($crate)
};
}
fn main() {
let b = foo!();
println!("{b}");
}
I expected a compile error. Instead, I got the following warning, but the code compiles fine and prints false.
warning: unexpected `cfg` condition name: `$crate`
--> src/main.rs:3:14
|
3 | cfg!($crate)
| ^^^^^^
...
8 | let b = foo!();
| ------ in this macro invocation
|
= help: expected names are: `docsrs`, `feature`, and `test` and 31 more
= help: consider using a Cargo feature instead
= help: or consider adding in `Cargo.toml` the `check-cfg` lint config for the lint:
[lints.rust]
unexpected_cfgs = { level = "warn", check-cfg = ['cfg($crate)'] }
= help: or consider adding `println!("cargo::rustc-check-cfg=cfg($crate)");` to the top of the `build.rs`
= 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 `foo` (in Nightly builds, run with -Z macro-backtrace for more info)
Since it's impossible to set the value of this cfg, I think this should cause a compile error.
See also #146967 for more nonsense with $crate.
Meta
Reproducible on the playground with version 1.92.0-nightly (2025-09-23 975e6c8fec280816d24f)
I tried this code:
I expected a compile error. Instead, I got the following warning, but the code compiles fine and prints
false.Since it's impossible to set the value of this
cfg, I think this should cause a compile error.See also #146967 for more nonsense with
$crate.Meta
Reproducible on the playground with version
1.92.0-nightly (2025-09-23 975e6c8fec280816d24f)