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)