2020-01-09 00:52:01 -06:00
|
|
|
use crate::context::{CheckLintNameResult, LintStore};
|
|
|
|
use crate::late::unerased_lint_store;
|
2020-04-27 12:56:11 -05:00
|
|
|
use rustc_ast as ast;
|
2020-01-11 10:02:46 -06:00
|
|
|
use rustc_ast_pretty::pprust;
|
2020-01-09 00:52:01 -06:00
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
2022-03-23 21:03:04 -05:00
|
|
|
use rustc_errors::{struct_span_err, Applicability, Diagnostic, MultiSpan};
|
2020-01-04 19:37:57 -06:00
|
|
|
use rustc_hir as hir;
|
2021-12-07 04:28:12 -06:00
|
|
|
use rustc_hir::{intravisit, HirId};
|
2021-11-03 18:03:12 -05:00
|
|
|
use rustc_middle::hir::nested_filter;
|
2020-12-21 16:40:50 -06:00
|
|
|
use rustc_middle::lint::{
|
2021-08-06 16:28:58 -05:00
|
|
|
struct_lint_level, LevelAndSource, LintDiagnosticBuilder, LintExpectation, LintLevelMap,
|
|
|
|
LintLevelSets, LintLevelSource, LintSet, LintStackIndex, COMMAND_LINE,
|
2020-12-21 16:40:50 -06:00
|
|
|
};
|
2020-03-29 10:19:48 -05:00
|
|
|
use rustc_middle::ty::query::Providers;
|
2021-09-28 17:17:54 -05:00
|
|
|
use rustc_middle::ty::{RegisteredTools, TyCtxt};
|
2021-01-29 18:06:00 -06:00
|
|
|
use rustc_session::lint::{
|
2022-05-10 14:15:30 -05:00
|
|
|
builtin::{self, FORBIDDEN_LINT_GROUPS, SINGLE_USE_LIFETIMES, UNFULFILLED_LINT_EXPECTATIONS},
|
2021-08-06 16:28:58 -05:00
|
|
|
Level, Lint, LintExpectationId, LintId,
|
2021-01-29 18:06:00 -06:00
|
|
|
};
|
2022-02-28 14:29:06 -06:00
|
|
|
use rustc_session::parse::{add_feature_diagnostics, feature_err};
|
2020-01-09 00:52:01 -06:00
|
|
|
use rustc_session::Session;
|
2020-01-11 08:03:15 -06:00
|
|
|
use rustc_span::symbol::{sym, Symbol};
|
2022-03-23 21:03:04 -05:00
|
|
|
use rustc_span::{Span, DUMMY_SP};
|
2021-01-29 18:06:00 -06:00
|
|
|
use tracing::debug;
|
2019-12-30 12:02:52 -06:00
|
|
|
|
2021-05-11 05:22:11 -05:00
|
|
|
fn lint_levels(tcx: TyCtxt<'_>, (): ()) -> LintLevelMap {
|
2020-01-08 20:45:42 -06:00
|
|
|
let store = unerased_lint_store(tcx);
|
2021-09-28 17:17:54 -05:00
|
|
|
let levels =
|
|
|
|
LintLevelsBuilder::new(tcx.sess, false, &store, &tcx.resolutions(()).registered_tools);
|
2021-09-27 16:28:49 -05:00
|
|
|
let mut builder = LintLevelMapBuilder { levels, tcx };
|
2019-12-30 12:02:52 -06:00
|
|
|
let krate = tcx.hir().krate();
|
|
|
|
|
2021-02-05 08:47:44 -06:00
|
|
|
builder.levels.id_to_set.reserve(krate.owners.len() + 1);
|
2020-10-16 04:43:39 -05:00
|
|
|
|
2021-11-20 13:45:27 -06:00
|
|
|
let push =
|
|
|
|
builder.levels.push(tcx.hir().attrs(hir::CRATE_HIR_ID), true, Some(hir::CRATE_HIR_ID));
|
|
|
|
|
2019-12-30 12:02:52 -06:00
|
|
|
builder.levels.register_id(hir::CRATE_HIR_ID);
|
2021-09-02 12:22:24 -05:00
|
|
|
tcx.hir().walk_toplevel_module(&mut builder);
|
2019-12-30 12:02:52 -06:00
|
|
|
builder.levels.pop(push);
|
|
|
|
|
2021-11-20 13:45:27 -06:00
|
|
|
builder.levels.update_unstable_expectation_ids();
|
2020-03-27 14:26:20 -05:00
|
|
|
builder.levels.build_map()
|
2019-12-30 12:02:52 -06:00
|
|
|
}
|
|
|
|
|
2020-01-31 06:24:57 -06:00
|
|
|
pub struct LintLevelsBuilder<'s> {
|
|
|
|
sess: &'s Session,
|
2022-03-02 11:10:07 -06:00
|
|
|
lint_expectations: Vec<(LintExpectationId, LintExpectation)>,
|
2021-11-20 13:45:27 -06:00
|
|
|
/// Each expectation has a stable and an unstable identifier. This map
|
|
|
|
/// is used to map from unstable to stable [`LintExpectationId`]s.
|
|
|
|
expectation_id_map: FxHashMap<LintExpectationId, LintExpectationId>,
|
2020-01-09 00:52:01 -06:00
|
|
|
sets: LintLevelSets,
|
2020-12-01 14:47:41 -06:00
|
|
|
id_to_set: FxHashMap<HirId, LintStackIndex>,
|
|
|
|
cur: LintStackIndex,
|
2020-01-09 00:52:01 -06:00
|
|
|
warn_about_weird_lints: bool,
|
2021-01-29 18:06:00 -06:00
|
|
|
store: &'s LintStore,
|
2021-09-28 17:17:54 -05:00
|
|
|
registered_tools: &'s RegisteredTools,
|
2020-01-09 00:52:01 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct BuilderPush {
|
2020-12-01 14:47:41 -06:00
|
|
|
prev: LintStackIndex,
|
2020-01-09 00:52:01 -06:00
|
|
|
pub changed: bool,
|
|
|
|
}
|
|
|
|
|
2020-01-31 06:24:57 -06:00
|
|
|
impl<'s> LintLevelsBuilder<'s> {
|
2021-03-16 16:33:03 -05:00
|
|
|
pub fn new(
|
|
|
|
sess: &'s Session,
|
|
|
|
warn_about_weird_lints: bool,
|
|
|
|
store: &'s LintStore,
|
2021-09-28 17:17:54 -05:00
|
|
|
registered_tools: &'s RegisteredTools,
|
2021-03-16 16:33:03 -05:00
|
|
|
) -> Self {
|
2020-01-09 00:52:01 -06:00
|
|
|
let mut builder = LintLevelsBuilder {
|
|
|
|
sess,
|
2021-08-06 16:28:58 -05:00
|
|
|
lint_expectations: Default::default(),
|
2021-11-20 13:45:27 -06:00
|
|
|
expectation_id_map: Default::default(),
|
2020-01-09 00:52:01 -06:00
|
|
|
sets: LintLevelSets::new(),
|
2020-12-01 14:47:41 -06:00
|
|
|
cur: COMMAND_LINE,
|
2020-01-09 00:52:01 -06:00
|
|
|
id_to_set: Default::default(),
|
|
|
|
warn_about_weird_lints,
|
2021-01-29 18:06:00 -06:00
|
|
|
store,
|
2021-09-28 17:17:54 -05:00
|
|
|
registered_tools,
|
2020-01-09 00:52:01 -06:00
|
|
|
};
|
|
|
|
builder.process_command_line(sess, store);
|
|
|
|
assert_eq!(builder.sets.list.len(), 1);
|
|
|
|
builder
|
|
|
|
}
|
|
|
|
|
2021-09-27 16:28:49 -05:00
|
|
|
pub(crate) fn sess(&self) -> &Session {
|
|
|
|
self.sess
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn lint_store(&self) -> &LintStore {
|
|
|
|
self.store
|
|
|
|
}
|
|
|
|
|
2022-02-22 22:53:17 -06:00
|
|
|
fn current_specs(&self) -> &FxHashMap<LintId, LevelAndSource> {
|
|
|
|
&self.sets.list[self.cur].specs
|
|
|
|
}
|
|
|
|
|
|
|
|
fn current_specs_mut(&mut self) -> &mut FxHashMap<LintId, LevelAndSource> {
|
|
|
|
&mut self.sets.list[self.cur].specs
|
|
|
|
}
|
|
|
|
|
2020-01-09 00:52:01 -06:00
|
|
|
fn process_command_line(&mut self, sess: &Session, store: &LintStore) {
|
|
|
|
self.sets.lint_cap = sess.opts.lint_cap.unwrap_or(Level::Forbid);
|
|
|
|
|
2022-02-28 14:29:06 -06:00
|
|
|
self.cur =
|
|
|
|
self.sets.list.push(LintSet { specs: FxHashMap::default(), parent: COMMAND_LINE });
|
2020-01-09 00:52:01 -06:00
|
|
|
for &(ref lint_name, level) in &sess.opts.lint_opts {
|
2021-09-28 17:17:54 -05:00
|
|
|
store.check_lint_name_cmdline(sess, &lint_name, level, self.registered_tools);
|
2020-11-02 00:37:26 -06:00
|
|
|
let orig_level = level;
|
2020-01-09 00:52:01 -06:00
|
|
|
let lint_flag_val = Symbol::intern(lint_name);
|
2020-05-13 16:43:21 -05:00
|
|
|
|
2022-02-18 17:48:49 -06:00
|
|
|
let Ok(ids) = store.find_lints(&lint_name) else {
|
|
|
|
// errors handled in check_lint_name_cmdline above
|
|
|
|
continue
|
2020-01-09 00:52:01 -06:00
|
|
|
};
|
|
|
|
for id in ids {
|
2022-03-14 20:00:08 -05:00
|
|
|
// ForceWarn and Forbid cannot be overridden
|
2022-02-22 22:53:17 -06:00
|
|
|
if let Some((Level::ForceWarn | Level::Forbid, _)) = self.current_specs().get(&id) {
|
2021-07-08 02:05:38 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-02-22 22:53:17 -06:00
|
|
|
if self.check_gated_lint(id, DUMMY_SP) {
|
|
|
|
let src = LintLevelSource::CommandLine(lint_flag_val, orig_level);
|
|
|
|
self.current_specs_mut().insert(id, (level, src));
|
|
|
|
}
|
2020-01-09 00:52:01 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-15 13:17:35 -05:00
|
|
|
/// Attempts to insert the `id` to `level_src` map entry. If unsuccessful
|
|
|
|
/// (e.g. if a forbid was already inserted on the same scope), then emits a
|
|
|
|
/// diagnostic with no change to `specs`.
|
2022-02-28 14:29:06 -06:00
|
|
|
fn insert_spec(&mut self, id: LintId, (level, src): LevelAndSource) {
|
2021-06-04 16:21:43 -05:00
|
|
|
let (old_level, old_src) =
|
2022-02-22 22:53:17 -06:00
|
|
|
self.sets.get_lint_level(id.lint, self.cur, Some(self.current_specs()), &self.sess);
|
2020-11-07 17:14:38 -06:00
|
|
|
// Setting to a non-forbid level is an error if the lint previously had
|
|
|
|
// a forbid level. Note that this is not necessarily true even with a
|
2022-03-14 20:00:08 -05:00
|
|
|
// `#[forbid(..)]` attribute present, as that is overridden by `--cap-lints`.
|
2020-11-07 17:14:38 -06:00
|
|
|
//
|
|
|
|
// This means that this only errors if we're truly lowering the lint
|
|
|
|
// level from forbid.
|
|
|
|
if level != Level::Forbid {
|
2021-06-04 16:21:43 -05:00
|
|
|
if let Level::Forbid = old_level {
|
2021-01-29 18:06:00 -06:00
|
|
|
// Backwards compatibility check:
|
|
|
|
//
|
|
|
|
// We used to not consider `forbid(lint_group)`
|
|
|
|
// as preventing `allow(lint)` for some lint `lint` in
|
|
|
|
// `lint_group`. For now, issue a future-compatibility
|
|
|
|
// warning for this case.
|
|
|
|
let id_name = id.lint.name_lower();
|
|
|
|
let fcw_warning = match old_src {
|
|
|
|
LintLevelSource::Default => false,
|
|
|
|
LintLevelSource::Node(symbol, _, _) => self.store.is_lint_group(symbol),
|
|
|
|
LintLevelSource::CommandLine(symbol, _) => self.store.is_lint_group(symbol),
|
|
|
|
};
|
|
|
|
debug!(
|
|
|
|
"fcw_warning={:?}, specs.get(&id) = {:?}, old_src={:?}, id_name={:?}",
|
2022-02-28 14:29:06 -06:00
|
|
|
fcw_warning,
|
|
|
|
self.current_specs(),
|
|
|
|
old_src,
|
|
|
|
id_name
|
2020-06-15 13:17:35 -05:00
|
|
|
);
|
2021-01-29 18:06:00 -06:00
|
|
|
|
2022-01-27 03:44:25 -06:00
|
|
|
let decorate_diag = |diag: &mut Diagnostic| {
|
|
|
|
diag.span_label(src.span(), "overruled by previous forbid");
|
2021-01-29 18:06:00 -06:00
|
|
|
match old_src {
|
|
|
|
LintLevelSource::Default => {
|
2022-01-27 03:44:25 -06:00
|
|
|
diag.note(&format!(
|
2021-01-29 18:06:00 -06:00
|
|
|
"`forbid` lint level is the default for {}",
|
|
|
|
id.to_string()
|
|
|
|
));
|
|
|
|
}
|
|
|
|
LintLevelSource::Node(_, forbid_source_span, reason) => {
|
2022-01-27 03:44:25 -06:00
|
|
|
diag.span_label(forbid_source_span, "`forbid` level set here");
|
2021-01-29 18:06:00 -06:00
|
|
|
if let Some(rationale) = reason {
|
2022-01-27 03:44:25 -06:00
|
|
|
diag.note(rationale.as_str());
|
2021-01-29 18:06:00 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
LintLevelSource::CommandLine(_, _) => {
|
2022-01-27 03:44:25 -06:00
|
|
|
diag.note("`forbid` lint level was set on command line");
|
2020-06-15 13:17:35 -05:00
|
|
|
}
|
|
|
|
}
|
2021-01-29 18:06:00 -06:00
|
|
|
};
|
|
|
|
if !fcw_warning {
|
2022-01-27 03:44:25 -06:00
|
|
|
let mut diag_builder = struct_span_err!(
|
2021-01-29 18:06:00 -06:00
|
|
|
self.sess,
|
|
|
|
src.span(),
|
|
|
|
E0453,
|
|
|
|
"{}({}) incompatible with previous forbid",
|
|
|
|
level.as_str(),
|
|
|
|
src.name(),
|
|
|
|
);
|
2022-01-27 03:44:25 -06:00
|
|
|
decorate_diag(&mut diag_builder);
|
|
|
|
diag_builder.emit();
|
2021-01-29 18:06:00 -06:00
|
|
|
} else {
|
|
|
|
self.struct_lint(
|
|
|
|
FORBIDDEN_LINT_GROUPS,
|
|
|
|
Some(src.span().into()),
|
|
|
|
|diag_builder| {
|
2022-01-27 03:44:25 -06:00
|
|
|
let mut diag_builder = diag_builder.build(&format!(
|
2021-01-29 18:06:00 -06:00
|
|
|
"{}({}) incompatible with previous forbid",
|
|
|
|
level.as_str(),
|
|
|
|
src.name(),
|
|
|
|
));
|
2022-01-27 03:44:25 -06:00
|
|
|
decorate_diag(&mut diag_builder);
|
|
|
|
diag_builder.emit();
|
2021-01-29 18:06:00 -06:00
|
|
|
},
|
|
|
|
);
|
2020-06-15 13:17:35 -05:00
|
|
|
}
|
2020-11-07 17:14:38 -06:00
|
|
|
|
2021-01-29 18:06:00 -06:00
|
|
|
// Retain the forbid lint level, unless we are
|
|
|
|
// issuing a FCW. In the FCW case, we want to
|
|
|
|
// respect the new setting.
|
|
|
|
if !fcw_warning {
|
|
|
|
return;
|
|
|
|
}
|
2020-06-15 13:17:35 -05:00
|
|
|
}
|
|
|
|
}
|
2022-03-06 07:18:28 -06:00
|
|
|
|
|
|
|
// The lint `unfulfilled_lint_expectations` can't be expected, as it would suppress itself.
|
|
|
|
// Handling expectations of this lint would add additional complexity with little to no
|
|
|
|
// benefit. The expect level for this lint will therefore be ignored.
|
|
|
|
if let Level::Expect(_) = level && id == LintId::of(UNFULFILLED_LINT_EXPECTATIONS) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-04 16:21:43 -05:00
|
|
|
if let Level::ForceWarn = old_level {
|
2022-02-22 22:53:17 -06:00
|
|
|
self.current_specs_mut().insert(id, (old_level, old_src));
|
2021-06-04 16:21:43 -05:00
|
|
|
} else {
|
2022-02-22 22:53:17 -06:00
|
|
|
self.current_specs_mut().insert(id, (level, src));
|
2021-06-04 16:21:43 -05:00
|
|
|
}
|
2020-06-15 13:17:35 -05:00
|
|
|
}
|
|
|
|
|
2020-01-09 00:52:01 -06:00
|
|
|
/// Pushes a list of AST lint attributes onto this context.
|
|
|
|
///
|
|
|
|
/// This function will return a `BuilderPush` object which should be passed
|
|
|
|
/// to `pop` when this scope for the attributes provided is exited.
|
|
|
|
///
|
|
|
|
/// This function will perform a number of tasks:
|
|
|
|
///
|
|
|
|
/// * It'll validate all lint-related attributes in `attrs`
|
|
|
|
/// * It'll mark all lint-related attributes as used
|
|
|
|
/// * Lint levels will be updated based on the attributes provided
|
2020-05-01 15:28:15 -05:00
|
|
|
/// * Lint attributes are validated, e.g., a `#[forbid]` can't be switched to
|
|
|
|
/// `#[allow]`
|
2020-01-09 00:52:01 -06:00
|
|
|
///
|
|
|
|
/// Don't forget to call `pop`!
|
2021-11-20 13:45:27 -06:00
|
|
|
pub(crate) fn push(
|
|
|
|
&mut self,
|
|
|
|
attrs: &[ast::Attribute],
|
|
|
|
is_crate_node: bool,
|
|
|
|
source_hir_id: Option<HirId>,
|
|
|
|
) -> BuilderPush {
|
2022-02-22 22:53:17 -06:00
|
|
|
let prev = self.cur;
|
2022-02-28 14:29:06 -06:00
|
|
|
self.cur = self.sets.list.push(LintSet { specs: FxHashMap::default(), parent: prev });
|
|
|
|
|
2020-01-09 00:52:01 -06:00
|
|
|
let sess = self.sess;
|
|
|
|
let bad_attr = |span| struct_span_err!(sess, span, E0452, "malformed lint attribute input");
|
2021-11-20 13:45:27 -06:00
|
|
|
for (attr_index, attr) in attrs.iter().enumerate() {
|
2022-05-10 14:15:30 -05:00
|
|
|
if attr.has_name(sym::automatically_derived) {
|
|
|
|
self.current_specs_mut().insert(
|
|
|
|
LintId::of(SINGLE_USE_LIFETIMES),
|
|
|
|
(Level::Allow, LintLevelSource::Default),
|
|
|
|
);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-11-24 14:57:38 -06:00
|
|
|
let level = match Level::from_attr(attr) {
|
2021-11-20 13:45:27 -06:00
|
|
|
None => continue,
|
2021-11-25 10:45:11 -06:00
|
|
|
Some(Level::Expect(unstable_id)) if let Some(hir_id) = source_hir_id => {
|
|
|
|
let stable_id = self.create_stable_id(unstable_id, hir_id, attr_index);
|
|
|
|
|
2021-11-20 13:45:27 -06:00
|
|
|
Level::Expect(stable_id)
|
|
|
|
}
|
|
|
|
Some(lvl) => lvl,
|
2020-01-09 00:52:01 -06:00
|
|
|
};
|
|
|
|
|
2021-12-02 20:25:11 -06:00
|
|
|
let Some(mut metas) = attr.meta_item_list() else {
|
|
|
|
continue
|
2021-10-08 11:32:16 -05:00
|
|
|
};
|
2020-01-09 00:52:01 -06:00
|
|
|
|
|
|
|
if metas.is_empty() {
|
2022-03-03 15:28:56 -06:00
|
|
|
// This emits the unused_attributes lint for `#[level()]`
|
2020-01-09 00:52:01 -06:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Before processing the lint names, look for a reason (RFC 2383)
|
|
|
|
// at the end.
|
|
|
|
let mut reason = None;
|
|
|
|
let tail_li = &metas[metas.len() - 1];
|
|
|
|
if let Some(item) = tail_li.meta_item() {
|
|
|
|
match item.kind {
|
|
|
|
ast::MetaItemKind::Word => {} // actual lint names handled later
|
|
|
|
ast::MetaItemKind::NameValue(ref name_value) => {
|
|
|
|
if item.path == sym::reason {
|
|
|
|
if let ast::LitKind::Str(rationale, _) = name_value.kind {
|
|
|
|
if !self.sess.features_untracked().lint_reasons {
|
|
|
|
feature_err(
|
|
|
|
&self.sess.parse_sess,
|
|
|
|
sym::lint_reasons,
|
|
|
|
item.span,
|
|
|
|
"lint reasons are experimental",
|
|
|
|
)
|
|
|
|
.emit();
|
|
|
|
}
|
|
|
|
reason = Some(rationale);
|
|
|
|
} else {
|
|
|
|
bad_attr(name_value.span)
|
|
|
|
.span_label(name_value.span, "reason must be a string literal")
|
|
|
|
.emit();
|
|
|
|
}
|
2021-04-10 12:08:31 -05:00
|
|
|
// found reason, reslice meta list to exclude it
|
|
|
|
metas.pop().unwrap();
|
2020-01-09 00:52:01 -06:00
|
|
|
} else {
|
|
|
|
bad_attr(item.span)
|
|
|
|
.span_label(item.span, "bad attribute argument")
|
|
|
|
.emit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ast::MetaItemKind::List(_) => {
|
|
|
|
bad_attr(item.span).span_label(item.span, "bad attribute argument").emit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-24 14:57:38 -06:00
|
|
|
for (lint_index, li) in metas.iter_mut().enumerate() {
|
|
|
|
let level = match level {
|
|
|
|
Level::Expect(mut id) => {
|
2021-11-24 16:21:10 -06:00
|
|
|
id.set_lint_index(Some(lint_index as u16));
|
2021-11-24 14:57:38 -06:00
|
|
|
Level::Expect(id)
|
|
|
|
}
|
|
|
|
level => level,
|
|
|
|
};
|
|
|
|
|
2021-04-10 12:08:31 -05:00
|
|
|
let sp = li.span();
|
2021-11-24 14:57:38 -06:00
|
|
|
let meta_item = match li {
|
2021-04-10 12:08:31 -05:00
|
|
|
ast::NestedMetaItem::MetaItem(meta_item) if meta_item.is_word() => meta_item,
|
2020-01-09 00:52:01 -06:00
|
|
|
_ => {
|
|
|
|
let mut err = bad_attr(sp);
|
|
|
|
let mut add_label = true;
|
|
|
|
if let Some(item) = li.meta_item() {
|
|
|
|
if let ast::MetaItemKind::NameValue(_) = item.kind {
|
|
|
|
if item.path == sym::reason {
|
|
|
|
err.span_label(sp, "reason in lint attribute must come last");
|
|
|
|
add_label = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if add_label {
|
|
|
|
err.span_label(sp, "bad attribute argument");
|
|
|
|
}
|
|
|
|
err.emit();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
};
|
2021-07-06 19:19:20 -05:00
|
|
|
let tool_ident = if meta_item.path.segments.len() > 1 {
|
|
|
|
Some(meta_item.path.segments.remove(0).ident)
|
2020-01-09 00:52:01 -06:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2021-07-06 19:19:20 -05:00
|
|
|
let tool_name = tool_ident.map(|ident| ident.name);
|
2021-04-10 12:08:31 -05:00
|
|
|
let name = pprust::path_to_string(&meta_item.path);
|
2021-09-27 16:28:49 -05:00
|
|
|
let lint_result =
|
2021-09-28 17:17:54 -05:00
|
|
|
self.store.check_lint_name(&name, tool_name, self.registered_tools);
|
2021-02-27 23:12:54 -06:00
|
|
|
match &lint_result {
|
2020-01-09 00:52:01 -06:00
|
|
|
CheckLintNameResult::Ok(ids) => {
|
2022-03-06 07:18:28 -06:00
|
|
|
// This checks for instances where the user writes `#[expect(unfulfilled_lint_expectations)]`
|
|
|
|
// in that case we want to avoid overriding the lint level but instead add an expectation that
|
|
|
|
// can't be fulfilled. The lint message will include an explanation, that the
|
|
|
|
// `unfulfilled_lint_expectations` lint can't be expected.
|
|
|
|
if let Level::Expect(expect_id) = level {
|
2022-03-09 14:58:13 -06:00
|
|
|
// The `unfulfilled_lint_expectations` lint is not part of any lint groups. Therefore. we
|
|
|
|
// only need to check the slice if it contains a single lint.
|
2022-03-06 07:18:28 -06:00
|
|
|
let is_unfulfilled_lint_expectations = match ids {
|
|
|
|
[lint] => *lint == LintId::of(UNFULFILLED_LINT_EXPECTATIONS),
|
|
|
|
_ => false,
|
|
|
|
};
|
|
|
|
self.lint_expectations.push((
|
|
|
|
expect_id,
|
2022-03-28 17:10:45 -05:00
|
|
|
LintExpectation::new(
|
|
|
|
reason,
|
|
|
|
sp,
|
|
|
|
is_unfulfilled_lint_expectations,
|
|
|
|
tool_name,
|
|
|
|
),
|
2022-03-06 07:18:28 -06:00
|
|
|
));
|
|
|
|
}
|
2021-04-10 12:08:31 -05:00
|
|
|
let src = LintLevelSource::Node(
|
|
|
|
meta_item.path.segments.last().expect("empty lint name").ident.name,
|
|
|
|
sp,
|
|
|
|
reason,
|
|
|
|
);
|
2021-02-27 23:12:54 -06:00
|
|
|
for &id in *ids {
|
2022-02-22 22:53:17 -06:00
|
|
|
if self.check_gated_lint(id, attr.span) {
|
|
|
|
self.insert_spec(id, (level, src));
|
|
|
|
}
|
2020-01-09 00:52:01 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CheckLintNameResult::Tool(result) => {
|
2021-02-27 23:12:54 -06:00
|
|
|
match *result {
|
2020-01-09 00:52:01 -06:00
|
|
|
Ok(ids) => {
|
2021-07-06 19:19:20 -05:00
|
|
|
let complete_name =
|
|
|
|
&format!("{}::{}", tool_ident.unwrap().name, name);
|
2020-12-21 16:17:53 -06:00
|
|
|
let src = LintLevelSource::Node(
|
2020-01-09 00:52:01 -06:00
|
|
|
Symbol::intern(complete_name),
|
2021-04-10 12:08:31 -05:00
|
|
|
sp,
|
2020-01-09 00:52:01 -06:00
|
|
|
reason,
|
|
|
|
);
|
|
|
|
for id in ids {
|
2022-02-22 22:53:17 -06:00
|
|
|
self.insert_spec(*id, (level, src));
|
2020-01-09 00:52:01 -06:00
|
|
|
}
|
2021-11-24 14:57:38 -06:00
|
|
|
if let Level::Expect(expect_id) = level {
|
2022-03-28 17:10:45 -05:00
|
|
|
self.lint_expectations.push((
|
|
|
|
expect_id,
|
|
|
|
LintExpectation::new(reason, sp, false, tool_name),
|
|
|
|
));
|
2021-11-24 14:57:38 -06:00
|
|
|
}
|
2020-01-09 00:52:01 -06:00
|
|
|
}
|
2021-02-27 23:12:54 -06:00
|
|
|
Err((Some(ids), ref new_lint_name)) => {
|
2020-01-09 00:52:01 -06:00
|
|
|
let lint = builtin::RENAMED_AND_REMOVED_LINTS;
|
2022-02-28 14:29:06 -06:00
|
|
|
let (lvl, src) = self.sets.get_lint_level(
|
|
|
|
lint,
|
|
|
|
self.cur,
|
|
|
|
Some(self.current_specs()),
|
|
|
|
&sess,
|
|
|
|
);
|
2020-01-09 00:52:01 -06:00
|
|
|
struct_lint_level(
|
|
|
|
self.sess,
|
|
|
|
lint,
|
|
|
|
lvl,
|
|
|
|
src,
|
2021-04-10 12:08:31 -05:00
|
|
|
Some(sp.into()),
|
2020-01-31 06:24:57 -06:00
|
|
|
|lint| {
|
2020-02-05 09:27:46 -06:00
|
|
|
let msg = format!(
|
|
|
|
"lint name `{}` is deprecated \
|
2021-01-17 13:23:25 -06:00
|
|
|
and may not have an effect in the future.",
|
2020-02-05 09:27:46 -06:00
|
|
|
name
|
|
|
|
);
|
2020-01-31 06:24:57 -06:00
|
|
|
lint.build(&msg)
|
2020-02-01 17:47:58 -06:00
|
|
|
.span_suggestion(
|
2021-04-10 12:08:31 -05:00
|
|
|
sp,
|
2020-02-01 17:47:58 -06:00
|
|
|
"change it to",
|
|
|
|
new_lint_name.to_string(),
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
)
|
|
|
|
.emit();
|
2020-01-31 06:24:57 -06:00
|
|
|
},
|
|
|
|
);
|
2020-01-09 00:52:01 -06:00
|
|
|
|
2020-12-21 16:17:53 -06:00
|
|
|
let src = LintLevelSource::Node(
|
2020-01-09 00:52:01 -06:00
|
|
|
Symbol::intern(&new_lint_name),
|
2021-04-10 12:08:31 -05:00
|
|
|
sp,
|
2020-01-09 00:52:01 -06:00
|
|
|
reason,
|
|
|
|
);
|
|
|
|
for id in ids {
|
2022-02-22 22:53:17 -06:00
|
|
|
self.insert_spec(*id, (level, src));
|
2020-01-09 00:52:01 -06:00
|
|
|
}
|
2021-11-24 14:57:38 -06:00
|
|
|
if let Level::Expect(expect_id) = level {
|
2022-03-28 17:10:45 -05:00
|
|
|
self.lint_expectations.push((
|
|
|
|
expect_id,
|
|
|
|
LintExpectation::new(reason, sp, false, tool_name),
|
|
|
|
));
|
2021-11-24 14:57:38 -06:00
|
|
|
}
|
2020-01-09 00:52:01 -06:00
|
|
|
}
|
|
|
|
Err((None, _)) => {
|
|
|
|
// If Tool(Err(None, _)) is returned, then either the lint does not
|
|
|
|
// exist in the tool or the code was not compiled with the tool and
|
|
|
|
// therefore the lint was never added to the `LintStore`. To detect
|
|
|
|
// this is the responsibility of the lint tool.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-06 19:19:20 -05:00
|
|
|
&CheckLintNameResult::NoTool => {
|
|
|
|
let mut err = struct_span_err!(
|
|
|
|
sess,
|
|
|
|
tool_ident.map_or(DUMMY_SP, |ident| ident.span),
|
|
|
|
E0710,
|
|
|
|
"unknown tool name `{}` found in scoped lint: `{}::{}`",
|
|
|
|
tool_name.unwrap(),
|
|
|
|
tool_name.unwrap(),
|
|
|
|
pprust::path_to_string(&meta_item.path),
|
|
|
|
);
|
|
|
|
if sess.is_nightly_build() {
|
|
|
|
err.help(&format!(
|
|
|
|
"add `#![register_tool({})]` to the crate root",
|
|
|
|
tool_name.unwrap()
|
|
|
|
));
|
|
|
|
}
|
|
|
|
err.emit();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-01-09 00:52:01 -06:00
|
|
|
_ if !self.warn_about_weird_lints => {}
|
|
|
|
|
|
|
|
CheckLintNameResult::Warning(msg, renamed) => {
|
|
|
|
let lint = builtin::RENAMED_AND_REMOVED_LINTS;
|
2022-02-28 14:29:06 -06:00
|
|
|
let (renamed_lint_level, src) = self.sets.get_lint_level(
|
|
|
|
lint,
|
|
|
|
self.cur,
|
|
|
|
Some(self.current_specs()),
|
|
|
|
&sess,
|
|
|
|
);
|
2020-01-31 06:24:57 -06:00
|
|
|
struct_lint_level(
|
2020-01-09 00:52:01 -06:00
|
|
|
self.sess,
|
|
|
|
lint,
|
2021-02-27 23:12:54 -06:00
|
|
|
renamed_lint_level,
|
2020-01-09 00:52:01 -06:00
|
|
|
src,
|
2021-04-10 12:08:31 -05:00
|
|
|
Some(sp.into()),
|
2020-01-31 06:24:57 -06:00
|
|
|
|lint| {
|
|
|
|
let mut err = lint.build(&msg);
|
2021-02-27 23:12:54 -06:00
|
|
|
if let Some(new_name) = &renamed {
|
2020-01-31 06:24:57 -06:00
|
|
|
err.span_suggestion(
|
2021-04-10 12:08:31 -05:00
|
|
|
sp,
|
2020-01-31 06:24:57 -06:00
|
|
|
"use the new name",
|
2021-02-27 23:12:54 -06:00
|
|
|
new_name.to_string(),
|
2020-01-31 06:24:57 -06:00
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
err.emit();
|
|
|
|
},
|
2020-01-09 00:52:01 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
CheckLintNameResult::NoLint(suggestion) => {
|
|
|
|
let lint = builtin::UNKNOWN_LINTS;
|
2022-02-28 14:29:06 -06:00
|
|
|
let (level, src) = self.sets.get_lint_level(
|
|
|
|
lint,
|
|
|
|
self.cur,
|
|
|
|
Some(self.current_specs()),
|
|
|
|
self.sess,
|
|
|
|
);
|
2021-04-10 12:08:31 -05:00
|
|
|
struct_lint_level(self.sess, lint, level, src, Some(sp.into()), |lint| {
|
2021-07-06 19:19:20 -05:00
|
|
|
let name = if let Some(tool_ident) = tool_ident {
|
|
|
|
format!("{}::{}", tool_ident.name, name)
|
2021-04-10 12:08:31 -05:00
|
|
|
} else {
|
|
|
|
name.to_string()
|
|
|
|
};
|
|
|
|
let mut db = lint.build(&format!("unknown lint: `{}`", name));
|
|
|
|
if let Some(suggestion) = suggestion {
|
|
|
|
db.span_suggestion(
|
|
|
|
sp,
|
|
|
|
"did you mean",
|
|
|
|
suggestion.to_string(),
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
db.emit();
|
|
|
|
});
|
2020-01-09 00:52:01 -06:00
|
|
|
}
|
|
|
|
}
|
2021-02-27 23:12:54 -06:00
|
|
|
// If this lint was renamed, apply the new lint instead of ignoring the attribute.
|
|
|
|
// This happens outside of the match because the new lint should be applied even if
|
|
|
|
// we don't warn about the name change.
|
|
|
|
if let CheckLintNameResult::Warning(_, Some(new_name)) = lint_result {
|
|
|
|
// Ignore any errors or warnings that happen because the new name is inaccurate
|
2021-03-27 23:25:07 -05:00
|
|
|
// NOTE: `new_name` already includes the tool name, so we don't have to add it again.
|
2021-07-07 12:50:50 -05:00
|
|
|
if let CheckLintNameResult::Ok(ids) =
|
2021-09-28 17:17:54 -05:00
|
|
|
self.store.check_lint_name(&new_name, None, self.registered_tools)
|
2021-07-07 12:50:50 -05:00
|
|
|
{
|
2021-04-10 12:08:31 -05:00
|
|
|
let src = LintLevelSource::Node(Symbol::intern(&new_name), sp, reason);
|
2021-02-27 23:12:54 -06:00
|
|
|
for &id in ids {
|
2022-02-22 22:53:17 -06:00
|
|
|
if self.check_gated_lint(id, attr.span) {
|
|
|
|
self.insert_spec(id, (level, src));
|
|
|
|
}
|
2021-02-27 23:12:54 -06:00
|
|
|
}
|
2021-11-24 14:57:38 -06:00
|
|
|
if let Level::Expect(expect_id) = level {
|
2022-03-28 17:10:45 -05:00
|
|
|
self.lint_expectations.push((
|
|
|
|
expect_id,
|
|
|
|
LintExpectation::new(reason, sp, false, tool_name),
|
|
|
|
));
|
2021-11-24 14:57:38 -06:00
|
|
|
}
|
2021-03-27 23:25:07 -05:00
|
|
|
} else {
|
|
|
|
panic!("renamed lint does not exist: {}", new_name);
|
2021-02-27 23:12:54 -06:00
|
|
|
}
|
|
|
|
}
|
2020-01-09 00:52:01 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-12 20:58:24 -05:00
|
|
|
if !is_crate_node {
|
2022-02-22 22:53:17 -06:00
|
|
|
for (id, &(level, ref src)) in self.current_specs().iter() {
|
2020-06-12 20:58:24 -05:00
|
|
|
if !id.lint.crate_level_only {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-12-02 20:25:11 -06:00
|
|
|
let LintLevelSource::Node(lint_attr_name, lint_attr_span, _) = *src else {
|
|
|
|
continue
|
2020-06-12 20:58:24 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
let lint = builtin::UNUSED_ATTRIBUTES;
|
|
|
|
let (lint_level, lint_src) =
|
2022-02-22 22:53:17 -06:00
|
|
|
self.sets.get_lint_level(lint, self.cur, Some(self.current_specs()), self.sess);
|
2020-06-12 20:58:24 -05:00
|
|
|
struct_lint_level(
|
|
|
|
self.sess,
|
|
|
|
lint,
|
|
|
|
lint_level,
|
|
|
|
lint_src,
|
|
|
|
Some(lint_attr_span.into()),
|
|
|
|
|lint| {
|
|
|
|
let mut db = lint.build(&format!(
|
|
|
|
"{}({}) is ignored unless specified at crate level",
|
|
|
|
level.as_str(),
|
|
|
|
lint_attr_name
|
|
|
|
));
|
|
|
|
db.emit();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
// don't set a separate error for every lint in the group
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-22 22:53:17 -06:00
|
|
|
if self.current_specs().is_empty() {
|
|
|
|
self.sets.list.pop();
|
|
|
|
self.cur = prev;
|
2020-01-09 00:52:01 -06:00
|
|
|
}
|
|
|
|
|
2020-03-06 12:28:44 -06:00
|
|
|
BuilderPush { prev, changed: prev != self.cur }
|
2020-01-09 00:52:01 -06:00
|
|
|
}
|
|
|
|
|
2021-11-20 13:45:27 -06:00
|
|
|
fn create_stable_id(
|
|
|
|
&mut self,
|
|
|
|
unstable_id: LintExpectationId,
|
|
|
|
hir_id: HirId,
|
|
|
|
attr_index: usize,
|
|
|
|
) -> LintExpectationId {
|
2021-11-24 16:21:10 -06:00
|
|
|
let stable_id =
|
|
|
|
LintExpectationId::Stable { hir_id, attr_index: attr_index as u16, lint_index: None };
|
2021-11-20 13:45:27 -06:00
|
|
|
|
|
|
|
self.expectation_id_map.insert(unstable_id, stable_id);
|
|
|
|
|
|
|
|
stable_id
|
|
|
|
}
|
|
|
|
|
2020-06-03 21:16:29 -05:00
|
|
|
/// Checks if the lint is gated on a feature that is not enabled.
|
2022-02-22 22:53:17 -06:00
|
|
|
///
|
|
|
|
/// Returns `true` if the lint's feature is enabled.
|
|
|
|
fn check_gated_lint(&self, lint_id: LintId, span: Span) -> bool {
|
2020-06-03 21:16:29 -05:00
|
|
|
if let Some(feature) = lint_id.lint.feature_gate {
|
|
|
|
if !self.sess.features_untracked().enabled(feature) {
|
2022-03-01 18:10:39 -06:00
|
|
|
let lint = builtin::UNKNOWN_LINTS;
|
2022-02-28 14:29:06 -06:00
|
|
|
let (level, src) = self.lint_level(builtin::UNKNOWN_LINTS);
|
2022-03-01 18:10:39 -06:00
|
|
|
struct_lint_level(self.sess, lint, level, src, Some(span.into()), |lint_db| {
|
2022-02-28 14:29:06 -06:00
|
|
|
let mut db =
|
2022-03-01 18:10:39 -06:00
|
|
|
lint_db.build(&format!("unknown lint: `{}`", lint_id.lint.name_lower()));
|
2022-02-28 14:29:06 -06:00
|
|
|
db.note(&format!("the `{}` lint is unstable", lint_id.lint.name_lower(),));
|
|
|
|
add_feature_diagnostics(&mut db, &self.sess.parse_sess, feature);
|
|
|
|
db.emit();
|
|
|
|
});
|
2022-02-22 22:53:17 -06:00
|
|
|
return false;
|
2020-06-03 21:16:29 -05:00
|
|
|
}
|
2020-05-13 16:43:21 -05:00
|
|
|
}
|
2022-02-22 22:53:17 -06:00
|
|
|
true
|
2020-05-13 16:43:21 -05:00
|
|
|
}
|
|
|
|
|
2020-01-09 00:52:01 -06:00
|
|
|
/// Called after `push` when the scope of a set of attributes are exited.
|
|
|
|
pub fn pop(&mut self, push: BuilderPush) {
|
|
|
|
self.cur = push.prev;
|
|
|
|
}
|
|
|
|
|
2020-04-24 20:38:31 -05:00
|
|
|
/// Find the lint level for a lint.
|
2020-12-21 16:17:53 -06:00
|
|
|
pub fn lint_level(&self, lint: &'static Lint) -> (Level, LintLevelSource) {
|
2020-04-24 20:38:31 -05:00
|
|
|
self.sets.get_lint_level(lint, self.cur, None, self.sess)
|
|
|
|
}
|
|
|
|
|
2020-01-09 00:52:01 -06:00
|
|
|
/// Used to emit a lint-related diagnostic based on the current state of
|
|
|
|
/// this lint context.
|
|
|
|
pub fn struct_lint(
|
|
|
|
&self,
|
|
|
|
lint: &'static Lint,
|
|
|
|
span: Option<MultiSpan>,
|
2022-01-22 18:49:12 -06:00
|
|
|
decorate: impl for<'a> FnOnce(LintDiagnosticBuilder<'a, ()>),
|
2020-01-31 06:24:57 -06:00
|
|
|
) {
|
2020-04-24 20:38:31 -05:00
|
|
|
let (level, src) = self.lint_level(lint);
|
2020-01-31 06:24:57 -06:00
|
|
|
struct_lint_level(self.sess, lint, level, src, span, decorate)
|
2020-01-09 00:52:01 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Registers the ID provided with the current set of lints stored in
|
|
|
|
/// this context.
|
|
|
|
pub fn register_id(&mut self, id: HirId) {
|
|
|
|
self.id_to_set.insert(id, self.cur);
|
|
|
|
}
|
|
|
|
|
2021-11-20 13:45:27 -06:00
|
|
|
fn update_unstable_expectation_ids(&self) {
|
|
|
|
self.sess.diagnostic().update_unstable_expectation_id(&self.expectation_id_map);
|
|
|
|
}
|
|
|
|
|
2020-01-09 00:52:01 -06:00
|
|
|
pub fn build_map(self) -> LintLevelMap {
|
2021-08-06 16:28:58 -05:00
|
|
|
LintLevelMap {
|
|
|
|
sets: self.sets,
|
|
|
|
id_to_set: self.id_to_set,
|
|
|
|
lint_expectations: self.lint_expectations,
|
|
|
|
}
|
2020-01-09 00:52:01 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-27 16:28:49 -05:00
|
|
|
struct LintLevelMapBuilder<'tcx> {
|
2019-12-30 12:02:52 -06:00
|
|
|
levels: LintLevelsBuilder<'tcx>,
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
}
|
|
|
|
|
2021-09-27 16:28:49 -05:00
|
|
|
impl LintLevelMapBuilder<'_> {
|
2020-11-25 16:25:57 -06:00
|
|
|
fn with_lint_attrs<F>(&mut self, id: hir::HirId, f: F)
|
2019-12-30 12:02:52 -06:00
|
|
|
where
|
|
|
|
F: FnOnce(&mut Self),
|
|
|
|
{
|
2020-06-12 20:58:24 -05:00
|
|
|
let is_crate_hir = id == hir::CRATE_HIR_ID;
|
2020-11-25 16:25:57 -06:00
|
|
|
let attrs = self.tcx.hir().attrs(id);
|
2021-11-20 13:45:27 -06:00
|
|
|
let push = self.levels.push(attrs, is_crate_hir, Some(id));
|
|
|
|
|
2019-12-30 12:02:52 -06:00
|
|
|
if push.changed {
|
|
|
|
self.levels.register_id(id);
|
|
|
|
}
|
|
|
|
f(self);
|
|
|
|
self.levels.pop(push);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-27 16:28:49 -05:00
|
|
|
impl<'tcx> intravisit::Visitor<'tcx> for LintLevelMapBuilder<'tcx> {
|
2021-11-03 18:03:12 -05:00
|
|
|
type NestedFilter = nested_filter::All;
|
2020-01-07 10:25:33 -06:00
|
|
|
|
2021-11-03 18:03:12 -05:00
|
|
|
fn nested_visit_map(&mut self) -> Self::Map {
|
|
|
|
self.tcx.hir()
|
2019-12-30 12:02:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_param(&mut self, param: &'tcx hir::Param<'tcx>) {
|
2020-11-25 16:25:57 -06:00
|
|
|
self.with_lint_attrs(param.hir_id, |builder| {
|
2019-12-30 12:02:52 -06:00
|
|
|
intravisit::walk_param(builder, param);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_item(&mut self, it: &'tcx hir::Item<'tcx>) {
|
2020-11-25 16:25:57 -06:00
|
|
|
self.with_lint_attrs(it.hir_id(), |builder| {
|
2019-12-30 12:02:52 -06:00
|
|
|
intravisit::walk_item(builder, it);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_foreign_item(&mut self, it: &'tcx hir::ForeignItem<'tcx>) {
|
2020-11-25 16:25:57 -06:00
|
|
|
self.with_lint_attrs(it.hir_id(), |builder| {
|
2019-12-30 12:02:52 -06:00
|
|
|
intravisit::walk_foreign_item(builder, it);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
Fix inconsistencies in handling of inert attributes on statements
When the 'early' and 'late' visitors visit an attribute target, they
activate any lint attributes (e.g. `#[allow]`) that apply to it.
This can affect warnings emitted on sibiling attributes. For example,
the following code does not produce an `unused_attributes` for
`#[inline]`, since the sibiling `#[allow(unused_attributes)]` suppressed
the warning.
```rust
trait Foo {
#[allow(unused_attributes)] #[inline] fn first();
#[inline] #[allow(unused_attributes)] fn second();
}
```
However, we do not do this for statements - instead, the lint attributes
only become active when we visit the struct nested inside `StmtKind`
(e.g. `Item`).
Currently, this is difficult to observe due to another issue - the
`HasAttrs` impl for `StmtKind` ignores attributes for `StmtKind::Item`.
As a result, the `unused_doc_comments` lint will never see attributes on
item statements.
This commit makes two interrelated fixes to the handling of inert
(non-proc-macro) attributes on statements:
* The `HasAttr` impl for `StmtKind` now returns attributes for
`StmtKind::Item`, treating it just like every other `StmtKind`
variant. The only place relying on the old behavior was macro
which has been updated to explicitly ignore attributes on item
statements. This allows the `unused_doc_comments` lint to fire for
item statements.
* The `early` and `late` lint visitors now activate lint attributes when
invoking the callback for `Stmt`. This ensures that a lint
attribute (e.g. `#[allow(unused_doc_comments)]`) can be applied to
sibiling attributes on an item statement.
For now, the `unused_doc_comments` lint is explicitly disabled on item
statements, which preserves the current behavior. The exact locatiosn
where this lint should fire are being discussed in PR #78306
2020-10-23 17:17:00 -05:00
|
|
|
fn visit_stmt(&mut self, e: &'tcx hir::Stmt<'tcx>) {
|
|
|
|
// We will call `with_lint_attrs` when we walk
|
|
|
|
// the `StmtKind`. The outer statement itself doesn't
|
|
|
|
// define the lint levels.
|
|
|
|
intravisit::walk_stmt(self, e);
|
|
|
|
}
|
|
|
|
|
2019-12-30 12:02:52 -06:00
|
|
|
fn visit_expr(&mut self, e: &'tcx hir::Expr<'tcx>) {
|
2020-11-25 16:25:57 -06:00
|
|
|
self.with_lint_attrs(e.hir_id, |builder| {
|
2019-12-30 12:02:52 -06:00
|
|
|
intravisit::walk_expr(builder, e);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-03-15 16:36:07 -05:00
|
|
|
fn visit_field_def(&mut self, s: &'tcx hir::FieldDef<'tcx>) {
|
2020-11-25 16:25:57 -06:00
|
|
|
self.with_lint_attrs(s.hir_id, |builder| {
|
2021-03-15 16:36:07 -05:00
|
|
|
intravisit::walk_field_def(builder, s);
|
2019-12-30 12:02:52 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_variant(
|
|
|
|
&mut self,
|
|
|
|
v: &'tcx hir::Variant<'tcx>,
|
|
|
|
g: &'tcx hir::Generics<'tcx>,
|
|
|
|
item_id: hir::HirId,
|
|
|
|
) {
|
2020-11-25 16:25:57 -06:00
|
|
|
self.with_lint_attrs(v.id, |builder| {
|
2019-12-30 12:02:52 -06:00
|
|
|
intravisit::walk_variant(builder, v, g, item_id);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_local(&mut self, l: &'tcx hir::Local<'tcx>) {
|
2020-11-25 16:25:57 -06:00
|
|
|
self.with_lint_attrs(l.hir_id, |builder| {
|
2019-12-30 12:02:52 -06:00
|
|
|
intravisit::walk_local(builder, l);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_arm(&mut self, a: &'tcx hir::Arm<'tcx>) {
|
2020-11-25 16:25:57 -06:00
|
|
|
self.with_lint_attrs(a.hir_id, |builder| {
|
2019-12-30 12:02:52 -06:00
|
|
|
intravisit::walk_arm(builder, a);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_trait_item(&mut self, trait_item: &'tcx hir::TraitItem<'tcx>) {
|
2020-11-25 16:25:57 -06:00
|
|
|
self.with_lint_attrs(trait_item.hir_id(), |builder| {
|
2019-12-30 12:02:52 -06:00
|
|
|
intravisit::walk_trait_item(builder, trait_item);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem<'tcx>) {
|
2020-11-25 16:25:57 -06:00
|
|
|
self.with_lint_attrs(impl_item.hir_id(), |builder| {
|
2019-12-30 12:02:52 -06:00
|
|
|
intravisit::walk_impl_item(builder, impl_item);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-05 15:00:14 -05:00
|
|
|
pub fn provide(providers: &mut Providers) {
|
2019-12-30 12:02:52 -06:00
|
|
|
providers.lint_levels = lint_levels;
|
|
|
|
}
|