Skip to content

Added new lint: reserve_after_initialization #11373

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

Merged
merged 9 commits into from
Aug 23, 2023
Merged

Conversation

agroudiev
Copy link
Contributor

Closes #11330.

A new lint that informs the user about a more concise way to create a vector with a known capacity.
Example:

let mut v: Vec<usize> = vec![];
v.reserve(10);

Produces the following help:

  |
2 | /     let mut v: Vec<usize> = vec![];
3 | |     v.reserve(10);
  | |__________________^ help: consider using `Vec::with_capacity(space_hint)`: `let v: Vec<usize> = Vec::with_capacity(10);`
  |

And can be rewritten as:

let v: Vec<usize> = Vec::with_capacity(10);

changelog: new lint [reserve_after_initialization]

@rustbot
Copy link
Collaborator

rustbot commented Aug 21, 2023

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @blyxyas (or someone else) soon.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Aug 21, 2023
Copy link
Member

@y21 y21 left a comment

Choose a reason for hiding this comment

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

Looks pretty good to me already for a first contribution 👀 but I have one question

@agroudiev agroudiev requested a review from y21 August 22, 2023 10:30
Copy link
Member

@blyxyas blyxyas left a comment

Choose a reason for hiding this comment

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

WOW, really cool for a first lint, just some minor nits, but very very good overall ❤️, I'm really impressed!

@agroudiev
Copy link
Contributor Author

agroudiev commented Aug 22, 2023

I added some tests and fixed everything you mentioned except for:

  • the use of sym!, since it apparently will not improve performance (I'm glad, I couldn't get it to work properly anyway)
  • the macros tests: I have trouble using an external crate while running tests manually, as I explained above

@y21
Copy link
Member

y21 commented Aug 22, 2023

Btw I think you can mark things that are done as resolved, that might make it a little easier to read through and see what's left ^^ (saying this because my very first comment is taking up lots of space and that comment is not relevant anymore/already applied, I can't click "resolve" myself it looks like)

@blyxyas
Copy link
Member

blyxyas commented Aug 22, 2023

I just noticed that actually testing with macros isn't really documented anywhere, so I'll do a tutorial here.

In our test suite headers can be used with the "//@" syntax. E.g. "//@run-rustfix" as the first line is a header.
You can use the header //@aux-build:proc_macros.rs:proc-macro to import the auxiliary create just for testing procedural macros and how lints interact with them.

So, after using //@aux-build:proc_macros.rs:proc-macro you can then extern macro proc_macro;, and then use proc_macros::{external, with_span}.

The auxiliary proc macro external! simulates a proc macro that is external to the crate (I think), and the with_span macro simulates a proc macro that changes the span, so you can verify if you're linting the correct expression and such.

We really should document this, #10597
If in the future something isn't in the book or other documentation (and a reviewer isn't online), you can always check the rustc-dev-guide, it's a book mainly for developing rustc and maybe cargo, but it has some things that also apply to Clippy. (if a reviewer is online, you can always ask for help here or on the Zulip server, we all check that chat more or less periodically ❤️ )

@agroudiev agroudiev requested review from y21 and blyxyas August 22, 2023 17:51
Copy link
Member

@y21 y21 left a comment

Choose a reason for hiding this comment

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

Just the last few tiny nits I could find ^^. After these it looks good to me 👍

@agroudiev agroudiev requested a review from y21 August 23, 2023 09:40
Copy link
Member

@blyxyas blyxyas left a comment

Choose a reason for hiding this comment

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

Just this mini-nit and it should be good to go! Really impressive for a first contribution ❤️

Edit: @y21 let me know if you also think this is ready, your reviews were very helpful!

Copy link
Member

@y21 y21 left a comment

Choose a reason for hiding this comment

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

LGTM!

your reviews were very helpful!

Thanks :D Glad to be of help

@blyxyas
Copy link
Member

blyxyas commented Aug 23, 2023

@bors r=blyxyas,y21

@bors
Copy link
Contributor

bors commented Aug 23, 2023

📌 Commit f3c5877 has been approved by blyxyas,y21

It is now in the queue for this repository.

@bors
Copy link
Contributor

bors commented Aug 23, 2023

⌛ Testing commit f3c5877 with merge 4932d05...

@agroudiev
Copy link
Contributor Author

Thank you so much for your help, @blyxyas and @y21!

@bors
Copy link
Contributor

bors commented Aug 23, 2023

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: blyxyas,y21
Pushing 4932d05 to master...

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.

Concisely creating a vector of a known capacity
5 participants