Skip to content

Commit 18cea90

Browse files
committed
Handle existing parentheses when suggesting trailing-tuple-comma
1 parent c734c32 commit 18cea90

File tree

1 file changed

+21
-8
lines changed
  • compiler/rustc_infer/src/infer/error_reporting

1 file changed

+21
-8
lines changed

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

+21-8
Original file line numberDiff line numberDiff line change
@@ -2045,14 +2045,27 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
20452045
// parentheses around it, perhaps the user meant to write `(expr,)` to
20462046
// build a tuple (issue #86100)
20472047
(ty::Tuple(_), _) if expected.tuple_fields().count() == 1 => {
2048-
err.multipart_suggestion(
2049-
"use a trailing comma to create a tuple with one element",
2050-
vec![
2051-
(span.shrink_to_lo(), "(".into()),
2052-
(span.shrink_to_hi(), ",)".into()),
2053-
],
2054-
Applicability::MaybeIncorrect,
2055-
);
2048+
if let Ok(code) = self.tcx.sess().source_map().span_to_snippet(span) {
2049+
if code.starts_with('(') && code.ends_with(')') {
2050+
let before_close = span.hi() - BytePos::from_u32(1);
2051+
2052+
err.span_suggestion(
2053+
span.with_hi(before_close).shrink_to_hi(),
2054+
"use a trailing comma to create a tuple with one element",
2055+
",".into(),
2056+
Applicability::MaybeIncorrect,
2057+
);
2058+
} else {
2059+
err.multipart_suggestion(
2060+
"use a trailing comma to create a tuple with one element",
2061+
vec![
2062+
(span.shrink_to_lo(), "(".into()),
2063+
(span.shrink_to_hi(), ",)".into()),
2064+
],
2065+
Applicability::MaybeIncorrect,
2066+
);
2067+
}
2068+
}
20562069
}
20572070
// If a character was expected and the found expression is a string literal
20582071
// containing a single character, perhaps the user meant to write `'c'` to

0 commit comments

Comments
 (0)