This can help with verifying that the crate is correctly handling conditional compilation for different target platforms or features. It ensures that the cfg settings are consistent between what is intended and what is used, helping to catch potential bugs or errors early in the development process.This can help with verifying that the crate is correctly handling conditional compilation for different target platforms or features. It ensures that the cfg settings are consistent between what is intended and what is used, helping to catch potential bugs or errors early in the development process.

The Automatic Checking of cfgs: How It Works

2025/11/03 01:15

The Cargo and Compiler team are delighted to announce that starting with Rust 1.80 (or nightly-2024-05-05) every reachable #[cfg] will be automatically checked that they match the expected config names and values.

\ This can help with verifying that the crate is correctly handling conditional compilation for different target platforms or features. It ensures that the cfg settings are consistent between what is intended and what is used, helping to catch potential bugs or errors early in the development process.

\ This addresses a common pitfall for new and advanced users.

\ This is another step to our commitment to provide user-focused tooling and we are eager and excited to finally see it fixed, after more than two years since the original RFC 30131.

A look at the feature

Every time a Cargo feature is declared that feature is transformed into a config that is passed to rustc (the Rust compiler) so it can verify with it along with well known cfgs if any of the #[cfg], #![cfg_attr] and cfg! have unexpected configs and report a warning with the unexpected_cfgs lint.

Cargo.toml:

[package] name = "foo" [features] lasers = [] zapping = []

\ src/lib.rs:

#[cfg(feature = "lasers")] // This condition is expected // as "lasers" is an expected value // of the `feature` cfg fn shoot_lasers() {} #[cfg(feature = "monkeys")] // This condition is UNEXPECTED // as "monkeys" is NOT an expected // value of the `feature` cfg fn write_shakespeare() {} #[cfg(windosw)] // This condition is UNEXPECTED // it's supposed to be `windows` fn win() {}

\ cargo check:

Expecting custom cfgs

UPDATE: This section was added with the release of nightly-2024-05-19.

\ Some crates might use custom cfgs, like loom, fuzzing or tokio_unstable that they expected from the environment (RUSTFLAGS or other means) and which are always statically known at compile time. For those cases, Cargo provides via the [lints] table a way to statically declare those cfgs as expected.

\ Defining those custom cfgs as expected is done through the special check-cfg config under [lints.rust.unexpected_cfgs]:

Cargo.toml

[lints.rust] unexpected_cfgs = { level = "warn", check-cfg = ['cfg(loom)', 'cfg(fuzzing)'] }

Custom cfgs in build scripts

On the other hand some crates use custom cfgs that are enabled by some logic in the crate build.rs. For those crates Cargo provides a new instruction: cargo::rustc-check-cfg2 (or cargo:rustc-check-cfg for older Cargo version).

\ The syntax to use is described in the rustc book section checking configuration, but in a nutshell the basic syntax of --check-cfg is:

cfg(name, values("value1", "value2", ..., "valueN"))

\ Note that every custom cfgs must always be expected, regardless if the cfg is active or not!

build.rs example

build.rs:

