Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve string concatenation suggestion #92843

Merged
merged 6 commits into from
Jan 22, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 38 additions & 57 deletions compiler/rustc_typeck/src/check/op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -549,16 +549,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
is_assign: IsAssign,
op: hir::BinOp,
) -> bool {
let source_map = self.tcx.sess.source_map();
let remove_borrow_msg = "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 msg = "`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";
let str_concat_note = "String concatenation appends the string on the right to the
camelid marked this conversation as resolved.
Show resolved Hide resolved
camelid marked this conversation as resolved.
Show resolved Hide resolved
camelid marked this conversation as resolved.
Show resolved Hide resolved
string on the left and may require reallocation.
This requires ownership of the string on the left.";
camelid marked this conversation as resolved.
Show resolved Hide resolved
let rm_borrow_msg = "remove the borrow to obtain an owned `String`";
let to_owned_msg = "use `to_owned()` to create an owned `String` from a string reference";
camelid marked this conversation as resolved.
Show resolved Hide resolved

let string_type = self.tcx.get_diagnostic_item(sym::String);
let is_std_string = |ty: Ty<'tcx>| match ty.ty_adt_def() {
Expand All @@ -574,31 +569,23 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
) =>
{
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",
);
match source_map.span_to_snippet(lhs_expr.span) {
Ok(lstring) => {
err.span_suggestion(
lhs_expr.span,
if lstring.starts_with('&') {
remove_borrow_msg
} else {
msg
},
if let Some(stripped) = lstring.strip_prefix('&') {
// let a = String::new();
// let _ = &a + "bar";
stripped.to_string()
} else {
format!("{}.to_owned()", lstring)
},
Applicability::MachineApplicable,
)
}
_ => err.help(msg),
};
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 {
err.span_suggestion(
lhs_expr.span.until(lhs_inner_expr.span),
rm_borrow_msg,
"".to_owned(),
Applicability::MachineApplicable
);
} else {
err.span_suggestion(
lhs_expr.span.shrink_to_hi(),
to_owned_msg,
".to_owned()".to_owned(),
Applicability::MachineApplicable
);
}
}
true
}
Expand All @@ -609,32 +596,26 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
op.span,
"`+` cannot be used to concatenate a `&str` with a `String`",
);
match (
source_map.span_to_snippet(lhs_expr.span),
source_map.span_to_snippet(rhs_expr.span),
is_assign,
) {
(Ok(l), Ok(r), IsAssign::No) => {
let to_string = if let Some(stripped) = l.strip_prefix('&') {
// let a = String::new(); let b = String::new();
// let _ = &a + b;
stripped.to_string()
match is_assign {
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 {
format!("{}.to_owned()", l)
sugg_msg = "call `.to_owned()` on the left and add a borrow on the right";
camelid marked this conversation as resolved.
Show resolved Hide resolved
(lhs_expr.span.shrink_to_hi(), ".to_owned()".to_owned())
};
err.multipart_suggestion(
msg,
vec![
(lhs_expr.span, to_string),
(rhs_expr.span, format!("&{}", r)),
],
Applicability::MachineApplicable,
);
let suggestions = vec![
lhs_sugg,
(rhs_expr.span.shrink_to_lo(), "&".to_owned()),
];
err.multipart_suggestion(sugg_msg, suggestions, Applicability::MachineApplicable);
}
_ => {
err.help(msg);
IsAssign::Yes => {
err.note(str_concat_note);
}
};
}
true
}
_ => false,
Expand Down
7 changes: 5 additions & 2 deletions src/test/ui/issues/issue-47377.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ LL | let _a = b + ", World!";
| | `+` cannot be used to concatenate two `&str` strings
| &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!";
| ~~~~~~~~~~~~
| +++++++++++

error: aborting due to previous error

Expand Down
7 changes: 5 additions & 2 deletions src/test/ui/issues/issue-47380.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ LL | println!("🦀🦀🦀🦀🦀"); let _a = b + ", World!";
| | `+` cannot be used to concatenate two `&str` strings
| &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!";
| ~~~~~~~~~~~~
| +++++++++++

error: aborting due to previous error

Expand Down
65 changes: 42 additions & 23 deletions src/test/ui/span/issue-39018.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ LL | let x = "Hello " + "World!";
| | `+` cannot be used to concatenate two `&str` strings
| &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!";
| ~~~~~~~~~~~~~~~~~~~
| +++++++++++

error[E0369]: cannot add `World` to `World`
--> $DIR/issue-39018.rs:8:26
Expand Down Expand Up @@ -46,10 +49,10 @@ LL | let x = "Hello " + "World!".to_owned();
| | `+` cannot be used to concatenate a `&str` with a `String`
| &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
camelid marked this conversation as resolved.
Show resolved Hide resolved
|
LL | let x = "Hello ".to_owned() + &"World!".to_owned();
| ~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~
| +++++++++++ +

error[E0369]: cannot add `&String` to `&String`
--> $DIR/issue-39018.rs:26:16
Expand All @@ -59,11 +62,11 @@ LL | let _ = &a + &b;
| | |
| | `+` cannot be used to concatenate two `&str` strings
| &String
| help: remove the borrow to obtain an owned `String`
camelid marked this conversation as resolved.
Show resolved Hide resolved
|
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;
| ~
= 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.

error[E0369]: cannot add `String` to `&String`
--> $DIR/issue-39018.rs:27:16
Expand All @@ -74,10 +77,11 @@ LL | let _ = &a + b;
| | `+` cannot be used to concatenate a `&str` with a `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;
|

error[E0308]: mismatched types
--> $DIR/issue-39018.rs:29:17
Expand All @@ -97,10 +101,10 @@ LL | let _ = e + b;
| | `+` cannot be used to concatenate a `&str` with a `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;
| ~~~~~~~~~~~~ ~~
| +++++++++++ +

error[E0369]: cannot add `&String` to `&String`
--> $DIR/issue-39018.rs:31:15
Expand All @@ -111,10 +115,13 @@ LL | let _ = e + &b;
| | `+` cannot be used to concatenate two `&str` strings
| &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;
| ~~~~~~~~~~~~
| +++++++++++

error[E0369]: cannot add `&str` to `&String`
--> $DIR/issue-39018.rs:32:15
Expand All @@ -125,10 +132,13 @@ LL | let _ = e + d;
| | `+` cannot be used to concatenate two `&str` strings
| &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;
| ~~~~~~~~~~~~
| +++++++++++

error[E0369]: cannot add `&&str` to `&String`
--> $DIR/issue-39018.rs:33:15
Expand All @@ -139,10 +149,13 @@ LL | let _ = e + &d;
| | `+` cannot be used to concatenate two `&str` strings
| &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;
| ~~~~~~~~~~~~
| +++++++++++

error[E0369]: cannot add `&&str` to `&&str`
--> $DIR/issue-39018.rs:34:16
Expand All @@ -169,10 +182,13 @@ LL | let _ = c + &d;
| | `+` cannot be used to concatenate two `&str` strings
| &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;
| ~~~~~~~~~~~~
| +++++++++++

error[E0369]: cannot add `&str` to `&str`
--> $DIR/issue-39018.rs:37:15
Expand All @@ -183,10 +199,13 @@ LL | let _ = c + d;
| | `+` cannot be used to concatenate two `&str` strings
| &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;
| ~~~~~~~~~~~~
| +++++++++++

error: aborting due to 14 previous errors

Expand Down
7 changes: 5 additions & 2 deletions src/test/ui/str/str-concat-on-double-ref.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ LL | let c = a + b;
| | `+` cannot be used to concatenate two `&str` strings
| &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;
| ~~~~~~~~~~~~
| +++++++++++

error: aborting due to previous error

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ LL | ...ཽཾཿ྄ཱྀྀྂྃ྅྆྇ྈྉྊྋྌྍྎྏྐྑྒྒྷྔ
| | `+` cannot be used to concatenate two `&str` strings
| &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!";
| ~~~~~~~~~~~~~~~~~~~~~~~~~
| +++++++++++

error: aborting due to previous error

Expand Down