Skip to content

Better error for extern crate accidental-hyphen #48437

Closed
@gibfahn

Description

@gibfahn

AIUI currently the standard is for crate names to be hyphenated, but the module name (not sure on the correct terminology) must be underscored.

For example you add bit-vec to the Cargo.toml, but extern crate bit_vec; to src/lib.rs.

image

cargo new rust-foo && cd rust-foo
cargo add bit_vec
sed -i '1s/^/extern crate bit-vec;\n/' src/lib.rs
cat src/lib.rs
cargo build
sed -i 's/bit-vec/bit_vec/' src/lib.rs
cargo build

So instead of this error, it would be great if the compiler could suggest extern crate bit_vec;.

cargo build                                                                                                                                                                       ~/tmp/rust-foo (!✦)
   Compiling foo v0.1.0 (file:///Users/gib/tmp/rust-foo)
error: expected one of `;` or `as`, found `-`
 --> src/lib.rs:1:17
  |
1 | extern crate bit-vec;
  |                 ^ expected one of `;` or `as` here

error: aborting due to previous error

error: Could not compile `foo`.

To learn more, run the command again with --verbose.

Came from a discussion with @nrc on twitter: https://twitter.com/gibfahn/status/966755092383739904

I couldn't find an existing issue for this, sorry if it's a duplicate.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions