Skip to content

Commit a43403b

Browse files
committed
Auto merge of #3542 - creativcoder:check-digit-name, r=alexcrichton
Add a check for names starting with a digit According to Rust grammer https://doc.rust-lang.org/grammar.html#extern-crate-declarations for extern crate declarations, a crate name cannot start with a digit. But, currently this rule is not upheld by `cargo new` as creating a project like: `cargo new 2048` would create a project named 2048 which obviously won't compile with crate declaration like `extern crate 2048` by a consumer. This obviously is a rare case in practice, but its always good to check i guess. This PR adds a check to the `new` method and `bail`s out with a message for any names starting with a digit. PS: I noticed it while making a 2048 puzzle game as a library so thought it would be nice to add this check :)
2 parents 6c79cf9 + 73aa117 commit a43403b

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

src/cargo/ops/cargo_new.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,13 @@ fn check_name(name: &str) -> CargoResult<()> {
135135
name)
136136
}
137137

138+
if let Some(ref c) = name.chars().nth(0) {
139+
if c.is_digit(10) {
140+
bail!("Package names starting with a digit cannot be used as a crate name\n\
141+
use --name to override crate name")
142+
}
143+
}
144+
138145
for c in name.chars() {
139146
if c.is_alphanumeric() { continue }
140147
if c == '_' || c == '-' { continue }

0 commit comments

Comments
 (0)