Suggest 1-tuple parentheses, without existing parens

This commit is contained in:
Rob Pilling 2021-12-04 17:33:59 +00:00
parent 427eba2f0b
commit 4738ce463e
3 changed files with 18 additions and 10 deletions

View file

@ -2046,16 +2046,16 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
// build a tuple (issue #86100)
(ty::Tuple(_), _) if expected.tuple_fields().count() == 1 => {
if let Ok(code) = self.tcx.sess().source_map().span_to_snippet(span) {
if let Some(code) =
code.strip_prefix('(').and_then(|s| s.strip_suffix(')'))
{
err.span_suggestion(
span,
"use a trailing comma to create a tuple with one element",
format!("({},)", code),
Applicability::MaybeIncorrect,
);
}
let code_stripped = code
.strip_prefix('(')
.and_then(|s| s.strip_suffix(')'))
.unwrap_or(&code);
err.span_suggestion(
span,
"use a trailing comma to create a tuple with one element",
format!("({},)", code_stripped),
Applicability::MaybeIncorrect,
);
}
}
// If a character was expected and the found expression is a string literal

View file

@ -6,6 +6,10 @@ LL | const TUP: (usize,) = 5usize << 64;
|
= note: expected tuple `(usize,)`
found type `usize`
help: use a trailing comma to create a tuple with one element
|
LL | const TUP: (usize,) = (5usize << 64,);
| ~~~~~~~~~~~~~~~
error: aborting due to previous error

View file

@ -12,6 +12,10 @@ LL | <F as FnOnce(&mut u8)>::call_once(f, 1)
|
= note: expected tuple `(&mut u8,)`
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