Skip to content

Commit 69db5e2

Browse files
committed
Split up very long message
This should make it easier to read.
1 parent 2835ace commit 69db5e2

File tree

6 files changed

+44
-36
lines changed

6 files changed

+44
-36
lines changed

compiler/rustc_typeck/src/check/op.rs

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -549,15 +549,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
549549
is_assign: IsAssign,
550550
op: hir::BinOp,
551551
) -> bool {
552-
let remove_borrow_msg = "String concatenation appends the string on the right to the \
553-
string on the left and may require reallocation. This \
554-
requires ownership of the string on the left";
555-
556-
let msg = "`to_owned()` can be used to create an owned `String` \
557-
from a string reference. String concatenation \
558-
appends the string on the right to the string \
559-
on the left and may require reallocation. This \
560-
requires ownership of the string on the left";
552+
let str_concat_note = "String concatenation appends the string on the right to the \
553+
string on the left and may require reallocation. This \
554+
requires ownership of the string on the left.";
555+
let rm_borrow_msg = "remove the borrow to obtain an owned `String`";
556+
let to_owned_msg = "use `to_owned()` to create an owned `String` from a string reference";
561557

562558
let string_type = self.tcx.get_diagnostic_item(sym::String);
563559
let is_std_string = |ty: Ty<'tcx>| match ty.ty_adt_def() {
@@ -574,17 +570,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
574570
{
575571
if let IsAssign::No = is_assign { // Do not supply this message if `&str += &str`
576572
err.span_label(op.span, "`+` cannot be used to concatenate two `&str` strings");
573+
err.note(str_concat_note);
577574
if let hir::ExprKind::AddrOf(_,_,lhs_inner_expr) = lhs_expr.kind {
578575
err.span_suggestion(
579576
lhs_expr.span.until(lhs_inner_expr.span),
580-
remove_borrow_msg,
577+
rm_borrow_msg,
581578
"".to_owned(),
582579
Applicability::MachineApplicable
583580
);
584581
} else {
585582
err.span_suggestion(
586583
lhs_expr.span.shrink_to_hi(),
587-
msg,
584+
to_owned_msg,
588585
".to_owned()".to_owned(),
589586
Applicability::MachineApplicable
590587
);
@@ -601,18 +598,22 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
601598
);
602599
match is_assign {
603600
IsAssign::No => {
601+
let sugg_msg;
602+
let lhs_sugg = if let hir::ExprKind::AddrOf(_, _, lhs_inner_expr) = lhs_expr.kind {
603+
sugg_msg = "remove the borrow on the left and add one on the right";
604+
(lhs_expr.span.until(lhs_inner_expr.span), "".to_owned())
605+
} else {
606+
sugg_msg = "call `.to_owned()` on the left and add a borrow on the right";
607+
(lhs_expr.span.shrink_to_hi(), ".to_owned()".to_owned())
608+
};
604609
let suggestions = vec![
605-
if let hir::ExprKind::AddrOf(_, _, lhs_inner_expr) = lhs_expr.kind {
606-
(lhs_expr.span.until(lhs_inner_expr.span), "".to_owned())
607-
} else {
608-
(lhs_expr.span.shrink_to_hi(), ".to_owned()".to_owned())
609-
},
610+
lhs_sugg,
610611
(rhs_expr.span.shrink_to_lo(), "&".to_owned()),
611612
];
612-
err.multipart_suggestion(msg, suggestions, Applicability::MachineApplicable);
613+
err.multipart_suggestion(sugg_msg, suggestions, Applicability::MachineApplicable);
613614
}
614615
IsAssign::Yes => {
615-
err.help(msg);
616+
err.note(str_concat_note);
616617
}
617618
}
618619
true

src/test/ui/issues/issue-47377.stderr

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ LL | let _a = b + ", World!";
77
| | `+` cannot be used to concatenate two `&str` strings
88
| &str
99
|
10-
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
10+
= 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.
11+
help: use `to_owned()` to create an owned `String` from a string reference
1112
|
1213
LL | let _a = b.to_owned() + ", World!";
1314
| +++++++++++

src/test/ui/issues/issue-47380.stderr

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ LL | println!("🦀🦀🦀🦀🦀"); let _a = b + ", World!";
77
| | `+` cannot be used to concatenate two `&str` strings
88
| &str
99
|
10-
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
10+
= 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.
11+
help: use `to_owned()` to create an owned `String` from a string reference
1112
|
1213
LL | println!("🦀🦀🦀🦀🦀"); let _a = b.to_owned() + ", World!";
1314
| +++++++++++

src/test/ui/span/issue-39018.stderr

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ LL | let x = "Hello " + "World!";
77
| | `+` cannot be used to concatenate two `&str` strings
88
| &str
99
|
10-
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
10+
= 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.
11+
help: use `to_owned()` to create an owned `String` from a string reference
1112
|
1213
LL | let x = "Hello ".to_owned() + "World!";
1314
| +++++++++++
@@ -46,7 +47,7 @@ LL | let x = "Hello " + "World!".to_owned();
4647
| | `+` cannot be used to concatenate a `&str` with a `String`
4748
| &str
4849
|
49-
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
50+
help: call `.to_owned()` on the left and add a borrow on the right
5051
|
5152
LL | let x = "Hello ".to_owned() + &"World!".to_owned();
5253
| +++++++++++ +
@@ -59,12 +60,9 @@ LL | let _ = &a + &b;
5960
| | |
6061
| | `+` cannot be used to concatenate two `&str` strings
6162
| &String
63+
| help: remove the borrow to obtain an owned `String`
6264
|
63-
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
64-
|
65-
LL - let _ = &a + &b;
66-
LL + let _ = a + &b;
67-
|
65+
= 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.
6866

6967
error[E0369]: cannot add `String` to `&String`
7068
--> $DIR/issue-39018.rs:27:16
@@ -75,7 +73,7 @@ LL | let _ = &a + b;
7573
| | `+` cannot be used to concatenate a `&str` with a `String`
7674
| &String
7775
|
78-
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
76+
help: remove the borrow on the left and add one on the right
7977
|
8078
LL - let _ = &a + b;
8179
LL + let _ = a + &b;
@@ -99,7 +97,7 @@ LL | let _ = e + b;
9997
| | `+` cannot be used to concatenate a `&str` with a `String`
10098
| &String
10199
|
102-
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
100+
help: call `.to_owned()` on the left and add a borrow on the right
103101
|
104102
LL | let _ = e.to_owned() + &b;
105103
| +++++++++++ +
@@ -113,7 +111,8 @@ LL | let _ = e + &b;
113111
| | `+` cannot be used to concatenate two `&str` strings
114112
| &String
115113
|
116-
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
114+
= 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.
115+
help: use `to_owned()` to create an owned `String` from a string reference
117116
|
118117
LL | let _ = e.to_owned() + &b;
119118
| +++++++++++
@@ -127,7 +126,8 @@ LL | let _ = e + d;
127126
| | `+` cannot be used to concatenate two `&str` strings
128127
| &String
129128
|
130-
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
129+
= 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.
130+
help: use `to_owned()` to create an owned `String` from a string reference
131131
|
132132
LL | let _ = e.to_owned() + d;
133133
| +++++++++++
@@ -141,7 +141,8 @@ LL | let _ = e + &d;
141141
| | `+` cannot be used to concatenate two `&str` strings
142142
| &String
143143
|
144-
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
144+
= 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.
145+
help: use `to_owned()` to create an owned `String` from a string reference
145146
|
146147
LL | let _ = e.to_owned() + &d;
147148
| +++++++++++
@@ -171,7 +172,8 @@ LL | let _ = c + &d;
171172
| | `+` cannot be used to concatenate two `&str` strings
172173
| &str
173174
|
174-
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
175+
= 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.
176+
help: use `to_owned()` to create an owned `String` from a string reference
175177
|
176178
LL | let _ = c.to_owned() + &d;
177179
| +++++++++++
@@ -185,7 +187,8 @@ LL | let _ = c + d;
185187
| | `+` cannot be used to concatenate two `&str` strings
186188
| &str
187189
|
188-
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
190+
= 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.
191+
help: use `to_owned()` to create an owned `String` from a string reference
189192
|
190193
LL | let _ = c.to_owned() + d;
191194
| +++++++++++

src/test/ui/str/str-concat-on-double-ref.stderr

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ LL | let c = a + b;
77
| | `+` cannot be used to concatenate two `&str` strings
88
| &String
99
|
10-
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
10+
= 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.
11+
help: use `to_owned()` to create an owned `String` from a string reference
1112
|
1213
LL | let c = a.to_owned() + b;
1314
| +++++++++++

src/test/ui/terminal-width/non-1-width-unicode-multiline-label.stderr

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

0 commit comments

Comments
 (0)