Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit d479677

Browse files
authoredFeb 22, 2025
Rollup merge of rust-lang#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!
2 parents dd763f3 + ec88bc2 commit d479677

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
@@ -305,8 +305,41 @@ fn early_lint_checks(tcx: TyCtxt<'_>, (): ()) {
305305
for (ident, mut spans) in identifiers.drain(..) {
306306
spans.sort();
307307
if ident == sym::ferris {
308+
enum FerrisFix {
309+
SnakeCase,
310+
ScreamingSnakeCase,
311+
PascalCase,
312+
}
313+
314+
impl FerrisFix {
315+
const fn as_str(self) -> &'static str {
316+
match self {
317+
FerrisFix::SnakeCase => "ferris",
318+
FerrisFix::ScreamingSnakeCase => "FERRIS",
319+
FerrisFix::PascalCase => "Ferris",
320+
}
321+
}
322+
}
323+
308324
let first_span = spans[0];
309-
sess.dcx().emit_err(errors::FerrisIdentifier { spans, first_span });
325+
let prev_source = sess.psess.source_map().span_to_prev_source(first_span);
326+
let ferris_fix = prev_source
327+
.map_or(FerrisFix::SnakeCase, |source| {
328+
let mut source_before_ferris = source.trim_end().split_whitespace().rev();
329+
match source_before_ferris.next() {
330+
Some("struct" | "trait" | "mod" | "union" | "type" | "enum") => {
331+
FerrisFix::PascalCase
332+
}
333+
Some("const" | "static") => FerrisFix::ScreamingSnakeCase,
334+
Some("mut") if source_before_ferris.next() == Some("static") => {
335+
FerrisFix::ScreamingSnakeCase
336+
}
337+
_ => FerrisFix::SnakeCase,
338+
}
339+
})
340+
.as_str();
341+
342+
sess.dcx().emit_err(errors::FerrisIdentifier { spans, first_span, ferris_fix });
310343
} else {
311344
sess.dcx().emit_err(errors::EmojiIdentifier { spans, ident });
312345
}

‎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)
Please sign in to comment.