-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Update unexpected_cfgs
lint for Cargo new check-cfg
config
#125219
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+210
−58
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
05f77d1
Update `unexpected_cfgs` lint for Cargo new `check-cfg` config
Urgau 7cb84fb
Prefer suggesting string-literal for Cargo `check-cfg` lint config
Urgau b5ec9bb
Add Cargo specific doc regarding his interaction with `--check-cfg`
Urgau 3b47f4c
Refer to the Cargo specific doc in the check-cfg diagnostics
Urgau bc8e034
Link to the check-cfg doc section in the `unexpected_cfgs` lint doc
Urgau ccd3e99
Fix quote escaping inside check-cfg value
Urgau File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Urgau marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# Cargo Specifics - Checking Conditional Configurations | ||
|
||
<!-- | ||
This page is currently (as of May 2024) the canonical place for describing the interaction | ||
between Cargo and --check-cfg. It is placed in the rustc book rather than the Cargo book | ||
since check-cfg is primarely a Rust/rustc feature and is therefor consider by T-cargo to | ||
be an implementation detail, at least --check-cfg and the unexpected_cfgs are owned by | ||
rustc, not Cargo. | ||
--> | ||
|
||
This document is intented to summarize the principal ways Cargo interacts with | ||
the `unexpected_cfgs` lint and `--check-cfg` flag. It is not intended to provide | ||
individual details, for that refer to the [`--check-cfg` documentation](../check-cfg.md) and | ||
to the [Cargo book](../../cargo/index.html). | ||
|
||
## Cargo feature | ||
|
||
*See the [`[features]` section in the Cargo book][cargo-features] for more details.* | ||
|
||
With the `[features]` table Cargo provides a mechanism to express conditional compilation and | ||
optional dependencies. Cargo *automatically* declares corresponding cfgs for every feature as | ||
expected. | ||
|
||
`Cargo.toml`: | ||
```toml | ||
[features] | ||
serde = ["dep:serde"] | ||
my_feature = [] | ||
``` | ||
|
||
[cargo-features]: ../../cargo/reference/features.html | ||
|
||
## `check-cfg` in `[lints.rust]` table | ||
|
||
<!-- Note that T-Cargo considers `[lints.rust.unexpected_cfgs.check-cfg]` to be an | ||
implementation detail and is therefor not documented in Cargo, we therefor do that ourself --> | ||
|
||
*See the [`[lints]` section in the Cargo book][cargo-lints-table] for more details.* | ||
|
||
When using a staticlly known custom config (ie. not dependant on a build-script), Cargo provides | ||
the custom lint config `check-cfg` under `[lints.rust.unexpected_cfgs]`. | ||
|
||
It can be used to set custom static [`--check-cfg`](../check-cfg.md) args, it is mainly useful when | ||
the list of expected cfgs is known is advance. | ||
|
||
`Cargo.toml`: | ||
```toml | ||
[lints.rust] | ||
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(has_foo)'] } | ||
``` | ||
|
||
[cargo-lints-table]: ../../cargo/reference/manifest.html#the-lints-section | ||
|
||
## `cargo::rustc-check-cfg` for `build.rs`/build-script | ||
|
||
*See the [`cargo::rustc-check-cfg` section in the Cargo book][cargo-rustc-check-cfg] for more details.* | ||
|
||
When setting a custom config with [`cargo::rustc-cfg`][cargo-rustc-cfg], Cargo provides the | ||
corollary instruction: [`cargo::rustc-check-cfg`][cargo-rustc-check-cfg] to expect custom configs. | ||
|
||
`build.rs`: | ||
```rust,ignore (cannot-test-this-because-has_foo-isnt-declared) | ||
fn main() { | ||
println!("cargo::rustc-check-cfg=cfg(has_foo)"); | ||
// ^^^^^^^^^^^^^^^^^^^^^^ new with Cargo 1.80 | ||
if has_foo() { | ||
println!("cargo::rustc-cfg=has_foo"); | ||
} | ||
} | ||
``` | ||
|
||
[cargo-rustc-cfg]: ../../cargo/reference/build-scripts.html#rustc-cfg | ||
[cargo-rustc-check-cfg]: ../../cargo/reference/build-scripts.html#rustc-check-cfg |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.