rust-lang / rust-lang/rust-clippy

Clippy incorrectly binds block expression in if, in if

Open
#12,592 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

The error in question:

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

  * crates/storage/src/storage.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-clippy/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 expression, found `let` statement
  --> crates/storage/src/storage.rs:60:26
   |
60 |         let mut bucket = let res = {
   |                          ^^^
   |
   = note: only supported directly in conditions of `if` and `while` expressions

error: aborting due to previous error

Original diagnostics will follow.

The likely relevant lint:

warning: in an `if` condition, avoid complex blocks or closures with blocks; instead, move the block or closure higher and bind it with a `let`
  --> crates/storage/src/storage.rs:60:26
   |
60 |           let mut bucket = if {
   |  __________________________^
61 | |             let resp = bucket
62 | |                 .put_object("check_if_bucket_exists.txt", b"it does")
63 | |                 .await?;
64 | |             resp.status_code() == 200
65 | |         } {
   | |_________^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#blocks_in_if_conditions
   = note: `#[warn(clippy::blocks_in_if_conditions)]` on by default
help: try
   |
60 ~         let mut bucket = let res = {
61 +             let resp = bucket
62 +                 .put_object("check_if_bucket_exists.txt", b"it does")
63 +                 .await?;
64 +             resp.status_code() == 200
65 ~         }; if res {
   |

The actual block of code:

let mut bucket = if {
    let resp = bucket
        .put_object("check_if_bucket_exists.txt", b"it does")
        .await?;
    resp.status_code() == 200
} {
    bucket
} else {
    Self::init(connect).await?
};
Version
rustc 1.75.0 (82e1608df 2023-12-21)
binary: rustc
commit-hash: 82e1608dfa6e0b5569232559e3d385fea5a93112
commit-date: 2023-12-21
host: aarch64-apple-darwin
release: 1.75.0
LLVM version: 17.0.6

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

Start by reproducing the blocks_in_if_conditions lint on the Rust code shown in crates/storage/src/storage.rs, using the reported rustc 1.75.0 context. Trace the lint's suggestion generation and verify that applying the suggestion no longer produces invalid let syntax while preserving the original conditional behavior.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.