Compile-time checking that all variants of a cfg() macro are valid

Say I have the following code:

#[cfg(unix)]
use x::X;

X::new()

This code will compile on Unix platforms, but not on Windows platforms (X is not in scope).

Is there a Cargo command that will check, for all possible configurations/feature flags/etc, that they are valid? I don't necessarily want to build for Windows, but I want to make sure that anyone building for Windows won't have this sort of issue. This is especially key because it's pretty easy to mess up like this, especially in larger codebases.

Kinda sounds like you're looking for CI

Other than running cargo check --target={TARGET} multiple times for several targets, I'm not aware of any local tools that do this. (Though this is probably very similar to/the same as what I'd do in CI for this)

Of course, there may be something for this that I'm unaware of.

1 Like

I don't think this covers everything, but you might be interested in following

https://github.com/rust-lang/rfcs/pull/3013

1 Like

Here is an extension for Cargo that will run the given subcommand (check, test, etc.) with all combinations of features. However, this doesn't cover built-in cfg items like unix, windows, target_arch = "...", etc. For those you probably want to cargo test on each target you care about in CI.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.