Only suggest 1-tuple if expected and found types match
This commit is contained in:
parent
18cea90d4a
commit
91a43f0423
2 changed files with 27 additions and 23 deletions
|
@ -2044,26 +2044,34 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
|
||||||
// If a tuple of length one was expected and the found expression has
|
// If a tuple of length one was expected and the found expression has
|
||||||
// parentheses around it, perhaps the user meant to write `(expr,)` to
|
// parentheses around it, perhaps the user meant to write `(expr,)` to
|
||||||
// build a tuple (issue #86100)
|
// build a tuple (issue #86100)
|
||||||
(ty::Tuple(_), _) if expected.tuple_fields().count() == 1 => {
|
(ty::Tuple(_), _) => {
|
||||||
if let Ok(code) = self.tcx.sess().source_map().span_to_snippet(span) {
|
if let [expected_tup_elem] =
|
||||||
if code.starts_with('(') && code.ends_with(')') {
|
expected.tuple_fields().collect::<Vec<_>>()[..]
|
||||||
let before_close = span.hi() - BytePos::from_u32(1);
|
{
|
||||||
|
if same_type_modulo_infer(expected_tup_elem, found) {
|
||||||
|
if let Ok(code) =
|
||||||
|
self.tcx.sess().source_map().span_to_snippet(span)
|
||||||
|
{
|
||||||
|
if code.starts_with('(') && code.ends_with(')') {
|
||||||
|
let before_close = span.hi() - BytePos::from_u32(1);
|
||||||
|
|
||||||
err.span_suggestion(
|
err.span_suggestion(
|
||||||
span.with_hi(before_close).shrink_to_hi(),
|
span.with_hi(before_close).shrink_to_hi(),
|
||||||
"use a trailing comma to create a tuple with one element",
|
"use a trailing comma to create a tuple with one element",
|
||||||
",".into(),
|
",".into(),
|
||||||
Applicability::MaybeIncorrect,
|
Applicability::MaybeIncorrect,
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
err.multipart_suggestion(
|
err.multipart_suggestion(
|
||||||
"use a trailing comma to create a tuple with one element",
|
"use a trailing comma to create a tuple with one element",
|
||||||
vec![
|
vec![
|
||||||
(span.shrink_to_lo(), "(".into()),
|
(span.shrink_to_lo(), "(".into()),
|
||||||
(span.shrink_to_hi(), ",)".into()),
|
(span.shrink_to_hi(), ",)".into()),
|
||||||
],
|
],
|
||||||
Applicability::MaybeIncorrect,
|
Applicability::MaybeIncorrect,
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,10 +12,6 @@ LL | <F as FnOnce(&mut u8)>::call_once(f, 1)
|
||||||
|
|
|
|
||||||
= note: expected tuple `(&mut u8,)`
|
= note: expected tuple `(&mut u8,)`
|
||||||
found type `{integer}`
|
found type `{integer}`
|
||||||
help: use a trailing comma to create a tuple with one element
|
|
||||||
|
|
|
||||||
LL | <F as FnOnce(&mut u8)>::call_once(f, (1,))
|
|
||||||
| + ++
|
|
||||||
|
|
||||||
error: aborting due to 2 previous errors
|
error: aborting due to 2 previous errors
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue