Skip to content

False positive on needless_continue when continuing to outer label without a nested if block #13641

Closed
@zheland

Description

Summary

needless_continue lint is triggered when continuing to outer label when used without a nested if block.
Clippy suggests "dropping the continue expression" which will lead to a change in the code logic.

The similar issue was fixed if countinue 'outer found within if blocks in: #2329.
However, it is not very clear why an exception is made only for continue in if expressions.
In general continue 'outer seems like it always should not be considered redundant, unless the inner loop can be proven to never repeat again and there are no other statements in the outer loop after the inner loop.

Reproducer

Original "realistic" example with early continue to outer loop on condition.

#[warn(clippy::needless_continue)]
fn main() {
    fn some_code1() {
        unimplemented!()
    }
    fn some_code2() {
        unimplemented!()
    }
    fn condition1() -> bool {
        unimplemented!()
    }
    fn condition2() -> bool {
        unimplemented!()
    }

    'a: while condition1() {
        loop {
            some_code1();
            if condition2() {
                continue;
            }
            some_code2();
            continue 'a;
        }
    }
}

Simplified example:

#[warn(clippy::needless_continue)]
#[allow(clippy::never_loop)]
fn main() {
    fn condition() -> bool {
        unimplemented!()
    }

    'a: while condition() {
        // This already triggers `never_loop`.
        loop {
            continue 'a; // Anyway this should not trigger `needless_continue`.
        }
    }
}

Expected

No warnings.

Actual output

warning: this `continue` expression is redundant
  --> src/main.rs:12:13
   |
12 |             continue 'a;
   |             ^^^^^^^^^^^^
   |
   = help: consider dropping the `continue` expression
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_continue
note: the lint level is defined here
  --> src/main.rs:1:8
   |
1  | #[warn(clippy::needless_continue)]
   |        ^^^^^^^^^^^^^^^^^^^^^^^^^

Meta

rustc --version --verbose:

rustc 1.82.0 (f6e511eec 2024-10-15)
binary: rustc
commit-hash: f6e511eec7342f59a25f7c0534f1dbea00d01b14
commit-date: 2024-10-15
host: x86_64-unknown-linux-gnu
release: 1.82.0
LLVM version: 19.1.1

Also reproduced on the actual master (8568ca8).

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    I-false-positiveIssue: The lint was triggered on code it shouldn't have

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions