Skip to content

Commit

Permalink
Add function to manually fulfill lint expectations (RFC 2383)
Browse files Browse the repository at this point in the history
  • Loading branch information
xFrednet committed Jul 6, 2022
1 parent 6c6388c commit c8b4873
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
33 changes: 32 additions & 1 deletion compiler/rustc_lint/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use rustc_middle::middle::stability;
use rustc_middle::ty::layout::{LayoutError, LayoutOfHelpers, TyAndLayout};
use rustc_middle::ty::print::with_no_trimmed_paths;
use rustc_middle::ty::{self, print::Printer, subst::GenericArg, RegisteredTools, Ty, TyCtxt};
use rustc_session::lint::BuiltinLintDiagnostics;
use rustc_session::lint::{BuiltinLintDiagnostics, LintExpectationId};
use rustc_session::lint::{FutureIncompatibleInfo, Level, Lint, LintBuffer, LintId};
use rustc_session::Session;
use rustc_span::lev_distance::find_best_match_for_name;
Expand Down Expand Up @@ -887,6 +887,29 @@ pub trait LintContext: Sized {
) {
self.lookup(lint, None as Option<Span>, decorate);
}

/// This returns the lint level for the given lint at the current location.
fn get_lint_level(&self, lint: &'static Lint) -> Level;

/// This function can be used to manually fulfill an expectation. This can
/// be used for lints which contain several spans, and should be suppressed,
/// if either location was marked with an expectation.
///
/// Note that this function should only be called for [`LintExpectationId`]s
/// retrieved from the current lint pass. Buffered or manually created ids can
/// cause ICEs.
fn fulfill_expectation(&self, expectation: LintExpectationId) {
// We need to make sure that submitted expectation ids are correctly fulfilled suppressed
// and stored between compilation sessions. To not manually do these steps, we simply create
// a dummy diagnostic and emit is as usual, which will be suppressed and stored like a normal
// expected lint diagnostic.
self.sess()
.struct_expect(
"this is a dummy diagnostic, to submit and store an expectation",
expectation,
)
.emit();
}
}

impl<'a> EarlyContext<'a> {
Expand Down Expand Up @@ -934,6 +957,10 @@ impl LintContext for LateContext<'_> {
None => self.tcx.struct_lint_node(lint, hir_id, decorate),
}
}

fn get_lint_level(&self, lint: &'static Lint) -> Level {
self.tcx.lint_level_at_node(lint, self.last_node_with_lint_attrs).0
}
}

impl LintContext for EarlyContext<'_> {
Expand All @@ -956,6 +983,10 @@ impl LintContext for EarlyContext<'_> {
) {
self.builder.struct_lint(lint, span.map(|s| s.into()), decorate)
}

fn get_lint_level(&self, lint: &'static Lint) -> Level {
self.builder.lint_level(lint).0
}
}

impl<'tcx> LateContext<'tcx> {
Expand Down
7 changes: 7 additions & 0 deletions compiler/rustc_lint_defs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,13 @@ impl Level {
Level::Deny | Level::Forbid => true,
}
}

pub fn get_expectation_id(&self) -> Option<LintExpectationId> {
match self {
Level::Expect(id) | Level::ForceWarn(Some(id)) => Some(*id),
_ => None,
}
}
}

/// Specification of a single lint.
Expand Down

0 comments on commit c8b4873

Please sign in to comment.