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

false positive on while_immutable_condition #4648

Closed
kpp opened this issue Oct 10, 2019 · 7 comments · Fixed by #4730
Closed

false positive on while_immutable_condition #4648

kpp opened this issue Oct 10, 2019 · 7 comments · Fixed by #4730
Labels
C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages good-first-issue These issues are a good way to get started with Clippy L-correctness Lint: Belongs in the correctness lint group L-style Lint: Belongs in the style lint group

Comments

@kpp
Copy link

kpp commented Oct 10, 2019

Code:

pub fn skip_while<St, F, Fut>(stream: St, f: F) -> impl Stream<Item = St::Item>
where
    St: Stream,
    F: FnMut(&St::Item) -> Fut,
    Fut: Future<Output = bool>,
{
    let stream = Box::pin(stream);
    let should_skip = true;
    crate::stream::unfold(
        (stream, f, should_skip),
        |(mut stream, mut f, should_skip)| async move {
            while should_skip {
                if let Some(item) = next(&mut stream).await {
                    if f(&item).await {
                        continue;
                    } else {
                        return Some((item, (stream, f, /* should_skip */ false)));
                    }
                } else {
                    return None;
                }
            }
            if let Some(item) = next(&mut stream).await {
                Some((item, (stream, f, /* should_skip */ false)))
            } else {
                None
            }
        },
    )
}

Error:

error: Variable in the condition are not mutated in the loop body. This either leads to an infinite or to a never running loop.
   --> src/stream.rs:717:19
    |
717 |             while should_skip {
    |                   ^^^^^^^^^^^
    |

https://github.com/kpp/futures-async-combinators/blob/d8da69c9f3b4587a99ddf1e0e5545bcff81cf57c/src/stream.rs#L718

@flip1995
Copy link
Member

I don't see the FP. As long as next(&mut stream).await is Some and f(&item).await is true this is an infinite loop, or am I missing something?

@kpp
Copy link
Author

kpp commented Oct 10, 2019

There are two termination conditions in the loop, so it is not always an infinite loop.

@sinkuu
Copy link
Contributor

sinkuu commented Oct 11, 2019

I would like to use if cond { loop { } } in this case for more clarity. while's condition should be part of its termination condition.

@flip1995
Copy link
Member

flip1995 commented Oct 11, 2019

There are two termination conditions in the loop, so it is not always an infinite loop.

Ah I see. I think the lint triggering here is correct. We should at least reword the lint to ...may lead to..., though. Best case would be to split up this lint in a correctness part, that triggers, when there are no return or break statements in the loop body, and a style part, that suggests if cond { loop { } } otherwise.

@flip1995 flip1995 added L-correctness Lint: Belongs in the correctness lint group L-style Lint: Belongs in the style lint group good-first-issue These issues are a good way to get started with Clippy C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages labels Oct 11, 2019
@kpp
Copy link
Author

kpp commented Oct 11, 2019

Yep, if cond { loop { } } looks better, however this FP should not be an error but a suggestion.

@yerke
Copy link
Contributor

yerke commented Oct 24, 2019

Can I claim this issue?

@flip1995
Copy link
Member

Yeah sure, if you have any questions just ask here or open a WIP PR.

flip1995 added a commit to flip1995/rust-clippy that referenced this issue Nov 23, 2019
…p1995

Fix check_infinite_loop (while_immutable_condition) by checking for break or return inside loop body

changelog: Fix check_infinite_loop (while_immutable_condition) by checking for break or return inside loop body
fixes rust-lang#4648
bors added a commit that referenced this issue Nov 23, 2019
Fix check_infinite_loop (while_immutable_condition) by checking for break or return inside loop body

changelog: Fix check_infinite_loop (while_immutable_condition) by checking for break or return inside loop body
fixes #4648
@bors bors closed this as completed in 016857d Nov 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages good-first-issue These issues are a good way to get started with Clippy L-correctness Lint: Belongs in the correctness lint group L-style Lint: Belongs in the style lint group
Projects
None yet
4 participants