fn main() { println!("cargo::rustc-check-cfg=cfg(has_foo)"); // ^^^^^^^^^^^^^^^^^^^^^^ new with Cargo 1.80 if has_foo() { println!("cargo::rustc-cfg=has_foo"); } }

Equivalence table

\ More details can be found in the rustc book.

Frequently asked questions

Can it be disabled?

For Cargo users, the feature is always on and cannot be disabled, but like any other lints it can be controlled: #![warn(unexpected_cfgs)].

Does the lint affect dependencies?

No, like most lints, unexpected_cfgs will only be reported for local packages thanks to cap-lints.

How does it interact with the RUSTFLAGS env?

You should be able to use the RUSTFLAGS environment variable like it was before. Currently --cfg arguments are not checked, only usage in code are.

\ This means that doing RUSTFLAGS="--cfg tokio_unstable" cargo check will not report any warnings, unless tokio_unstable is used within your local crates, in which case crate author will need to make sure that that custom cfg is expected with cargo::rustc-check-cfg in the build.rs of that crate.

How to expect custom cfgs without a build.rs?

UPDATE: Cargo with nightly-2024-05-19 now provides the [lints.rust.unexpected_cfgs.check-cfg] config to address the statically known custom cfgs.

\ There is currently no way to expect a custom cfg other than with cargo::rustc-check-cfg in a build.rs.

\ Crate authors that don't want to use a build.rs and cannot use [lints.rust.unexpected_cfgs.check-cfg], are encouraged to use Cargo features instead.

How does it interact with other build systems?

Non-Cargo based build systems are not affected by the lint by default. Build system authors that wish to have the same functionality should look at the rustc documentation for the --check-cfg flag for a detailed explanation of how to achieve the same functionality.

\

  1. The stabilized implementation and RFC 3013 diverge significantly, in particular there is only one form for --check-cfg: cfg() (instead of values() and names() being incomplete and subtlety incompatible with each other). ↩
  2. cargo::rustc-check-cfg will start working in Rust 1.80 (or nightly-2024-05-05). From Rust 1.77 to Rust 1.79 (inclusive) it is silently ignored. In Rust 1.76 and below a warning is emitted when used without the unstable Cargo flag -Zcheck-cfg. ↩

Urgau on behalf of The Cargo Team

\ Also published here

Disclaimer: The articles reposted on this site are sourced from public platforms and are provided for informational purposes only. They do not necessarily reflect the views of MEXC. All rights remain with the original authors. If you believe any content infringes on third-party rights, please contact [email protected] for removal. MEXC makes no guarantees regarding the accuracy, completeness, or timeliness of the content and is not responsible for any actions taken based on the information provided. The content does not constitute financial, legal, or other professional advice, nor should it be considered a recommendation or endorsement by MEXC.

You May Also Like

CME Group to launch options on XRP and SOL futures

CME Group to launch options on XRP and SOL futures

The post CME Group to launch options on XRP and SOL futures appeared on BitcoinEthereumNews.com. CME Group will offer options based on the derivative markets on Solana (SOL) and XRP. The new markets will open on October 13, after regulatory approval.  CME Group will expand its crypto products with options on the futures markets of Solana (SOL) and XRP. The futures market will start on October 13, after regulatory review and approval.  The options will allow the trading of MicroSol, XRP, and MicroXRP futures, with expiry dates available every business day, monthly, and quarterly. The new products will be added to the existing BTC and ETH options markets. ‘The launch of these options contracts builds on the significant growth and increasing liquidity we have seen across our suite of Solana and XRP futures,’ said Giovanni Vicioso, CME Group Global Head of Cryptocurrency Products. The options contracts will have two main sizes, tracking the futures contracts. The new market will be suitable for sophisticated institutional traders, as well as active individual traders. The addition of options markets singles out XRP and SOL as liquid enough to offer the potential to bet on a market direction.  The options on futures arrive a few months after the launch of SOL futures. Both SOL and XRP had peak volumes in August, though XRP activity has slowed down in September. XRP and SOL options to tap both institutions and active traders Crypto options are one of the indicators of market attitudes, with XRP and SOL receiving a new way to gauge sentiment. The contracts will be supported by the Cumberland team.  ‘As one of the biggest liquidity providers in the ecosystem, the Cumberland team is excited to support CME Group’s continued expansion of crypto offerings,’ said Roman Makarov, Head of Cumberland Options Trading at DRW. ‘The launch of options on Solana and XRP futures is the latest example of the…
Share
BitcoinEthereumNews2025/09/18 00:56
SEC Approves Decision Concerning Bitcoin and 9 Altcoins – The Dow Jones of Cryptocurrencies May Have Arrived

SEC Approves Decision Concerning Bitcoin and 9 Altcoins – The Dow Jones of Cryptocurrencies May Have Arrived

The post SEC Approves Decision Concerning Bitcoin and 9 Altcoins – The Dow Jones of Cryptocurrencies May Have Arrived appeared on BitcoinEthereumNews.com. While the cryptocurrency market doesn’t yet have a comprehensive index like the Dow Jones or S&P 500, Bitwise is one step closer to filling this void. The company’s new exchange-traded product, Bitwise 10 Crypto Index ETF (BITW), has begun trading, offering individual investors and financial advisors access to the 10 largest crypto assets in a single product. BITW’s portfolio includes the following digital assets: Bitcoin, Ethereum, XRP, Solana, Chainlink, Litecoin, Cardano, Avalanche, Sui, and Polkadot. Bitwise CEO and co-founder Hunter Horsley told CNBC that this conversion makes the company the first to include altcoins like Cardano, Avalanche, Sui, and Polkadot, which don’t currently have spot ETFs, in an ETF from a major asset manager. “This step significantly broadens the investor base that can access various crypto assets,” Horsley said. “This is particularly important for assets without a spot ETF.” According to the CEO, this ETF also provides significant accessibility for smaller investors who invest through individual retirement accounts (IRAs) or pension funds and are only able to access ETFs. BITW, previously an index fund containing the same assets, has been converted to an ETF and is now listed on the stock exchange with $1.5 billion in assets under management. The ETF structure provides additional benefits to investors by offering greater trading flexibility, tax advantages, and lower costs, along with broader trading permissions. This development follows an expanded wave of ETFs that followed the U.S. Securities and Exchange Commission (SEC) approval of spot Bitcoin ETFs in January 2024. Since then, asset managers have sought approval for a wider range of ETFs, from altcoins like Sui and Aptos to Trump-themed tokens and memecoins like Dogecoin. However, as the market matures, crypto assets are beginning to take on their own dynamics, suggesting that broad-based products like BITW could offer a diversification tool similar…
Share
BitcoinEthereumNews2025/12/10 06:40