Split up very long message

This should make it easier to read.
This commit is contained in:
Noah Lev 2022-01-12 22:20:13 -08:00
parent 2835ace4ca
commit 69db5e2c8c
6 changed files with 44 additions and 36 deletions

View File

@ -549,15 +549,11 @@ fn check_str_addition(
is_assign: IsAssign, is_assign: IsAssign,
op: hir::BinOp, op: hir::BinOp,
) -> bool { ) -> bool {
let remove_borrow_msg = "String concatenation appends the string on the right to the \ let str_concat_note = "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.";
let rm_borrow_msg = "remove the borrow to obtain an owned `String`";
let msg = "`to_owned()` can be used to create an owned `String` \ let to_owned_msg = "use `to_owned()` to create an owned `String` from a string reference";
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";
let string_type = self.tcx.get_diagnostic_item(sym::String); let string_type = self.tcx.get_diagnostic_item(sym::String);
let is_std_string = |ty: Ty<'tcx>| match ty.ty_adt_def() { let is_std_string = |ty: Ty<'tcx>| match ty.ty_adt_def() {
@ -574,17 +570,18 @@ 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(op.span, "`+` cannot be used to concatenate two `&str` strings"); err.span_label(op.span, "`+` cannot be used to concatenate two `&str` strings");
err.note(str_concat_note);
if let hir::ExprKind::AddrOf(_,_,lhs_inner_expr) = lhs_expr.kind { if let hir::ExprKind::AddrOf(_,_,lhs_inner_expr) = lhs_expr.kind {
err.span_suggestion( err.span_suggestion(
lhs_expr.span.until(lhs_inner_expr.span), lhs_expr.span.until(lhs_inner_expr.span),
remove_borrow_msg, rm_borrow_msg,
"".to_owned(), "".to_owned(),
Applicability::MachineApplicable Applicability::MachineApplicable
); );
} else { } else {
err.span_suggestion( err.span_suggestion(
lhs_expr.span.shrink_to_hi(), lhs_expr.span.shrink_to_hi(),
msg, to_owned_msg,
".to_owned()".to_owned(), ".to_owned()".to_owned(),
Applicability::MachineApplicable Applicability::MachineApplicable
); );
@ -601,18 +598,22 @@ fn check_str_addition(
); );
match is_assign { match is_assign {
IsAssign::No => { IsAssign::No => {
let sugg_msg;
let lhs_sugg = if let hir::ExprKind::AddrOf(_, _, lhs_inner_expr) = lhs_expr.kind {
sugg_msg = "remove the borrow on the left and add one on the right";
(lhs_expr.span.until(lhs_inner_expr.span), "".to_owned())
} else {
sugg_msg = "call `.to_owned()` on the left and add a borrow on the right";
(lhs_expr.span.shrink_to_hi(), ".to_owned()".to_owned())
};
let suggestions = vec![ let suggestions = vec![
if let hir::ExprKind::AddrOf(_, _, lhs_inner_expr) = lhs_expr.kind { lhs_sugg,
(lhs_expr.span.until(lhs_inner_expr.span), "".to_owned())
} else {
(lhs_expr.span.shrink_to_hi(), ".to_owned()".to_owned())
},
(rhs_expr.span.shrink_to_lo(), "&".to_owned()), (rhs_expr.span.shrink_to_lo(), "&".to_owned()),
]; ];
err.multipart_suggestion(msg, suggestions, Applicability::MachineApplicable); err.multipart_suggestion(sugg_msg, suggestions, Applicability::MachineApplicable);
} }
IsAssign::Yes => { IsAssign::Yes => {
err.help(msg); err.note(str_concat_note);
} }
} }
true true

View File

@ -7,7 +7,8 @@ LL | let _a = b + ", World!";
| | `+` cannot be used to concatenate two `&str` strings | | `+` cannot be used to concatenate two `&str` strings
| &str | &str
| |
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 = note: 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: use `to_owned()` to create an owned `String` from a string reference
| |
LL | let _a = b.to_owned() + ", World!"; LL | let _a = b.to_owned() + ", World!";
| +++++++++++ | +++++++++++

View File

@ -7,7 +7,8 @@ LL | println!("🦀🦀🦀🦀🦀"); let _a = b + ", World!";
| | `+` cannot be used to concatenate two `&str` strings | | `+` cannot be used to concatenate two `&str` strings
| &str | &str
| |
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 = note: 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: use `to_owned()` to create an owned `String` from a string reference
| |
LL | println!("🦀🦀🦀🦀🦀"); let _a = b.to_owned() + ", World!"; LL | println!("🦀🦀🦀🦀🦀"); let _a = b.to_owned() + ", World!";
| +++++++++++ | +++++++++++

View File

@ -7,7 +7,8 @@ LL | let x = "Hello " + "World!";
| | `+` cannot be used to concatenate two `&str` strings | | `+` cannot be used to concatenate two `&str` strings
| &str | &str
| |
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 = note: 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: use `to_owned()` to create an owned `String` from a string reference
| |
LL | let x = "Hello ".to_owned() + "World!"; LL | let x = "Hello ".to_owned() + "World!";
| +++++++++++ | +++++++++++
@ -46,7 +47,7 @@ LL | let x = "Hello " + "World!".to_owned();
| | `+` cannot be used to concatenate a `&str` with a `String` | | `+` cannot be used to concatenate a `&str` with a `String`
| &str | &str
| |
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: call `.to_owned()` on the left and add a borrow on the right
| |
LL | let x = "Hello ".to_owned() + &"World!".to_owned(); LL | let x = "Hello ".to_owned() + &"World!".to_owned();
| +++++++++++ + | +++++++++++ +
@ -59,12 +60,9 @@ LL | let _ = &a + &b;
| | | | | |
| | `+` cannot be used to concatenate two `&str` strings | | `+` cannot be used to concatenate two `&str` strings
| &String | &String
| help: remove the borrow to obtain an owned `String`
| |
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 = note: 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;
|
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
@ -75,7 +73,7 @@ LL | let _ = &a + b;
| | `+` cannot be used to concatenate a `&str` with a `String` | | `+` cannot be used to concatenate a `&str` with a `String`
| &String | &String
| |
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: remove the borrow on the left and add one on the right
| |
LL - let _ = &a + b; LL - let _ = &a + b;
LL + let _ = a + &b; LL + let _ = a + &b;
@ -99,7 +97,7 @@ LL | let _ = e + b;
| | `+` cannot be used to concatenate a `&str` with a `String` | | `+` cannot be used to concatenate a `&str` with a `String`
| &String | &String
| |
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: call `.to_owned()` on the left and add a borrow on the right
| |
LL | let _ = e.to_owned() + &b; LL | let _ = e.to_owned() + &b;
| +++++++++++ + | +++++++++++ +
@ -113,7 +111,8 @@ LL | let _ = e + &b;
| | `+` cannot be used to concatenate two `&str` strings | | `+` cannot be used to concatenate two `&str` strings
| &String | &String
| |
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 = note: 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: use `to_owned()` to create an owned `String` from a string reference
| |
LL | let _ = e.to_owned() + &b; LL | let _ = e.to_owned() + &b;
| +++++++++++ | +++++++++++
@ -127,7 +126,8 @@ LL | let _ = e + d;
| | `+` cannot be used to concatenate two `&str` strings | | `+` cannot be used to concatenate two `&str` strings
| &String | &String
| |
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 = note: 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: use `to_owned()` to create an owned `String` from a string reference
| |
LL | let _ = e.to_owned() + d; LL | let _ = e.to_owned() + d;
| +++++++++++ | +++++++++++
@ -141,7 +141,8 @@ LL | let _ = e + &d;
| | `+` cannot be used to concatenate two `&str` strings | | `+` cannot be used to concatenate two `&str` strings
| &String | &String
| |
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 = note: 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: use `to_owned()` to create an owned `String` from a string reference
| |
LL | let _ = e.to_owned() + &d; LL | let _ = e.to_owned() + &d;
| +++++++++++ | +++++++++++
@ -171,7 +172,8 @@ LL | let _ = c + &d;
| | `+` cannot be used to concatenate two `&str` strings | | `+` cannot be used to concatenate two `&str` strings
| &str | &str
| |
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 = note: 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: use `to_owned()` to create an owned `String` from a string reference
| |
LL | let _ = c.to_owned() + &d; LL | let _ = c.to_owned() + &d;
| +++++++++++ | +++++++++++
@ -185,7 +187,8 @@ LL | let _ = c + d;
| | `+` cannot be used to concatenate two `&str` strings | | `+` cannot be used to concatenate two `&str` strings
| &str | &str
| |
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 = note: 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: use `to_owned()` to create an owned `String` from a string reference
| |
LL | let _ = c.to_owned() + d; LL | let _ = c.to_owned() + d;
| +++++++++++ | +++++++++++

View File

@ -7,7 +7,8 @@ LL | let c = a + b;
| | `+` cannot be used to concatenate two `&str` strings | | `+` cannot be used to concatenate two `&str` strings
| &String | &String
| |
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 = note: 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: use `to_owned()` to create an owned `String` from a string reference
| |
LL | let c = a.to_owned() + b; LL | let c = a.to_owned() + b;
| +++++++++++ | +++++++++++

View File

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