Improve suggestions for type errors with string concatenation

Now, multipart suggestions are used instead of `span_to_snippet`, which
improves code quality, makes the suggestion work even without access to
source code, and, most importantly, improves the rendering of the
suggestion.
This commit is contained in:
Noah Lev 2022-01-12 22:04:39 -08:00
parent 124555a69e
commit 2835ace4ca
6 changed files with 47 additions and 65 deletions

View File

@ -549,7 +549,6 @@ fn check_str_addition(
is_assign: IsAssign, is_assign: IsAssign,
op: hir::BinOp, op: hir::BinOp,
) -> bool { ) -> bool {
let source_map = self.tcx.sess.source_map();
let remove_borrow_msg = "String concatenation appends the string on the right to the \ let remove_borrow_msg = "String concatenation appends the string on the right to the \
string on the left and may require reallocation. This \ string on the left and may require reallocation. This \
requires ownership of the string on the left"; requires ownership of the string on the left";
@ -574,31 +573,22 @@ fn check_str_addition(
) => ) =>
{ {
if let IsAssign::No = is_assign { // Do not supply this message if `&str += &str` if let IsAssign::No = is_assign { // Do not supply this message if `&str += &str`
err.span_label( err.span_label(op.span, "`+` cannot be used to concatenate two `&str` strings");
op.span, if let hir::ExprKind::AddrOf(_,_,lhs_inner_expr) = lhs_expr.kind {
"`+` cannot be used to concatenate two `&str` strings", err.span_suggestion(
); lhs_expr.span.until(lhs_inner_expr.span),
match source_map.span_to_snippet(lhs_expr.span) { remove_borrow_msg,
Ok(lstring) => { "".to_owned(),
err.span_suggestion( Applicability::MachineApplicable
lhs_expr.span, );
if lstring.starts_with('&') { } else {
remove_borrow_msg err.span_suggestion(
} else { lhs_expr.span.shrink_to_hi(),
msg msg,
}, ".to_owned()".to_owned(),
if let Some(stripped) = lstring.strip_prefix('&') { Applicability::MachineApplicable
// let a = String::new(); );
// let _ = &a + "bar"; }
stripped.to_string()
} else {
format!("{}.to_owned()", lstring)
},
Applicability::MachineApplicable,
)
}
_ => err.help(msg),
};
} }
true true
} }
@ -609,32 +599,22 @@ fn check_str_addition(
op.span, op.span,
"`+` cannot be used to concatenate a `&str` with a `String`", "`+` cannot be used to concatenate a `&str` with a `String`",
); );
match ( match is_assign {
source_map.span_to_snippet(lhs_expr.span), IsAssign::No => {
source_map.span_to_snippet(rhs_expr.span), let suggestions = vec![
is_assign, if let hir::ExprKind::AddrOf(_, _, lhs_inner_expr) = lhs_expr.kind {
) { (lhs_expr.span.until(lhs_inner_expr.span), "".to_owned())
(Ok(l), Ok(r), IsAssign::No) => { } else {
let to_string = if let Some(stripped) = l.strip_prefix('&') { (lhs_expr.span.shrink_to_hi(), ".to_owned()".to_owned())
// let a = String::new(); let b = String::new(); },
// let _ = &a + b; (rhs_expr.span.shrink_to_lo(), "&".to_owned()),
stripped.to_string() ];
} else { err.multipart_suggestion(msg, suggestions, Applicability::MachineApplicable);
format!("{}.to_owned()", l)
};
err.multipart_suggestion(
msg,
vec![
(lhs_expr.span, to_string),
(rhs_expr.span, format!("&{}", r)),
],
Applicability::MachineApplicable,
);
} }
_ => { IsAssign::Yes => {
err.help(msg); err.help(msg);
} }
}; }
true true
} }
_ => false, _ => false,

View File

@ -10,7 +10,7 @@ LL | let _a = b + ", World!";
help: `to_owned()` can be used to create an owned `String` from a string reference. String concatenation appends the string on the right to the string on the left and may require reallocation. This requires ownership of the string on the left help: `to_owned()` can be used to create an owned `String` from a string reference. String concatenation appends the string on the right to the string on the left and may require reallocation. This requires ownership of the string on the left
| |
LL | let _a = b.to_owned() + ", World!"; LL | let _a = b.to_owned() + ", World!";
| ~~~~~~~~~~~~ | +++++++++++
error: aborting due to previous error error: aborting due to previous error

