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
2 changes: 2 additions & 0 deletions tests/ui/lint/unreachable_pub.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,6 @@ fn main() {
let _ = private_mod::crate_in_private::CARBON;
let _ = private_mod::pub_in_private::NITROGEN;
let _ = unsafe { private_mod::catalyze() };
todo!();
let _ = 1;
}
2 changes: 2 additions & 0 deletions tests/ui/lint/unreachable_pub.rs
Copy link
Member

Choose a reason for hiding this comment

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

This is the wrong file. unreachable_pub is about visibility

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, I'll move into its own.test file.

Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,6 @@ fn main() {
let _ = private_mod::crate_in_private::CARBON;
let _ = private_mod::pub_in_private::NITROGEN;
let _ = unsafe { private_mod::catalyze() };
todo!();
let _ = 1;
}
Loading