Skip to content

Commit 096a69d

Browse files
committed
lint: port non-standard style diagnostics
Signed-off-by: David Wood <[email protected]>
1 parent 7ee4aa7 commit 096a69d

File tree

2 files changed

+38
-18
lines changed

2 files changed

+38
-18
lines changed

compiler/rustc_error_messages/locales/en-US/lint.ftl

+16
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,19 @@ lint-non-fmt-panic-braces =
108108
}
109109
.note = this message is not used as a format string, but will be in Rust 2021
110110
.suggestion = add a "{"{"}{"}"}" format string to use the message literally
111+
112+
lint-non-camel-case-type = {$sort} `{$name}` should have an upper camel case name
113+
.suggestion = convert the identifier to upper camel case
114+
.label = should have an UpperCamelCase name
115+
116+
lint-non-snake-case = {$sort} `{$name}` should have a snake case name
117+
.rename-or-convert-suggestion = rename the identifier or convert it to a snake case raw identifier
118+
.cannot-convert-note = `{$sc}` cannot be used as a raw identifier
119+
.rename-suggestion = rename the identifier
120+
.convert-suggestion = convert the identifier to snake case
121+
.help = convert the identifier to snake case: `{$sc}`
122+
.label = should have a snake_case name
123+
124+
lint-non-upper_case-global = {$sort} `{$name}` should have an upper case name
125+
.suggestion = convert the identifier to upper case
126+
.label = should have an UPPER_CASE name

compiler/rustc_lint/src/nonstandard_style.rs

+22-18
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintContext};
22
use rustc_ast as ast;
33
use rustc_attr as attr;
4-
use rustc_errors::Applicability;
4+
use rustc_errors::{fluent, Applicability};
55
use rustc_hir as hir;
66
use rustc_hir::def::{DefKind, Res};
77
use rustc_hir::intravisit::FnKind;
@@ -137,22 +137,23 @@ impl NonCamelCaseTypes {
137137

138138
if !is_camel_case(name) {
139139
cx.struct_span_lint(NON_CAMEL_CASE_TYPES, ident.span, |lint| {
140-
let msg = format!("{} `{}` should have an upper camel case name", sort, name);
141-
let mut err = lint.build(&msg);
140+
let mut err = lint.build(fluent::lint::non_camel_case_type);
142141
let cc = to_camel_case(name);
143142
// We cannot provide meaningful suggestions
144143
// if the characters are in the category of "Lowercase Letter".
145144
if *name != cc {
146145
err.span_suggestion(
147146
ident.span,
148-
"convert the identifier to upper camel case",
147+
fluent::lint::suggestion,
149148
to_camel_case(name),
150149
Applicability::MaybeIncorrect,
151150
);
152151
} else {
153-
err.span_label(ident.span, "should have an UpperCamelCase name");
152+
err.span_label(ident.span, fluent::lint::label);
154153
}
155154

155+
err.set_arg("sort", sort);
156+
err.set_arg("name", name);
156157
err.emit();
157158
})
158159
}
@@ -281,11 +282,10 @@ impl NonSnakeCase {
281282
if !is_snake_case(name) {
282283
cx.struct_span_lint(NON_SNAKE_CASE, ident.span, |lint| {
283284
let sc = NonSnakeCase::to_snake_case(name);
284-
let msg = format!("{} `{}` should have a snake case name", sort, name);
285-
let mut err = lint.build(&msg);
285+
let mut err = lint.build(fluent::lint::non_snake_case);
286286
// We cannot provide meaningful suggestions
287287
// if the characters are in the category of "Uppercase Letter".
288-
if *name != sc {
288+
if name != sc {
289289
// We have a valid span in almost all cases, but we don't have one when linting a crate
290290
// name provided via the command line.
291291
if !ident.span.is_dummy() {
@@ -295,13 +295,13 @@ impl NonSnakeCase {
295295
// Instead, recommend renaming the identifier entirely or, if permitted,
296296
// escaping it to create a raw identifier.
297297
if sc_ident.name.can_be_raw() {
298-
("rename the identifier or convert it to a snake case raw identifier", sc_ident.to_string())
298+
(fluent::lint::rename_or_convert_suggestion, sc_ident.to_string())
299299
} else {
300-
err.note(&format!("`{}` cannot be used as a raw identifier", sc));
301-
("rename the identifier", String::new())
300+
err.note(fluent::lint::cannot_convert_note);
301+
(fluent::lint::rename_suggestion, String::new())
302302
}
303303
} else {
304-
("convert the identifier to snake case", sc)
304+
(fluent::lint::convert_suggestion, sc.clone())
305305
};
306306

307307
err.span_suggestion(
@@ -311,12 +311,15 @@ impl NonSnakeCase {
311311
Applicability::MaybeIncorrect,
312312
);
313313
} else {
314-
err.help(&format!("convert the identifier to snake case: `{}`", sc));
314+
err.help(fluent::lint::help);
315315
}
316316
} else {
317-
err.span_label(ident.span, "should have a snake_case name");
317+
err.span_label(ident.span, fluent::lint::label);
318318
}
319319

320+
err.set_arg("sort", sort);
321+
err.set_arg("name", name);
322+
err.set_arg("sc", sc);
320323
err.emit();
321324
});
322325
}
@@ -488,21 +491,22 @@ impl NonUpperCaseGlobals {
488491
if name.chars().any(|c| c.is_lowercase()) {
489492
cx.struct_span_lint(NON_UPPER_CASE_GLOBALS, ident.span, |lint| {
490493
let uc = NonSnakeCase::to_snake_case(&name).to_uppercase();
491-
let mut err =
492-
lint.build(&format!("{} `{}` should have an upper case name", sort, name));
494+
let mut err = lint.build(fluent::lint::non_upper_case_global);
493495
// We cannot provide meaningful suggestions
494496
// if the characters are in the category of "Lowercase Letter".
495497
if *name != uc {
496498
err.span_suggestion(
497499
ident.span,
498-
"convert the identifier to upper case",
500+
fluent::lint::suggestion,
499501
uc,
500502
Applicability::MaybeIncorrect,
501503
);
502504
} else {
503-
err.span_label(ident.span, "should have an UPPER_CASE name");
505+
err.span_label(ident.span, fluent::lint::label);
504506
}
505507

508+
err.set_arg("sort", sort);
509+
err.set_arg("name", name);
506510
err.emit();
507511
})
508512
}

0 commit comments

Comments
 (0)