Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit a9bbe31

Browse files
committed
migrate: nonstandard_style.rs
1 parent 56fc66d commit a9bbe31

File tree

2 files changed

+156
-81
lines changed

2 files changed

+156
-81
lines changed

compiler/rustc_lint/src/lints.rs

Lines changed: 105 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,114 @@ use rustc_errors::{fluent, AddSubdiagnostic, Applicability, DecorateLint, Emissi
22
use rustc_hir::def_id::DefId;
33
use rustc_macros::{LintDiagnostic, SessionSubdiagnostic};
44
use rustc_middle::ty::{Predicate, Ty, TyCtxt};
5-
use rustc_span::{Span, Symbol};
5+
use rustc_span::{symbol::Ident, Span, Symbol};
66

77
use crate::LateContext;
88

9+
#[derive(LintDiagnostic)]
10+
#[diag(lint_non_camel_case_type)]
11+
pub struct NonCamelCaseType<'a> {
12+
pub sort: &'a str,
13+
pub name: &'a str,
14+
#[subdiagnostic]
15+
pub sub: NonCamelCaseTypeSub,
16+
}
17+
18+
#[derive(SessionSubdiagnostic)]
19+
pub enum NonCamelCaseTypeSub {
20+
#[label(label)]
21+
Label {
22+
#[primary_span]
23+
span: Span,
24+
},
25+
#[suggestion(suggestion, code = "{replace}", applicability = "maybe-incorrect")]
26+
Suggestion {
27+
#[primary_span]
28+
span: Span,
29+
replace: String,
30+
},
31+
}
32+
33+
#[derive(LintDiagnostic)]
34+
#[diag(lint_non_snake_case)]
35+
pub struct NonSnakeCaseDiag<'a> {
36+
pub sort: &'a str,
37+
pub name: &'a str,
38+
pub sc: String,
39+
#[subdiagnostic]
40+
pub sub: NonSnakeCaseDiagSub,
41+
}
42+
43+
pub enum NonSnakeCaseDiagSub {
44+
Label { span: Span },
45+
Help,
46+
RenameOrConvertSuggestion { span: Span, suggestion: Ident },
47+
ConvertSuggestion { span: Span, suggestion: String },
48+
SuggestionAndNote { span: Span },
49+
}
50+
51+
impl AddSubdiagnostic for NonSnakeCaseDiagSub {
52+
fn add_to_diagnostic(self, diag: &mut rustc_errors::Diagnostic) {
53+
match self {
54+
NonSnakeCaseDiagSub::Label { span } => {
55+
diag.span_label(span, fluent::label);
56+
}
57+
NonSnakeCaseDiagSub::Help => {
58+
diag.help(fluent::help);
59+
}
60+
NonSnakeCaseDiagSub::ConvertSuggestion { span, suggestion } => {
61+
diag.span_suggestion(
62+
span,
63+
fluent::convert_suggestion,
64+
suggestion,
65+
Applicability::MaybeIncorrect,
66+
);
67+
}
68+
NonSnakeCaseDiagSub::RenameOrConvertSuggestion { span, suggestion } => {
69+
diag.span_suggestion(
70+
span,
71+
fluent::rename_or_convert_suggestion,
72+
suggestion,
73+
Applicability::MaybeIncorrect,
74+
);
75+
}
76+
NonSnakeCaseDiagSub::SuggestionAndNote { span } => {
77+
diag.note(fluent::cannot_convert_note);
78+
diag.span_suggestion(
79+
span,
80+
fluent::rename_suggestion,
81+
"",
82+
Applicability::MaybeIncorrect,
83+
);
84+
}
85+
};
86+
}
87+
}
88+
89+
#[derive(LintDiagnostic)]
90+
#[diag(lint_non_upper_case_global)]
91+
pub struct NonUpperCaseGlobal<'a> {
92+
pub sort: &'a str,
93+
pub name: &'a str,
94+
#[subdiagnostic]
95+
pub sub: NonUpperCaseGlobalSub,
96+
}
97+
98+
#[derive(SessionSubdiagnostic)]
99+
pub enum NonUpperCaseGlobalSub {
100+
#[label(label)]
101+
Label {
102+
#[primary_span]
103+
span: Span,
104+
},
105+
#[suggestion(suggestion, code = "{replace}", applicability = "maybe-incorrect")]
106+
Suggestion {
107+
#[primary_span]
108+
span: Span,
109+
replace: String,
110+
},
111+
}
112+
9113
#[derive(LintDiagnostic)]
10114
#[diag(lint_noop_method_call)]
11115
#[note]

