Skip to content

Commit 4ff0809

Browse files
committed
fix(build-rs)!: Prefer None/empty-Vec to empty-String
1 parent 27c5772 commit 4ff0809

File tree

2 files changed

+28
-21
lines changed

2 files changed

+28
-21
lines changed

crates/build-rs/src/allow_use.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{process::Command, sync::OnceLock};
33
fn rust_version_minor() -> u32 {
44
static VERSION_MINOR: OnceLock<u32> = OnceLock::new();
55
*VERSION_MINOR.get_or_init(|| {
6-
version_minor(&crate::input::cargo_pkg_rust_version())
6+
version_minor(&crate::input::cargo_pkg_rust_version().unwrap_or_default())
77
// assume build-rs's MSRV if none specified for the current package
88
.unwrap_or_else(|| version_minor(env!("CARGO_PKG_RUST_VERSION")).unwrap())
99
})

crates/build-rs/src/input.rs

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -197,12 +197,12 @@ mod cfg {
197197
/// Disambiguation of the [target ABI](https://doc.rust-lang.org/stable/reference/conditional-compilation.html#target_abi)
198198
/// when the [target env](cargo_cfg_target_env) isn't sufficient.
199199
///
200-
/// For historical reasons, this value is only defined as not the empty-string when
200+
/// For historical reasons, this value is only defined as `Some` when
201201
/// actually needed for disambiguation. Thus, for example, on many GNU platforms,
202-
/// this value will be empty.
202+
/// this value will be `None`.
203203
#[track_caller]
204-
pub fn cargo_cfg_target_abi() -> String {
205-
to_string(var_or_panic("CARGO_CFG_TARGET_ABI"))
204+
pub fn cargo_cfg_target_abi() -> Option<String> {
205+
to_opt(var_or_panic("CARGO_CFG_TARGET_ABI")).map(to_string)
206206
}
207207

208208
/// The CPU [target architecture](https://doc.rust-lang.org/stable/reference/conditional-compilation.html#target_arch).
@@ -480,8 +480,8 @@ pub fn cargo_pkg_version_patch() -> u64 {
480480

481481
/// The pre-release version of your package.
482482
#[track_caller]
483-
pub fn cargo_pkg_version_pre() -> String {
484-
to_string(var_or_panic("CARGO_PKG_VERSION_PRE"))
483+
pub fn cargo_pkg_version_pre() -> Option<String> {
484+
to_opt(var_or_panic("CARGO_PKG_VERSION_PRE")).map(to_string)
485485
}
486486

487487
/// Colon separated list of authors from the manifest of your package.
@@ -498,45 +498,45 @@ pub fn cargo_pkg_name() -> String {
498498

499499
/// The description from the manifest of your package.
500500
#[track_caller]
501-
pub fn cargo_pkg_description() -> String {
502-
to_string(var_or_panic("CARGO_PKG_DESCRIPTION"))
501+
pub fn cargo_pkg_description() -> Option<String> {
502+
to_opt(var_or_panic("CARGO_PKG_DESCRIPTION")).map(to_string)
503503
}
504504

505505
/// The home page from the manifest of your package.
506506
#[track_caller]
507-
pub fn cargo_pkg_homepage() -> String {
508-
to_string(var_or_panic("CARGO_PKG_HOMEPAGE"))
507+
pub fn cargo_pkg_homepage() -> Option<String> {
508+
to_opt(var_or_panic("CARGO_PKG_HOMEPAGE")).map(to_string)
509509
}
510510

511511
/// The repository from the manifest of your package.
512512
#[track_caller]
513-
pub fn cargo_pkg_repository() -> String {
514-
to_string(var_or_panic("CARGO_PKG_REPOSITORY"))
513+
pub fn cargo_pkg_repository() -> Option<String> {
514+
to_opt(var_or_panic("CARGO_PKG_REPOSITORY")).map(to_string)
515515
}
516516

517517
/// The license from the manifest of your package.
518518
#[track_caller]
519-
pub fn cargo_pkg_license() -> String {
520-
to_string(var_or_panic("CARGO_PKG_LICENSE"))
519+
pub fn cargo_pkg_license() -> Option<String> {
520+
to_opt(var_or_panic("CARGO_PKG_LICENSE")).map(to_string)
521521
}
522522

523523
/// The license file from the manifest of your package.
524524
#[track_caller]
525-
pub fn cargo_pkg_license_file() -> PathBuf {
526-
to_path(var_or_panic("CARGO_PKG_LICENSE_FILE"))
525+
pub fn cargo_pkg_license_file() -> Option<PathBuf> {
526+
to_opt(var_or_panic("CARGO_PKG_LICENSE_FILE")).map(to_path)
527527
}
528528

529529
/// The Rust version from the manifest of your package. Note that this is the
530530
/// minimum Rust version supported by the package, not the current Rust version.
531531
#[track_caller]
532-
pub fn cargo_pkg_rust_version() -> String {
533-
to_string(var_or_panic("CARGO_PKG_RUST_VERSION"))
532+
pub fn cargo_pkg_rust_version() -> Option<String> {
533+
to_opt(var_or_panic("CARGO_PKG_RUST_VERSION")).map(to_string)
534534
}
535535

536536
/// Path to the README file of your package.
537537
#[track_caller]
538-
pub fn cargo_pkg_readme() -> PathBuf {
539-
to_path(var_or_panic("CARGO_PKG_README"))
538+
pub fn cargo_pkg_readme() -> Option<PathBuf> {
539+
to_opt(var_or_panic("CARGO_PKG_README")).map(to_path)
540540
}
541541

542542
fn is_present(key: &str) -> bool {
@@ -563,8 +563,15 @@ fn to_string(value: std::ffi::OsString) -> String {
563563
}
564564
}
565565

566+
fn to_opt(value: std::ffi::OsString) -> Option<std::ffi::OsString> {
567+
(!value.is_empty()).then_some(value)
568+
}
569+
566570
#[track_caller]
567571
fn to_strings(value: std::ffi::OsString, sep: char) -> Vec<String> {
572+
if value.is_empty() {
573+
return Vec::new();
574+
}
568575
let value = to_string(value);
569576
value.split(sep).map(str::to_owned).collect()
570577
}

0 commit comments

Comments
 (0)