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

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

Open
teor2345 opened this issue Dec 20, 2021 · 4 comments
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@teor2345
Copy link
Contributor

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

@teor2345 teor2345 added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Dec 20, 2021
@rustbot rustbot added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Dec 20, 2021
@smoelius
Copy link
Contributor

I'll get started on a fix right away.

@teor2345
Copy link
Contributor Author

Thanks!

I'm not sure if this will help, but here's the workaround we ended up merging:

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

We felt it was clearer than the original code.

@smoelius
Copy link
Contributor

That is helpful. Thank you, @teor2345.

bors added a commit that referenced this issue Jan 8, 2022
Change `unnecessary_to_owned` `into_iter` suggestions to `MaybeIncorrect`

I am having a hard time finding a good solution for #8148, so I am wondering if is enough to just change the suggestion's applicability to `MaybeIncorrect`?

I apologize, as I realize this is a bit of a cop out.

changelog: none
@rhysd
Copy link
Contributor

rhysd commented Dec 17, 2022

I encountered this too. Here is another smaller reproduction only with prelude:

fn repro() {
    let mut v = vec![1];
    v.to_vec().into_iter().map(|i| v.push(i));
}

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 I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

No branches or pull requests

4 participants