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

Clippy suggests using a for-loop instead of while-let, even when the for loop doesn't compile. #1156

Closed
DemiMarie opened this issue Aug 9, 2016 · 8 comments
Labels
C-bug Category: Clippy is not doing the correct thing E-needs-mcve Call for participation: This issue needs a Minimal Complete and Verifiable Example

Comments

@DemiMarie
Copy link

Clippy suggests using a for loop instead of any while let loop over an iterator. But this gives a lifetime error if we don't own the iterator.

@Manishearth
Copy link
Member

You may need to for i in &iter

probably can improve the suggestion, bit tricky though.

@llogiq
Copy link
Contributor

llogiq commented Aug 9, 2016

That can fail if &iter fails to implement Iterator though. I have also seen cases where the iterator was used after the loop, which gives us trouble because for loops consume their iterators.

@DemiMarie
Copy link
Author

I am in the latter case: the iterator outlives the loop (and, in some
cases, is used by the loop body).

On Aug 9, 2016 12:29 AM, "llogiq" [email protected] wrote:

That can fail if &iter fails to implement Iterator though. I have also
seen cases where the iterator was used after the loop, which gives us
trouble because for loops consume their iterators.


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/Manishearth/rust-clippy/issues/1156#issuecomment-238450470,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AGGWB3VruxLAJuspbxRxLFd4ecE1eQA9ks5qeAIpgaJpZM4JfsQo
.

@Manishearth
Copy link
Member

So does &iter work?

@DemiMarie
Copy link
Author

Not in my case, where the iterator is already a reference.

On Aug 9, 2016 2:13 AM, "Manish Goregaokar" [email protected]
wrote:

So does &iter work?


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/Manishearth/rust-clippy/issues/1156#issuecomment-238463020,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AGGWB_VMfi6_258VpmGAWmGeUYtEKb1bks5qeBqRgaJpZM4JfsQo
.

@Manishearth
Copy link
Member

References are Copy though. It's fine to use them like this. Is it a mutable reference? In that case, does &mut iter work?

Could you post a snippet of the function?

@leoyvens
Copy link

Similar to #600

@DemiMarie
Copy link
Author

In my case I use the iterator in the loop body.

On Aug 17, 2016 08:05, "Leonardo Yvens" [email protected] wrote:

Similar to #600 https://github.com/Manishearth/rust-clippy/issues/600


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/Manishearth/rust-clippy/issues/1156#issuecomment-240391783,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AGGWB7YLiT-whTF3f42WN1MqQgBPEMi0ks5qgvkNgaJpZM4JfsQo
.

@phansch phansch added E-needs-mcve Call for participation: This issue needs a Minimal Complete and Verifiable Example C-bug Category: Clippy is not doing the correct thing labels Dec 18, 2020
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-needs-mcve Call for participation: This issue needs a Minimal Complete and Verifiable Example
Projects
None yet
Development

No branches or pull requests

6 participants