-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Lint against unexpected cfgs in [target.'cfg(...)']
#14581
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
Open
Urgau
wants to merge
9
commits into
rust-lang:master
Choose a base branch
from
Urgau:check-cfg-target-lint
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
710d5c8
Bump cargo-platform to 0.3.1
Urgau b0dd76e
Add `CfgExpr::walk` utility method
Urgau 9347c7c
Add `CheckCfg`/`ExpectedValues` to cargo-platform
Urgau 808d149
Add `-Zcheck-target-cfgs` in preparation for linting
Urgau 14ef0eb
Replace `rustc --print=cfg` parsing with handrolled loop
Urgau e63115f
Retrieve and parse `--print=check-cfg` in target info
Urgau d8d70f4
Add more unexpected target cfgs tests
Urgau bed778e
Lint against some unexpected target cfgs
Urgau decce97
Respect `[lints.rust.unexpected_cfgs.check-cfg]` lint config
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
//! check-cfg | ||
|
||
use std::{ | ||
collections::{HashMap, HashSet}, | ||
error::Error, | ||
fmt::Display, | ||
}; | ||
|
||
/// Check Config (aka `--check-cfg`/`--print=check-cfg` representation) | ||
#[derive(Debug, Default, Clone)] | ||
pub struct CheckCfg { | ||
/// Is `--check-cfg` activated | ||
pub exhaustive: bool, | ||
/// List of expected cfgs | ||
pub expecteds: HashMap<String, ExpectedValues>, | ||
} | ||
|
||
/// List of expected check-cfg values | ||
#[derive(Debug, Clone, PartialEq, Eq)] | ||
pub enum ExpectedValues { | ||
/// List of expected values | ||
/// | ||
/// - `#[cfg(foo)]` value is `None` | ||
/// - `#[cfg(foo = "")]` value is `Some("")` | ||
/// - `#[cfg(foo = "bar")]` value is `Some("bar")` | ||
Some(HashSet<Option<String>>), | ||
/// All values expected | ||
Any, | ||
} | ||
|
||
/// Error when parse a line from `--print=check-cfg` | ||
#[derive(Debug)] | ||
#[non_exhaustive] | ||
pub struct PrintCheckCfgParsingError; | ||
|
||
impl Error for PrintCheckCfgParsingError {} | ||
|
||
impl Display for PrintCheckCfgParsingError { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
f.write_str("error when parsing a `--print=check-cfg` line") | ||
} | ||
} | ||
|
||
impl CheckCfg { | ||
/// Parse a line from `--print=check-cfg` | ||
pub fn parse_print_check_cfg_line( | ||
&mut self, | ||
line: &str, | ||
) -> Result<(), PrintCheckCfgParsingError> { | ||
if line == "any()=any()" || line == "any()" { | ||
self.exhaustive = false; | ||
return Ok(()); | ||
} | ||
|
||
let mut value: HashSet<Option<String>> = HashSet::default(); | ||
let mut value_any_specified = false; | ||
let name: String; | ||
|
||
if let Some((n, val)) = line.split_once('=') { | ||
name = n.to_string(); | ||
|
||
if val == "any()" { | ||
value_any_specified = true; | ||
} else if val.is_empty() { | ||
// no value, nothing to add | ||
} else if let Some(val) = maybe_quoted_value(val) { | ||
value.insert(Some(val.to_string())); | ||
} else { | ||
// missing quotes and non-empty | ||
return Err(PrintCheckCfgParsingError); | ||
} | ||
} else { | ||
name = line.to_string(); | ||
value.insert(None); | ||
} | ||
|
||
self.expecteds | ||
.entry(name) | ||
.and_modify(|v| match v { | ||
ExpectedValues::Some(_) if value_any_specified => *v = ExpectedValues::Any, | ||
ExpectedValues::Some(v) => v.extend(value.clone()), | ||
ExpectedValues::Any => {} | ||
}) | ||
.or_insert_with(|| { | ||
if value_any_specified { | ||
ExpectedValues::Any | ||
} else { | ||
ExpectedValues::Some(value) | ||
} | ||
}); | ||
Ok(()) | ||
} | ||
} | ||
|
||
fn maybe_quoted_value<'a>(v: &'a str) -> Option<&'a str> { | ||
// strip "" around the value, e.g. "linux" -> linux | ||
v.strip_prefix('"').and_then(|v| v.strip_suffix('"')) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
use cargo_platform::{CheckCfg, ExpectedValues}; | ||
use std::collections::HashSet; | ||
|
||
#[test] | ||
fn print_check_cfg_none() { | ||
let mut check_cfg = CheckCfg::default(); | ||
|
||
check_cfg.parse_print_check_cfg_line("cfg_a").unwrap(); | ||
assert_eq!( | ||
*check_cfg.expecteds.get("cfg_a").unwrap(), | ||
ExpectedValues::Some(HashSet::from([None])) | ||
); | ||
} | ||
|
||
#[test] | ||
fn print_check_cfg_empty() { | ||
let mut check_cfg = CheckCfg::default(); | ||
|
||
check_cfg.parse_print_check_cfg_line("cfg_b=").unwrap(); | ||
assert_eq!( | ||
*check_cfg.expecteds.get("cfg_b").unwrap(), | ||
ExpectedValues::Some(HashSet::from([])) | ||
); | ||
} | ||
|
||
#[test] | ||
fn print_check_cfg_any() { | ||
let mut check_cfg = CheckCfg::default(); | ||
|
||
check_cfg.parse_print_check_cfg_line("cfg_c=any()").unwrap(); | ||
assert_eq!( | ||
*check_cfg.expecteds.get("cfg_c").unwrap(), | ||
ExpectedValues::Any | ||
); | ||
|
||
check_cfg.parse_print_check_cfg_line("cfg_c").unwrap(); | ||
assert_eq!( | ||
*check_cfg.expecteds.get("cfg_c").unwrap(), | ||
ExpectedValues::Any | ||
); | ||
} | ||
|
||
#[test] | ||
fn print_check_cfg_value() { | ||
let mut check_cfg = CheckCfg::default(); | ||
|
||
check_cfg | ||
.parse_print_check_cfg_line("cfg_d=\"test\"") | ||
.unwrap(); | ||
assert_eq!( | ||
*check_cfg.expecteds.get("cfg_d").unwrap(), | ||
ExpectedValues::Some(HashSet::from([Some("test".to_string())])) | ||
); | ||
|
||
check_cfg | ||
.parse_print_check_cfg_line("cfg_d=\"tmp\"") | ||
.unwrap(); | ||
assert_eq!( | ||
*check_cfg.expecteds.get("cfg_d").unwrap(), | ||
ExpectedValues::Some(HashSet::from([ | ||
Some("test".to_string()), | ||
Some("tmp".to_string()) | ||
])) | ||
); | ||
} | ||
|
||
#[test] | ||
fn print_check_cfg_none_and_value() { | ||
let mut check_cfg = CheckCfg::default(); | ||
|
||
check_cfg.parse_print_check_cfg_line("cfg").unwrap(); | ||
check_cfg.parse_print_check_cfg_line("cfg=\"foo\"").unwrap(); | ||
assert_eq!( | ||
*check_cfg.expecteds.get("cfg").unwrap(), | ||
ExpectedValues::Some(HashSet::from([None, Some("foo".to_string())])) | ||
); | ||
} | ||
|
||
#[test] | ||
fn print_check_cfg_quote_in_value() { | ||
let mut check_cfg = CheckCfg::default(); | ||
|
||
check_cfg | ||
.parse_print_check_cfg_line("cfg_e=\"quote_in_value\"\"") | ||
.unwrap(); | ||
assert_eq!( | ||
*check_cfg.expecteds.get("cfg_e").unwrap(), | ||
ExpectedValues::Some(HashSet::from([Some("quote_in_value\"".to_string())])) | ||
); | ||
} | ||
|
||
#[test] | ||
fn print_check_cfg_value_and_any() { | ||
let mut check_cfg = CheckCfg::default(); | ||
|
||
// having both a value and `any()` shouldn't be possible but better | ||
// handle this correctly anyway | ||
|
||
check_cfg | ||
.parse_print_check_cfg_line("cfg_1=\"foo\"") | ||
.unwrap(); | ||
check_cfg.parse_print_check_cfg_line("cfg_1=any()").unwrap(); | ||
assert_eq!( | ||
*check_cfg.expecteds.get("cfg_1").unwrap(), | ||
ExpectedValues::Any | ||
); | ||
|
||
check_cfg.parse_print_check_cfg_line("cfg_2=any()").unwrap(); | ||
check_cfg | ||
.parse_print_check_cfg_line("cfg_2=\"foo\"") | ||
.unwrap(); | ||
assert_eq!( | ||
*check_cfg.expecteds.get("cfg_2").unwrap(), | ||
ExpectedValues::Any | ||
); | ||
} | ||
|
||
#[test] | ||
#[should_panic] | ||
fn print_check_cfg_missing_quote_value() { | ||
let mut check_cfg = CheckCfg::default(); | ||
check_cfg.parse_print_check_cfg_line("foo=bar").unwrap(); | ||
} |
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.