Skip to content

Commit f358d92

Browse files
authored
Rollup merge of rust-lang#67500 - JohnTitor:non-shorthand-field-patterns, r=Centril
Tweak non_shorthand_field_patterns' suggestion Fixes rust-lang#66434 r? @estebank
2 parents cdb8cd3 + 30e84b0 commit f358d92

File tree

6 files changed

+129
-22
lines changed

6 files changed

+129
-22
lines changed

src/librustc_lint/builtin.rs

+16-7
Original file line numberDiff line numberDiff line change
@@ -174,18 +174,27 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NonShorthandFieldPatterns {
174174
// (Issue #49588)
175175
continue;
176176
}
177-
if let PatKind::Binding(_, _, ident, None) = fieldpat.pat.kind {
177+
if let PatKind::Binding(binding_annot, _, ident, None) = fieldpat.pat.kind {
178178
if cx.tcx.find_field_index(ident, &variant) ==
179179
Some(cx.tcx.field_index(fieldpat.hir_id, cx.tables)) {
180180
let mut err = cx.struct_span_lint(NON_SHORTHAND_FIELD_PATTERNS,
181181
fieldpat.span,
182182
&format!("the `{}:` in this pattern is redundant", ident));
183-
let subspan = cx.tcx.sess.source_map().span_through_char(fieldpat.span,
184-
':');
185-
err.span_suggestion_short(
186-
subspan,
187-
"remove this",
188-
ident.to_string(),
183+
let binding = match binding_annot {
184+
hir::BindingAnnotation::Unannotated => None,
185+
hir::BindingAnnotation::Mutable => Some("mut"),
186+
hir::BindingAnnotation::Ref => Some("ref"),
187+
hir::BindingAnnotation::RefMut => Some("ref mut"),
188+
};
189+
let ident = if let Some(binding) = binding {
190+
format!("{} {}", binding, ident)
191+
} else {
192+
ident.to_string()
193+
};
194+
err.span_suggestion(
195+
fieldpat.span,
196+
"use shorthand field pattern",
197+
ident,
189198
Applicability::MachineApplicable
190199
);
191200
err.emit();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// run-rustfix
2+
3+
#![allow(nonstandard_style, unused_variables, unused_mut)]
4+
#![deny(non_shorthand_field_patterns)]
5+
6+
struct Foo {
7+
x: isize,
8+
y: isize,
9+
}
10+
11+
fn main() {
12+
{
13+
let Foo {
14+
x, //~ ERROR the `x:` in this pattern is redundant
15+
ref y, //~ ERROR the `y:` in this pattern is redundant
16+
} = Foo { x: 0, y: 0 };
17+
18+
let Foo {
19+
x,
20+
ref y,
21+
} = Foo { x: 0, y: 0 };
22+
}
23+
24+
{
25+
const x: isize = 1;
26+
27+
match (Foo { x: 1, y: 1 }) {
28+
Foo { x: x, ..} => {},
29+
_ => {},
30+
}
31+
}
32+
33+
{
34+
struct Bar {
35+
x: x,
36+
}
37+
38+
struct x;
39+
40+
match (Bar { x: x }) {
41+
Bar { x: x } => {},
42+
}
43+
}
44+
45+
{
46+
struct Bar {
47+
x: Foo,
48+
}
49+
50+
enum Foo { x }
51+
52+
match (Bar { x: Foo::x }) {
53+
Bar { x: Foo::x } => {},
54+
}
55+
}
56+
57+
{
58+
struct Baz {
59+
x: isize,
60+
y: isize,
61+
z: isize,
62+
}
63+
64+
let Baz {
65+
mut x, //~ ERROR the `x:` in this pattern is redundant
66+
ref y, //~ ERROR the `y:` in this pattern is redundant
67+
ref mut z, //~ ERROR the `z:` in this pattern is redundant
68+
} = Baz { x: 0, y: 0, z: 0 };
69+
}
70+
}

src/test/ui/lint/lint-shorthand-field.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
#![allow(nonstandard_style, unused_variables)]
1+
// run-rustfix
2+
3+
#![allow(nonstandard_style, unused_variables, unused_mut)]
24
#![deny(non_shorthand_field_patterns)]
35

46
struct Foo {
@@ -51,4 +53,18 @@ fn main() {
5153
Bar { x: Foo::x } => {},
5254
}
5355
}
56+
57+
{
58+
struct Baz {
59+
x: isize,
60+
y: isize,
61+
z: isize,
62+
}
63+
64+
let Baz {
65+
x: mut x, //~ ERROR the `x:` in this pattern is redundant
66+
y: ref y, //~ ERROR the `y:` in this pattern is redundant
67+
z: ref mut z, //~ ERROR the `z:` in this pattern is redundant
68+
} = Baz { x: 0, y: 0, z: 0 };
69+
}
5470
}
+24-10
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,38 @@
11
error: the `x:` in this pattern is redundant
2-
--> $DIR/lint-shorthand-field.rs:12:13
2+
--> $DIR/lint-shorthand-field.rs:14:13
33
|
44
LL | x: x,
5-
| --^^
6-
| |
7-
| help: remove this
5+
| ^^^^ help: use shorthand field pattern: `x`
86
|
97
note: lint level defined here
10-
--> $DIR/lint-shorthand-field.rs:2:9
8+
--> $DIR/lint-shorthand-field.rs:4:9
119
|
1210
LL | #![deny(non_shorthand_field_patterns)]
1311
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1412

1513
error: the `y:` in this pattern is redundant
16-
--> $DIR/lint-shorthand-field.rs:13:13
14+
--> $DIR/lint-shorthand-field.rs:15:13
1715
|
1816
LL | y: ref y,
19-
| --^^^^^^
20-
| |
21-
| help: remove this
17+
| ^^^^^^^^ help: use shorthand field pattern: `ref y`
2218

23-
error: aborting due to 2 previous errors
19+
error: the `x:` in this pattern is redundant
20+
--> $DIR/lint-shorthand-field.rs:65:13
21+
|
22+
LL | x: mut x,
23+
| ^^^^^^^^ help: use shorthand field pattern: `mut x`
24+
25+
error: the `y:` in this pattern is redundant
26+
--> $DIR/lint-shorthand-field.rs:66:13
27+
|
28+
LL | y: ref y,
29+
| ^^^^^^^^ help: use shorthand field pattern: `ref y`
30+
31+
error: the `z:` in this pattern is redundant
32+
--> $DIR/lint-shorthand-field.rs:67:13
33+
|
34+
LL | z: ref mut z,
35+
| ^^^^^^^^^^^^ help: use shorthand field pattern: `ref mut z`
36+
37+
error: aborting due to 5 previous errors
2438

src/test/ui/lint/suggestions.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ fn main() {
6060
match d {
6161
Equinox { warp_factor: warp_factor } => {}
6262
//~^ WARN this pattern is redundant
63-
//~| HELP remove this
63+
//~| HELP use shorthand field pattern
6464
}
6565
println!("{} {}", registry_no, b);
6666
}

src/test/ui/lint/suggestions.stderr

+1-3
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,7 @@ warning: the `warp_factor:` in this pattern is redundant
7777
--> $DIR/suggestions.rs:61:23
7878
|
7979
LL | Equinox { warp_factor: warp_factor } => {}
80-
| ------------^^^^^^^^^^^^
81-
| |
82-
| help: remove this
80+
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: use shorthand field pattern: `warp_factor`
8381
|
8482
= note: `#[warn(non_shorthand_field_patterns)]` on by default
8583

0 commit comments

Comments
 (0)