diff --git a/compiler/rustc_infer/src/infer/error_reporting/mod.rs b/compiler/rustc_infer/src/infer/error_reporting/mod.rs index 1eb8190bd7d..486dec50d9e 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/mod.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/mod.rs @@ -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 diff --git a/src/test/ui/consts/const-tup-index-span.stderr b/src/test/ui/consts/const-tup-index-span.stderr index 6724984d8d7..392d83a6f34 100644 --- a/src/test/ui/consts/const-tup-index-span.stderr +++ b/src/test/ui/consts/const-tup-index-span.stderr @@ -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 diff --git a/src/test/ui/typeck/issue-84768.stderr b/src/test/ui/typeck/issue-84768.stderr index 0a79d539ea9..f99d389f940 100644 --- a/src/test/ui/typeck/issue-84768.stderr +++ b/src/test/ui/typeck/issue-84768.stderr @@ -12,6 +12,10 @@ LL | ::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 | ::call_once(f, (1,)) + | ~~~~ error: aborting due to 2 previous errors