rust-lang / rust-lang/rust-clippy

short-circuit-statement misses final ";"

Open
#9,889 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-suggestion-causes-error
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Summary

.

Lint Name

short-circuit-statement

Reproducer

I tried this code:

fn unused_template_func<T>(x: T) {
    let mut i = 0;
    while i < 10 {
        i != 0 || i != 0;
        i += 1;
    }
}

fn main() -> Result<(), u8> {
    Ok(())
}

I saw this happen:
cargo clippy --fix -- -Aclippy::all -Wclippy::short_circuit_statement

warning: failed to automatically apply fixes suggested by rustc to crate `clpy`

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: expected `;`, found `i`
 --> src/main.rs:4:38
  |
4 |         let _ = if i == 0 { i != 0; }
  |                                      ^ help: add `;` here
5 |         i += 1;
  |         - unexpected token

error: aborting due to previous error

Original diagnostics will follow.

warning: unused variable: `x`
 --> src/main.rs:1:28
  |
1 | fn unused_template_func<T>(x: T) {
  |                            ^ help: if this is intentional, prefix it with an underscore: `_x`
  |
  = note: `#[warn(unused_variables)]` on by default

warning: function `unused_template_func` is never used
 --> src/main.rs:1:4
  |
1 | fn unused_template_func<T>(x: T) {
  |    ^^^^^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(dead_code)]` on by default

warning: boolean short circuit operator in statement may be clearer using an explicit test
 --> src/main.rs:4:9
  |
4 |         i != 0 || i != 0;
  |         ^^^^^^^^^^^^^^^^^ help: replace it with: `if i == 0 { i != 0; }`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#short_circuit_statement
  = note: requested on the command line with `-W clippy::short-circuit-statement`

warning: unused logical operation that must be used
 --> src/main.rs:4:9
  |
4 |         i != 0 || i != 0;
  |         ^^^^^^^^^^^^^^^^ the logical operation produces a value
  |
  = note: `#[warn(unused_must_use)]` on by default
help: use `let _ = ...` to ignore the resulting value
  |
4 |         let _ = i != 0 || i != 0;
  |         +++++++

I expected to see this happen:

Version
rustc 1.67.0-nightly (c5d82ed7a 2022-11-19)
binary: rustc
commit-hash: c5d82ed7a4ad94a538bb87e5016e7d5ce0bd434b
commit-date: 2022-11-19
host: x86_64-unknown-linux-gnu
release: 1.67.0-nightly
LLVM version: 15.0.4
Additional Labels

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue with the provided Rust code and cargo clippy command, focusing on the short-circuit-statement lint's suggested fix. Inspect the lint implementation and its tests to determine why the generated if statement lacks a final semicolon; done means the fix applies cleanly without the reported compiler error.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.