1
Fork 0

Handle existing parentheses when suggesting trailing-tuple-comma

This commit is contained in:
Rob Pilling 2022-01-28 22:32:16 +00:00
parent c734c32776
commit 18cea90d4a

View file

@ -2045,6 +2045,17 @@ 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 => {
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![
@ -2054,6 +2065,8 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
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
// specify a character literal (issue #92479)