|
| 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 | +}; |
1 | 7 | use crate::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintContext}; |
2 | 8 | use rustc_ast as ast; |
3 | 9 | use rustc_attr as attr; |
4 | | -use rustc_errors::{fluent, Applicability}; |
5 | 10 | use rustc_hir as hir; |
6 | 11 | use rustc_hir::def::{DefKind, Res}; |
7 | 12 | use rustc_hir::intravisit::FnKind; |
@@ -136,30 +141,17 @@ impl NonCamelCaseTypes { |
136 | 141 | let name = ident.name.as_str(); |
137 | 142 |
|
138 | 143 | 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( |
140 | 151 | NON_CAMEL_CASE_TYPES, |
141 | 152 | 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 | + ); |
163 | 155 | } |
164 | 156 | } |
165 | 157 | } |
@@ -294,47 +286,37 @@ impl NonSnakeCase { |
294 | 286 | let name = ident.name.as_str(); |
295 | 287 |
|
296 | 288 | 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, |
315 | 306 | } |
316 | 307 | } 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 | + } |
326 | 310 | } else { |
327 | | - lint.help(fluent::help); |
| 311 | + NonSnakeCaseDiagSub::ConvertSuggestion { span, suggestion: sc.clone() } |
328 | 312 | } |
329 | 313 | } else { |
330 | | - lint.span_label(ident.span, fluent::label); |
| 314 | + NonSnakeCaseDiagSub::Help |
331 | 315 | } |
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 }); |
338 | 320 | } |
339 | 321 | } |
340 | 322 | } |
@@ -490,30 +472,19 @@ impl NonUpperCaseGlobals { |
490 | 472 | fn check_upper_case(cx: &LateContext<'_>, sort: &str, ident: &Ident) { |
491 | 473 | let name = ident.name.as_str(); |
492 | 474 | 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( |
494 | 484 | NON_UPPER_CASE_GLOBALS, |
495 | 485 | 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 | + ); |
517 | 488 | } |
518 | 489 | } |
519 | 490 | } |
|
0 commit comments