Skip to content

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

@teor2345

Description

@teor2345

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't haveI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions