Skip to content

Commit f5418fc

Browse files
committed
Auto merge of #12766 - shnaramn:WarnAboutNameCase, r=epage
Warn about crate name's format when creating new crate ### What does this PR try to resolve? Warns about a crate's name during creation (`crate new ...`) if it doesn't follow the preferred snake_case format. Fixes #2708 The warning message uses the language mentioned in [RFC 430](https://github.com/rust-lang/rfcs/blob/master/text/0430-finalizing-naming-conventions.md#general-naming-conventions). ### How should we test and review this PR? Verified existing tests succeeded with updates. Added new tests to verify fix. ### Additional information The link to [API naming guidelines](https://rust-lang.github.io/api-guidelines/naming.html) was not used since it still stays `unclear` for naming convention for crates.
2 parents d2f6a04 + 901018c commit f5418fc

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/cargo/ops/cargo_new.rs

+6
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,12 @@ fn check_name(
258258
name
259259
))?;
260260
}
261+
let name_in_lowercase = name.to_lowercase();
262+
if name != name_in_lowercase {
263+
shell.warn(format!(
264+
"the name `{name}` is not snake_case or kebab-case which is recommended for package names, consider `{name_in_lowercase}`"
265+
))?;
266+
}
261267

262268
Ok(())
263269
}

tests/testsuite/new.rs

+24
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,7 @@ fn non_ascii_name() {
451451
"\
452452
[WARNING] the name `Привет` contains non-ASCII characters
453453
Non-ASCII crate names are not supported by Rust.
454+
[WARNING] the name `Привет` is not snake_case or kebab-case which is recommended for package names, consider `привет`
454455
[CREATED] binary (application) `Привет` package
455456
",
456457
)
@@ -501,6 +502,29 @@ or change the name in Cargo.toml with:
501502
.run();
502503
}
503504

505+
#[cargo_test]
506+
fn non_snake_case_name() {
507+
cargo_process("new UPPERcase_name")
508+
.with_stderr(
509+
"\
510+
[WARNING] the name `UPPERcase_name` is not snake_case or kebab-case which is recommended for package names, consider `uppercase_name`
511+
[CREATED] binary (application) `UPPERcase_name` package
512+
",
513+
)
514+
.run();
515+
}
516+
517+
#[cargo_test]
518+
fn kebab_case_name_is_accepted() {
519+
cargo_process("new kebab-case-is-valid")
520+
.with_stderr(
521+
"\
522+
[CREATED] binary (application) `kebab-case-is-valid` package
523+
",
524+
)
525+
.run();
526+
}
527+
504528
#[cargo_test]
505529
fn git_default_branch() {
506530
// Check for init.defaultBranch support.

0 commit comments

Comments
 (0)