Skip to content

Conversation

@creativcoder
Copy link
Contributor

According to Rust grammer https://doc.rust-lang.org/grammar.html#extern-crate-declarations for extern crate declarations, a crate name cannot start with a digit.

But, currently this rule is not upheld by cargo new as creating a project like:

cargo new 2048 would create a project named 2048 which obviously won't compile with crate declaration like extern crate 2048 by a consumer.

This obviously is a rare case in practice, but its always good to check i guess.

This PR adds a check to the new method and bails out with a message for any names starting with a digit.

PS: I noticed it while making a 2048 puzzle game as a library so thought it would be nice to add this check :)

@rust-highfive
Copy link

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @brson (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@alexcrichton
Copy link
Member

@bors: r+

Thanks!

@bors
Copy link
Contributor

bors commented Jan 15, 2017

📌 Commit 73aa117 has been approved by alexcrichton

@bors
Copy link
Contributor

bors commented Jan 15, 2017

⌛ Testing commit 73aa117 with merge 54f5248...

@bors
Copy link
Contributor

bors commented Jan 16, 2017

💥 Test timed out

@alexcrichton
Copy link
Member

alexcrichton commented Jan 16, 2017 via email

@bors
Copy link
Contributor

bors commented Jan 16, 2017

⌛ Testing commit 73aa117 with merge c20cda0...

@bors
Copy link
Contributor

bors commented Jan 17, 2017

💥 Test timed out

@alexcrichton
Copy link
Member

alexcrichton commented Jan 17, 2017 via email

@bors
Copy link
Contributor

bors commented Jan 17, 2017

⌛ Testing commit 73aa117 with merge a43403b...

bors added a commit that referenced this pull request Jan 17, 2017
Add a check for names starting with a digit

According to Rust grammer https://doc.rust-lang.org/grammar.html#extern-crate-declarations for extern crate declarations, a crate name cannot start with a digit.

But, currently this rule is not upheld by `cargo new` as creating a project like:

`cargo new 2048` would create a project named 2048 which obviously won't compile with crate declaration like `extern crate 2048` by a consumer.

This obviously is a rare case in practice, but its always good to check i guess.

This PR adds a check to the `new` method and `bail`s out with a message for any names starting with a digit.

PS: I noticed it while making a 2048 puzzle game as a library so thought it would be nice to add this check :)
@bors
Copy link
Contributor

bors commented Jan 17, 2017

☀️ Test successful - status-appveyor, status-travis
Approved by: alexcrichton
Pushing a43403b to master...

@bors bors merged commit 73aa117 into rust-lang:master Jan 17, 2017
name)
}

if let Some(ref c) = name.chars().nth(0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be better style to use .next() here instead of .nth(0)?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, char is Copy so we don't need the ref here

@ehuss ehuss added this to the 1.16.0 milestone Feb 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants