Skip to content

Commit ec88bc2

Browse files
committed
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: _ _ _ _ _
1 parent 28b83ee commit ec88bc2

File tree

6 files changed

+58
-2
lines changed

6 files changed

+58
-2
lines changed

compiler/rustc_interface/src/errors.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ pub(crate) struct CrateNameInvalid<'a> {
2424
pub struct FerrisIdentifier {
2525
#[primary_span]
2626
pub spans: Vec<Span>,
27-
#[suggestion(code = "ferris", applicability = "maybe-incorrect")]
27+
#[suggestion(code = "{ferris_fix}", applicability = "maybe-incorrect")]
2828
pub first_span: Span,
29+
pub ferris_fix: &'static str,
2930
}
3031

3132
#[derive(Diagnostic)]

compiler/rustc_interface/src/passes.rs

+34-1
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,41 @@ fn early_lint_checks(tcx: TyCtxt<'_>, (): ()) {
301301
for (ident, mut spans) in identifiers.drain(..) {
302302
spans.sort();
303303
if ident == sym::ferris {
304+
enum FerrisFix {
305+
SnakeCase,
306+
ScreamingSnakeCase,
307+
PascalCase,
308+
}
309+
310+
impl FerrisFix {
311+
const fn as_str(self) -> &'static str {
312+
match self {
313+
FerrisFix::SnakeCase => "ferris",
314+
FerrisFix::ScreamingSnakeCase => "FERRIS",
315+
FerrisFix::PascalCase => "Ferris",
316+
}
317+
}
318+
}
319+
304320
let first_span = spans[0];
305-
sess.dcx().emit_err(errors::FerrisIdentifier { spans, first_span });
321+
let prev_source = sess.psess.source_map().span_to_prev_source(first_span);
322+
let ferris_fix = prev_source
323+
.map_or(FerrisFix::SnakeCase, |source| {
324+
let mut source_before_ferris = source.trim_end().split_whitespace().rev();
325+
match source_before_ferris.next() {
326+
Some("struct" | "trait" | "mod" | "union" | "type" | "enum") => {
327+
FerrisFix::PascalCase
328+
}
329+
Some("const" | "static") => FerrisFix::ScreamingSnakeCase,
330+
Some("mut") if source_before_ferris.next() == Some("static") => {
331+
FerrisFix::ScreamingSnakeCase
332+
}
333+
_ => FerrisFix::SnakeCase,
334+
}
335+
})
336+
.as_str();
337+
338+
sess.dcx().emit_err(errors::FerrisIdentifier { spans, first_span, ferris_fix });
306339
} else {
307340
sess.dcx().emit_err(errors::EmojiIdentifier { spans, ident });
308341
}

tests/ui/parser/ferris-static-mut.rs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
static mut 🦀: &str = "ferris!";//~ ERROR Ferris cannot be used as an identifier
2+
3+
fn main() {}
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: Ferris cannot be used as an identifier
2+
--> $DIR/ferris-static-mut.rs:1:12
3+
|
4+
LL | static mut 🦀: &str = "ferris!";
5+
| ^^ help: try using their name instead: `FERRIS`
6+
7+
error: aborting due to 1 previous error
8+

tests/ui/parser/ferris-struct.rs

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

tests/ui/parser/ferris-struct.stderr

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: Ferris cannot be used as an identifier
2+
--> $DIR/ferris-struct.rs:1:8
3+
|
4+
LL | struct 🦀 {}
5+
| ^^ help: try using their name instead: `Ferris`
6+
7+
error: aborting due to 1 previous error
8+

0 commit comments

Comments
 (0)