Tweak non_shorthand_field_patterns' suggestion

This commit is contained in:
Yuki Okushi 2019-12-22 07:59:38 +09:00
parent fc5deca214
commit 30e84b0244
6 changed files with 129 additions and 22 deletions

View File

@ -174,18 +174,27 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NonShorthandFieldPatterns {
// (Issue #49588) // (Issue #49588)
continue; continue;
} }
if let PatKind::Binding(_, _, ident, None) = fieldpat.pat.kind { if let PatKind::Binding(binding_annot, _, ident, None) = fieldpat.pat.kind {
if cx.tcx.find_field_index(ident, &variant) == if cx.tcx.find_field_index(ident, &variant) ==
Some(cx.tcx.field_index(fieldpat.hir_id, cx.tables)) { Some(cx.tcx.field_index(fieldpat.hir_id, cx.tables)) {
let mut err = cx.struct_span_lint(NON_SHORTHAND_FIELD_PATTERNS, let mut err = cx.struct_span_lint(NON_SHORTHAND_FIELD_PATTERNS,
fieldpat.span, fieldpat.span,
&format!("the `{}:` in this pattern is redundant", ident)); &format!("the `{}:` in this pattern is redundant", ident));
let subspan = cx.tcx.sess.source_map().span_through_char(fieldpat.span, let binding = match binding_annot {
':'); hir::BindingAnnotation::Unannotated => None,
err.span_suggestion_short( hir::BindingAnnotation::Mutable => Some("mut"),
subspan, hir::BindingAnnotation::Ref => Some("ref"),
"remove this", hir::BindingAnnotation::RefMut => Some("ref mut"),
ident.to_string(), };
let 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 Applicability::MachineApplicable
); );
err.emit(); err.emit();

View File

@ -0,0 +1,70 @@
// run-rustfix
#![allow(nonstandard_style, unused_variables, unused_mut)]
#![deny(non_shorthand_field_patterns)]
struct Foo {
x: isize,
y: isize,
}
fn main() {
{
let Foo {
x, //~ ERROR the `x:` in this pattern is redundant
ref y, //~ ERROR the `y:` in this pattern is redundant
} = Foo { x: 0, y: 0 };
let Foo {
x,
ref y,
} = Foo { x: 0, y: 0 };
}
{
const x: isize = 1;
match (Foo { x: 1, y: 1 }) {
Foo { x: x, ..} => {},
_ => {},
}
}
{
struct Bar {
x: x,
}
struct x;
match (Bar { x: x }) {
Bar { x: x } => {},
}
}
{
struct Bar {
x: Foo,
}
enum Foo { x }
match (Bar { x: Foo::x }) {
Bar { x: Foo::x } => {},
}
}
{
struct Baz {
x: isize,
y: isize,
z: isize,
}
let Baz {
mut x, //~ ERROR the `x:` in this pattern is redundant
ref y, //~ ERROR the `y:` in this pattern is redundant
ref mut z, //~ ERROR the `z:` in this pattern is redundant
} = Baz { x: 0, y: 0, z: 0 };
}
}

View File

@ -1,4 +1,6 @@
#![allow(nonstandard_style, unused_variables)] // run-rustfix
#![allow(nonstandard_style, unused_variables, unused_mut)]
#![deny(non_shorthand_field_patterns)] #![deny(non_shorthand_field_patterns)]
struct Foo { struct Foo {
@ -51,4 +53,18 @@ fn main() {
Bar { x: Foo::x } => {}, Bar { x: Foo::x } => {},
} }
} }
{
struct Baz {
x: isize,
y: isize,
z: isize,
}
let Baz {
x: mut x, //~ ERROR the `x:` in this pattern is redundant
y: ref y, //~ ERROR the `y:` in this pattern is redundant
z: ref mut z, //~ ERROR the `z:` in this pattern is redundant
} = Baz { x: 0, y: 0, z: 0 };
}
} }

View File

@ -1,24 +1,38 @@
error: the `x:` in this pattern is redundant error: the `x:` in this pattern is redundant
--> $DIR/lint-shorthand-field.rs:12:13 --> $DIR/lint-shorthand-field.rs:14:13
| |
LL | x: x, LL | x: x,
| --^^ | ^^^^ help: use shorthand field pattern: `x`
| |
| help: remove this
| |
note: lint level defined here note: lint level defined here
--> $DIR/lint-shorthand-field.rs:2:9 --> $DIR/lint-shorthand-field.rs:4:9
| |
LL | #![deny(non_shorthand_field_patterns)] LL | #![deny(non_shorthand_field_patterns)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: the `y:` in this pattern is redundant error: the `y:` in this pattern is redundant
--> $DIR/lint-shorthand-field.rs:13:13 --> $DIR/lint-shorthand-field.rs:15:13
| |
LL | y: ref y, LL | y: ref y,
| --^^^^^^ | ^^^^^^^^ help: use shorthand field pattern: `ref y`
| |
| help: remove this
error: aborting due to 2 previous errors error: the `x:` in this pattern is redundant
--> $DIR/lint-shorthand-field.rs:65:13
|
LL | x: mut x,
| ^^^^^^^^ help: use shorthand field pattern: `mut x`
error: the `y:` in this pattern is redundant
--> $DIR/lint-shorthand-field.rs:66:13
|
LL | y: ref y,
| ^^^^^^^^ help: use shorthand field pattern: `ref y`
error: the `z:` in this pattern is redundant
--> $DIR/lint-shorthand-field.rs:67:13
|
LL | z: ref mut z,
| ^^^^^^^^^^^^ help: use shorthand field pattern: `ref mut z`
error: aborting due to 5 previous errors

View File

@ -60,7 +60,7 @@ fn main() {
match d { match d {
Equinox { warp_factor: warp_factor } => {} Equinox { warp_factor: warp_factor } => {}
//~^ WARN this pattern is redundant //~^ WARN this pattern is redundant
//~| HELP remove this //~| HELP use shorthand field pattern
} }
println!("{} {}", registry_no, b); println!("{} {}", registry_no, b);
} }

View File

@ -77,9 +77,7 @@ warning: the `warp_factor:` in this pattern is redundant
--> $DIR/suggestions.rs:61:23 --> $DIR/suggestions.rs:61:23
| |
LL | Equinox { warp_factor: warp_factor } => {} LL | Equinox { warp_factor: warp_factor } => {}
| ------------^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^ help: use shorthand field pattern: `warp_factor`
| |
| help: remove this
| |
= note: `#[warn(non_shorthand_field_patterns)]` on by default = note: `#[warn(non_shorthand_field_patterns)]` on by default