compiler/rustc_lint/src/nonstandard_style.rs

Lines changed: 51 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
#![deny(rustc::untranslatable_diagnostic)]
2+
#![deny(rustc::diagnostic_outside_of_impl)]
3+
use crate::lints::{
4+
NonCamelCaseType, NonCamelCaseTypeSub, NonSnakeCaseDiag, NonSnakeCaseDiagSub,
5+
NonUpperCaseGlobal, NonUpperCaseGlobalSub,
6+
};
17
use crate::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintContext};
28
use rustc_ast as ast;
39
use rustc_attr as attr;
4-
use rustc_errors::{fluent, Applicability};
510
use rustc_hir as hir;
611
use rustc_hir::def::{DefKind, Res};
712
use rustc_hir::intravisit::FnKind;
@@ -136,30 +141,17 @@ impl NonCamelCaseTypes {
136141
let name = ident.name.as_str();
137142

138143
if !is_camel_case(name) {
139-
cx.struct_span_lint(
144+
let cc = to_camel_case(name);
145+
let sub = if *name != cc {
146+
NonCamelCaseTypeSub::Suggestion { span: ident.span, replace: cc }
147+
} else {
148+
NonCamelCaseTypeSub::Label { span: ident.span }
149+
};
150+
cx.emit_spanned_lint(
140151
NON_CAMEL_CASE_TYPES,
141152
ident.span,
142-
fluent::lint_non_camel_case_type,
143-
|lint| {
144-
let cc = to_camel_case(name);
145-
// We cannot provide meaningful suggestions
146-
// if the characters are in the category of "Lowercase Letter".
147-
if *name != cc {
148-
lint.span_suggestion(
149-
ident.span,
150-
fluent::suggestion,
151-
to_camel_case(name),
152-
Applicability::MaybeIncorrect,
153-
);
154-
} else {
155-
lint.span_label(ident.span, fluent::label);
156-
}
157-
158-
lint.set_arg("sort", sort);
159-
lint.set_arg("name", name);
160-
lint
161-
},
162-
)
153+
NonCamelCaseType { sort, name, sub },
154+
);
163155
}
164156
}
165157
}
@@ -294,47 +286,37 @@ impl NonSnakeCase {
294286
let name = ident.name.as_str();
295287

296288
if !is_snake_case(name) {
297-
cx.struct_span_lint(NON_SNAKE_CASE, ident.span, fluent::lint_non_snake_case, |lint| {
298-
let sc = NonSnakeCase::to_snake_case(name);
299-
// We cannot provide meaningful suggestions
300-
// if the characters are in the category of "Uppercase Letter".
301-
if name != sc {
302-
// We have a valid span in almost all cases, but we don't have one when linting a crate
303-
// name provided via the command line.
304-
if !ident.span.is_dummy() {
305-
let sc_ident = Ident::from_str_and_span(&sc, ident.span);
306-
let (message, suggestion) = if sc_ident.is_reserved() {
307-
// We shouldn't suggest a reserved identifier to fix non-snake-case identifiers.
308-
// Instead, recommend renaming the identifier entirely or, if permitted,
309-
// escaping it to create a raw identifier.
310-
if sc_ident.name.can_be_raw() {
311-
(fluent::rename_or_convert_suggestion, sc_ident.to_string())
312-
} else {
313-
lint.note(fluent::cannot_convert_note);
314-
(fluent::rename_suggestion, String::new())
289+
let span = ident.span;
290+
let sc = NonSnakeCase::to_snake_case(name);
291+
// We cannot provide meaningful suggestions
292+
// if the characters are in the category of "Uppercase Letter".
293+
let sub = if name != sc {
294+
// We have a valid span in almost all cases, but we don't have one when linting a crate
295+
// name provided via the command line.
296+
if !span.is_dummy() {
297+
let sc_ident = Ident::from_str_and_span(&sc, span);
298+
if sc_ident.is_reserved() {
299+
// We shouldn't suggest a reserved identifier to fix non-snake-case identifiers.
300+
// Instead, recommend renaming the identifier entirely or, if permitted,
301+
// escaping it to create a raw identifier.
302+
if sc_ident.name.can_be_raw() {
303+
NonSnakeCaseDiagSub::RenameOrConvertSuggestion {
304+
span,
305+
suggestion: sc_ident,
315306
}
316307
} else {
317-
(fluent::convert_suggestion, sc.clone())
318-
};
319-
320-
lint.span_suggestion(
321-
ident.span,
322-
message,
323-
suggestion,
324-
Applicability::MaybeIncorrect,
325-
);
308+
NonSnakeCaseDiagSub::SuggestionAndNote { span }
309+
}
326310
} else {
327-
lint.help(fluent::help);
311+
NonSnakeCaseDiagSub::ConvertSuggestion { span, suggestion: sc.clone() }
328312
}
329313
} else {
330-
lint.span_label(ident.span, fluent::label);
314+
NonSnakeCaseDiagSub::Help
331315
}
332-
333-
lint.set_arg("sort", sort);
334-
lint.set_arg("name", name);
335-
lint.set_arg("sc", sc);
336-
lint
337-
});
316+
} else {
317+
NonSnakeCaseDiagSub::Label { span }
318+
};
319+
cx.emit_spanned_lint(NON_SNAKE_CASE, span, NonSnakeCaseDiag { sort, name, sc, sub });
338320
}
339321
}
340322
}
@@ -490,30 +472,19 @@ impl NonUpperCaseGlobals {
490472
fn check_upper_case(cx: &LateContext<'_>, sort: &str, ident: &Ident) {
491473
let name = ident.name.as_str();
492474
if name.chars().any(|c| c.is_lowercase()) {
493-
cx.struct_span_lint(
475+
let uc = NonSnakeCase::to_snake_case(&name).to_uppercase();
476+
// We cannot provide meaningful suggestions
477+
// if the characters are in the category of "Lowercase Letter".
478+
let sub = if *name != uc {
479+
NonUpperCaseGlobalSub::Suggestion { span: ident.span, replace: uc }
480+
} else {
481+
NonUpperCaseGlobalSub::Label { span: ident.span }
482+
};
483+
cx.emit_spanned_lint(
494484
NON_UPPER_CASE_GLOBALS,
495485
ident.span,
496-
fluent::lint_non_upper_case_global,
497-
|lint| {
498-
let uc = NonSnakeCase::to_snake_case(&name).to_uppercase();
499-
// We cannot provide meaningful suggestions
500-
// if the characters are in the category of "Lowercase Letter".
501-
if *name != uc {
502-
lint.span_suggestion(
503-
ident.span,
504-
fluent::suggestion,
505-
uc,
506-
Applicability::MaybeIncorrect,
507-
);
508-
} else {
509-
lint.span_label(ident.span, fluent::label);
510-
}
511-
512-
lint.set_arg("sort", sort);
513-
lint.set_arg("name", name);
514-
lint
515-
},
516-
)
486+
NonUpperCaseGlobal { sort, name, sub },
487+
);
517488
}
518489
}
519490
}

0 commit comments

Comments
 (0)