rust-lang / rust-lang/rust-clippy

False positivie (I think): blocks_in_conditions

Open
#12,642 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Summary

The project is using the rocket web framework, and I get the issue on a struct deriving FromForm.

This is the output I get for cargo clippy:

warning: unused manifest key: build
    Checking news-site v0.1.0 (/home/USER/Development/news-site)
error: in a `match` scrutinee, avoid complex blocks or closures with blocks; instead, move the block or closure higher and bind it with a `let`
  --> src/app/control_panel.rs:21:28
   |
21 |   struct PublishTextForm<'a> {
   |  ____________________________^
22 | |     #[field(name = "text-type")]
23 | |     text_type: TextType,
   | |_______________________^ help: try: `let res = TextType; match res`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#blocks_in_conditions
note: the lint level is defined here
  --> src/main.rs:2:9
   |
2  | #![deny(warnings)]
   |         ^^^^^^^^
   = note: `#[deny(clippy::blocks_in_conditions)]` implied by `#[deny(warnings)]`

error: in a `match` scrutinee, avoid complex blocks or closures with blocks; instead, move the block or closure higher and bind it with a `let`
  --> src/app/control_panel.rs:21:28
   |
21 |   struct PublishTextForm<'a> {
   |  ____________________________^
22 | |     #[field(name = "text-type")]
23 | |     text_type: TextType,
24 | |     title: &'a str,
   | |____________^ help: try: `let res = &; match res`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#blocks_in_conditions

error: in a `match` scrutinee, avoid complex blocks or closures with blocks; instead, move the block or closure higher and bind it with a `let`
  --> src/app/control_panel.rs:21:28
   |
21 |   struct PublishTextForm<'a> {
   |  ____________________________^
22 | |     #[field(name = "text-type")]
23 | |     text_type: TextType,
24 | |     title: &'a str,
25 | |     #[field(name = "leading-paragraph")]
26 | |     leading_paragraph: &'a str,
   | |________________________^ help: try: `let res = &; match res`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#blocks_in_conditions

error: in a `match` scrutinee, avoid complex blocks or closures with blocks; instead, move the block or closure higher and bind it with a `let`
  --> src/app/control_panel.rs:21:28
   |
21 |   struct PublishTextForm<'a> {
   |  ____________________________^
22 | |     #[field(name = "text-type")]
23 | |     text_type: TextType,
24 | |     title: &'a str,
...  |
27 | |     #[field(name = "text-body")]
28 | |     text_body: &'a str,
   | |________________^ help: try: `let res = &; match res`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#blocks_in_conditions

error: in a `match` scrutinee, avoid complex blocks or closures with blocks; instead, move the block or closure higher and bind it with a `let`
  --> src/app/control_panel.rs:21:28
   |
21 |   struct PublishTextForm<'a> {
   |  ____________________________^
22 | |     #[field(name = "text-type")]
23 | |     text_type: TextType,
24 | |     title: &'a str,
...  |
28 | |     text_body: &'a str,
29 | |     tags: &'a str,
   | |___________^ help: try: `let res = &; match res`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#blocks_in_conditions

error: could not compile `news-site` (bin "news-site") due to 5 previous errors

Using clippy 0.1.77 (7cf61eb 2024-03-27)

Full code can be found here: https://github.com/LinneaStudenttidning/news-site

Lint Name

blocks_in_conditions

Reproducer

I tried this code:

#[derive(FromForm)]
struct PublishTextForm<'a> {
    #[field(name = "text-type")]
    text_type: TextType,
    title: &'a str,
    #[field(name = "leading-paragraph")]
    leading_paragraph: &'a str,
    #[field(name = "text-body")]
    text_body: &'a str,
    tags: &'a str,
}

I also tried to suppress the error like this:

#[allow(clippy::blocks_in_conditions)]
#[derive(FromForm)]
struct PublishTextForm<'a> {
    #[field(name = "text-type")]
    text_type: TextType,
    title: &'a str,
    #[field(name = "leading-paragraph")]
    leading_paragraph: &'a str,
    #[field(name = "text-body")]
    text_body: &'a str,
    tags: &'a str,
}

But that still fails! (With the same error...)

Then I saw the error mentioned above.

I expected to see this happen:
Nothing, since the code does not directly reference a match statement. I guess there could be some "hidden" match statement behind for example the derive macro...

Version
rustc 1.77.1 (7cf61ebde 2024-03-27)
binary: rustc
commit-hash: 7cf61ebde7b22796c69757901dd346d0fe70bd97
commit-date: 2024-03-27
host: x86_64-unknown-linux-gnu
release: 1.77.1
LLVM version: 17.0.6
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 report using the provided FromForm struct and the blocks_in_conditions lint with rustc 1.77.1 and Clippy 0.1.77. Inspect how the derive macro's generated code is attributed to the struct, and confirm that suppressing the lint on the derive does not leave the reported diagnostics. Done means the valid reproducer no longer produces these diagnostics, with regression coverage added in the relevant Clippy tests.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.