diff options
| author | David Wood <david.wood@huawei.com> | 2022-06-28 10:56:18 +0100 |
|---|---|---|
| committer | David Wood <david.wood@huawei.com> | 2022-06-30 08:59:22 +0100 |
| commit | 4c63a2145c171bdf498c56f0c923baa7c135bb20 (patch) | |
| tree | 15445e4dc4504326baebcf1d9ed0488225e98011 | |
| parent | d433c9a4464110134d749f22163b1b3f108f2614 (diff) | |
lint: port non-shorthand pattern diagnostics
Signed-off-by: David Wood <david.wood@huawei.com>
| -rw-r--r-- | compiler/rustc_error_messages/locales/en-US/lint.ftl | 3 | ||||
| -rw-r--r-- | compiler/rustc_lint/src/builtin.rs | 20 |
2 files changed, 13 insertions, 10 deletions
diff --git a/compiler/rustc_error_messages/locales/en-US/lint.ftl b/compiler/rustc_error_messages/locales/en-US/lint.ftl index 34e211ba1df..a549c9fd98a 100644 --- a/compiler/rustc_error_messages/locales/en-US/lint.ftl +++ b/compiler/rustc_error_messages/locales/en-US/lint.ftl @@ -291,3 +291,6 @@ lint-builtin-while-true = denote infinite loops with `loop {"{"} ... {"}"}` .suggestion = use `loop` lint-builtin-box-pointers = type uses owned (Box type) pointers: {$ty} + +lint-builtin-non-shorthand-field-patterns = the `{$ident}:` in this pattern is redundant + .suggestion = use shorthand field pattern diff --git a/compiler/rustc_lint/src/builtin.rs b/compiler/rustc_lint/src/builtin.rs index 815916ac9e8..ed51a446c5a 100644 --- a/compiler/rustc_lint/src/builtin.rs +++ b/compiler/rustc_lint/src/builtin.rs @@ -256,26 +256,26 @@ impl<'tcx> LateLintPass<'tcx> for NonShorthandFieldPatterns { == Some(cx.tcx.field_index(fieldpat.hir_id, cx.typeck_results())) { cx.struct_span_lint(NON_SHORTHAND_FIELD_PATTERNS, fieldpat.span, |lint| { - let mut err = lint - .build(&format!("the `{}:` in this pattern is redundant", ident)); let binding = match binding_annot { hir::BindingAnnotation::Unannotated => None, hir::BindingAnnotation::Mutable => Some("mut"), hir::BindingAnnotation::Ref => Some("ref"), hir::BindingAnnotation::RefMut => Some("ref mut"), }; - let ident = if let Some(binding) = binding { + let suggested_ident = if let Some(binding) = binding { format!("{} {}", binding, ident) } else { ident.to_string() }; - err.span_suggestion( - fieldpat.span, - "use shorthand field pattern", - ident, - Applicability::MachineApplicable, - ); - err.emit(); + lint.build(fluent::lint::builtin_non_shorthand_field_patterns) + .set_arg("ident", ident.clone()) + .span_suggestion( + fieldpat.span, + fluent::lint::suggestion, + suggested_ident, + Applicability::MachineApplicable, + ) + .emit(); }); } } |
