Skip to content

Commit 4738ce4

Browse files
committed
Suggest 1-tuple parentheses, without existing parens
1 parent 427eba2 commit 4738ce4

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
lines changed

compiler/rustc_infer/src/infer/error_reporting/mod.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2046,16 +2046,16 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
20462046
// build a tuple (issue #86100)
20472047
(ty::Tuple(_), _) if expected.tuple_fields().count() == 1 => {
20482048
if let Ok(code) = self.tcx.sess().source_map().span_to_snippet(span) {
2049-
if let Some(code) =
2050-
code.strip_prefix('(').and_then(|s| s.strip_suffix(')'))
2051-
{
2052-
err.span_suggestion(
2053-
span,
2054-
"use a trailing comma to create a tuple with one element",
2055-
format!("({},)", code),
2056-
Applicability::MaybeIncorrect,
2057-
);
2058-
}
2049+
let code_stripped = code
2050+
.strip_prefix('(')
2051+
.and_then(|s| s.strip_suffix(')'))
2052+
.unwrap_or(&code);
2053+
err.span_suggestion(
2054+
span,
2055+
"use a trailing comma to create a tuple with one element",
2056+
format!("({},)", code_stripped),
2057+
Applicability::MaybeIncorrect,
2058+
);
20592059
}
20602060
}
20612061
// If a character was expected and the found expression is a string literal

src/test/ui/consts/const-tup-index-span.stderr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ LL | const TUP: (usize,) = 5usize << 64;
66
|
77
= note: expected tuple `(usize,)`
88
found type `usize`
9+
help: use a trailing comma to create a tuple with one element
10+
|
11+
LL | const TUP: (usize,) = (5usize << 64,);
12+
| ~~~~~~~~~~~~~~~
913

1014
error: aborting due to previous error
1115

src/test/ui/typeck/issue-84768.stderr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ LL | <F as FnOnce(&mut u8)>::call_once(f, 1)
1212
|
1313
= note: expected tuple `(&mut u8,)`
1414
found type `{integer}`
15+
help: use a trailing comma to create a tuple with one element
16+
|
17+
LL | <F as FnOnce(&mut u8)>::call_once(f, (1,))
18+
| ~~~~
1519

1620
error: aborting due to 2 previous errors
1721

0 commit comments

Comments
 (0)