Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions compiler/rustc_hir_typeck/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use rustc_session::parse::feature_err;
use rustc_span::edit_distance::find_best_match_for_name;
use rustc_span::hygiene::DesugaringKind;
use rustc_span::source_map::Spanned;
use rustc_span::{Ident, Span, Symbol, kw, sym};
use rustc_span::{Ident, Span, Symbol, SyntaxContext, kw, sym};
use rustc_trait_selection::infer::InferCtxtExt;
use rustc_trait_selection::traits::{self, ObligationCauseCode, ObligationCtxt};
use tracing::{debug, instrument, trace};
Expand Down Expand Up @@ -220,6 +220,20 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
self.check_expr_with_expectation_and_needs(expr, NoExpectation, needs)
}

fn is_todo_macro(&self, span: Span) -> bool {
let mut ctxt = span.ctxt();
while ctxt != SyntaxContext::root() {
let data = ctxt.outer_expn_data();
if let Some(def_id) = data.macro_def_id
&& self.tcx.is_diagnostic_item(sym::todo_macro, def_id)
{
return true;
}
ctxt = data.call_site.ctxt();
}
false
}

/// Check an expr with an expectation type which may be used to eagerly
/// guide inference when evaluating that expr.
#[instrument(skip(self, expr), level = "debug")]
Expand Down Expand Up @@ -322,7 +336,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
if self.try_structurally_resolve_type(expr.span, ty).is_never()
&& self.expr_guaranteed_to_constitute_read_for_never(expr)
{
self.diverges.set(self.diverges.get() | Diverges::always(expr.span));
let diverges = if self.is_todo_macro(expr.span) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we find that this is too heavy on perf (which it would only be in macro-heavy crates where macros produce a lot of exprs of type !), we could first check that the expression has ExprKind::Call(path, [txt]) where path refers to core::panic::panic.

// We don't warn for `todo!()` that diverges to avoid flooding the
// user with warnings while they are still working on their code.
Diverges::WarnedAlways
} else {
Diverges::always(expr.span)
};
self.diverges.set(self.diverges.get() | diverges);
}

// Record the type, which applies it effects.
Expand Down
37 changes: 37 additions & 0 deletions tests/ui/lint/unreachable_code.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//@ check-pass
//@ edition: 2018

#![allow(unused)]
#![warn(unreachable_code)]


fn foo() {
todo!();
let this_is_unreachable = 1;
}

fn bar() {
panic!("This is really unreachable");
let really_unreachable = true;
//~^ WARNING: unreachable
}

fn baz() -> bool {
if true {
todo!();
false
} else if todo!() {
true
} else {
false
}
}

fn main() {
foo();
bar();
if baz() {
todo!();
}
let this_is_reachable = 1;
}
16 changes: 16 additions & 0 deletions tests/ui/lint/unreachable_code.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
warning: unreachable statement
--> $DIR/unreachable_code.rs:15:5
|
LL | panic!("This is really unreachable");
| ------------------------------------ any code following this expression is unreachable
LL | let really_unreachable = true;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ unreachable statement
|
note: the lint level is defined here
--> $DIR/unreachable_code.rs:5:9
|
LL | #![warn(unreachable_code)]
| ^^^^^^^^^^^^^^^^

warning: 1 warning emitted

Loading