Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ferris 🦀 Identifier naming conventions #137356

Merged
merged 1 commit into from
Feb 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion compiler/rustc_interface/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ pub(crate) struct CrateNameInvalid<'a> {
pub struct FerrisIdentifier {
#[primary_span]
pub spans: Vec<Span>,
#[suggestion(code = "ferris", applicability = "maybe-incorrect")]
#[suggestion(code = "{ferris_fix}", applicability = "maybe-incorrect")]
pub first_span: Span,
pub ferris_fix: &'static str,
}

#[derive(Diagnostic)]
Expand Down
35 changes: 34 additions & 1 deletion compiler/rustc_interface/src/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,41 @@ fn early_lint_checks(tcx: TyCtxt<'_>, (): ()) {
for (ident, mut spans) in identifiers.drain(..) {
spans.sort();
if ident == sym::ferris {
enum FerrisFix {
SnakeCase,
ScreamingSnakeCase,
PascalCase,
}

impl FerrisFix {
const fn as_str(self) -> &'static str {
match self {
FerrisFix::SnakeCase => "ferris",
FerrisFix::ScreamingSnakeCase => "FERRIS",
FerrisFix::PascalCase => "Ferris",
}
}
}
Comment on lines +304 to +318
Copy link
Contributor

Choose a reason for hiding this comment

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

This doesn't really need to be an enum, it's used for a single purpose so constants would have been fine and more compact. Or even literals considering SnakeCase and PascalCase are each constructed once and ScreamingSnakeCase only twice.

(but already approved so 🤷 )


let first_span = spans[0];
sess.dcx().emit_err(errors::FerrisIdentifier { spans, first_span });
let prev_source = sess.psess.source_map().span_to_prev_source(first_span);
let ferris_fix = prev_source
.map_or(FerrisFix::SnakeCase, |source| {
let mut source_before_ferris = source.trim_end().split_whitespace().rev();
match source_before_ferris.next() {
Some("struct" | "trait" | "mod" | "union" | "type" | "enum") => {
FerrisFix::PascalCase
}
Some("const" | "static") => FerrisFix::ScreamingSnakeCase,
Some("mut") if source_before_ferris.next() == Some("static") => {
FerrisFix::ScreamingSnakeCase
}
_ => FerrisFix::SnakeCase,
}
})
.as_str();

sess.dcx().emit_err(errors::FerrisIdentifier { spans, first_span, ferris_fix });
} else {
sess.dcx().emit_err(errors::EmojiIdentifier { spans, ident });
}
Expand Down
3 changes: 3 additions & 0 deletions tests/ui/parser/ferris-static-mut.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
static mut 🦀: &str = "ferris!";//~ ERROR Ferris cannot be used as an identifier

fn main() {}
8 changes: 8 additions & 0 deletions tests/ui/parser/ferris-static-mut.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: Ferris cannot be used as an identifier
--> $DIR/ferris-static-mut.rs:1:12
|
LL | static mut 🦀: &str = "ferris!";
| ^^ help: try using their name instead: `FERRIS`

error: aborting due to 1 previous error

3 changes: 3 additions & 0 deletions tests/ui/parser/ferris-struct.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
struct 🦀 {}//~ ERROR Ferris cannot be used as an identifier

fn main() {}
8 changes: 8 additions & 0 deletions tests/ui/parser/ferris-struct.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: Ferris cannot be used as an identifier
--> $DIR/ferris-struct.rs:1:8
|
LL | struct 🦀 {}
| ^^ help: try using their name instead: `Ferris`

error: aborting due to 1 previous error

Loading