Closed
Description
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:
- 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. - Add special handling for
return
statements. If possible, try to detect that the cause of the unreachability was 'inside' of thereturn
, and add a note pointing directly at it (e.g. 'the expression ' unconditionally diverges')