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";
) =>
{
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");
+ if let hir::ExprKind::AddrOf(_,_,lhs_inner_expr) = lhs_expr.kind {
+ err.span_suggestion(
+ lhs_expr.span.until(lhs_inner_expr.span),
+ remove_borrow_msg,
+ "".to_owned(),
+ Applicability::MachineApplicable
+ );
+ } else {
+ err.span_suggestion(
+ lhs_expr.span.shrink_to_hi(),
+ msg,
+ ".to_owned()".to_owned(),
+ Applicability::MachineApplicable
+ );
+ }
}
true
}
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()
- } else {
- format!("{}.to_owned()", l)
- };
- err.multipart_suggestion(
- msg,
- vec![
- (lhs_expr.span, to_string),
- (rhs_expr.span, format!("&{}", r)),
- ],
- Applicability::MachineApplicable,
- );
+ match is_assign {
+ IsAssign::No => {
+ let suggestions = vec![
+ if let hir::ExprKind::AddrOf(_, _, lhs_inner_expr) = lhs_expr.kind {
+ (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()),
+ ];
+ err.multipart_suggestion(msg, suggestions, Applicability::MachineApplicable);
}
- _ => {
+ IsAssign::Yes => {
err.help(msg);
}
- };
+ }
true
}
_ => false,
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!";
- | ~~~~~~~~~~~~
+ | +++++++++++
error: aborting due to previous error
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!";
- | ~~~~~~~~~~~~
+ | +++++++++++
error: aborting due to previous error
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!";
- | ~~~~~~~~~~~~~~~~~~~
+ | +++++++++++
error[E0369]: cannot add `World` to `World`
--> $DIR/issue-39018.rs:8:26
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();
- | ~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~
+ | +++++++++++ +
error[E0369]: cannot add `&String` to `&String`
--> $DIR/issue-39018.rs:26:16
|
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`
--> $DIR/issue-39018.rs:27:16
|
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
--> $DIR/issue-39018.rs:29:17
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;
- | ~~~~~~~~~~~~ ~~
+ | +++++++++++ +
error[E0369]: cannot add `&String` to `&String`
--> $DIR/issue-39018.rs:31:15
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;
- | ~~~~~~~~~~~~
+ | +++++++++++
error[E0369]: cannot add `&str` to `&String`
--> $DIR/issue-39018.rs:32:15
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;
- | ~~~~~~~~~~~~
+ | +++++++++++
error[E0369]: cannot add `&&str` to `&String`
--> $DIR/issue-39018.rs:33:15
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;
- | ~~~~~~~~~~~~
+ | +++++++++++
error[E0369]: cannot add `&&str` to `&&str`
--> $DIR/issue-39018.rs:34:16
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;
- | ~~~~~~~~~~~~
+ | +++++++++++
error[E0369]: cannot add `&str` to `&str`
--> $DIR/issue-39018.rs:37:15
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;
- | ~~~~~~~~~~~~
+ | +++++++++++
error: aborting due to 14 previous errors
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;
- | ~~~~~~~~~~~~
+ | +++++++++++
error: aborting due to previous error
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!";
- | ~~~~~~~~~~~~~~~~~~~~~~~~~
+ | +++++++++++
error: aborting due to previous error