Skip to content

if-then-some-else-none does not account for extra exprs before the Some(...) #15005

Open
@matthiaskrgr

Description

@matthiaskrgr

Using the following flags

--force-warn clippy::if-then-some-else-none

this code:

struct Counter {
    count: u32,
}

impl Counter {
    fn new() -> Counter {
        Counter { count: 0 }
    }
}

// ANCHOR: ch19
impl Iterator for Counter {
    type Item = u32;

    fn next(&mut self) -> Option<Self::Item> {
        // --snip--
        // ANCHOR_END: ch19
        if self.count < 5 {
            self.count += 1;
            Some(self.count)
        } else {
            None
        }
    }
}

caused the following diagnostics:

    Checking _lib v0.1.0 (/tmp/icemaker_global_tempdir.1WUapHwIh9Kn/icemaker_clippyfix_tempdir.HUCch1ocE4dy/_lib)
warning: this could be simplified with `bool::then`
  --> src/lib.rs:18:9
   |
18 | /         if self.count < 5 {
19 | |             self.count += 1;
20 | |             Some(self.count)
21 | |         } else {
22 | |             None
23 | |         }
   | |_________^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#if_then_some_else_none
   = note: requested on the command line with `--force-warn clippy::if-then-some-else-none`
help: try
   |
18 ~         (self.count < 5).then(||  { self.count += 1;
19 +             Some(; self.count })
   |

warning: `_lib` (lib) generated 1 warning (run `cargo clippy --fix --lib -p _lib` to apply 1 suggestion)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.11s

However after applying these diagnostics, the resulting code:

struct Counter {
    count: u32,
}

impl Counter {
    fn new() -> Counter {
        Counter { count: 0 }
    }
}

// ANCHOR: ch19
impl Iterator for Counter {
    type Item = u32;

    fn next(&mut self) -> Option<Self::Item> {
        // --snip--
        // ANCHOR_END: ch19
        (self.count < 5).then(||  { self.count += 1;
            Some(; self.count })
    }
}

no longer compiled:

    Checking _lib v0.1.0 (/tmp/icemaker_global_tempdir.1WUapHwIh9Kn/icemaker_clippyfix_tempdir.HUCch1ocE4dy/_lib)
error: mismatched closing delimiter: `}`
  --> src/lib.rs:19:17
   |
19 |             Some(; self.count })
   |                 ^             ^ mismatched closing delimiter
   |                 |
   |                 unclosed delimiter

error: could not compile `_lib` (lib) due to 1 previous error
warning: build failed, waiting for other jobs to finish...
error: could not compile `_lib` (lib test) due to 1 previous error

Version:

rustc 1.89.0-nightly (f315e6145 2025-06-06)
binary: rustc
commit-hash: f315e6145802e091ff9fceab6db627a4b4ec2b86
commit-date: 2025-06-06
host: x86_64-unknown-linux-gnu
release: 1.89.0-nightly
LLVM version: 20.1.5

Metadata

Metadata

Assignees

Labels

I-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