Skip to content

Commit b7837ea

Browse files
committed
handle another round of feedback
1 parent 50e9c09 commit b7837ea

File tree

5 files changed

+35
-39
lines changed

5 files changed

+35
-39
lines changed

clippy_lints/src/rest_when_destructuring_struct.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,23 @@ impl<'tcx> LateLintPass<'tcx> for RestWhenDestructuringStruct {
8080
fmt_fields.push_str(", ..");
8181
}
8282

83+
let message = if a.variants()[vid].fields.is_empty() {
84+
"consider remove rest pattern (`..`)"
85+
} else if fields.is_empty() {
86+
"consider explicitly ignoring fields with wildcard patterns (`x: _`)"
87+
} else {
88+
"consider explicitly ignoring remaining fields with wildcard patterns (`x: _`)"
89+
};
90+
8391
span_lint_and_then(
8492
cx,
8593
REST_WHEN_DESTRUCTURING_STRUCT,
8694
pat.span,
87-
"struct destructuring with rest (..)",
95+
"struct destructuring with rest (`..`)",
8896
|diag| {
8997
diag.span_suggestion_verbose(
9098
dotdot,
91-
"consider explicitly ignoring remaining fields with wildcard patterns (x: _)",
99+
message,
92100
fmt_fields,
93101
rustc_errors::Applicability::MachineApplicable,
94102
);

clippy_utils/src/check_proc_macro.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@ fn pat_search_pat(tcx: TyCtxt<'_>, pat: &rustc_hir::Pat<'_>) -> (Pat, Pat) {
590590
let (_, end) = pat_search_pat(tcx, p);
591591
(Pat::Str("&"), end)
592592
},
593-
PatKind::Expr(expr) => pat_search_pat_expr_kind(expr),
593+
PatKind::Expr(expr) => pat_expr_search_pat(expr),
594594
PatKind::Guard(pat, guard) => {
595595
let (start, _) = pat_search_pat(tcx, pat);
596596
let (_, end) = expr_search_pat(tcx, guard);
@@ -602,15 +602,15 @@ fn pat_search_pat(tcx: TyCtxt<'_>, pat: &rustc_hir::Pat<'_>) -> (Pat, Pat) {
602602
},
603603
PatKind::Range(r_start, r_end, range) => {
604604
let start = match r_start {
605-
Some(e) => pat_search_pat_expr_kind(e).0,
605+
Some(e) => pat_expr_search_pat(e).0,
606606
None => match range {
607607
rustc_hir::RangeEnd::Included => Pat::Str("..="),
608608
rustc_hir::RangeEnd::Excluded => Pat::Str(".."),
609609
},
610610
};
611611

612612
let end = match r_end {
613-
Some(e) => pat_search_pat_expr_kind(e).1,
613+
Some(e) => pat_expr_search_pat(e).1,
614614
None => match range {
615615
rustc_hir::RangeEnd::Included => Pat::Str("..="),
616616
rustc_hir::RangeEnd::Excluded => Pat::Str(".."),
@@ -622,7 +622,7 @@ fn pat_search_pat(tcx: TyCtxt<'_>, pat: &rustc_hir::Pat<'_>) -> (Pat, Pat) {
622622
}
623623
}
624624

625-
fn pat_search_pat_expr_kind(expr: &PatExpr<'_>) -> (Pat, Pat) {
625+
fn pat_expr_search_pat(expr: &PatExpr<'_>) -> (Pat, Pat) {
626626
match expr.kind {
627627
PatExprKind::Lit { lit, negated } => {
628628
let (start, end) = lit_search_pat(&lit.node);

tests/ui/rest_when_destructuring_struct.fixed

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
//@aux-build:proc_macros.rs
22
//@aux-build:non-exhaustive-struct.rs
33
#![warn(clippy::rest_when_destructuring_struct)]
4-
#![allow(dead_code)]
5-
#![allow(unused_variables)]
6-
7-
extern crate proc_macros;
8-
9-
extern crate non_exhaustive_struct;
104

115
use non_exhaustive_struct::NonExhaustiveStruct;
126

tests/ui/rest_when_destructuring_struct.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
//@aux-build:proc_macros.rs
22
//@aux-build:non-exhaustive-struct.rs
33
#![warn(clippy::rest_when_destructuring_struct)]
4-
#![allow(dead_code)]
5-
#![allow(unused_variables)]
6-
7-
extern crate proc_macros;
8-
9-
extern crate non_exhaustive_struct;
104

115
use non_exhaustive_struct::NonExhaustiveStruct;
126

tests/ui/rest_when_destructuring_struct.stderr

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,83 @@
1-
error: struct destructuring with rest (..)
2-
--> tests/ui/rest_when_destructuring_struct.rs:37:9
1+
error: struct destructuring with rest (`..`)
2+
--> tests/ui/rest_when_destructuring_struct.rs:31:9
33
|
44
LL | let S { a, b, .. } = s;
55
| ^^^^^^^^^^^^^^
66
|
77
= note: `-D clippy::rest-when-destructuring-struct` implied by `-D warnings`
88
= help: to override `-D warnings` add `#[allow(clippy::rest_when_destructuring_struct)]`
9-
help: consider explicitly ignoring remaining fields with wildcard patterns (x: _)
9+
help: consider explicitly ignoring remaining fields with wildcard patterns (`x: _`)
1010
|
1111
LL - let S { a, b, .. } = s;
1212
LL + let S { a, b, c: _ } = s;
1313
|
1414

15-
error: struct destructuring with rest (..)
16-
--> tests/ui/rest_when_destructuring_struct.rs:40:9
15+
error: struct destructuring with rest (`..`)
16+
--> tests/ui/rest_when_destructuring_struct.rs:34:9
1717
|
1818
LL | let S { a, b, c, .. } = s;
1919
| ^^^^^^^^^^^^^^^^^
2020
|
21-
help: consider explicitly ignoring remaining fields with wildcard patterns (x: _)
21+
help: consider explicitly ignoring remaining fields with wildcard patterns (`x: _`)
2222
|
2323
LL - let S { a, b, c, .. } = s;
2424
LL + let S { a, b, c, } = s;
2525
|
2626

27-
error: struct destructuring with rest (..)
28-
--> tests/ui/rest_when_destructuring_struct.rs:47:9
27+
error: struct destructuring with rest (`..`)
28+
--> tests/ui/rest_when_destructuring_struct.rs:41:9
2929
|
3030
LL | E::B { .. } => (),
3131
| ^^^^^^^^^^^
3232
|
33-
help: consider explicitly ignoring remaining fields with wildcard patterns (x: _)
33+
help: consider explicitly ignoring fields with wildcard patterns (`x: _`)
3434
|
3535
LL - E::B { .. } => (),
3636
LL + E::B { b1: _, b2: _ } => (),
3737
|
3838

39-
error: struct destructuring with rest (..)
40-
--> tests/ui/rest_when_destructuring_struct.rs:49:9
39+
error: struct destructuring with rest (`..`)
40+
--> tests/ui/rest_when_destructuring_struct.rs:43:9
4141
|
4242
LL | E::C { .. } => (),
4343
| ^^^^^^^^^^^
4444
|
45-
help: consider explicitly ignoring remaining fields with wildcard patterns (x: _)
45+
help: consider remove rest pattern (`..`)
4646
|
4747
LL - E::C { .. } => (),
4848
LL + E::C { } => (),
4949
|
5050

51-
error: struct destructuring with rest (..)
52-
--> tests/ui/rest_when_destructuring_struct.rs:55:9
51+
error: struct destructuring with rest (`..`)
52+
--> tests/ui/rest_when_destructuring_struct.rs:49:9
5353
|
5454
LL | E::B { b1: _, .. } => (),
5555
| ^^^^^^^^^^^^^^^^^^
5656
|
57-
help: consider explicitly ignoring remaining fields with wildcard patterns (x: _)
57+
help: consider explicitly ignoring remaining fields with wildcard patterns (`x: _`)
5858
|
5959
LL - E::B { b1: _, .. } => (),
6060
LL + E::B { b1: _, b2: _ } => (),
6161
|
6262

63-
error: struct destructuring with rest (..)
64-
--> tests/ui/rest_when_destructuring_struct.rs:72:9
63+
error: struct destructuring with rest (`..`)
64+
--> tests/ui/rest_when_destructuring_struct.rs:66:9
6565
|
6666
LL | let NonExhaustiveStruct { field1: _, .. } = ne;
6767
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6868
|
69-
help: consider explicitly ignoring remaining fields with wildcard patterns (x: _)
69+
help: consider explicitly ignoring remaining fields with wildcard patterns (`x: _`)
7070
|
7171
LL | let NonExhaustiveStruct { field1: _, field2: _, .. } = ne;
7272
| ++++++++++
7373

74-
error: struct destructuring with rest (..)
75-
--> tests/ui/rest_when_destructuring_struct.rs:82:9
74+
error: struct destructuring with rest (`..`)
75+
--> tests/ui/rest_when_destructuring_struct.rs:76:9
7676
|
7777
LL | let Sm { .. } = Sm::default();
7878
| ^^^^^^^^^
7979
|
80-
help: consider explicitly ignoring remaining fields with wildcard patterns (x: _)
80+
help: consider explicitly ignoring fields with wildcard patterns (`x: _`)
8181
|
8282
LL | let Sm { a: _, b: _, .. } = Sm::default();
8383
| +++++++++++

0 commit comments

Comments
 (0)