Skip to content

Two consecutive if lets cannot be used as function result #54186

@hellow554

Description

@hellow554

MCVE

fn nowork(a: Option<u32>, b: Option<u32>) -> bool {
    if let Some(x) = a { true } else { false }
    &&
    if let Some(y) = a { true } else { false }
}

fn work(a: Option<u32>, b: Option<u32>) -> bool {
    let z = if let Some(x) = a { true } else { false }
    &&
    if let Some(y) = a { true } else { false };
    z
}

Error:

error[E0308]: mismatched types
 --> src/main.rs:2:26
  |
2 |     if let Some(x) = a { true } else { false }
  |                          ^^^^ expected (), found bool
  |
  = note: expected type `()`
             found type `bool`

error[E0308]: mismatched types
 --> src/main.rs:2:40
  |
2 |     if let Some(x) = a { true } else { false }
  |                                        ^^^^^ expected (), found bool
  |
  = note: expected type `()`
             found type `bool`

error[E0308]: mismatched types
 --> src/main.rs:3:5
  |
1 |   fn nowork(a: Option<u32>, b: Option<u32>) -> bool {
  |                                                ---- expected `bool` because of return type
2 |       if let Some(x) = a { true } else { false }
3 | /     &&
4 | |     if let Some(y) = a { true } else { false }
  | |______________________________________________^ expected bool, found &&bool
  |
  = note: expected type `bool`
             found type `&&bool`

Am I missing something? Why is the first one not allowed, but the second one?

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-parserArea: The lexing & parsing of Rust source code to an ASTD-papercutDiagnostics: An error or lint that needs small tweaks.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions