1 use crate::context::{CheckLintNameResult, LintStore};
2 use crate::late::unerased_lint_store;
4 use rustc_ast_pretty::pprust;
5 use rustc_data_structures::fx::FxHashMap;
6 use rustc_errors::{Applicability, Diagnostic, LintDiagnosticBuilder, MultiSpan};
8 use rustc_hir::{intravisit, HirId};
9 use rustc_middle::hir::nested_filter;
10 use rustc_middle::lint::{
11 struct_lint_level, LevelAndSource, LintExpectation, LintLevelMap, LintLevelSets,
12 LintLevelSource, LintSet, LintStackIndex, COMMAND_LINE,
14 use rustc_middle::ty::query::Providers;
15 use rustc_middle::ty::{RegisteredTools, TyCtxt};
16 use rustc_session::lint::{
17 builtin::{self, FORBIDDEN_LINT_GROUPS, SINGLE_USE_LIFETIMES, UNFULFILLED_LINT_EXPECTATIONS},
18 Level, Lint, LintExpectationId, LintId,
20 use rustc_session::parse::{add_feature_diagnostics, feature_err};
21 use rustc_session::Session;
22 use rustc_span::symbol::{sym, Symbol};
23 use rustc_span::{Span, DUMMY_SP};
26 MalformedAttribute, MalformedAttributeSub, OverruledAttribute, OverruledAttributeSub,
27 UnknownToolInScopedLint,
30 fn lint_levels(tcx: TyCtxt<'_>, (): ()) -> LintLevelMap {
31 let store = unerased_lint_store(tcx);
33 LintLevelsBuilder::new(tcx.sess, false, &store, &tcx.resolutions(()).registered_tools);
34 let mut builder = LintLevelMapBuilder { levels, tcx };
35 let krate = tcx.hir().krate();
37 builder.levels.id_to_set.reserve(krate.owners.len() + 1);
40 builder.levels.push(tcx.hir().attrs(hir::CRATE_HIR_ID), true, Some(hir::CRATE_HIR_ID));
42 builder.levels.register_id(hir::CRATE_HIR_ID);
43 tcx.hir().walk_toplevel_module(&mut builder);
44 builder.levels.pop(push);
46 builder.levels.update_unstable_expectation_ids();
47 builder.levels.build_map()
50 pub struct LintLevelsBuilder<'s> {
52 lint_expectations: Vec<(LintExpectationId, LintExpectation)>,
53 /// Each expectation has a stable and an unstable identifier. This map
54 /// is used to map from unstable to stable [`LintExpectationId`]s.
55 expectation_id_map: FxHashMap<LintExpectationId, LintExpectationId>,
57 id_to_set: FxHashMap<HirId, LintStackIndex>,
59 warn_about_weird_lints: bool,
61 registered_tools: &'s RegisteredTools,
64 pub struct BuilderPush {
69 impl<'s> LintLevelsBuilder<'s> {
72 warn_about_weird_lints: bool,
74 registered_tools: &'s RegisteredTools,
76 let mut builder = LintLevelsBuilder {
78 lint_expectations: Default::default(),
79 expectation_id_map: Default::default(),
80 sets: LintLevelSets::new(),
82 id_to_set: Default::default(),
83 warn_about_weird_lints,
87 builder.process_command_line(sess, store);
88 assert_eq!(builder.sets.list.len(), 1);
92 pub(crate) fn sess(&self) -> &Session {
96 pub(crate) fn lint_store(&self) -> &LintStore {
100 fn current_specs(&self) -> &FxHashMap<LintId, LevelAndSource> {
101 &self.sets.list[self.cur].specs
104 fn current_specs_mut(&mut self) -> &mut FxHashMap<LintId, LevelAndSource> {
105 &mut self.sets.list[self.cur].specs
108 fn process_command_line(&mut self, sess: &Session, store: &LintStore) {
109 self.sets.lint_cap = sess.opts.lint_cap.unwrap_or(Level::Forbid);
112 self.sets.list.push(LintSet { specs: FxHashMap::default(), parent: COMMAND_LINE });
113 for &(ref lint_name, level) in &sess.opts.lint_opts {
114 store.check_lint_name_cmdline(sess, &lint_name, level, self.registered_tools);
115 let orig_level = level;
116 let lint_flag_val = Symbol::intern(lint_name);
118 let Ok(ids) = store.find_lints(&lint_name) else {
119 // errors handled in check_lint_name_cmdline above
123 // ForceWarn and Forbid cannot be overridden
124 if let Some((Level::ForceWarn(_) | Level::Forbid, _)) =
125 self.current_specs().get(&id)
130 if self.check_gated_lint(id, DUMMY_SP) {
131 let src = LintLevelSource::CommandLine(lint_flag_val, orig_level);
132 self.current_specs_mut().insert(id, (level, src));
138 /// Attempts to insert the `id` to `level_src` map entry. If unsuccessful
139 /// (e.g. if a forbid was already inserted on the same scope), then emits a
140 /// diagnostic with no change to `specs`.
141 fn insert_spec(&mut self, id: LintId, (level, src): LevelAndSource) {
142 let (old_level, old_src) =
143 self.sets.get_lint_level(id.lint, self.cur, Some(self.current_specs()), &self.sess);
144 // Setting to a non-forbid level is an error if the lint previously had
145 // a forbid level. Note that this is not necessarily true even with a
146 // `#[forbid(..)]` attribute present, as that is overridden by `--cap-lints`.
148 // This means that this only errors if we're truly lowering the lint
149 // level from forbid.
150 if level != Level::Forbid {
151 if let Level::Forbid = old_level {
152 // Backwards compatibility check:
154 // We used to not consider `forbid(lint_group)`
155 // as preventing `allow(lint)` for some lint `lint` in
156 // `lint_group`. For now, issue a future-compatibility
157 // warning for this case.
158 let id_name = id.lint.name_lower();
159 let fcw_warning = match old_src {
160 LintLevelSource::Default => false,
161 LintLevelSource::Node(symbol, _, _) => self.store.is_lint_group(symbol),
162 LintLevelSource::CommandLine(symbol, _) => self.store.is_lint_group(symbol),
165 "fcw_warning={:?}, specs.get(&id) = {:?}, old_src={:?}, id_name={:?}",
167 self.current_specs(),
172 let decorate_diag = |diag: &mut Diagnostic| {
173 diag.span_label(src.span(), "overruled by previous forbid");
175 LintLevelSource::Default => {
177 "`forbid` lint level is the default for {}",
181 LintLevelSource::Node(_, forbid_source_span, reason) => {
182 diag.span_label(forbid_source_span, "`forbid` level set here");
183 if let Some(rationale) = reason {
184 diag.note(rationale.as_str());
187 LintLevelSource::CommandLine(_, _) => {
188 diag.note("`forbid` lint level was set on command line");
193 self.sess.emit_err(OverruledAttribute {
195 overruled: src.span(),
196 lint_level: level.as_str().to_string(),
197 lint_source: src.name(),
199 LintLevelSource::Default => {
200 OverruledAttributeSub::DefaultSource { id: id.to_string() }
202 LintLevelSource::Node(_, forbid_source_span, reason) => {
203 OverruledAttributeSub::NodeSource {
204 span: forbid_source_span,
208 LintLevelSource::CommandLine(_, _) => {
209 OverruledAttributeSub::CommandLineSource
215 FORBIDDEN_LINT_GROUPS,
216 Some(src.span().into()),
218 let mut diag_builder = diag_builder.build(&format!(
219 "{}({}) incompatible with previous forbid",
223 decorate_diag(&mut diag_builder);
229 // Retain the forbid lint level, unless we are
230 // issuing a FCW. In the FCW case, we want to
231 // respect the new setting.
238 // The lint `unfulfilled_lint_expectations` can't be expected, as it would suppress itself.
239 // Handling expectations of this lint would add additional complexity with little to no
240 // benefit. The expect level for this lint will therefore be ignored.
241 if let Level::Expect(_) = level && id == LintId::of(UNFULFILLED_LINT_EXPECTATIONS) {
245 match (old_level, level) {
246 // If the new level is an expectation store it in `ForceWarn`
247 (Level::ForceWarn(_), Level::Expect(expectation_id)) => self
249 .insert(id, (Level::ForceWarn(Some(expectation_id)), old_src)),
250 // Keep `ForceWarn` level but drop the expectation
251 (Level::ForceWarn(_), _) => {
252 self.current_specs_mut().insert(id, (Level::ForceWarn(None), old_src))
254 // Set the lint level as normal
255 _ => self.current_specs_mut().insert(id, (level, src)),
259 /// Pushes a list of AST lint attributes onto this context.
261 /// This function will return a `BuilderPush` object which should be passed
262 /// to `pop` when this scope for the attributes provided is exited.
264 /// This function will perform a number of tasks:
266 /// * It'll validate all lint-related attributes in `attrs`
267 /// * It'll mark all lint-related attributes as used
268 /// * Lint levels will be updated based on the attributes provided
269 /// * Lint attributes are validated, e.g., a `#[forbid]` can't be switched to
272 /// Don't forget to call `pop`!
275 attrs: &[ast::Attribute],
277 source_hir_id: Option<HirId>,
280 self.cur = self.sets.list.push(LintSet { specs: FxHashMap::default(), parent: prev });
282 let sess = self.sess;
283 for (attr_index, attr) in attrs.iter().enumerate() {
284 if attr.has_name(sym::automatically_derived) {
285 self.current_specs_mut().insert(
286 LintId::of(SINGLE_USE_LIFETIMES),
287 (Level::Allow, LintLevelSource::Default),
292 let level = match Level::from_attr(attr) {
294 // This is the only lint level with a `LintExpectationId` that can be created from an attribute
295 Some(Level::Expect(unstable_id)) if let Some(hir_id) = source_hir_id => {
296 let stable_id = self.create_stable_id(unstable_id, hir_id, attr_index);
298 Level::Expect(stable_id)
303 let Some(mut metas) = attr.meta_item_list() else {
307 if metas.is_empty() {
308 // This emits the unused_attributes lint for `#[level()]`
312 // Before processing the lint names, look for a reason (RFC 2383)
314 let mut reason = None;
315 let tail_li = &metas[metas.len() - 1];
316 if let Some(item) = tail_li.meta_item() {
318 ast::MetaItemKind::Word => {} // actual lint names handled later
319 ast::MetaItemKind::NameValue(ref name_value) => {
320 if item.path == sym::reason {
321 if let ast::LitKind::Str(rationale, _) = name_value.kind {
322 if !self.sess.features_untracked().lint_reasons {
324 &self.sess.parse_sess,
327 "lint reasons are experimental",
331 reason = Some(rationale);
333 sess.emit_err(MalformedAttribute {
334 span: name_value.span,
335 sub: MalformedAttributeSub::ReasonMustBeStringLiteral(
340 // found reason, reslice meta list to exclude it
341 metas.pop().unwrap();
343 sess.emit_err(MalformedAttribute {
345 sub: MalformedAttributeSub::BadAttributeArgument(item.span),
349 ast::MetaItemKind::List(_) => {
350 sess.emit_err(MalformedAttribute {
352 sub: MalformedAttributeSub::BadAttributeArgument(item.span),
358 for (lint_index, li) in metas.iter_mut().enumerate() {
359 let level = match level {
360 Level::Expect(mut id) => {
361 id.set_lint_index(Some(lint_index as u16));
368 let meta_item = match li {
369 ast::NestedMetaItem::MetaItem(meta_item) if meta_item.is_word() => meta_item,
371 if let Some(item) = li.meta_item() {
372 if let ast::MetaItemKind::NameValue(_) = item.kind {
373 if item.path == sym::reason {
374 sess.emit_err(MalformedAttribute {
376 sub: MalformedAttributeSub::ReasonMustComeLast(sp),
382 sess.emit_err(MalformedAttribute {
384 sub: MalformedAttributeSub::BadAttributeArgument(sp),
389 let tool_ident = if meta_item.path.segments.len() > 1 {
390 Some(meta_item.path.segments.remove(0).ident)
394 let tool_name = tool_ident.map(|ident| ident.name);
395 let name = pprust::path_to_string(&meta_item.path);
397 self.store.check_lint_name(&name, tool_name, self.registered_tools);
399 CheckLintNameResult::Ok(ids) => {
400 // This checks for instances where the user writes `#[expect(unfulfilled_lint_expectations)]`
401 // in that case we want to avoid overriding the lint level but instead add an expectation that
402 // can't be fulfilled. The lint message will include an explanation, that the
403 // `unfulfilled_lint_expectations` lint can't be expected.
404 if let Level::Expect(expect_id) = level {
405 // The `unfulfilled_lint_expectations` lint is not part of any lint groups. Therefore. we
406 // only need to check the slice if it contains a single lint.
407 let is_unfulfilled_lint_expectations = match ids {
408 [lint] => *lint == LintId::of(UNFULFILLED_LINT_EXPECTATIONS),
411 self.lint_expectations.push((
413 LintExpectation::new(
416 is_unfulfilled_lint_expectations,
421 let src = LintLevelSource::Node(
422 meta_item.path.segments.last().expect("empty lint name").ident.name,
427 if self.check_gated_lint(id, attr.span) {
428 self.insert_spec(id, (level, src));
433 CheckLintNameResult::Tool(result) => {
437 &format!("{}::{}", tool_ident.unwrap().name, name);
438 let src = LintLevelSource::Node(
439 Symbol::intern(complete_name),
444 self.insert_spec(*id, (level, src));
446 if let Level::Expect(expect_id) = level {
447 self.lint_expectations.push((
449 LintExpectation::new(reason, sp, false, tool_name),
453 Err((Some(ids), ref new_lint_name)) => {
454 let lint = builtin::RENAMED_AND_REMOVED_LINTS;
455 let (lvl, src) = self.sets.get_lint_level(
458 Some(self.current_specs()),
469 "lint name `{}` is deprecated \
470 and may not have an effect in the future.",
478 Applicability::MachineApplicable,
484 let src = LintLevelSource::Node(
485 Symbol::intern(&new_lint_name),
490 self.insert_spec(*id, (level, src));
492 if let Level::Expect(expect_id) = level {
493 self.lint_expectations.push((
495 LintExpectation::new(reason, sp, false, tool_name),
500 // If Tool(Err(None, _)) is returned, then either the lint does not
501 // exist in the tool or the code was not compiled with the tool and
502 // therefore the lint was never added to the `LintStore`. To detect
503 // this is the responsibility of the lint tool.
508 &CheckLintNameResult::NoTool => {
509 sess.emit_err(UnknownToolInScopedLint {
510 span: tool_ident.map(|ident| ident.span),
511 tool_name: tool_name.unwrap(),
512 lint_name: pprust::path_to_string(&meta_item.path),
513 is_nightly_build: sess.is_nightly_build().then_some(()),
518 _ if !self.warn_about_weird_lints => {}
520 CheckLintNameResult::Warning(msg, renamed) => {
521 let lint = builtin::RENAMED_AND_REMOVED_LINTS;
522 let (renamed_lint_level, src) = self.sets.get_lint_level(
525 Some(self.current_specs()),
535 let mut err = lint.build(msg);
536 if let Some(new_name) = &renamed {
541 Applicability::MachineApplicable,
548 CheckLintNameResult::NoLint(suggestion) => {
549 let lint = builtin::UNKNOWN_LINTS;
550 let (level, src) = self.sets.get_lint_level(
553 Some(self.current_specs()),
556 struct_lint_level(self.sess, lint, level, src, Some(sp.into()), |lint| {
557 let name = if let Some(tool_ident) = tool_ident {
558 format!("{}::{}", tool_ident.name, name)
562 let mut db = lint.build(format!("unknown lint: `{}`", name));
563 if let Some(suggestion) = suggestion {
568 Applicability::MachineApplicable,
575 // If this lint was renamed, apply the new lint instead of ignoring the attribute.
576 // This happens outside of the match because the new lint should be applied even if
577 // we don't warn about the name change.
578 if let CheckLintNameResult::Warning(_, Some(new_name)) = lint_result {
579 // Ignore any errors or warnings that happen because the new name is inaccurate
580 // NOTE: `new_name` already includes the tool name, so we don't have to add it again.
581 if let CheckLintNameResult::Ok(ids) =
582 self.store.check_lint_name(&new_name, None, self.registered_tools)
584 let src = LintLevelSource::Node(Symbol::intern(&new_name), sp, reason);
586 if self.check_gated_lint(id, attr.span) {
587 self.insert_spec(id, (level, src));
590 if let Level::Expect(expect_id) = level {
591 self.lint_expectations.push((
593 LintExpectation::new(reason, sp, false, tool_name),
597 panic!("renamed lint does not exist: {}", new_name);
604 for (id, &(level, ref src)) in self.current_specs().iter() {
605 if !id.lint.crate_level_only {
609 let LintLevelSource::Node(lint_attr_name, lint_attr_span, _) = *src else {
613 let lint = builtin::UNUSED_ATTRIBUTES;
614 let (lint_level, lint_src) =
615 self.sets.get_lint_level(lint, self.cur, Some(self.current_specs()), self.sess);
621 Some(lint_attr_span.into()),
623 let mut db = lint.build(&format!(
624 "{}({}) is ignored unless specified at crate level",
631 // don't set a separate error for every lint in the group
636 if self.current_specs().is_empty() {
637 self.sets.list.pop();
641 BuilderPush { prev, changed: prev != self.cur }
646 unstable_id: LintExpectationId,
649 ) -> LintExpectationId {
651 LintExpectationId::Stable { hir_id, attr_index: attr_index as u16, lint_index: None };
653 self.expectation_id_map.insert(unstable_id, stable_id);
658 /// Checks if the lint is gated on a feature that is not enabled.
660 /// Returns `true` if the lint's feature is enabled.
661 fn check_gated_lint(&self, lint_id: LintId, span: Span) -> bool {
662 if let Some(feature) = lint_id.lint.feature_gate {
663 if !self.sess.features_untracked().enabled(feature) {
664 let lint = builtin::UNKNOWN_LINTS;
665 let (level, src) = self.lint_level(builtin::UNKNOWN_LINTS);
666 struct_lint_level(self.sess, lint, level, src, Some(span.into()), |lint_db| {
668 lint_db.build(&format!("unknown lint: `{}`", lint_id.lint.name_lower()));
669 db.note(&format!("the `{}` lint is unstable", lint_id.lint.name_lower(),));
670 add_feature_diagnostics(&mut db, &self.sess.parse_sess, feature);
679 /// Called after `push` when the scope of a set of attributes are exited.
680 pub fn pop(&mut self, push: BuilderPush) {
681 self.cur = push.prev;
684 /// Find the lint level for a lint.
685 pub fn lint_level(&self, lint: &'static Lint) -> (Level, LintLevelSource) {
686 self.sets.get_lint_level(lint, self.cur, None, self.sess)
689 /// Used to emit a lint-related diagnostic based on the current state of
690 /// this lint context.
694 span: Option<MultiSpan>,
695 decorate: impl for<'a> FnOnce(LintDiagnosticBuilder<'a, ()>),
697 let (level, src) = self.lint_level(lint);
698 struct_lint_level(self.sess, lint, level, src, span, decorate)
701 /// Registers the ID provided with the current set of lints stored in
703 pub fn register_id(&mut self, id: HirId) {
704 self.id_to_set.insert(id, self.cur);
707 fn update_unstable_expectation_ids(&self) {
708 self.sess.diagnostic().update_unstable_expectation_id(&self.expectation_id_map);
711 pub fn build_map(self) -> LintLevelMap {
714 id_to_set: self.id_to_set,
715 lint_expectations: self.lint_expectations,
720 struct LintLevelMapBuilder<'tcx> {
721 levels: LintLevelsBuilder<'tcx>,
725 impl LintLevelMapBuilder<'_> {
726 fn with_lint_attrs<F>(&mut self, id: hir::HirId, f: F)
728 F: FnOnce(&mut Self),
730 let is_crate_hir = id == hir::CRATE_HIR_ID;
731 let attrs = self.tcx.hir().attrs(id);
732 let push = self.levels.push(attrs, is_crate_hir, Some(id));
735 self.levels.register_id(id);
738 self.levels.pop(push);
742 impl<'tcx> intravisit::Visitor<'tcx> for LintLevelMapBuilder<'tcx> {
743 type NestedFilter = nested_filter::All;
745 fn nested_visit_map(&mut self) -> Self::Map {
749 fn visit_param(&mut self, param: &'tcx hir::Param<'tcx>) {
750 self.with_lint_attrs(param.hir_id, |builder| {
751 intravisit::walk_param(builder, param);
755 fn visit_item(&mut self, it: &'tcx hir::Item<'tcx>) {
756 self.with_lint_attrs(it.hir_id(), |builder| {
757 intravisit::walk_item(builder, it);
761 fn visit_foreign_item(&mut self, it: &'tcx hir::ForeignItem<'tcx>) {
762 self.with_lint_attrs(it.hir_id(), |builder| {
763 intravisit::walk_foreign_item(builder, it);
767 fn visit_stmt(&mut self, e: &'tcx hir::Stmt<'tcx>) {
768 // We will call `with_lint_attrs` when we walk
769 // the `StmtKind`. The outer statement itself doesn't
770 // define the lint levels.
771 intravisit::walk_stmt(self, e);
774 fn visit_expr(&mut self, e: &'tcx hir::Expr<'tcx>) {
775 self.with_lint_attrs(e.hir_id, |builder| {
776 intravisit::walk_expr(builder, e);
780 fn visit_expr_field(&mut self, field: &'tcx hir::ExprField<'tcx>) {
781 self.with_lint_attrs(field.hir_id, |builder| {
782 intravisit::walk_expr_field(builder, field);
786 fn visit_field_def(&mut self, s: &'tcx hir::FieldDef<'tcx>) {
787 self.with_lint_attrs(s.hir_id, |builder| {
788 intravisit::walk_field_def(builder, s);
792 fn visit_variant(&mut self, v: &'tcx hir::Variant<'tcx>) {
793 self.with_lint_attrs(v.id, |builder| {
794 intravisit::walk_variant(builder, v);
798 fn visit_local(&mut self, l: &'tcx hir::Local<'tcx>) {
799 self.with_lint_attrs(l.hir_id, |builder| {
800 intravisit::walk_local(builder, l);
804 fn visit_arm(&mut self, a: &'tcx hir::Arm<'tcx>) {
805 self.with_lint_attrs(a.hir_id, |builder| {
806 intravisit::walk_arm(builder, a);
810 fn visit_trait_item(&mut self, trait_item: &'tcx hir::TraitItem<'tcx>) {
811 self.with_lint_attrs(trait_item.hir_id(), |builder| {
812 intravisit::walk_trait_item(builder, trait_item);
816 fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem<'tcx>) {
817 self.with_lint_attrs(impl_item.hir_id(), |builder| {
818 intravisit::walk_impl_item(builder, impl_item);
822 fn visit_pat_field(&mut self, field: &'tcx hir::PatField<'tcx>) {
823 self.with_lint_attrs(field.hir_id, |builder| {
824 intravisit::walk_pat_field(builder, field);
828 fn visit_generic_param(&mut self, p: &'tcx hir::GenericParam<'tcx>) {
829 self.with_lint_attrs(p.hir_id, |builder| {
830 intravisit::walk_generic_param(builder, p);
835 pub fn provide(providers: &mut Providers) {
836 providers.lint_levels = lint_levels;