Skip to content

FP single-element-loop: nested control flow #10018

Closed
@matthiaskrgr

Description

@matthiaskrgr

Summary

We have to traverse the entire body to check for related control flow (break / continue) maybe and not warn then?

Lint Name

single-element-loop

Reproducer

I tried this code:

#![warn(clippy::if_same_then_else)]
#![allow(
    clippy::blacklisted_name,
    clippy::cognitive_complexity,
    clippy::collapsible_if,
    clippy::ifs_same_cond,
    clippy::needless_return
)]

fn if_same_then_else2() -> Result<&'static str, ()> {
    if true {
        for _ in &[42] {
            let foo: &Option<_> = &Some::<u8>(42);
            if true {
                break;
            } else {
                continue;
            }
        }
    } else {

    }
    todo!()
}

fn main() {}

I saw this happen:

after fixes were automatically applied the compiler reported errors within these files:

  * src/main.rs

This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the `--broken-code` flag

The following errors were reported:
error[E0268]: `break` outside of a loop or labeled block
  --> src/main.rs:16:17
   |
16 |                 break;
   |                 ^^^^^ cannot `break` outside of a loop or labeled block

error[E0268]: `continue` outside of a loop
  --> src/main.rs:18:17
   |
18 |                 continue;
   |                 ^^^^^^^^ cannot `continue` outside of a loop

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0268`.
Original diagnostics will follow.

warning: lint `clippy::blacklisted_name` has been renamed to `clippy::disallowed_names`
 --> src/main.rs:3:5
  |
3 |     clippy::blacklisted_name,
  |     ^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::disallowed_names`
  |
  = note: `#[warn(renamed_and_removed_lints)]` on by default

warning: unused variable: `foo`
  --> src/main.rs:13:17
   |
13 |             let foo: &Option<_> = &Some::<u8>(42);
   |                 ^^^ help: if this is intentional, prefix it with an underscore: `_foo`
   |
   = note: `#[warn(unused_variables)]` on by default

warning: function `if_same_then_else2` is never used
  --> src/main.rs:10:4
   |
10 | fn if_same_then_else2() -> Result<&'static str, ()> {
   |    ^^^^^^^^^^^^^^^^^^
   |
   = note: `#[warn(dead_code)]` on by default

warning: for loop over a single element
  --> src/main.rs:12:9
   |
12 | /         for _ in &[42] {
13 | |             let foo: &Option<_> = &Some::<u8>(42);
14 | |             if true {
15 | |                 break;
...  |
18 | |             }
19 | |         }
   | |_________^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#single_element_loop
   = note: requested on the command line with `-W clippy::single-element-loop`
help: try
   |
12 ~         {
13 +             let _ = &42;
14 +             let foo: &Option<_> = &Some::<u8>(42);
15 +             if true {
16 +                 break;
17 +             } else {
18 +                 continue;
19 +             }
20 +         }
   |

I expected to see this happen:

Version

rustc 1.67.0-nightly (c97b539e4 2022-11-30)
binary: rustc
commit-hash: c97b539e408ea353f4fde2f9251d598291fec421
commit-date: 2022-11-30
host: x86_64-unknown-linux-gnu
release: 1.67.0-nightly
LLVM version: 15.0.4

Additional Labels

No response

Metadata

Metadata

Assignees

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