resolve: improve "try using tuple struct" message

This commit improves the tuple struct case added in rust-lang/rust#77341
so that the context is mentioned in more of the message.

Signed-off-by: David Wood <david@davidtw.co>
This commit is contained in:
David Wood 2020-10-12 16:52:51 +01:00
parent adf31e95e4
commit f897162f3e
No known key found for this signature in database
GPG Key ID: 2592E76C87381FD9
3 changed files with 20 additions and 20 deletions

View File

@ -1341,21 +1341,6 @@ fn suggest_using_enum_variant(
let non_suggestable_variant_count = variants.len() - suggestable_variants.len();
if !suggestable_variants.is_empty() {
let msg = if non_suggestable_variant_count == 0 && suggestable_variants.len() == 1 {
"try using the enum's variant"
} else {
"try using one of the enum's variants"
};
err.span_suggestions(
span,
msg,
suggestable_variants.drain(..),
Applicability::MaybeIncorrect,
);
}
let source_msg = if source.is_call() {
"to construct"
} else if matches!(source, PathSource::TupleStruct(..)) {
@ -1364,6 +1349,21 @@ fn suggest_using_enum_variant(
unreachable!()
};
if !suggestable_variants.is_empty() {
let msg = if non_suggestable_variant_count == 0 && suggestable_variants.len() == 1 {
format!("try {} the enum's variant", source_msg)
} else {
format!("try {} one of the enum's variants", source_msg)
};
err.span_suggestions(
span,
&msg,
suggestable_variants.drain(..),
Applicability::MaybeIncorrect,
);
}
// If the enum has no tuple variants..
if non_suggestable_variant_count == variants.len() {
err.help(&format!("the enum has no tuple variants {}", source_msg));

View File

@ -2,7 +2,7 @@ error[E0423]: expected function, tuple struct or tuple variant, found enum `Opti
--> $DIR/issue-43871-enum-instead-of-variant.rs:19:13
|
LL | let x = Option(1);
| ^^^^^^ help: try using one of the enum's variants: `std::option::Option::Some`
| ^^^^^^ help: try to construct one of the enum's variants: `std::option::Option::Some`
|
= help: you might have meant to construct the enum's non-tuple variant
@ -10,7 +10,7 @@ error[E0532]: expected tuple struct or tuple variant, found enum `Option`
--> $DIR/issue-43871-enum-instead-of-variant.rs:21:12
|
LL | if let Option(_) = x {
| ^^^^^^ help: try using one of the enum's variants: `std::option::Option::Some`
| ^^^^^^ help: try to match against one of the enum's variants: `std::option::Option::Some`
|
= help: you might have meant to match against the enum's non-tuple variant
@ -18,7 +18,7 @@ error[E0532]: expected tuple struct or tuple variant, found enum `Example`
--> $DIR/issue-43871-enum-instead-of-variant.rs:27:12
|
LL | if let Example(_) = y {
| ^^^^^^^ help: try using one of the enum's variants: `Example::Ex`
| ^^^^^^^ help: try to match against one of the enum's variants: `Example::Ex`
|
= help: you might have meant to match against the enum's non-tuple variant
note: the enum is defined here

View File

@ -118,7 +118,7 @@ LL | | Tuple(),
LL | | Unit,
LL | | }
| |_^
help: try using one of the enum's variants
help: try to construct one of the enum's variants
|
LL | let x = A::TupleWithFields(3);
| ^^^^^^^^^^^^^^^^^^
@ -143,7 +143,7 @@ LL | | Tuple(),
LL | | Unit,
LL | | }
| |_^
help: try using one of the enum's variants
help: try to match against one of the enum's variants
|
LL | if let A::TupleWithFields(3) = x { }
| ^^^^^^^^^^^^^^^^^^