Rollup merge of #137356 - nik-rev:FERRIS, r=compiler-errors

Ferris 🦀 Identifier naming conventions

You cannot use Ferris as an identifier in Rust, this code will suggest to correct the  🦀 to `ferris`:

```rs
fn main() {
  let  🦀 = 4;
}
```

But it also suggests to correct to `ferris` in these cases, too:

```rs
struct  🦀 {}
fn main() {}
```

^ suggests: `ferris`
~ with this PR: `Ferris`

```rs
static 🦀: &str = "ferris!";
fn main() {}
```

^ suggests: `ferris`
~ with this PR: `FERRIS`

This is my first pull requests here!
This commit is contained in:
Matthias Krüger 2025-02-22 11:36:44 +01:00 committed by GitHub
commit 72e41e5d65
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 58 additions and 2 deletions

View file

@ -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)]

View file

@ -305,8 +305,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",
}
}
}
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 });
}