1
Fork 0

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` .suggestion = use `loop`
lint-builtin-box-pointers = type uses owned (Box type) pointers: {$ty} 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())) == Some(cx.tcx.field_index(fieldpat.hir_id, cx.typeck_results()))
{ {
cx.struct_span_lint(NON_SHORTHAND_FIELD_PATTERNS, fieldpat.span, |lint| { 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 { let binding = match binding_annot {
hir::BindingAnnotation::Unannotated => None, hir::BindingAnnotation::Unannotated => None,
hir::BindingAnnotation::Mutable => Some("mut"), hir::BindingAnnotation::Mutable => Some("mut"),
hir::BindingAnnotation::Ref => Some("ref"), hir::BindingAnnotation::Ref => Some("ref"),
hir::BindingAnnotation::RefMut => Some("ref mut"), hir::BindingAnnotation::RefMut => Some("ref mut"),
}; };
let ident = if let Some(binding) = binding { let suggested_ident = if let Some(binding) = binding {
format!("{} {}", binding, ident) format!("{} {}", binding, ident)
} else { } else {
ident.to_string() ident.to_string()
}; };
err.span_suggestion( lint.build(fluent::lint::builtin_non_shorthand_field_patterns)
.set_arg("ident", ident.clone())
.span_suggestion(
fieldpat.span, fieldpat.span,
"use shorthand field pattern", fluent::lint::suggestion,
ident, suggested_ident,
Applicability::MachineApplicable, Applicability::MachineApplicable,
); )
err.emit(); .emit();
}); });
} }
} }