-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Closed
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsA-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..)C-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: 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.Relevant to the compiler team, which will review and decide on the PR/issue.
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')
Metadata
Metadata
Assignees
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsA-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..)C-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: 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.Relevant to the compiler team, which will review and decide on the PR/issue.