From 466c74a996458c288aa526cb1514319a7f7b86fd Mon Sep 17 00:00:00 2001 From: Stephen Becker IV Date: Mon, 16 May 2016 22:37:05 -0700 Subject: [PATCH] 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 --- src/cargo/ops/cargo_new.rs | 16 +++++++++++++++- tests/test_cargo_new.rs | 8 ++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/cargo/ops/cargo_new.rs b/src/cargo/ops/cargo_new.rs index 2a878452c43..b364f8dea4e 100644 --- a/src/cargo/ops/cargo_new.rs +++ b/src/cargo/ops/cargo_new.rs @@ -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) diff --git a/tests/test_cargo_new.rs b/tests/test_cargo_new.rs index 8a4330d656b..88943b75c64 100644 --- a/tests/test_cargo_new.rs +++ b/tests/test_cargo_new.rs @@ -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)