Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve string concatenation suggestion #92843

Merged
merged 6 commits into from
Jan 22, 2022
Merged

Conversation

camelid
Copy link
Member

@camelid camelid commented Jan 13, 2022

Before:

error[E0369]: cannot add `&str` to `&str`
 --> file.rs:2:22
  |
2 |     let _x = "hello" + " world";
  |              ------- ^ -------- &str
  |              |       |
  |              |       `+` cannot be used to concatenate two `&str` strings
  |              &str
  |
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
  |
2 |     let _x = "hello".to_owned() + " world";
  |              ~~~~~~~~~~~~~~~~~~

After:

error[E0369]: cannot add `&str` to `&str`
 --> file.rs:2:22
  |
2 |     let _x = "hello" + " world";
  |              ------- ^ -------- &str
  |              |       |
  |              |       `+` cannot be used to concatenate two `&str` strings
  |              &str
  |
  = note: string concatenation requires an owned `String` on the left
help: create an owned `String` from a string reference
  |
2 |     let _x = "hello".to_owned() + " world";
  |                     +++++++++++

Now, multipart suggestions are used instead of `span_to_snippet`, which
improves code quality, makes the suggestion work even without access to
source code, and, most importantly, improves the rendering of the
suggestion.
This should make it easier to read.
This code is sufficiently complex -- I'm guessing due to the very long
`match` guards -- that rustfmt doesn't format it.
@camelid camelid added C-enhancement Category: An issue proposing an enhancement or a PR with one. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` labels Jan 13, 2022
@rustbot rustbot added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Jan 13, 2022
@rust-highfive
Copy link
Collaborator

r? @matthewjasper

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jan 13, 2022
@camelid
Copy link
Member Author

camelid commented Jan 13, 2022

r? @davidtwco

Copy link
Member

@davidtwco davidtwco left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like these changes, I've left some nitpicks and comments regarding the specific messages but this looks good otherwise.

compiler/rustc_typeck/src/check/op.rs Outdated Show resolved Hide resolved
compiler/rustc_typeck/src/check/op.rs Outdated Show resolved Hide resolved
compiler/rustc_typeck/src/check/op.rs Outdated Show resolved Hide resolved
compiler/rustc_typeck/src/check/op.rs Outdated Show resolved Hide resolved
compiler/rustc_typeck/src/check/op.rs Outdated Show resolved Hide resolved
src/test/ui/span/issue-39018.stderr Outdated Show resolved Hide resolved
src/test/ui/span/issue-39018.stderr Outdated Show resolved Hide resolved
compiler/rustc_typeck/src/check/op.rs Outdated Show resolved Hide resolved
@davidtwco davidtwco added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 16, 2022
@camelid camelid added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jan 19, 2022
@camelid
Copy link
Member Author

camelid commented Jan 19, 2022

I went with a shorter and slightly different version of the note you suggested. What do you think?

@davidtwco
Copy link
Member

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Jan 21, 2022

📌 Commit 7c4eca0 has been approved by davidtwco

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 21, 2022
bors added a commit to rust-lang-ci/rust that referenced this pull request Jan 21, 2022
…askrgr

Rollup of 10 pull requests

Successful merges:

 - rust-lang#91965 (Add more granular `--exclude` in `x.py`)
 - rust-lang#92467 (Ensure that early-bound function lifetimes are always 'local')
 - rust-lang#92586 (Set the allocation MIN_ALIGN for espidf to 4.)
 - rust-lang#92835 (Improve error message for key="value" cfg arguments.)
 - rust-lang#92843 (Improve string concatenation suggestion)
 - rust-lang#92963 (Implement tuple array diagnostic)
 - rust-lang#93046 (Use let_else in even more places)
 - rust-lang#93109 (Improve `Arc` and `Rc` documentation)
 - rust-lang#93134 (delete `Stdin::split` forwarder)
 - rust-lang#93139 (rustdoc: fix overflow-wrap for table layouts)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 430673f into rust-lang:master Jan 22, 2022
@rustbot rustbot added this to the 1.60.0 milestone Jan 22, 2022
@camelid camelid deleted the str-concat-sugg branch January 22, 2022 02:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-enhancement Category: An issue proposing an enhancement or a PR with one. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants