Skip to content

Commit 466c74a

Browse files
committed
Ban keywords from crate names
Dearest Reviewer, This pull request extends the banned list of project names to include all of the current keywords for rust. I got the list of keywords from https://doc.rust-lang.org/grammar.html#keywords . This closes #2699 . The original ticket said warn but I figured how test is handled is most likely how all banned names should be handled. Oddly enough a few keywords have made their way to the crates.io. fn and proc are both examples I found spot checking keywords. I do not there is any action to take on those crates. Thanks Becker
1 parent 4e009a6 commit 466c74a

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/cargo/ops/cargo_new.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,21 @@ fn get_name<'a>(path: &'a Path, opts: &'a NewOptions, config: &Config) -> CargoR
8888
}
8989

9090
fn check_name(name: &str) -> CargoResult<()> {
91-
if name == "test" {
91+
92+
// Ban keywords + test list found at
93+
// https://doc.rust-lang.org/grammar.html#keywords
94+
let blacklist = ["abstract", "alignof", "as", "become", "box",
95+
"break", "const", "continue", "crate", "do",
96+
"else", "enum", "extern", "false", "final",
97+
"fn", "for", "if", "impl", "in",
98+
"let", "loop", "macro", "match", "mod",
99+
"move", "mut", "offsetof", "override", "priv",
100+
"proc", "pub", "pure", "ref", "return",
101+
"self", "sizeof", "static", "struct",
102+
"super", "test", "trait", "true", "type", "typeof",
103+
"unsafe", "unsized", "use", "virtual", "where",
104+
"while", "yield"];
105+
if blacklist.contains(&name) {
92106
bail!("The name `{}` cannot be used as a crate name\n\
93107
use --name to override crate name",
94108
name)

tests/test_cargo_new.rs

+8
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,14 @@ test!(reserved_name {
101101
use --name to override crate name"));
102102
});
103103

104+
test!(keyword_name {
105+
assert_that(cargo_process("new").arg("pub"),
106+
execs().with_status(101)
107+
.with_stderr("\
108+
[ERROR] The name `pub` cannot be used as a crate name\n\
109+
use --name to override crate name"));
110+
});
111+
104112
test!(rust_prefix_stripped {
105113
assert_that(cargo_process("new").arg("rust-foo").env("USER", "foo"),
106114
execs().with_status(0)

0 commit comments

Comments
 (0)