Skip to content

Confusing error message with missing return #22216

Closed
@mrmonday

Description

@mrmonday

The following code:

#![allow(dead_code)]

fn forgot_to_return() -> usize {
    while 4 > 3 {
        /* do something */
    }
}

fn main() {
    let _ = forgot_to_return();
}

Gives the error message:

<anon>:4:5: 6:6 error: mismatched types:
 expected `usize`,
    found `()`
(expected usize,
    found ()) [E0308]
<anon>:4     while 4 > 3 {
<anon>:5         /* do something */
<anon>:6     }
error: aborting due to previous error

Playpen: http://is.gd/0sudKI

This is confusing, since it looks like the type of either 4 or 3 is () - it is not clear that the actual error is that the while loop is being treated as the final expression in the function, and its type does match the return type of the function.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-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