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

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

Closed
matthiaskrgr opened this issue Dec 1, 2022 · 1 comment · Fixed by #10162
Closed

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

matthiaskrgr opened this issue Dec 1, 2022 · 1 comment · Fixed by #10162
Assignees
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

@matthiaskrgr
Copy link
Member

matthiaskrgr commented Dec 1, 2022

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

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

@rustbot claim

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

Successfully merging a pull request may close this issue.

2 participants