Skip to content

Commit

Permalink
Ban keywords from crate names
Browse files Browse the repository at this point in the history
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
  • Loading branch information
sbeckeriv committed May 17, 2016
1 parent 4e009a6 commit 466c74a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/cargo/ops/cargo_new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,21 @@ fn get_name<'a>(path: &'a Path, opts: &'a NewOptions, config: &Config) -> CargoR
}

fn check_name(name: &str) -> CargoResult<()> {
if name == "test" {

// Ban keywords + test list found at
// https://doc.rust-lang.org/grammar.html#keywords
let blacklist = ["abstract", "alignof", "as", "become", "box",
"break", "const", "continue", "crate", "do",
"else", "enum", "extern", "false", "final",
"fn", "for", "if", "impl", "in",
"let", "loop", "macro", "match", "mod",
"move", "mut", "offsetof", "override", "priv",
"proc", "pub", "pure", "ref", "return",
"self", "sizeof", "static", "struct",
"super", "test", "trait", "true", "type", "typeof",
"unsafe", "unsized", "use", "virtual", "where",
"while", "yield"];
if blacklist.contains(&name) {
bail!("The name `{}` cannot be used as a crate name\n\
use --name to override crate name",
name)
Expand Down
8 changes: 8 additions & 0 deletions tests/test_cargo_new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@ test!(reserved_name {
use --name to override crate name"));
});

test!(keyword_name {
assert_that(cargo_process("new").arg("pub"),
execs().with_status(101)
.with_stderr("\
[ERROR] The name `pub` cannot be used as a crate name\n\
use --name to override crate name"));
});

test!(rust_prefix_stripped {
assert_that(cargo_process("new").arg("rust-foo").env("USER", "foo"),
execs().with_status(0)
Expand Down

0 comments on commit 466c74a

Please sign in to comment.