Skip to content

Commit

Permalink
fix: naming convention "ferris" suggestion for idents named 🦀
Browse files Browse the repository at this point in the history
test: add tests for correct ferris capitalization

fix: add "struct"

style: use rustfmt

style: remove newline

fix: _

_

_
  • Loading branch information
nik-rev committed Feb 21, 2025
1 parent 28b83ee commit 7df0af0
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 2 deletions.
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
52 changes: 51 additions & 1 deletion compiler/rustc_interface/src/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,57 @@ fn early_lint_checks(tcx: TyCtxt<'_>, (): ()) {
spans.sort();
if ident == sym::ferris {
let first_span = spans[0];
sess.dcx().emit_err(errors::FerrisIdentifier { spans, first_span });

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",
}
}
}

// Obtain the symbol that is before Ferris.
let symbols = &sess.psess.symbol_gallery.symbols.lock();
let mut symbols: Vec<_> = symbols.iter().collect();

// sort by spans
symbols.sort_unstable_by_key(|k| k.1);
let ferris_fix = match symbols.binary_search_by(|(_, span)| span.cmp(&&first_span))
{
Ok(index) | Err(index) if index > 0 => {
let keyword = symbols[index - 1].0.as_str();
// if we have e.g. `static mut`
if keyword == "mut" {
let keyword_before =
symbols.get(index - 2).map(|s| s.0.as_str()).unwrap_or_default();
if keyword_before == "static" {
FerrisFix::ScreamingSnakeCase
} else {
FerrisFix::SnakeCase
}
} else {
match keyword {
"const" | "static" => FerrisFix::ScreamingSnakeCase,
"struct" | "trait" | "mod" | "union" | "type" | "enum" => {
FerrisFix::PascalCase
}
_ => FerrisFix::SnakeCase,
}
}
}
_ => 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

0 comments on commit 7df0af0

Please sign in to comment.