fix: naming convention "ferris" suggestion for idents named 🦀

test: add tests for correct ferris capitalization

fix: add "struct"

style: use rustfmt

style: remove newline

fix: _

_

_

_

_
This commit is contained in:
Nikita Revenco 2025-02-21 01:00:19 +00:00
parent 28b83ee596
commit ec88bc2e00
6 changed files with 58 additions and 2 deletions

View file

@ -24,8 +24,9 @@ pub(crate) struct CrateNameInvalid<'a> {
pub struct FerrisIdentifier { pub struct FerrisIdentifier {
#[primary_span] #[primary_span]
pub spans: Vec<Span>, pub spans: Vec<Span>,
#[suggestion(code = "ferris", applicability = "maybe-incorrect")] #[suggestion(code = "{ferris_fix}", applicability = "maybe-incorrect")]
pub first_span: Span, pub first_span: Span,
pub ferris_fix: &'static str,
} }
#[derive(Diagnostic)] #[derive(Diagnostic)]

View file

@ -301,8 +301,41 @@ fn early_lint_checks(tcx: TyCtxt<'_>, (): ()) {
for (ident, mut spans) in identifiers.drain(..) { for (ident, mut spans) in identifiers.drain(..) {
spans.sort(); spans.sort();
if ident == sym::ferris { 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]; 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 { } else {
sess.dcx().emit_err(errors::EmojiIdentifier { spans, ident }); sess.dcx().emit_err(errors::EmojiIdentifier { spans, ident });
} }

View file

@ -0,0 +1,3 @@
static mut 🦀: &str = "ferris!";//~ ERROR Ferris cannot be used as an identifier
fn main() {}

View file

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

View file

@ -0,0 +1,3 @@
struct 🦀 {}//~ ERROR Ferris cannot be used as an identifier
fn main() {}

View file

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