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

needless_range_loop invalid suggesstion with mut iterator and take #5945

Closed
j-schwar opened this issue Aug 23, 2020 · 1 comment · Fixed by #6102
Closed

needless_range_loop invalid suggesstion with mut iterator and take #5945

j-schwar opened this issue Aug 23, 2020 · 1 comment · Fixed by #6102
Labels
C-bug Category: Clippy is not doing the correct thing E-medium Call for participation: Medium difficulty level problem and requires some initial experience. I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@j-schwar
Copy link

Hello! I found a little bug with the needless_range_loop lint. It's suggestion produces a compiler error when attempting to mutably iterate over part of a collection.

For example:

let mut vec = vec![1, 2, 3, 4];
for i in 0..vec.len() - 1 {
    vec[i] += 1;
}
println!("{:?}", vec);

Gives the following suggestion:

warning: the loop variable `i` is only used to index `vec`.
 --> src/main.rs:4:14
  |
4 |     for i in 0..vec.len() - 1 {
  |              ^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(clippy::needless_range_loop)]` on by default
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_range_loop
help: consider using an iterator
  |
4 |     for <item> in vec.iter_mut().take(vec.len() - 1) {
  |    

The suggested change doesn't compile however:

for x in vec.iter_mut().take(vec.len() - 1) {
    *x += 1;
}
error[E0502]: cannot borrow `vec` as immutable because it is also borrowed as mutable
  --> src/main.rs:10:34
   |
10 |     for x in vec.iter_mut().take(vec.len() - 1) {
   |              ---            ---- ^^^ immutable borrow occurs here
   |              |              |
   |              |              mutable borrow later used by call
   |              mutable borrow occurs here

Playground Link

Meta

  • cargo clippy -V: clippy 0.0.212 (663d2f5cd 2020-08-22)
  • rustc -Vv:
    rustc 1.47.0-nightly (663d2f5cd 2020-08-22)
    binary: rustc
    commit-hash: 663d2f5cd3163f17eddb74ee1e028d542255f21a
    commit-date: 2020-08-22
    host: x86_64-apple-darwin
    release: 1.47.0-nightly
    LLVM version: 10.0
    
@j-schwar j-schwar added the C-bug Category: Clippy is not doing the correct thing label Aug 23, 2020
@flip1995 flip1995 added I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied E-medium Call for participation: Medium difficulty level problem and requires some initial experience. labels Aug 26, 2020
@giraffate
Copy link
Contributor

It seems to be same as #1864.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing E-medium Call for participation: Medium difficulty level problem and requires some initial experience. I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants