Skip to content

Commit

Permalink
Clean up cfg name checks
Browse files Browse the repository at this point in the history
  • Loading branch information
mqudsi committed May 13, 2024
1 parent 2314147 commit fb3c305
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,14 @@ pub fn declare_feature(name: &str, enabled: bool) {
///
/// See also: [`declare_cfg_values()`].
pub fn declare_cfg(name: &str, enabled: bool) {
if name.chars().any(|c| !c.is_ascii_alphanumeric()) {
if name.chars().any(|c| !c.is_ascii_alphanumeric() && c != '_') {
panic!("Invalid cfg name {name}");
}
// Use #[cfg(version = "1.80.0")] when RFC 2523 finally lands
if rustc_version().map(|v| !v.cmp(&(1, 80, 0)).is_lt()).unwrap_or(true) {
if rustc_version()
.map(|v| !v.cmp(&(1, 80, 0)).is_lt())
.unwrap_or(true)
{
println!("cargo:rustc-check-cfg=cfg({name})");
}
if enabled {
Expand Down Expand Up @@ -251,11 +254,14 @@ pub fn enable_cfg(name: &str) {
/// Call this before calling [`set_cfg_value()`] to avoid compiler warnings about unrecognized cfg
/// values under rust 1.80+.
pub fn declare_cfg_values(name: &str, values: &[&str]) {
if name.chars().any(|c| !c.is_ascii_alphanumeric()) {
if name.chars().any(|c| !c.is_ascii_alphanumeric() && c != '_') {
panic!("Invalid cfg name {name}");
}
// Use #[cfg(version = "1.80.0")] when RFC 2523 finally lands
if rustc_version().map(|v| !v.cmp(&(1, 80, 0)).is_lt()).unwrap_or(true) {
if rustc_version()
.map(|v| !v.cmp(&(1, 80, 0)).is_lt())
.unwrap_or(true)
{
let payload = values
.iter()
.inspect(|value| {
Expand Down Expand Up @@ -897,7 +903,7 @@ impl From<cc::Build> for Target {

/// Sanitizes a string for use in a file name
fn fs_sanitize(s: &str) -> Cow<'_, str> {
if s.chars().all(|c| c.is_ascii_alphanumeric()) {
if s.chars().all(|c| c.is_ascii_alphanumeric() || c == '_') {
return Cow::Borrowed(s);
}

Expand Down

0 comments on commit fb3c305

Please sign in to comment.