Let's assume I have the following feature defined in Cargo.toml
:
[features]
my_feature = []
And the following code lives in src/lib.rs
:
#[cfg(feature = "my_feature")]
fn f() { /* ... */ }
#[cfg(not(feature = "my_faeture"))] // <-- Mind the typo!
fn f() { /* ... */ }
How can I enforce, that the feature-strings are matched against the list of both explicitly defined and implicitly available features in Cargo.toml
so that for instance typos could be avoided?
Turn on (or off) automatic spelling and grammar checking In the Spelling & Grammar dialog box, under Spelling, check or clear the Check spelling as you type box. Under Grammar, check or clear the Check grammar as you type box. Close the dialog box to save your changes.
To automatically correct common errors as you type, use the options in the AutoCorrect tab of the AutoCorrect dialog box to set up automatic correction of capitalization errors and commonly misspelled words. You can also create special characters.
Use dynamic spelling To enable dynamic spelling, choose Edit > Spelling > Dynamic Spelling.
Here's how. Select the text where you'd like to disable spell check or press Ctrl+A to select the entire document. On the Review tab, click Editor, and then click Set Proofing Language. In the Language box, click Don't check spelling or grammar, and then click OK.
When RFC 3013, "Checking conditional compilation at compile time", is implemented, there will be warnings for a #[cfg]
referring to a feature name that is not declared by Cargo, just as you're asking for. However, the implementation only just got started (Sep 28, 2021).
The means of operation described in the RFC is just as you suggested, ‘cargo
would pass down the flags to rustc
’.
It may be worth noting that this will not check all conditions appearing in the source text; as described in the RFC:
This lint will not be able to detect invalid #[cfg] tests that are within modules that are not compiled, presumably because an ancestor mod is disabled.
So, it will not confirm that all #[cfg(feature)]
are valid on a single cargo check
— you will need to test with your various features or combinations of features. But those are the same combinations that you would need anyway to check for compile errors in all of the regular source code that could be enabled; once you do that, the lint will assure you that you don't have any #[cfg(feature)]
that are never enabled due to a typo.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With