lint: port non-shorthand pattern diagnostics

Signed-off-by: David Wood <david.wood@huawei.com>
This commit is contained in:
David Wood 2022-06-28 10:56:18 +01:00
parent d433c9a446
commit 4c63a2145c
2 changed files with 13 additions and 10 deletions

View File

@ -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

View File

@ -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();
});
}
}