View File

@ -10,7 +10,7 @@ LL | println!("🦀🦀🦀🦀🦀"); let _a = b + ", World!";
help: `to_owned()` can be used to create an owned `String` from a string reference. String concatenation appends the string on the right to the string on the left and may require reallocation. This requires ownership of the string on the left help: `to_owned()` can be used to create an owned `String` from a string reference. String concatenation appends the string on the right to the string on the left and may require reallocation. This requires ownership of the string on the left
| |
LL | println!("🦀🦀🦀🦀🦀"); let _a = b.to_owned() + ", World!"; LL | println!("🦀🦀🦀🦀🦀"); let _a = b.to_owned() + ", World!";
| ~~~~~~~~~~~~ | +++++++++++
error: aborting due to previous error error: aborting due to previous error

View File

@ -10,7 +10,7 @@ LL | let x = "Hello " + "World!";
help: `to_owned()` can be used to create an owned `String` from a string reference. String concatenation appends the string on the right to the string on the left and may require reallocation. This requires ownership of the string on the left help: `to_owned()` can be used to create an owned `String` from a string reference. String concatenation appends the string on the right to the string on the left and may require reallocation. This requires ownership of the string on the left
| |
LL | let x = "Hello ".to_owned() + "World!"; LL | let x = "Hello ".to_owned() + "World!";
| ~~~~~~~~~~~~~~~~~~~ | +++++++++++
error[E0369]: cannot add `World` to `World` error[E0369]: cannot add `World` to `World`
--> $DIR/issue-39018.rs:8:26 --> $DIR/issue-39018.rs:8:26
@ -49,7 +49,7 @@ LL | let x = "Hello " + "World!".to_owned();
help: `to_owned()` can be used to create an owned `String` from a string reference. String concatenation appends the string on the right to the string on the left and may require reallocation. This requires ownership of the string on the left help: `to_owned()` can be used to create an owned `String` from a string reference. String concatenation appends the string on the right to the string on the left and may require reallocation. This requires ownership of the string on the left
| |
LL | let x = "Hello ".to_owned() + &"World!".to_owned(); LL | let x = "Hello ".to_owned() + &"World!".to_owned();
| ~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~ | +++++++++++ +
error[E0369]: cannot add `&String` to `&String` error[E0369]: cannot add `&String` to `&String`
--> $DIR/issue-39018.rs:26:16 --> $DIR/issue-39018.rs:26:16
@ -62,8 +62,9 @@ LL | let _ = &a + &b;
| |
help: String concatenation appends the string on the right to the string on the left and may require reallocation. This requires ownership of the string on the left help: String concatenation appends the string on the right to the string on the left and may require reallocation. This requires ownership of the string on the left
| |
LL | let _ = a + &b; LL - let _ = &a + &b;
| ~ LL + let _ = a + &b;
|
error[E0369]: cannot add `String` to `&String` error[E0369]: cannot add `String` to `&String`
--> $DIR/issue-39018.rs:27:16 --> $DIR/issue-39018.rs:27:16
@ -76,8 +77,9 @@ LL | let _ = &a + b;
| |
help: `to_owned()` can be used to create an owned `String` from a string reference. String concatenation appends the string on the right to the string on the left and may require reallocation. This requires ownership of the string on the left help: `to_owned()` can be used to create an owned `String` from a string reference. String concatenation appends the string on the right to the string on the left and may require reallocation. This requires ownership of the string on the left
| |
LL | let _ = a + &b; LL - let _ = &a + b;
| ~ ~~ LL + let _ = a + &b;
|
error[E0308]: mismatched types error[E0308]: mismatched types
--> $DIR/issue-39018.rs:29:17 --> $DIR/issue-39018.rs:29:17
@ -100,7 +102,7 @@ LL | let _ = e + b;
help: `to_owned()` can be used to create an owned `String` from a string reference. String concatenation appends the string on the right to the string on the left and may require reallocation. This requires ownership of the string on the left help: `to_owned()` can be used to create an owned `String` from a string reference. String concatenation appends the string on the right to the string on the left and may require reallocation. This requires ownership of the string on the left
| |
LL | let _ = e.to_owned() + &b; LL | let _ = e.to_owned() + &b;
| ~~~~~~~~~~~~ ~~ | +++++++++++ +
error[E0369]: cannot add `&String` to `&String` error[E0369]: cannot add `&String` to `&String`
--> $DIR/issue-39018.rs:31:15 --> $DIR/issue-39018.rs:31:15
@ -114,7 +116,7 @@ LL | let _ = e + &b;
help: `to_owned()` can be used to create an owned `String` from a string reference. String concatenation appends the string on the right to the string on the left and may require reallocation. This requires ownership of the string on the left help: `to_owned()` can be used to create an owned `String` from a string reference. String concatenation appends the string on the right to the string on the left and may require reallocation. This requires ownership of the string on the left
| |
LL | let _ = e.to_owned() + &b; LL | let _ = e.to_owned() + &b;
| ~~~~~~~~~~~~ | +++++++++++
error[E0369]: cannot add `&str` to `&String` error[E0369]: cannot add `&str` to `&String`
--> $DIR/issue-39018.rs:32:15 --> $DIR/issue-39018.rs:32:15
@ -128,7 +130,7 @@ LL | let _ = e + d;
help: `to_owned()` can be used to create an owned `String` from a string reference. String concatenation appends the string on the right to the string on the left and may require reallocation. This requires ownership of the string on the left help: `to_owned()` can be used to create an owned `String` from a string reference. String concatenation appends the string on the right to the string on the left and may require reallocation. This requires ownership of the string on the left
| |
LL | let _ = e.to_owned() + d; LL | let _ = e.to_owned() + d;
| ~~~~~~~~~~~~ | +++++++++++
error[E0369]: cannot add `&&str` to `&String` error[E0369]: cannot add `&&str` to `&String`
--> $DIR/issue-39018.rs:33:15 --> $DIR/issue-39018.rs:33:15
@ -142,7 +144,7 @@ LL | let _ = e + &d;
help: `to_owned()` can be used to create an owned `String` from a string reference. String concatenation appends the string on the right to the string on the left and may require reallocation. This requires ownership of the string on the left help: `to_owned()` can be used to create an owned `String` from a string reference. String concatenation appends the string on the right to the string on the left and may require reallocation. This requires ownership of the string on the left
| |
LL | let _ = e.to_owned() + &d; LL | let _ = e.to_owned() + &d;
| ~~~~~~~~~~~~ | +++++++++++
error[E0369]: cannot add `&&str` to `&&str` error[E0369]: cannot add `&&str` to `&&str`
--> $DIR/issue-39018.rs:34:16 --> $DIR/issue-39018.rs:34:16
@ -172,7 +174,7 @@ LL | let _ = c + &d;
help: `to_owned()` can be used to create an owned `String` from a string reference. String concatenation appends the string on the right to the string on the left and may require reallocation. This requires ownership of the string on the left help: `to_owned()` can be used to create an owned `String` from a string reference. String concatenation appends the string on the right to the string on the left and may require reallocation. This requires ownership of the string on the left
| |
LL | let _ = c.to_owned() + &d; LL | let _ = c.to_owned() + &d;
| ~~~~~~~~~~~~ | +++++++++++
error[E0369]: cannot add `&str` to `&str` error[E0369]: cannot add `&str` to `&str`
--> $DIR/issue-39018.rs:37:15 --> $DIR/issue-39018.rs:37:15
@ -186,7 +188,7 @@ LL | let _ = c + d;
help: `to_owned()` can be used to create an owned `String` from a string reference. String concatenation appends the string on the right to the string on the left and may require reallocation. This requires ownership of the string on the left help: `to_owned()` can be used to create an owned `String` from a string reference. String concatenation appends the string on the right to the string on the left and may require reallocation. This requires ownership of the string on the left
| |
LL | let _ = c.to_owned() + d; LL | let _ = c.to_owned() + d;
| ~~~~~~~~~~~~ | +++++++++++
error: aborting due to 14 previous errors error: aborting due to 14 previous errors

