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

Add manual_empty_string_creations lint #9295

Merged

Conversation

Guilherme-Vasconcelos
Copy link
Contributor

@Guilherme-Vasconcelos Guilherme-Vasconcelos commented Aug 6, 2022

Closes #2972

  • Followed [lint naming conventions][lint_naming]
  • Added passing UI tests (including committed .stderr file)
  • cargo test passes locally
  • Executed cargo dev update_lints
  • Added lint documentation
  • Run cargo dev fmt

changelog: [manual_empty_string_creations]: Add lint for empty String not being created with String::new()

@rust-highfive
Copy link

r? @dswij

(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 Aug 6, 2022
@Guilherme-Vasconcelos Guilherme-Vasconcelos force-pushed the manual-empty-string-creation branch 3 times, most recently from c2e205d to 0331eec Compare August 7, 2022 14:21
@bors
Copy link
Collaborator

bors commented Aug 9, 2022

☔ The latest upstream changes (presumably #9288) made this pull request unmergeable. Please resolve the merge conflicts.

@Guilherme-Vasconcelos Guilherme-Vasconcelos force-pushed the manual-empty-string-creation branch 2 times, most recently from a51c778 to 5de73c8 Compare August 9, 2022 21:22
@Guilherme-Vasconcelos Guilherme-Vasconcelos marked this pull request as ready for review August 9, 2022 21:33
"empty String is being created manually",
"consider using",
"String::new()".into(),
Unspecified,
Copy link

Choose a reason for hiding this comment

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

Why not make this MachineApplicable?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was a little bit unsure if this new lint can have false positives. Do you think it seems safe to make it MachineApplicable?

It worked fine in clippy's codebase, but since I'm still unexperienced with these lints I'm not sure.

Copy link

@ghost ghost Aug 14, 2022

Choose a reason for hiding this comment

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

IMO, machine applicable suggestions won't make the false positives any more annoying but it will allow people to use Cargo fix.

Copy link
Member

Choose a reason for hiding this comment

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

I don't see how the suggestions can go really wrong with this lint. Making it machine applicable seems fine, the lint will still trigger in FP cases either way.

Copy link
Contributor Author

@Guilherme-Vasconcelos Guilherme-Vasconcelos Aug 14, 2022

Choose a reason for hiding this comment

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

Thanks for the suggestions! Fixed in 4e9aebfbd2b9b94b08d27f0d61ea47d8e9e80633

@ghost
Copy link

ghost commented Aug 14, 2022

It looks like this lint needs a macro check. Here is a false positive from clap.

---> clap-3.2.17/src/util/id.rs:24:31                                 
warning: empty String is being created manually                       
  --> src/util/id.rs:24:31
   |
24 |                           name: $name.into(),
   |                                 ^^^^^^^^^^^^ help: consider using: `String::new()`
...
34 | / precomputed_hashes! {
35 | |     empty_hash,   0x1C9D_3ADB_639F_298E, "";
36 | |     help_hash,    0x5963_6393_CFFB_FE5F, "help";
37 | |     version_hash, 0x30FF_0B7C_4D07_9478, "version";
38 | | }
   | |_- in this macro invocation
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#manual_empty_string_creation
   = note: this warning originates in the macro `precomputed_hashes` (in Nightly builds, run with -Z macro-backtrace for more info)

@Guilherme-Vasconcelos
Copy link
Contributor Author

It looks like this lint needs a macro check. Here is a false positive from clap.

---> clap-3.2.17/src/util/id.rs:24:31                                 
warning: empty String is being created manually                       
  --> src/util/id.rs:24:31
   |
24 |                           name: $name.into(),
   |                                 ^^^^^^^^^^^^ help: consider using: `String::new()`
...
34 | / precomputed_hashes! {
35 | |     empty_hash,   0x1C9D_3ADB_639F_298E, "";
36 | |     help_hash,    0x5963_6393_CFFB_FE5F, "help";
37 | |     version_hash, 0x30FF_0B7C_4D07_9478, "version";
38 | | }
   | |_- in this macro invocation
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#manual_empty_string_creation
   = note: this warning originates in the macro `precomputed_hashes` (in Nightly builds, run with -Z macro-backtrace for more info)

Thanks for catching this! I thought checking for external macros would be enough, but it seems that is not the case. Fixed in 4e9aebf

Copy link
Member

@dswij dswij left a comment

Choose a reason for hiding this comment

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

Thanks for the PR! The lint itself looks great.

  1. Clippy prefers plural lint names. Can you help to rename the lint with plural form? i.e. manual_empty_string_creations.
  2. Can we add a test case covering the macro scenario?

@Guilherme-Vasconcelos Guilherme-Vasconcelos changed the title Add manual_empty_string_creation lint Add manual_empty_string_creations lint Aug 14, 2022
@dswij
Copy link
Member

dswij commented Aug 19, 2022

@Guilherme-Vasconcelos Huge thanks for this! This lint is certainly nice to have.

@bors r+

@bors
Copy link
Collaborator

bors commented Aug 19, 2022

📌 Commit 1bf8841 has been approved by dswij

It is now in the queue for this repository.

@bors
Copy link
Collaborator

bors commented Aug 19, 2022

⌛ Testing commit 1bf8841 with merge 868dba9...

@bors
Copy link
Collaborator

bors commented Aug 19, 2022

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: dswij
Pushing 868dba9 to master...

@bors bors merged commit 868dba9 into rust-lang:master Aug 19, 2022
bors added a commit that referenced this pull request Aug 23, 2022
Rename `manual_empty_string_creation` and move to pedantic

Renames it to `manual_string_new` and moves it to the pedantic category

Pedantic because it's a fairly minor style change but could be very noisy

changelog: *doesn't need its own entry, but remember to s/manual_empty_string_creation/manual_string_new/ the changelog entry for #9295*

r? `@xFrednet` to get it in before the upcoming sync as this isn't a `cargo dev rename_lint` style rename
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Performance: Suggest String::new() over "".to_string()
4 participants