Lint for cfg
usage which would provide a misleading result in a build script.
#9419
Labels
A-lint
Area: New lints
What it does
Lints for uses of
cfg!
,#[cfg]
,#[cfg_attr]
with keys which will have misleading1 values when used inside build scripts (e.g.build.rs
files), such aswindows
,unix
, and the ones that begin withtarget_*
.This is a restriction lint, as I'm assuming Clippy has no way of figuring out if we're in a build script on it's own (it seems completely impossible, as build scripts are just normal rust code).
The idea is that developers of build scripts that support cross compilation can add something like
#![deny(clippy::misleading_cfg_in_build_script)]
.Lint Name
misleading_cfg_in_build_script
Category
restriction
Advantage
The recommended code produces the expected value even when cross-compiling.
Drawbacks
#[cfg]
, there may be no alternative for uses of#[cfg]
/#[cfg_attr]
(perhaps that code needs to not compile).cfg!
to detect if you're cross-compiling.Example
The correct replacement is a bit tricky and has several cases, and IDK if Clippy should even suggest a replacement. Also, it's worth noting that none of these have any hope of working outside a build script.
Anyway, there are a few cases here:
For no-value
cfg
s, likecfg!(windows)
orcfg!(unix)
Can be written as:
For single-value
cfg
s, likecfg!(target_os = ...)
Can be written as:
For multi-value
cfg
s, likecfg!(target_feature = ...)
ortarget_family
(This also works for single-value cfgs, but you can see why it's less desirable)
Can be written as:
The complexity is caused by multi-value cfg being provided by cargo as comma-separated environment vars. This not a theoretical issue either (as
crt-static
is definitely useful for build scripts).Footnotes
These will report the values for the host, not the target, e.g.
cfg!(windows)
is true when compiling on Windows, so will give the wrong answer if cross compiling. This is because build scripts run on the machine performing compilation, rather than on the target. ↩The text was updated successfully, but these errors were encountered: