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

new lint: unnecessary_reserve #14114

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

wowinter13
Copy link
Contributor

@wowinter13 wowinter13 commented Jan 30, 2025

resurrection of #10157

fixes #8982

changelog: [unnecessary_reserve]: add new lint unnecessary_reserve

@rustbot
Copy link
Collaborator

rustbot commented Jan 30, 2025

r? @Jarcho

rustbot has assigned @Jarcho.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jan 30, 2025
@wowinter13 wowinter13 changed the title new lint: unnecessary_reserve new lint: unnecessary_reserve Jan 30, 2025
clippy_lints/src/unnecessary_reserve.rs Outdated Show resolved Hide resolved
clippy_lints/src/unnecessary_reserve.rs Outdated Show resolved Hide resolved
clippy_lints/src/unnecessary_reserve.rs Show resolved Hide resolved
clippy_lints/src/unnecessary_reserve.rs Outdated Show resolved Hide resolved
clippy_lints/src/unnecessary_reserve.rs Outdated Show resolved Hide resolved
clippy_lints/src/unnecessary_reserve.rs Outdated Show resolved Hide resolved
clippy_lints/src/unnecessary_reserve.rs Show resolved Hide resolved
@wowinter13
Copy link
Contributor Author

@samueltardieu Samuel, really appreciate your review and points!
Will go through them tomorrow and make sure to apply the lessons

Copy link
Contributor

@samueltardieu samueltardieu left a comment

Choose a reason for hiding this comment

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

Could you include a test (maybe I've missed it) where extend() happens before reserve(), to make sure the lint doesn't trigger?

vec.reserve(array1.len());
vec.extend(array2);

// do not lint
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not? Or it can be done in a later PR.

vec.reserve(1);
vec.extend([1]);

//// do lint
Copy link
Contributor

Choose a reason for hiding this comment

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

You should use error markers to check that the lint is correctly emitted (a comment starting with //~).

}

fn msrv_1_62() {
#![clippy::msrv = "1.61"]
Copy link
Contributor

Choose a reason for hiding this comment

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

It is unusual to:

  • apply an attribute from the inside of a function
  • name a function msrv_1_62 and apply it a clippy::msrv = "1.61" attribute

Maybe call the function insufficient_msrv and apply the attribute to it.

let array2: &[usize] = &[3, 4];

// do not lint - different arrays
;
Copy link
Contributor

Choose a reason for hiding this comment

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

The ";" should be removed as well. When you find the reserve(), you might want to look for the parent statement to get the span.

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.

Unnecessary Vec/VecDeque reserve lint
4 participants