Skip to content

Commit

Permalink
Auto merge of #12766 - shnaramn:WarnAboutNameCase, r=epage
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bors committed Oct 23, 2023
2 parents d2f6a04 + 901018c commit f5418fc
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/cargo/ops/cargo_new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,12 @@ fn check_name(
name
))?;
}
let name_in_lowercase = name.to_lowercase();
if name != name_in_lowercase {
shell.warn(format!(
"the name `{name}` is not snake_case or kebab-case which is recommended for package names, consider `{name_in_lowercase}`"
))?;
}

Ok(())
}
Expand Down
24 changes: 24 additions & 0 deletions tests/testsuite/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,7 @@ fn non_ascii_name() {
"\
[WARNING] the name `Привет` contains non-ASCII characters
Non-ASCII crate names are not supported by Rust.
[WARNING] the name `Привет` is not snake_case or kebab-case which is recommended for package names, consider `привет`
[CREATED] binary (application) `Привет` package
",
)
Expand Down Expand Up @@ -501,6 +502,29 @@ or change the name in Cargo.toml with:
.run();
}

#[cargo_test]
fn non_snake_case_name() {
cargo_process("new UPPERcase_name")
.with_stderr(
"\
[WARNING] the name `UPPERcase_name` is not snake_case or kebab-case which is recommended for package names, consider `uppercase_name`
[CREATED] binary (application) `UPPERcase_name` package
",
)
.run();
}

#[cargo_test]
fn kebab_case_name_is_accepted() {
cargo_process("new kebab-case-is-valid")
.with_stderr(
"\
[CREATED] binary (application) `kebab-case-is-valid` package
",
)
.run();
}

#[cargo_test]
fn git_default_branch() {
// Check for init.defaultBranch support.
Expand Down

0 comments on commit f5418fc

Please sign in to comment.