rust-lang / rust-lang/rust-clippy

`unnecessary_to_owned` suggestion causes error when iterator is re-borrowed mutably within a loop

Open
#8,148 4 comments 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-false-positive I-suggestion-causes-error
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Summary

When running:

cargo clippy --fix

On https://github.com/ZcashFoundation/zebra/tree/410133435e875336875aab32c338a284470ceab4/zebra-chain

I get an error:

after fixes were automatically applied the compiler reported errors

Lint Name

unnecessary_to_owned

Reproducer

I tried this code:

for mut input in transaction.inputs().to_vec().into_iter() { 
    // use input and &mut transaction
}

https://github.com/ZcashFoundation/zebra/blob/410133435e875336875aab32c338a284470ceab4/zebra-chain/src/block/arbitrary.rs#L554-L578

I saw this happen:

after fixes were automatically applied the compiler reported errors within these files:

  * zebra-chain/src/block/arbitrary.rs
...
The following errors were reported:                                                                                                                                                            
error[E0502]: cannot borrow `transaction` as mutable because it is also borrowed as immutable                                                                                                  
   --> zebra-chain/src/block/arbitrary.rs:559:17                                                                                                                                               
    |                                                                                                                                                                                          
554 |     for mut input in transaction.inputs().iter().cloned() {                                                                                                                              
    |                      ------------------------------------                                                                                                                                
    |                      |                                                                                                                                                                   
    |                      immutable borrow occurs here                                                                                                                                        
    |                      immutable borrow later used here                                                                                                                                    
...                                                                                                                                                                                            
559 |                 &mut transaction,                                                                                                                                                        
    |                 ^^^^^^^^^^^^^^^^ mutable borrow occurs here   

I expected to see this happen:

Either no lint, or a fix that compiles correctly.

Version
rustc 1.59.0-nightly (7abab1efb 2021-12-17)
binary: rustc
commit-hash: 7abab1efb21617ba6845fa86328dffa16cfcf1dc
commit-date: 2021-12-17
host: x86_64-unknown-linux-gnu
release: 1.59.0-nightly
LLVM version: 13.0.0
Additional Labels

@rustbot label +I-suggestion-causes-error

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the issue with cargo clippy --fix against zebra-chain/src/block/arbitrary.rs, especially lines 554-578, using the unnecessary_to_owned lint. Compare the suggested change with the compiler's borrow error. Done means the lint is suppressed or its automatic fix compiles correctly in this re-borrowed iterator case.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.