Skip to content

[if_then_some_else_none] extra ; and } inside Some()? #15055

Closed as duplicate of#15005
@madonuko

Description

@madonuko

Summary

reproed on my machine and also on playground

Lint Name

if_then_some_else_none

Reproducer

#[warn(clippy::if_then_some_else_none)]
fn f(bytes: Vec<u8>) -> Option<[u8; 32]> {
    if bytes.len() == 32 {
        let mut array = [0u8; 32];
        array.copy_from_slice(&bytes);
        Some(array)
    } else {
        None
    }
}
    Checking playground v0.0.1 (/playground)
warning: function `f` is never used
 --> src/lib.rs:2:4
  |
2 | fn f(bytes: Vec<u8>) -> Option<[u8; 32]> {
  |    ^
  |
  = note: `#[warn(dead_code)]` on by default

warning: this could be simplified with `bool::then`
 --> src/lib.rs:3:5
  |
3 | /     if bytes.len() == 32 {
4 | |         let mut array = [0u8; 32];
5 | |         array.copy_from_slice(&bytes);
6 | |         Some(array)
7 | |     } else {
8 | |         None
9 | |     }
  | |_____^
  |
  = 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/lib.rs:1:8
  |
1 | #[warn(clippy::if_then_some_else_none)]
  |        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: try
  |
3 ~     (bytes.len() == 32).then(||  { let mut array = [0u8; 32];
4 +         array.copy_from_slice(&bytes);
5 +         Some(; array })
  |

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

Version

rustc 1.87.0 (17067e9ac 2025-05-09)
binary: rustc
commit-hash: 17067e9ac6d7ecb70e50f92c1944e545188d2359
commit-date: 2025-05-09
host: x86_64-unknown-linux-gnu
release: 1.87.0
LLVM version: 20.1.1

Additional Labels

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't have

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions