From b061bc5e8317a2b776f793cb445a988814c50133 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Thu, 4 Apr 2024 13:52:58 -0500 Subject: [PATCH] refactor(toml): Abstract out bin name validation --- src/cargo/util/toml/targets.rs | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/src/cargo/util/toml/targets.rs b/src/cargo/util/toml/targets.rs index b76454a48..d90dac656 100644 --- a/src/cargo/util/toml/targets.rs +++ b/src/cargo/util/toml/targets.rs @@ -301,18 +301,17 @@ fn to_bin_targets( // This loop performs basic checks on each of the TomlTarget in `bins`. for bin in &bins { + validate_bin_name(bin, warnings)?; + // For each binary, check if the `filename` parameter is populated. If it is, // check if the corresponding cargo feature has been activated. if bin.filename.is_some() { features.require(Feature::different_binary_name())?; } - validate_target_name(bin, "binary", "bin", warnings)?; - - let name = name_or_panic(bin).to_owned(); - if let Some(crate_types) = bin.crate_types() { if !crate_types.is_empty() { + let name = name_or_panic(bin); errors.push(format!( "the target `{}` is a binary and can't have any \ crate-types set (currently \"{}\")", @@ -323,20 +322,13 @@ fn to_bin_targets( } if bin.proc_macro() == Some(true) { + let name = name_or_panic(bin); errors.push(format!( "the target `{}` is a binary and can't have `proc-macro` \ set `true`", name )); } - - if restricted_names::is_conflicting_artifact_name(&name) { - anyhow::bail!( - "the binary target name `{}` is forbidden, \ - it conflicts with cargo's build directory names", - name - ) - } } validate_unique_names(&bins, "binary")?; @@ -794,6 +786,19 @@ fn validate_lib_name(target: &TomlTarget, warnings: &mut Vec) -> CargoRe Ok(()) } +fn validate_bin_name(bin: &TomlTarget, warnings: &mut Vec) -> CargoResult<()> { + validate_target_name(bin, "binary", "bin", warnings)?; + let name = name_or_panic(bin).to_owned(); + if restricted_names::is_conflicting_artifact_name(&name) { + anyhow::bail!( + "the binary target name `{name}` is forbidden, \ + it conflicts with cargo's build directory names", + ) + } + + Ok(()) +} + fn validate_target_name( target: &TomlTarget, target_kind_human: &str,