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