Skip to content

Commit 4644c3a

Browse files
Add check for when left and right overlap and change span for explanation to point at operator
1 parent 4d648ce commit 4644c3a

9 files changed

+17
-47
lines changed

src/librustc_typeck/check/op.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
306306
if let Some(missing_trait) = missing_trait {
307307
if op.node == hir::BinOpKind::Add &&
308308
self.check_str_addition(expr, lhs_expr, rhs_expr, lhs_ty,
309-
rhs_ty, &mut err, true) {
309+
rhs_ty, &mut err, true, op) {
310310
// This has nothing here because it means we did string
311311
// concatenation (e.g., "Hello " += "World!"). This means
312312
// we don't want the note in the else clause to be emitted
@@ -332,8 +332,10 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
332332
op.node.as_str(),
333333
lhs_ty);
334334

335-
err.span_label(lhs_expr.span, lhs_ty.to_string());
336-
err.span_label(rhs_expr.span, rhs_ty.to_string());
335+
if !lhs_expr.span.eq(&rhs_expr.span) {
336+
err.span_label(lhs_expr.span, lhs_ty.to_string());
337+
err.span_label(rhs_expr.span, rhs_ty.to_string());
338+
}
337339

338340
let mut suggested_deref = false;
339341
if let Ref(_, mut rty, _) = lhs_ty.sty {
@@ -384,7 +386,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
384386
if let Some(missing_trait) = missing_trait {
385387
if op.node == hir::BinOpKind::Add &&
386388
self.check_str_addition(expr, lhs_expr, rhs_expr, lhs_ty,
387-
rhs_ty, &mut err, false) {
389+
rhs_ty, &mut err, false, op) {
388390
// This has nothing here because it means we did string
389391
// concatenation (e.g., "Hello " + "World!"). This means
390392
// we don't want the note in the else clause to be emitted
@@ -422,6 +424,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
422424
rhs_ty: Ty<'tcx>,
423425
err: &mut errors::DiagnosticBuilder<'_>,
424426
is_assign: bool,
427+
op: hir::BinOp,
425428
) -> bool {
426429
let source_map = self.tcx.sess.source_map();
427430
let msg = "`to_owned()` can be used to create an owned `String` \
@@ -435,7 +438,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
435438
(&Ref(_, l_ty, _), &Ref(_, r_ty, _))
436439
if l_ty.sty == Str && r_ty.sty == Str => {
437440
if !is_assign {
438-
err.span_label(expr.span,
441+
err.span_label(op.span,
439442
"`+` can't be used to concatenate two `&str` strings");
440443
match source_map.span_to_snippet(lhs_expr.span) {
441444
Ok(lstring) => err.span_suggestion(

src/test/ui/derives/derives-span-PartialEq-enum-struct-variant.stderr

-6
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@ error[E0369]: binary operation `==` cannot be applied to type `Error`
33
|
44
LL | x: Error
55
| ^^^^^^^^
6-
| |
7-
| Error
8-
| Error
96
|
107
= note: an implementation of `std::cmp::PartialEq` might be missing for `Error`
118

@@ -14,9 +11,6 @@ error[E0369]: binary operation `!=` cannot be applied to type `Error`
1411
|
1512
LL | x: Error
1613
| ^^^^^^^^
17-
| |
18-
| Error
19-
| Error
2014
|
2115
= note: an implementation of `std::cmp::PartialEq` might be missing for `Error`
2216

src/test/ui/derives/derives-span-PartialEq-enum.stderr

-6
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@ error[E0369]: binary operation `==` cannot be applied to type `Error`
33
|
44
LL | Error
55
| ^^^^^
6-
| |
7-
| Error
8-
| Error
96
|
107
= note: an implementation of `std::cmp::PartialEq` might be missing for `Error`
118

@@ -14,9 +11,6 @@ error[E0369]: binary operation `!=` cannot be applied to type `Error`
1411
|
1512
LL | Error
1613
| ^^^^^
17-
| |
18-
| Error
19-
| Error
2014
|
2115
= note: an implementation of `std::cmp::PartialEq` might be missing for `Error`
2216

src/test/ui/derives/derives-span-PartialEq-struct.stderr

-6
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@ error[E0369]: binary operation `==` cannot be applied to type `Error`
33
|
44
LL | x: Error
55
| ^^^^^^^^
6-
| |
7-
| Error
8-
| Error
96
|
107
= note: an implementation of `std::cmp::PartialEq` might be missing for `Error`
118

@@ -14,9 +11,6 @@ error[E0369]: binary operation `!=` cannot be applied to type `Error`
1411
|
1512
LL | x: Error
1613
| ^^^^^^^^
17-
| |
18-
| Error
19-
| Error
2014
|
2115
= note: an implementation of `std::cmp::PartialEq` might be missing for `Error`
2216

src/test/ui/derives/derives-span-PartialEq-tuple-struct.stderr

-6
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@ error[E0369]: binary operation `==` cannot be applied to type `Error`
33
|
44
LL | Error
55
| ^^^^^
6-
| |
7-
| Error
8-
| Error
96
|
107
= note: an implementation of `std::cmp::PartialEq` might be missing for `Error`
118

@@ -14,9 +11,6 @@ error[E0369]: binary operation `!=` cannot be applied to type `Error`
1411
|
1512
LL | Error
1613
| ^^^^^
17-
| |
18-
| Error
19-
| Error
2014
|
2115
= note: an implementation of `std::cmp::PartialEq` might be missing for `Error`
2216

src/test/ui/derives/deriving-no-inner-impl-error-message.stderr

-6
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@ error[E0369]: binary operation `==` cannot be applied to type `NoCloneOrEq`
33
|
44
LL | x: NoCloneOrEq
55
| ^^^^^^^^^^^^^^
6-
| |
7-
| NoCloneOrEq
8-
| NoCloneOrEq
96
|
107
= note: an implementation of `std::cmp::PartialEq` might be missing for `NoCloneOrEq`
118

@@ -14,9 +11,6 @@ error[E0369]: binary operation `!=` cannot be applied to type `NoCloneOrEq`
1411
|
1512
LL | x: NoCloneOrEq
1613
| ^^^^^^^^^^^^^^
17-
| |
18-
| NoCloneOrEq
19-
| NoCloneOrEq
2014
|
2115
= note: an implementation of `std::cmp::PartialEq` might be missing for `NoCloneOrEq`
2216

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

+3-4
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@ error[E0369]: binary operation `+` cannot be applied to type `&str`
22
--> $DIR/issue-47377.rs:4:14
33
|
44
LL | let _a = b + ", World!";
5-
| --^-----------
6-
| | |
7-
| | &str
5+
| - ^ ---------- &str
6+
| | |
7+
| | `+` can't be used to concatenate two `&str` strings
88
| &str
9-
| `+` can't be used to concatenate two `&str` strings
109
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
1110
|
1211
LL | let _a = b.to_owned() + ", World!";

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

+3-4
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@ error[E0369]: binary operation `+` cannot be applied to type `&str`
22
--> $DIR/issue-47380.rs:3:35
33
|
44
LL | println!("🦀🦀🦀🦀🦀"); let _a = b + ", World!";
5-
| --^-----------
6-
| | |
7-
| | &str
5+
| - ^ ---------- &str
6+
| | |
7+
| | `+` can't be used to concatenate two `&str` strings
88
| &str
9-
| `+` can't be used to concatenate two `&str` strings
109
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
1110
|
1211
LL | println!("🦀🦀🦀🦀🦀"); let _a = b.to_owned() + ", World!";

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

+3-4
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@ error[E0369]: binary operation `+` cannot be applied to type `&str`
22
--> $DIR/issue-39018.rs:2:22
33
|
44
LL | let x = "Hello " + "World!";
5-
| ---------^---------
6-
| | |
7-
| | &str
5+
| -------- ^ -------- &str
6+
| | |
7+
| | `+` can't be used to concatenate two `&str` strings
88
| &str
9-
| `+` can't be used to concatenate two `&str` strings
109
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
1110
|
1211
LL | let x = "Hello ".to_owned() + "World!";

0 commit comments

Comments
 (0)