View File

@ -10,7 +10,7 @@ LL | let c = a + b;
help: `to_owned()` can be used to create an owned `String` from a string reference. String concatenation appends the string on the right to the string on the left and may require reallocation. This requires ownership of the string on the left help: `to_owned()` can be used to create an owned `String` from a string reference. String concatenation appends the string on the right to the string on the left and may require reallocation. This requires ownership of the string on the left
| |
LL | let c = a.to_owned() + b; LL | let c = a.to_owned() + b;
| ~~~~~~~~~~~~ | +++++++++++
error: aborting due to previous error error: aborting due to previous error

View File

@ -10,7 +10,7 @@ LL | ...ཽཾཿ྄ཱྀྀྂྃ྅྆྇ྈྉྊྋྌྍྎྏྐྑྒྒྷྔ
help: `to_owned()` can be used to create an owned `String` from a string reference. String concatenation appends the string on the right to the string on the left and may require reallocation. This requires ownership of the string on the left help: `to_owned()` can be used to create an owned `String` from a string reference. String concatenation appends the string on the right to the string on the left and may require reallocation. This requires ownership of the string on the left
| |
LL | let _ = "ༀ༁༂༃༄༅༆༇༈༉༊་༌།༎༏༐༑༒༓༔༕༖༗༘༙༚༛༜༝༞༟༠༡༢༣༤༥༦༧༨༩༪༫༬༭༮༯༰༱༲༳༴༵༶༷༸༹༺༻༼༽༾༿ཀཁགགྷངཅཆཇ཈ཉཊཋཌཌྷཎཏཐདདྷནཔཕབབྷམཙཚཛཛྷཝཞཟའཡརལཤཥསཧཨཀྵཪཫཬ཭཮཯཰ཱཱཱིིུུྲྀཷླྀཹེཻོཽཾཿ྄ཱྀྀྂྃ྅྆྇ྈྉྊྋྌྍྎྏྐྑྒྒྷྔྕྖྗ྘ྙྚྛྜྜྷྞྟྠྡྡྷྣྤྥྦྦྷྨྩྪྫྫྷྭྮྯྰྱྲླྴྵྶྷྸྐྵྺྻྼ྽྾྿࿀࿁࿂࿃࿄࿅࿆࿇࿈࿉࿊࿋࿌࿍࿎࿏࿐࿑࿒࿓࿔࿕࿖࿗࿘࿙࿚"; let _a = unicode_is_fun.to_owned() + " really fun!"; LL | let _ = "ༀ༁༂༃༄༅༆༇༈༉༊་༌།༎༏༐༑༒༓༔༕༖༗༘༙༚༛༜༝༞༟༠༡༢༣༤༥༦༧༨༩༪༫༬༭༮༯༰༱༲༳༴༵༶༷༸༹༺༻༼༽༾༿ཀཁགགྷངཅཆཇ཈ཉཊཋཌཌྷཎཏཐདདྷནཔཕབབྷམཙཚཛཛྷཝཞཟའཡརལཤཥསཧཨཀྵཪཫཬ཭཮཯཰ཱཱཱིིུུྲྀཷླྀཹེཻོཽཾཿ྄ཱྀྀྂྃ྅྆྇ྈྉྊྋྌྍྎྏྐྑྒྒྷྔྕྖྗ྘ྙྚྛྜྜྷྞྟྠྡྡྷྣྤྥྦྦྷྨྩྪྫྫྷྭྮྯྰྱྲླྴྵྶྷྸྐྵྺྻྼ྽྾྿࿀࿁࿂࿃࿄࿅࿆࿇࿈࿉࿊࿋࿌࿍࿎࿏࿐࿑࿒࿓࿔࿕࿖࿗࿘࿙࿚"; let _a = unicode_is_fun.to_owned() + " really fun!";
| ~~~~~~~~~~~~~~~~~~~~~~~~~ | +++++++++++
error: aborting due to previous error error: aborting due to previous error