Skip to content

Commit

Permalink
Add check for when left and right overlap and change span for explana…
Browse files Browse the repository at this point in the history
…tion to point at operator
  • Loading branch information
hgallagher1993 committed Mar 28, 2019
1 parent 4d648ce commit 4644c3a
Show file tree
Hide file tree
Showing 9 changed files with 17 additions and 47 deletions.
13 changes: 8 additions & 5 deletions src/librustc_typeck/check/op.rs
Expand Up @@ -306,7 +306,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
if let Some(missing_trait) = missing_trait {
if op.node == hir::BinOpKind::Add &&
self.check_str_addition(expr, lhs_expr, rhs_expr, lhs_ty,
rhs_ty, &mut err, true) {
rhs_ty, &mut err, true, op) {
// This has nothing here because it means we did string
// concatenation (e.g., "Hello " += "World!"). This means
// we don't want the note in the else clause to be emitted
Expand All @@ -332,8 +332,10 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
op.node.as_str(),
lhs_ty);

err.span_label(lhs_expr.span, lhs_ty.to_string());
err.span_label(rhs_expr.span, rhs_ty.to_string());
if !lhs_expr.span.eq(&rhs_expr.span) {
err.span_label(lhs_expr.span, lhs_ty.to_string());
err.span_label(rhs_expr.span, rhs_ty.to_string());
}

let mut suggested_deref = false;
if let Ref(_, mut rty, _) = lhs_ty.sty {
Expand Down Expand Up @@ -384,7 +386,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
if let Some(missing_trait) = missing_trait {
if op.node == hir::BinOpKind::Add &&
self.check_str_addition(expr, lhs_expr, rhs_expr, lhs_ty,
rhs_ty, &mut err, false) {
rhs_ty, &mut err, false, op) {
// This has nothing here because it means we did string
// concatenation (e.g., "Hello " + "World!"). This means
// we don't want the note in the else clause to be emitted
Expand Down Expand Up @@ -422,6 +424,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
rhs_ty: Ty<'tcx>,
err: &mut errors::DiagnosticBuilder<'_>,
is_assign: bool,
op: hir::BinOp,
) -> bool {
let source_map = self.tcx.sess.source_map();
let msg = "`to_owned()` can be used to create an owned `String` \
Expand All @@ -435,7 +438,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
(&Ref(_, l_ty, _), &Ref(_, r_ty, _))
if l_ty.sty == Str && r_ty.sty == Str => {
if !is_assign {
err.span_label(expr.span,
err.span_label(op.span,
"`+` can't be used to concatenate two `&str` strings");
match source_map.span_to_snippet(lhs_expr.span) {
Ok(lstring) => err.span_suggestion(
Expand Down
Expand Up @@ -3,9 +3,6 @@ error[E0369]: binary operation `==` cannot be applied to type `Error`
|
LL | x: Error
| ^^^^^^^^
| |
| Error
| Error
|
= note: an implementation of `std::cmp::PartialEq` might be missing for `Error`

Expand All @@ -14,9 +11,6 @@ error[E0369]: binary operation `!=` cannot be applied to type `Error`
|
LL | x: Error
| ^^^^^^^^
| |
| Error
| Error
|
= note: an implementation of `std::cmp::PartialEq` might be missing for `Error`

Expand Down
6 changes: 0 additions & 6 deletions src/test/ui/derives/derives-span-PartialEq-enum.stderr
Expand Up @@ -3,9 +3,6 @@ error[E0369]: binary operation `==` cannot be applied to type `Error`
|
LL | Error
| ^^^^^
| |
| Error
| Error
|
= note: an implementation of `std::cmp::PartialEq` might be missing for `Error`

Expand All @@ -14,9 +11,6 @@ error[E0369]: binary operation `!=` cannot be applied to type `Error`
|
LL | Error
| ^^^^^
| |
| Error
| Error
|
= note: an implementation of `std::cmp::PartialEq` might be missing for `Error`

Expand Down
6 changes: 0 additions & 6 deletions src/test/ui/derives/derives-span-PartialEq-struct.stderr
Expand Up @@ -3,9 +3,6 @@ error[E0369]: binary operation `==` cannot be applied to type `Error`
|
LL | x: Error
| ^^^^^^^^
| |
| Error
| Error
|
= note: an implementation of `std::cmp::PartialEq` might be missing for `Error`

Expand All @@ -14,9 +11,6 @@ error[E0369]: binary operation `!=` cannot be applied to type `Error`
|
LL | x: Error
| ^^^^^^^^
| |
| Error
| Error
|
= note: an implementation of `std::cmp::PartialEq` might be missing for `Error`

Expand Down
Expand Up @@ -3,9 +3,6 @@ error[E0369]: binary operation `==` cannot be applied to type `Error`
|
LL | Error
| ^^^^^
| |
| Error
| Error
|
= note: an implementation of `std::cmp::PartialEq` might be missing for `Error`

Expand All @@ -14,9 +11,6 @@ error[E0369]: binary operation `!=` cannot be applied to type `Error`
|
LL | Error
| ^^^^^
| |
| Error
| Error
|
= note: an implementation of `std::cmp::PartialEq` might be missing for `Error`

Expand Down
Expand Up @@ -3,9 +3,6 @@ error[E0369]: binary operation `==` cannot be applied to type `NoCloneOrEq`
|
LL | x: NoCloneOrEq
| ^^^^^^^^^^^^^^
| |
| NoCloneOrEq
| NoCloneOrEq
|
= note: an implementation of `std::cmp::PartialEq` might be missing for `NoCloneOrEq`

Expand All @@ -14,9 +11,6 @@ error[E0369]: binary operation `!=` cannot be applied to type `NoCloneOrEq`
|
LL | x: NoCloneOrEq
| ^^^^^^^^^^^^^^
| |
| NoCloneOrEq
| NoCloneOrEq
|
= note: an implementation of `std::cmp::PartialEq` might be missing for `NoCloneOrEq`

Expand Down
7 changes: 3 additions & 4 deletions src/test/ui/issues/issue-47377.stderr
Expand Up @@ -2,11 +2,10 @@ error[E0369]: binary operation `+` cannot be applied to type `&str`
--> $DIR/issue-47377.rs:4:14
|
LL | let _a = b + ", World!";
| --^-----------
| | |
| | &str
| - ^ ---------- &str
| | |
| | `+` can't be used to concatenate two `&str` strings
| &str
| `+` can't be used to concatenate two `&str` strings
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!";
Expand Down
7 changes: 3 additions & 4 deletions src/test/ui/issues/issue-47380.stderr
Expand Up @@ -2,11 +2,10 @@ error[E0369]: binary operation `+` cannot be applied to type `&str`
--> $DIR/issue-47380.rs:3:35
|
LL | println!("🦀🦀🦀🦀🦀"); let _a = b + ", World!";
| --^-----------
| | |
| | &str
| - ^ ---------- &str
| | |
| | `+` can't be used to concatenate two `&str` strings
| &str
| `+` can't be used to concatenate two `&str` strings
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!";
Expand Down
7 changes: 3 additions & 4 deletions src/test/ui/span/issue-39018.stderr
Expand Up @@ -2,11 +2,10 @@ error[E0369]: binary operation `+` cannot be applied to type `&str`
--> $DIR/issue-39018.rs:2:22
|
LL | let x = "Hello " + "World!";
| ---------^---------
| | |
| | &str
| -------- ^ -------- &str
| | |
| | `+` can't be used to concatenate two `&str` strings
| &str
| `+` can't be used to concatenate two `&str` strings
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!";
Expand Down

0 comments on commit 4644c3a

Please sign in to comment.