Skip to content

Commit 24aa3ca

Browse files
committed
Auto merge of #4924 - daschl:name-help, r=alexcrichton
Do not suggest --name as a fix if its already used. This changeset partially fixes #4903 in that it doesn't suggest using --name if its already being used (but the name is still wrong).
2 parents 55b72dd + 7625cfa commit 24aa3ca

File tree

2 files changed

+29
-12
lines changed

2 files changed

+29
-12
lines changed

src/cargo/ops/cargo_new.rs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use toml;
2020
#[derive(Clone, Copy, Debug, PartialEq)]
2121
pub enum VersionControl { Git, Hg, Pijul, Fossil, NoVcs }
2222

23+
#[derive(Debug)]
2324
pub struct NewOptions<'a> {
2425
pub version_control: Option<VersionControl>,
2526
pub bin: bool,
@@ -118,7 +119,14 @@ fn get_name<'a>(path: &'a Path, opts: &'a NewOptions, config: &Config) -> CargoR
118119
}
119120
}
120121

121-
fn check_name(name: &str, is_bin: bool) -> CargoResult<()> {
122+
fn check_name(name: &str, opts: &NewOptions) -> CargoResult<()> {
123+
124+
// If --name is already used to override, no point in suggesting it
125+
// again as a fix.
126+
let name_help = match opts.name {
127+
Some(_) => "",
128+
None => "\nuse --name to override crate name",
129+
};
122130

123131
// Ban keywords + test list found at
124132
// https://doc.rust-lang.org/grammar.html#keywords
@@ -133,25 +141,26 @@ fn check_name(name: &str, is_bin: bool) -> CargoResult<()> {
133141
"super", "test", "trait", "true", "type", "typeof",
134142
"unsafe", "unsized", "use", "virtual", "where",
135143
"while", "yield"];
136-
if blacklist.contains(&name) || (is_bin && is_bad_artifact_name(name)) {
137-
bail!("The name `{}` cannot be used as a crate name\n\
138-
use --name to override crate name",
139-
name)
144+
if blacklist.contains(&name) || (opts.bin && is_bad_artifact_name(name)) {
145+
bail!("The name `{}` cannot be used as a crate name{}",
146+
name,
147+
name_help)
140148
}
141149

142150
if let Some(ref c) = name.chars().nth(0) {
143151
if c.is_digit(10) {
144-
bail!("Package names starting with a digit cannot be used as a crate name\n\
145-
use --name to override crate name")
152+
bail!("Package names starting with a digit cannot be used as a crate name{}",
153+
name_help)
146154
}
147155
}
148156

149157
for c in name.chars() {
150158
if c.is_alphanumeric() { continue }
151159
if c == '_' || c == '-' { continue }
152-
bail!("Invalid character `{}` in crate name: `{}`\n\
153-
use --name to override crate name",
154-
c, name)
160+
bail!("Invalid character `{}` in crate name: `{}`{}",
161+
c,
162+
name,
163+
name_help)
155164
}
156165
Ok(())
157166
}
@@ -279,7 +288,7 @@ pub fn new(opts: &NewOptions, config: &Config) -> CargoResult<()> {
279288
}
280289

281290
let name = get_name(&path, opts, config)?;
282-
check_name(name, opts.bin)?;
291+
check_name(name, opts)?;
283292

284293
let mkopts = MkOptions {
285294
version_control: opts.version_control,
@@ -309,7 +318,7 @@ pub fn init(opts: &NewOptions, config: &Config) -> CargoResult<()> {
309318
}
310319

311320
let name = get_name(&path, opts, config)?;
312-
check_name(name, opts.bin)?;
321+
check_name(name, opts)?;
313322

314323
let mut src_paths_types = vec![];
315324

tests/new.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,3 +416,11 @@ Usage:
416416
cargo new [..]
417417
"));
418418
}
419+
420+
#[test]
421+
fn explicit_invalid_name_not_suggested() {
422+
assert_that(cargo_process("new").arg("--name").arg("10-invalid").arg("a"),
423+
execs().with_status(101)
424+
.with_stderr("\
425+
[ERROR] Package names starting with a digit cannot be used as a crate name"));
426+
}

0 commit comments

Comments
 (0)