Suggest Variant(..)
if all of the mentioned fields are _
This commit is contained in:
parent
5fe61a79cc
commit
f3d9df54ee
4 changed files with 34 additions and 19 deletions
|
@ -20,7 +20,6 @@ use rustc_trait_selection::traits::{ObligationCause, Pattern};
|
||||||
|
|
||||||
use std::cmp;
|
use std::cmp;
|
||||||
use std::collections::hash_map::Entry::{Occupied, Vacant};
|
use std::collections::hash_map::Entry::{Occupied, Vacant};
|
||||||
use std::iter;
|
|
||||||
|
|
||||||
use super::report_unexpected_variant_res;
|
use super::report_unexpected_variant_res;
|
||||||
|
|
||||||
|
@ -1048,26 +1047,42 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
||||||
} else {
|
} else {
|
||||||
pat_span.with_hi(pat_span.hi() - BytePos(1)).shrink_to_hi()
|
pat_span.with_hi(pat_span.hi() - BytePos(1)).shrink_to_hi()
|
||||||
};
|
};
|
||||||
|
let all_fields_span = match subpats {
|
||||||
|
[] => after_fields_span,
|
||||||
|
[field] => field.span,
|
||||||
|
[first, .., last] => first.span.to(last.span),
|
||||||
|
};
|
||||||
|
|
||||||
|
// Check if all the fields in the pattern are wildcards.
|
||||||
|
let all_wildcards = subpats.iter().all(|pat| matches!(pat.kind, PatKind::Wild));
|
||||||
|
|
||||||
let mut wildcard_sugg = vec!["_"; fields.len() - subpats.len()].join(", ");
|
let mut wildcard_sugg = vec!["_"; fields.len() - subpats.len()].join(", ");
|
||||||
if !subpats.is_empty() {
|
if !subpats.is_empty() {
|
||||||
wildcard_sugg = String::from(", ") + &wildcard_sugg;
|
wildcard_sugg = String::from(", ") + &wildcard_sugg;
|
||||||
}
|
}
|
||||||
|
|
||||||
let rest_sugg = if subpats.is_empty() { "..".to_owned() } else { ", ..".to_owned() };
|
|
||||||
|
|
||||||
err.span_suggestion(
|
err.span_suggestion(
|
||||||
after_fields_span,
|
after_fields_span,
|
||||||
"use `_` to explicitly ignore each field",
|
"use `_` to explicitly ignore each field",
|
||||||
wildcard_sugg,
|
wildcard_sugg,
|
||||||
Applicability::MaybeIncorrect,
|
Applicability::MaybeIncorrect,
|
||||||
);
|
);
|
||||||
err.span_suggestion(
|
|
||||||
after_fields_span,
|
if subpats.is_empty() || all_wildcards {
|
||||||
"use `..` to ignore all unmentioned fields",
|
err.span_suggestion(
|
||||||
rest_sugg,
|
all_fields_span,
|
||||||
Applicability::MaybeIncorrect,
|
"use `..` to ignore all unmentioned fields",
|
||||||
);
|
String::from(".."),
|
||||||
|
Applicability::MaybeIncorrect,
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
err.span_suggestion(
|
||||||
|
after_fields_span,
|
||||||
|
"use `..` to ignore all unmentioned fields",
|
||||||
|
String::from(", .."),
|
||||||
|
Applicability::MaybeIncorrect,
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
err.emit();
|
err.emit();
|
||||||
|
|
|
@ -38,8 +38,8 @@ LL | TupleStruct(_, _) = TupleStruct(1, 2);
|
||||||
| ^^^
|
| ^^^
|
||||||
help: use `..` to ignore all unmentioned fields
|
help: use `..` to ignore all unmentioned fields
|
||||||
|
|
|
|
||||||
LL | TupleStruct(_, ..) = TupleStruct(1, 2);
|
LL | TupleStruct(..) = TupleStruct(1, 2);
|
||||||
| ^^^^
|
| ^^
|
||||||
|
|
||||||
error[E0023]: this pattern has 3 fields, but the corresponding tuple variant has 2 fields
|
error[E0023]: this pattern has 3 fields, but the corresponding tuple variant has 2 fields
|
||||||
--> $DIR/tuple_struct_destructure_fail.rs:34:5
|
--> $DIR/tuple_struct_destructure_fail.rs:34:5
|
||||||
|
@ -65,8 +65,8 @@ LL | Enum::SingleVariant(_, _) = Enum::SingleVariant(1, 2);
|
||||||
| ^^^
|
| ^^^
|
||||||
help: use `..` to ignore all unmentioned fields
|
help: use `..` to ignore all unmentioned fields
|
||||||
|
|
|
|
||||||
LL | Enum::SingleVariant(_, ..) = Enum::SingleVariant(1, 2);
|
LL | Enum::SingleVariant(..) = Enum::SingleVariant(1, 2);
|
||||||
| ^^^^
|
| ^^
|
||||||
|
|
||||||
error[E0070]: invalid left-hand side of assignment
|
error[E0070]: invalid left-hand side of assignment
|
||||||
--> $DIR/tuple_struct_destructure_fail.rs:40:12
|
--> $DIR/tuple_struct_destructure_fail.rs:40:12
|
||||||
|
|
|
@ -13,8 +13,8 @@ LL | Color::Rgb(_, _, _) => { }
|
||||||
| ^^^
|
| ^^^
|
||||||
help: use `..` to ignore all unmentioned fields
|
help: use `..` to ignore all unmentioned fields
|
||||||
|
|
|
|
||||||
LL | Color::Rgb(_, _, ..) => { }
|
LL | Color::Rgb(..) => { }
|
||||||
| ^^^^
|
| ^^
|
||||||
|
|
||||||
error: aborting due to previous error
|
error: aborting due to previous error
|
||||||
|
|
||||||
|
|
|
@ -40,8 +40,8 @@ LL | S(_, _) => {}
|
||||||
| ^^^
|
| ^^^
|
||||||
help: use `..` to ignore all unmentioned fields
|
help: use `..` to ignore all unmentioned fields
|
||||||
|
|
|
|
||||||
LL | S(_, ..) => {}
|
LL | S(..) => {}
|
||||||
| ^^^^
|
| ^^
|
||||||
|
|
||||||
error[E0023]: this pattern has 0 fields, but the corresponding tuple struct has 2 fields
|
error[E0023]: this pattern has 0 fields, but the corresponding tuple struct has 2 fields
|
||||||
--> $DIR/pat-tuple-underfield.rs:20:9
|
--> $DIR/pat-tuple-underfield.rs:20:9
|
||||||
|
@ -94,8 +94,8 @@ LL | E::S(_, _) => {}
|
||||||
| ^^^
|
| ^^^
|
||||||
help: use `..` to ignore all unmentioned fields
|
help: use `..` to ignore all unmentioned fields
|
||||||
|
|
|
|
||||||
LL | E::S(_, ..) => {}
|
LL | E::S(..) => {}
|
||||||
| ^^^^
|
| ^^
|
||||||
|
|
||||||
error[E0023]: this pattern has 0 fields, but the corresponding tuple variant has 2 fields
|
error[E0023]: this pattern has 0 fields, but the corresponding tuple variant has 2 fields
|
||||||
--> $DIR/pat-tuple-underfield.rs:39:9
|
--> $DIR/pat-tuple-underfield.rs:39:9
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue