Skip to content

Confusing 'unreachable code' error message when macro is involved #64590

Closed
@Aaron1011

Description

@Aaron1011

This code:

macro_rules! early_return {
    () => {
        return ()
    }
}

fn main() {
    return early_return!();
}

Gives the following warning:

warning: unreachable expression
 --> src/main.rs:8:5
  |
8 |     return early_return!();
  |     ^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: #[warn(unreachable_code)] on by default

    Finished dev [unoptimized + debuginfo] target(s) in 0.40s
     Running `target/debug/playground`

This warning is misleading - it suggests that the entire return statement, including the call to early_return!() is unreachable.

This also occurs in the more 'obviously wrong' case of:

fn main() {
    return return;
}

In general, it could be hard to decide what kind of warning message to generate. I think there are two things we could do to improve error messages in most cases:

  1. If the root cause of the unreachable lint (e.g. whatever produces a !) was generated by a macro, explicitly indicate this in the warning. This should help make it clear to the user that there is hidden control flow going on.
  2. Add special handling for return statements. If possible, try to detect that the cause of the unreachability was 'inside' of the return, and add a note pointing directly at it (e.g. 'the expression ' unconditionally diverges')

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)C-enhancementCategory: An issue proposing an enhancement or a PR with one.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