Skip to content

Commit c8b4873

Browse files
committed
Add function to manually fulfill lint expectations (RFC 2383)
1 parent 6c6388c commit c8b4873

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

compiler/rustc_lint/src/context.rs

+32-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use rustc_middle::middle::stability;
3333
use rustc_middle::ty::layout::{LayoutError, LayoutOfHelpers, TyAndLayout};
3434
use rustc_middle::ty::print::with_no_trimmed_paths;
3535
use rustc_middle::ty::{self, print::Printer, subst::GenericArg, RegisteredTools, Ty, TyCtxt};
36-
use rustc_session::lint::BuiltinLintDiagnostics;
36+
use rustc_session::lint::{BuiltinLintDiagnostics, LintExpectationId};
3737
use rustc_session::lint::{FutureIncompatibleInfo, Level, Lint, LintBuffer, LintId};
3838
use rustc_session::Session;
3939
use rustc_span::lev_distance::find_best_match_for_name;
@@ -887,6 +887,29 @@ pub trait LintContext: Sized {
887887
) {
888888
self.lookup(lint, None as Option<Span>, decorate);
889889
}
890+
891+
/// This returns the lint level for the given lint at the current location.
892+
fn get_lint_level(&self, lint: &'static Lint) -> Level;
893+
894+
/// This function can be used to manually fulfill an expectation. This can
895+
/// be used for lints which contain several spans, and should be suppressed,
896+
/// if either location was marked with an expectation.
897+
///
898+
/// Note that this function should only be called for [`LintExpectationId`]s
899+
/// retrieved from the current lint pass. Buffered or manually created ids can
900+
/// cause ICEs.
901+
fn fulfill_expectation(&self, expectation: LintExpectationId) {
902+
// We need to make sure that submitted expectation ids are correctly fulfilled suppressed
903+
// and stored between compilation sessions. To not manually do these steps, we simply create
904+
// a dummy diagnostic and emit is as usual, which will be suppressed and stored like a normal
905+
// expected lint diagnostic.
906+
self.sess()
907+
.struct_expect(
908+
"this is a dummy diagnostic, to submit and store an expectation",
909+
expectation,
910+
)
911+
.emit();
912+
}
890913
}
891914

892915
impl<'a> EarlyContext<'a> {
@@ -934,6 +957,10 @@ impl LintContext for LateContext<'_> {
934957
None => self.tcx.struct_lint_node(lint, hir_id, decorate),
935958
}
936959
}
960+
961+
fn get_lint_level(&self, lint: &'static Lint) -> Level {
962+
self.tcx.lint_level_at_node(lint, self.last_node_with_lint_attrs).0
963+
}
937964
}
938965

939966
impl LintContext for EarlyContext<'_> {
@@ -956,6 +983,10 @@ impl LintContext for EarlyContext<'_> {
956983
) {
957984
self.builder.struct_lint(lint, span.map(|s| s.into()), decorate)
958985
}
986+
987+
fn get_lint_level(&self, lint: &'static Lint) -> Level {
988+
self.builder.lint_level(lint).0
989+
}
959990
}
960991

961992
impl<'tcx> LateContext<'tcx> {

compiler/rustc_lint_defs/src/lib.rs

+7
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,13 @@ impl Level {
229229
Level::Deny | Level::Forbid => true,
230230
}
231231
}
232+
233+
pub fn get_expectation_id(&self) -> Option<LintExpectationId> {
234+
match self {
235+
Level::Expect(id) | Level::ForceWarn(Some(id)) => Some(*id),
236+
_ => None,
237+
}
238+
}
232239
}
233240

234241
/// Specification of a single lint.

0 commit comments

Comments
 (0)