Skip to content

Type expectation appears to be reversed #78656

Closed
@camelid

Description

@camelid

The expected type and the found type seem to be reversed here. The error says it expected () and found i32, but it seems to me that it expected i32 and found ().

fn do_stuff(x: i32) -> i32 {
    if x < 0 {
        return 0;
    } else {
        if 1 == 1 {
            return 1;
        }
    }
}

(Playground)

Errors:

   Compiling playground v0.0.1 (/playground)
error[E0317]: `if` may be missing an `else` clause
 --> src/lib.rs:5:9
  |
5 | /         if 1 == 1 {
6 | |             return 1;
7 | |         }
  | |_________^ expected `()`, found `i32`
  |
  = note: `if` expressions without `else` evaluate to `()`
  = help: consider adding an `else` block that evaluates to the expected type

error: aborting due to previous error

For more information about this error, try `rustc --explain E0317`.
error: could not compile `playground`.

To learn more, run the command again with --verbose.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-type-systemArea: Type systemT-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