-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add clearer error message using
&str + &str
This is the first part of #39018. One of the common things for new users coming from more dynamic languages like JavaScript, Python or Ruby is to use `+` to concatenate strings. However, this doesn't work that way in Rust unless the first type is a `String`. This commit adds a check for this use case and outputs a new error as well as a suggestion to guide the user towards the desired behavior. It also adds a new test case to test the output of the error.
- Loading branch information
Showing
4 changed files
with
133 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
error[E0369]: binary operation `+` cannot be applied to type `&'static str` | ||
--> src/test/ui/span/issue-39018.rs:2:13 | ||
| | ||
2 | let x = "Hello " + "World!"; | ||
| ^^^^^^^^ | ||
| | ||
note: `+` can't be used to concatenate two `&str` strings | ||
--> src/test/ui/span/issue-39018.rs:2:13 | ||
| | ||
2 | let x = "Hello " + "World!"; | ||
| ^^^^^^^^ | ||
help: to_owned() can be used to create an owned `String` from a string reference. This allows concatenation since the `String` is owned. | ||
| let x = "Hello ".to_owned() + "World!"; | ||
|
||
error[E0369]: binary operation `+` cannot be applied to type `World` | ||
--> src/test/ui/span/issue-39018.rs:7:13 | ||
| | ||
7 | let y = World::Hello + World::Goodbye; | ||
| ^^^^^^^^^^^^ | ||
| | ||
note: an implementation of `std::ops::Add` might be missing for `World` | ||
--> src/test/ui/span/issue-39018.rs:7:13 | ||
| | ||
7 | let y = World::Hello + World::Goodbye; | ||
| ^^^^^^^^^^^^ | ||
|
||
error: aborting due to 2 previous errors | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
pub fn main() { | ||
let x = "Hello " + "World!"; | ||
|
||
// Make sure that the span outputs a warning | ||
// for not having an implementation for std::ops::Add | ||
// that won't output for the above string concatenation | ||
let y = World::Hello + World::Goodbye; | ||
} | ||
|
||
enum World { | ||
Hello, | ||
Goodbye, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
error[E0369]: binary operation `+` cannot be applied to type `&'static str` | ||
--> $DIR/issue-39018.rs:12:13 | ||
| | ||
12 | let x = "Hello " + "World!"; | ||
| ^^^^^^^^ | ||
| | ||
note: `+` can't be used to concatenate two `&str` strings | ||
--> $DIR/issue-39018.rs:12:13 | ||
| | ||
12 | let x = "Hello " + "World!"; | ||
| ^^^^^^^^ | ||
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. | ||
| let x = "Hello ".to_owned() + "World!"; | ||
|
||
error[E0369]: binary operation `+` cannot be applied to type `World` | ||
--> $DIR/issue-39018.rs:17:13 | ||
| | ||
17 | let y = World::Hello + World::Goodbye; | ||
| ^^^^^^^^^^^^ | ||
| | ||
note: an implementation of `std::ops::Add` might be missing for `World` | ||
--> $DIR/issue-39018.rs:17:13 | ||
| | ||
17 | let y = World::Hello + World::Goodbye; | ||
| ^^^^^^^^^^^^ | ||
|
||
error: aborting due to 2 previous errors | ||
|