Skip to content

Commit

Permalink
Replace span suggestion with multipart
Browse files Browse the repository at this point in the history
  • Loading branch information
bobrippling committed Jan 28, 2022
1 parent 4738ce4 commit c734c32
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 14 deletions.
20 changes: 8 additions & 12 deletions compiler/rustc_infer/src/infer/error_reporting/mod.rs
Expand Up @@ -2045,18 +2045,14 @@ 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) {
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,
);
}
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
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/consts/const-tup-index-span.stderr
Expand Up @@ -9,7 +9,7 @@ LL | const TUP: (usize,) = 5usize << 64;
help: use a trailing comma to create a tuple with one element
|
LL | const TUP: (usize,) = (5usize << 64,);
| ~~~~~~~~~~~~~~~
| + ++

error: aborting due to previous error

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/typeck/issue-84768.stderr
Expand Up @@ -15,7 +15,7 @@ LL | <F as FnOnce(&mut u8)>::call_once(f, 1)
help: use a trailing comma to create a tuple with one element
|
LL | <F as FnOnce(&mut u8)>::call_once(f, (1,))
| ~~~~
| + ++

error: aborting due to 2 previous errors

Expand Down

0 comments on commit c734c32

Please sign in to comment.