Skip to content

Bug when applying a fix for clippy::if-then-some-else-none #15395

@V0ldek

Description

@V0ldek

Summary

I have a block of code implementing next() for an iterator. Clippy reports a single warning in that file. Here's the unmodified source:

impl<'a, 'c> Iterator for Arguments<'a, 'c> {
    type Item = Result<BlockArgument<'c, 'a>, Error>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.idx < self.block.argument_count() {
            let res = self.block.argument(self.idx);
            self.idx += 1;
            Some(res)
        } else {
            None
        }
    }
}

This results in the following lint:

warning: this could be simplified with `bool::then`
  --> src/codegen/melior_ext.rs:27:9
   |
27 | /         if self.idx < self.block.argument_count() {
28 | |             let res = self.block.argument(self.idx);
29 | |             self.idx += 1;
30 | |             Some(res)
31 | |         } else {
32 | |             None
33 | |         }
   | |_________^
   |
   = 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 `-W clippy::if-then-some-else-none`
help: try
   |
27 ~         (self.idx < self.block.argument_count()).then(||  { let res = self.block.argument(self.idx);
28 +             self.idx += 1;
29 +             Some(; res })
   |

And causes the fix to create broken code:

The following errors were reported:
error: mismatched closing delimiter: `}`
  --> src/codegen/melior_ext.rs:29:17
   |
29 |             Some(; res })
   |                 ^      ^ mismatched closing delimiter
   |                 |
   |                 unclosed delimiter

error: aborting due to 1 previous error

I created an MRE below.

Reproducer

I tried this code:

#![warn(clippy::if_then_some_else_none)]

fn main() -> Result<(), Box<dyn std::error::Error>> {
    Ok(())
}

pub struct I {
    arr: Vec<u64>,
    idx: usize,
}

impl Iterator for I {
    type Item = u64;

    fn next(&mut self) -> Option<Self::Item> {
        if self.idx < self.arr.len() {
            let res = self.arr[self.idx];
            self.idx += 1;
            Some(res)
        } else {
            None
        }
    }
}

Full output:

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

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-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: mismatched closing delimiter: `}`
  --> src/main.rs:18:17
   |
18 |             Some(; res })
   |                 ^      ^ mismatched closing delimiter
   |                 |
   |                 unclosed delimiter

error: aborting due to 1 previous error

Original diagnostics will follow.

warning: this could be simplified with `bool::then`
  --> src/main.rs:16:9
   |
16 | /         if self.idx < self.arr.len() {
17 | |             let res = self.arr[self.idx];
18 | |             self.idx += 1;
19 | |             Some(res)
20 | |         } else {
21 | |             None
22 | |         }
   | |_________^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#if_then_some_else_none
note: the lint level is defined here
  --> src/main.rs:1:9
   |
 1 | #![warn(clippy::if_then_some_else_none)]
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: try
   |
16 ~         (self.idx < self.arr.len()).then(||  { let res = self.arr[self.idx];
17 +             self.idx += 1;
18 +             Some(; res })
   |

Version

rustc 1.88.0 (6b00bc388 2025-06-23)
binary: rustc
commit-hash: 6b00bc3880198600130e1cf62b8f8a93494488cc
commit-date: 2025-06-23
host: x86_64-unknown-linux-gnu
release: 1.88.0
LLVM version: 20.1.5

Additional Labels

@rustbot label +I-suggestion-causes-error

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-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