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

Complex use of Vec/VecDeque remove #14072

Open
paolobarbolini opened this issue Jan 24, 2025 · 1 comment
Open

Complex use of Vec/VecDeque remove #14072

paolobarbolini opened this issue Jan 24, 2025 · 1 comment
Labels
A-lint Area: New lints

Comments

@paolobarbolini
Copy link
Contributor

What it does

Detect uses of Vec::remove or VecDeque::remove when Vec::pop, VecDeque::pop_front or VecDeque::pop_back could have been used

Advantage

  • Makes the code easier to read
  • Possibly makes the code run faster

Drawbacks

  • In the case of Vec::remove it would break code because it changes the return type from T to Option<T>

Example

vec.remove(vec.len() - 1);

vec_deque.remove(0);
vec_deque.remove(vec_deque.len() - 1);

Could be written as:

vec.pop();

vec_deque.pop_front();
vec_deque.pop_back();
@paolobarbolini paolobarbolini added the A-lint Area: New lints label Jan 24, 2025
@samueltardieu
Copy link
Contributor

Not only the type changes from T to Option<T>, but that makes fallible code infallible: if for any reason this part of code is given an bogusly empty Vec or VecDeque, it will continue executing instead of panicking which may not be a good idea.

A more similar substitution would be to use vec.pop().unwrap() for example, but isn't that a bit heavy?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints
Projects
None yet
Development

No branches or pull requests

2 participants