diff options
| author | Rob Pilling <robpilling@gmail.com> | 2022-01-28 22:32:16 +0000 |
|---|---|---|
| committer | Rob Pilling <robpilling@gmail.com> | 2022-01-28 23:42:30 +0000 |
| commit | 18cea90d4a01cbb3a1285f633b1058fd39931956 (patch) | |
| tree | 82383c5e1766bc2369b2226f13e8941f8277890d | |
| parent | c734c32776e26861c89e261b8757522e46031978 (diff) | |
| download | rust-18cea90d4a01cbb3a1285f633b1058fd39931956.tar.gz rust-18cea90d4a01cbb3a1285f633b1058fd39931956.zip | |
Handle existing parentheses when suggesting trailing-tuple-comma
| -rw-r--r-- | compiler/rustc_infer/src/infer/error_reporting/mod.rs | 29 |
1 files changed, 21 insertions, 8 deletions
diff --git a/compiler/rustc_infer/src/infer/error_reporting/mod.rs b/compiler/rustc_infer/src/infer/error_reporting/mod.rs index e169e9a8431..71a81d12839 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/mod.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/mod.rs @@ -2045,14 +2045,27 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> { // parentheses around it, perhaps the user meant to write `(expr,)` to // build a tuple (issue #86100) (ty::Tuple(_), _) if expected.tuple_fields().count() == 1 => { - err.multipart_suggestion( - "use a trailing comma to create a tuple with one element", - vec![ - (span.shrink_to_lo(), "(".into()), - (span.shrink_to_hi(), ",)".into()), - ], - Applicability::MaybeIncorrect, - ); + 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( + span.with_hi(before_close).shrink_to_hi(), + "use a trailing comma to create a tuple with one element", + ",".into(), + Applicability::MaybeIncorrect, + ); + } else { + err.multipart_suggestion( + "use a trailing comma to create a tuple with one element", + vec![ + (span.shrink_to_lo(), "(".into()), + (span.shrink_to_hi(), ",)".into()), + ], + Applicability::MaybeIncorrect, + ); + } + } } // If a character was expected and the found expression is a string literal // containing a single character, perhaps the user meant to write `'c'` to |
