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-02-29 11:37:32 -06:00
|
|
|
use rustc_ast::attr;
|
|
|
|
use rustc_ast::unwrap_or;
|
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;
|
2021-01-29 18:06:00 -06:00
|
|
|
use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder};
|
2020-01-04 19:37:57 -06:00
|
|
|
use rustc_hir as hir;
|
|
|
|
use rustc_hir::def_id::{CrateNum, LOCAL_CRATE};
|
2020-01-11 08:03:15 -06:00
|
|
|
use rustc_hir::{intravisit, HirId};
|
2020-03-29 10:19:48 -05:00
|
|
|
use rustc_middle::hir::map::Map;
|
2021-01-11 20:02:09 -06:00
|
|
|
use rustc_middle::lint::LevelAndSource;
|
2020-03-29 10:19:48 -05:00
|
|
|
use rustc_middle::lint::LintDiagnosticBuilder;
|
2020-12-21 16:40:50 -06:00
|
|
|
use rustc_middle::lint::{
|
|
|
|
struct_lint_level, LintLevelMap, LintLevelSets, LintLevelSource, LintSet,
|
|
|
|
};
|
2020-03-29 10:19:48 -05:00
|
|
|
use rustc_middle::ty::query::Providers;
|
|
|
|
use rustc_middle::ty::TyCtxt;
|
2021-01-29 18:06:00 -06:00
|
|
|
use rustc_session::lint::{
|
|
|
|
builtin::{self, FORBIDDEN_LINT_GROUPS},
|
|
|
|
Level, Lint, LintId,
|
|
|
|
};
|
2020-01-11 08:03:15 -06:00
|
|
|
use rustc_session::parse::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};
|
2020-05-13 16:43:21 -05:00
|
|
|
use rustc_span::{source_map::MultiSpan, Span, DUMMY_SP};
|
2021-01-29 18:06:00 -06:00
|
|
|
use tracing::debug;
|
2019-12-30 12:02:52 -06:00
|
|
|
|
2020-01-09 00:52:01 -06:00
|
|
|
use std::cmp;
|
2019-12-30 12:02:52 -06:00
|
|
|
|
2020-03-27 14:26:20 -05:00
|
|
|
fn lint_levels(tcx: TyCtxt<'_>, cnum: CrateNum) -> LintLevelMap {
|
2019-12-30 12:02:52 -06:00
|
|
|
assert_eq!(cnum, LOCAL_CRATE);
|
2020-01-08 20:45:42 -06:00
|
|
|
let store = unerased_lint_store(tcx);
|
2020-01-08 21:06:33 -06:00
|
|
|
let levels = LintLevelsBuilder::new(tcx.sess, false, &store);
|
|
|
|
let mut builder = LintLevelMapBuilder { levels, tcx, store };
|
2019-12-30 12:02:52 -06:00
|
|
|
let krate = tcx.hir().krate();
|
|
|
|
|
2020-10-16 04:43:39 -05:00
|
|
|
builder.levels.id_to_set.reserve(krate.exported_macros.len() + 1);
|
|
|
|
|
2020-06-12 20:58:24 -05:00
|
|
|
let push = builder.levels.push(&krate.item.attrs, &store, true);
|
2019-12-30 12:02:52 -06:00
|
|
|
builder.levels.register_id(hir::CRATE_HIR_ID);
|
|
|
|
for macro_def in krate.exported_macros {
|
|
|
|
builder.levels.register_id(macro_def.hir_id);
|
|
|
|
}
|
|
|
|
intravisit::walk_crate(&mut builder, krate);
|
|
|
|
builder.levels.pop(push);
|
|
|
|
|
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,
|
2020-01-09 00:52:01 -06:00
|
|
|
sets: LintLevelSets,
|
|
|
|
id_to_set: FxHashMap<HirId, u32>,
|
|
|
|
cur: u32,
|
|
|
|
warn_about_weird_lints: bool,
|
2021-01-29 18:06:00 -06:00
|
|
|
store: &'s LintStore,
|
2020-01-09 00:52:01 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct BuilderPush {
|
|
|
|
prev: u32,
|
|
|
|
pub changed: bool,
|
|
|
|
}
|
|
|
|
|
2020-01-31 06:24:57 -06:00
|
|
|
impl<'s> LintLevelsBuilder<'s> {
|
2021-01-29 18:06:00 -06:00
|
|
|
pub fn new(sess: &'s Session, warn_about_weird_lints: bool, store: &'s LintStore) -> Self {
|
2020-01-09 00:52:01 -06:00
|
|
|
let mut builder = LintLevelsBuilder {
|
|
|
|
sess,
|
|
|
|
sets: LintLevelSets::new(),
|
|
|
|
cur: 0,
|
|
|
|
id_to_set: Default::default(),
|
|
|
|
warn_about_weird_lints,
|
2021-01-29 18:06:00 -06:00
|
|
|
store,
|
2020-01-09 00:52:01 -06:00
|
|
|
};
|
|
|
|
builder.process_command_line(sess, store);
|
|
|
|
assert_eq!(builder.sets.list.len(), 1);
|
|
|
|
builder
|
|
|
|
}
|
|
|
|
|
|
|
|
fn process_command_line(&mut self, sess: &Session, store: &LintStore) {
|
|
|
|
let mut specs = FxHashMap::default();
|
|
|
|
self.sets.lint_cap = sess.opts.lint_cap.unwrap_or(Level::Forbid);
|
|
|
|
|
|
|
|
for &(ref lint_name, level) in &sess.opts.lint_opts {
|
|
|
|
store.check_lint_name_cmdline(sess, &lint_name, level);
|
2020-11-02 00:37:26 -06:00
|
|
|
let orig_level = level;
|
2020-01-09 00:52:01 -06:00
|
|
|
|
|
|
|
// If the cap is less than this specified level, e.g., if we've got
|
|
|
|
// `--cap-lints allow` but we've also got `-D foo` then we ignore
|
|
|
|
// this specification as the lint cap will set it to allow anyway.
|
|
|
|
let level = cmp::min(level, self.sets.lint_cap);
|
|
|
|
|
|
|
|
let lint_flag_val = Symbol::intern(lint_name);
|
2020-05-13 16:43:21 -05:00
|
|
|
|
2020-01-09 00:52:01 -06:00
|
|
|
let ids = match store.find_lints(&lint_name) {
|
|
|
|
Ok(ids) => ids,
|
|
|
|
Err(_) => continue, // errors handled in check_lint_name_cmdline above
|
|
|
|
};
|
|
|
|
for id in ids {
|
2020-05-23 16:11:55 -05:00
|
|
|
self.check_gated_lint(id, DUMMY_SP);
|
2020-12-21 16:17:53 -06:00
|
|
|
let src = LintLevelSource::CommandLine(lint_flag_val, orig_level);
|
2020-01-09 00:52:01 -06:00
|
|
|
specs.insert(id, (level, src));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.sets.list.push(LintSet::CommandLine { specs });
|
|
|
|
}
|
|
|
|
|
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`.
|
|
|
|
fn insert_spec(
|
|
|
|
&mut self,
|
2021-01-11 20:02:09 -06:00
|
|
|
specs: &mut FxHashMap<LintId, LevelAndSource>,
|
2020-06-15 13:17:35 -05:00
|
|
|
id: LintId,
|
2021-01-11 20:02:09 -06:00
|
|
|
(level, src): LevelAndSource,
|
2020-06-15 13:17:35 -05:00
|
|
|
) {
|
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
|
|
|
|
// `#[forbid(..)]` attribute present, as that is overriden by `--cap-lints`.
|
|
|
|
//
|
|
|
|
// This means that this only errors if we're truly lowering the lint
|
|
|
|
// level from forbid.
|
|
|
|
if level != Level::Forbid {
|
|
|
|
if let (Level::Forbid, old_src) =
|
|
|
|
self.sets.get_lint_level(id.lint, self.cur, Some(&specs), &self.sess)
|
|
|
|
{
|
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={:?}",
|
|
|
|
fcw_warning, specs, old_src, id_name
|
2020-06-15 13:17:35 -05:00
|
|
|
);
|
2021-01-29 18:06:00 -06:00
|
|
|
|
|
|
|
let decorate_diag_builder = |mut diag_builder: DiagnosticBuilder<'_>| {
|
|
|
|
diag_builder.span_label(src.span(), "overruled by previous forbid");
|
|
|
|
match old_src {
|
|
|
|
LintLevelSource::Default => {
|
|
|
|
diag_builder.note(&format!(
|
|
|
|
"`forbid` lint level is the default for {}",
|
|
|
|
id.to_string()
|
|
|
|
));
|
|
|
|
}
|
|
|
|
LintLevelSource::Node(_, forbid_source_span, reason) => {
|
|
|
|
diag_builder.span_label(forbid_source_span, "`forbid` level set here");
|
|
|
|
if let Some(rationale) = reason {
|
|
|
|
diag_builder.note(&rationale.as_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
LintLevelSource::CommandLine(_, _) => {
|
|
|
|
diag_builder.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
|
|
|
diag_builder.emit();
|
|
|
|
};
|
|
|
|
if !fcw_warning {
|
|
|
|
let diag_builder = struct_span_err!(
|
|
|
|
self.sess,
|
|
|
|
src.span(),
|
|
|
|
E0453,
|
|
|
|
"{}({}) incompatible with previous forbid",
|
|
|
|
level.as_str(),
|
|
|
|
src.name(),
|
|
|
|
);
|
|
|
|
decorate_diag_builder(diag_builder);
|
|
|
|
} else {
|
|
|
|
self.struct_lint(
|
|
|
|
FORBIDDEN_LINT_GROUPS,
|
|
|
|
Some(src.span().into()),
|
|
|
|
|diag_builder| {
|
|
|
|
let diag_builder = diag_builder.build(&format!(
|
|
|
|
"{}({}) incompatible with previous forbid",
|
|
|
|
level.as_str(),
|
|
|
|
src.name(),
|
|
|
|
));
|
|
|
|
decorate_diag_builder(diag_builder);
|
|
|
|
},
|
|
|
|
);
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
specs.insert(id, (level, src));
|
|
|
|
}
|
|
|
|
|
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`!
|
2020-06-15 13:17:35 -05:00
|
|
|
pub(crate) fn push(
|
2020-06-12 20:58:24 -05:00
|
|
|
&mut self,
|
|
|
|
attrs: &[ast::Attribute],
|
|
|
|
store: &LintStore,
|
|
|
|
is_crate_node: bool,
|
|
|
|
) -> BuilderPush {
|
2020-01-09 00:52:01 -06:00
|
|
|
let mut specs = FxHashMap::default();
|
|
|
|
let sess = self.sess;
|
|
|
|
let bad_attr = |span| struct_span_err!(sess, span, E0452, "malformed lint attribute input");
|
|
|
|
for attr in attrs {
|
|
|
|
let level = match Level::from_symbol(attr.name_or_empty()) {
|
|
|
|
None => continue,
|
|
|
|
Some(lvl) => lvl,
|
|
|
|
};
|
|
|
|
|
|
|
|
let meta = unwrap_or!(attr.meta(), continue);
|
2020-07-29 20:27:50 -05:00
|
|
|
self.sess.mark_attr_used(attr);
|
2020-01-09 00:52:01 -06:00
|
|
|
|
|
|
|
let mut metas = unwrap_or!(meta.meta_item_list(), continue);
|
|
|
|
|
|
|
|
if metas.is_empty() {
|
|
|
|
// FIXME (#55112): issue unused-attributes lint for `#[level()]`
|
|
|
|
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 {
|
|
|
|
// found reason, reslice meta list to exclude it
|
|
|
|
metas = &metas[0..metas.len() - 1];
|
|
|
|
// FIXME (#55112): issue unused-attributes lint if we thereby
|
|
|
|
// don't have any lint names (`#[level(reason = "foo")]`)
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
} 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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for li in metas {
|
|
|
|
let meta_item = match li.meta_item() {
|
|
|
|
Some(meta_item) if meta_item.is_word() => meta_item,
|
|
|
|
_ => {
|
|
|
|
let sp = li.span();
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let tool_name = if meta_item.path.segments.len() > 1 {
|
|
|
|
let tool_ident = meta_item.path.segments[0].ident;
|
|
|
|
if !attr::is_known_lint_tool(tool_ident) {
|
|
|
|
struct_span_err!(
|
|
|
|
sess,
|
|
|
|
tool_ident.span,
|
|
|
|
E0710,
|
|
|
|
"an unknown tool name found in scoped lint: `{}`",
|
|
|
|
pprust::path_to_string(&meta_item.path),
|
|
|
|
)
|
|
|
|
.emit();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(tool_ident.name)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
let name = meta_item.path.segments.last().expect("empty lint name").ident.name;
|
|
|
|
match store.check_lint_name(&name.as_str(), tool_name) {
|
|
|
|
CheckLintNameResult::Ok(ids) => {
|
2020-12-21 16:17:53 -06:00
|
|
|
let src = LintLevelSource::Node(name, li.span(), reason);
|
2020-06-03 21:16:29 -05:00
|
|
|
for &id in ids {
|
|
|
|
self.check_gated_lint(id, attr.span);
|
2020-06-15 13:17:35 -05:00
|
|
|
self.insert_spec(&mut specs, id, (level, src));
|
2020-01-09 00:52:01 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CheckLintNameResult::Tool(result) => {
|
|
|
|
match result {
|
|
|
|
Ok(ids) => {
|
|
|
|
let complete_name = &format!("{}::{}", tool_name.unwrap(), 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),
|
|
|
|
li.span(),
|
|
|
|
reason,
|
|
|
|
);
|
|
|
|
for id in ids {
|
2020-06-15 13:17:35 -05:00
|
|
|
self.insert_spec(&mut specs, *id, (level, src));
|
2020-01-09 00:52:01 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Err((Some(ids), new_lint_name)) => {
|
|
|
|
let lint = builtin::RENAMED_AND_REMOVED_LINTS;
|
|
|
|
let (lvl, src) =
|
|
|
|
self.sets.get_lint_level(lint, self.cur, Some(&specs), &sess);
|
|
|
|
struct_lint_level(
|
|
|
|
self.sess,
|
|
|
|
lint,
|
|
|
|
lvl,
|
|
|
|
src,
|
|
|
|
Some(li.span().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(
|
|
|
|
li.span(),
|
|
|
|
"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),
|
|
|
|
li.span(),
|
|
|
|
reason,
|
|
|
|
);
|
|
|
|
for id in ids {
|
2020-06-15 13:17:35 -05:00
|
|
|
self.insert_spec(&mut specs, *id, (level, src));
|
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.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_ if !self.warn_about_weird_lints => {}
|
|
|
|
|
|
|
|
CheckLintNameResult::Warning(msg, renamed) => {
|
|
|
|
let lint = builtin::RENAMED_AND_REMOVED_LINTS;
|
|
|
|
let (level, src) =
|
|
|
|
self.sets.get_lint_level(lint, self.cur, Some(&specs), &sess);
|
2020-01-31 06:24:57 -06:00
|
|
|
struct_lint_level(
|
2020-01-09 00:52:01 -06:00
|
|
|
self.sess,
|
|
|
|
lint,
|
|
|
|
level,
|
|
|
|
src,
|
|
|
|
Some(li.span().into()),
|
2020-01-31 06:24:57 -06:00
|
|
|
|lint| {
|
|
|
|
let mut err = lint.build(&msg);
|
|
|
|
if let Some(new_name) = renamed {
|
|
|
|
err.span_suggestion(
|
|
|
|
li.span(),
|
|
|
|
"use the new name",
|
|
|
|
new_name,
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
err.emit();
|
|
|
|
},
|
2020-01-09 00:52:01 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
CheckLintNameResult::NoLint(suggestion) => {
|
|
|
|
let lint = builtin::UNKNOWN_LINTS;
|
|
|
|
let (level, src) =
|
|
|
|
self.sets.get_lint_level(lint, self.cur, Some(&specs), self.sess);
|
2020-01-31 06:24:57 -06:00
|
|
|
struct_lint_level(
|
2020-01-09 00:52:01 -06:00
|
|
|
self.sess,
|
|
|
|
lint,
|
|
|
|
level,
|
|
|
|
src,
|
|
|
|
Some(li.span().into()),
|
2020-01-31 06:24:57 -06:00
|
|
|
|lint| {
|
2020-12-30 12:49:28 -06:00
|
|
|
let name = if let Some(tool_name) = tool_name {
|
|
|
|
format!("{}::{}", tool_name, name)
|
|
|
|
} else {
|
|
|
|
name.to_string()
|
|
|
|
};
|
2020-02-05 09:27:46 -06:00
|
|
|
let mut db = lint.build(&format!("unknown lint: `{}`", name));
|
2020-01-31 06:24:57 -06:00
|
|
|
if let Some(suggestion) = suggestion {
|
|
|
|
db.span_suggestion(
|
|
|
|
li.span(),
|
|
|
|
"did you mean",
|
|
|
|
suggestion.to_string(),
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
db.emit();
|
|
|
|
},
|
2020-01-09 00:52:01 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-12 20:58:24 -05:00
|
|
|
if !is_crate_node {
|
|
|
|
for (id, &(level, ref src)) in specs.iter() {
|
|
|
|
if !id.lint.crate_level_only {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let (lint_attr_name, lint_attr_span) = match *src {
|
2020-12-21 16:17:53 -06:00
|
|
|
LintLevelSource::Node(name, span, _) => (name, span),
|
2020-06-12 20:58:24 -05:00
|
|
|
_ => continue,
|
|
|
|
};
|
|
|
|
|
|
|
|
let lint = builtin::UNUSED_ATTRIBUTES;
|
|
|
|
let (lint_level, lint_src) =
|
|
|
|
self.sets.get_lint_level(lint, self.cur, Some(&specs), self.sess);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-09 00:52:01 -06:00
|
|
|
let prev = self.cur;
|
2020-02-28 07:20:33 -06:00
|
|
|
if !specs.is_empty() {
|
2020-01-09 00:52:01 -06:00
|
|
|
self.cur = self.sets.list.len() as u32;
|
2020-03-06 12:28:44 -06:00
|
|
|
self.sets.list.push(LintSet::Node { specs, parent: 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
|
|
|
}
|
|
|
|
|
2020-06-03 21:16:29 -05:00
|
|
|
/// Checks if the lint is gated on a feature that is not enabled.
|
|
|
|
fn check_gated_lint(&self, lint_id: LintId, span: Span) {
|
|
|
|
if let Some(feature) = lint_id.lint.feature_gate {
|
|
|
|
if !self.sess.features_untracked().enabled(feature) {
|
|
|
|
feature_err(
|
|
|
|
&self.sess.parse_sess,
|
|
|
|
feature,
|
|
|
|
span,
|
|
|
|
&format!("the `{}` lint is unstable", lint_id.lint.name_lower()),
|
|
|
|
)
|
|
|
|
.emit();
|
|
|
|
}
|
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>,
|
2020-01-31 06:24:57 -06:00
|
|
|
decorate: impl for<'a> FnOnce(LintDiagnosticBuilder<'a>),
|
|
|
|
) {
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn build(self) -> LintLevelSets {
|
|
|
|
self.sets
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn build_map(self) -> LintLevelMap {
|
|
|
|
LintLevelMap { sets: self.sets, id_to_set: self.id_to_set }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-30 12:02:52 -06:00
|
|
|
struct LintLevelMapBuilder<'a, 'tcx> {
|
|
|
|
levels: LintLevelsBuilder<'tcx>,
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
store: &'a LintStore,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LintLevelMapBuilder<'_, '_> {
|
|
|
|
fn with_lint_attrs<F>(&mut self, id: hir::HirId, attrs: &[ast::Attribute], f: F)
|
|
|
|
where
|
|
|
|
F: FnOnce(&mut Self),
|
|
|
|
{
|
2020-06-12 20:58:24 -05:00
|
|
|
let is_crate_hir = id == hir::CRATE_HIR_ID;
|
|
|
|
let push = self.levels.push(attrs, self.store, is_crate_hir);
|
2019-12-30 12:02:52 -06:00
|
|
|
if push.changed {
|
|
|
|
self.levels.register_id(id);
|
|
|
|
}
|
|
|
|
f(self);
|
|
|
|
self.levels.pop(push);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> intravisit::Visitor<'tcx> for LintLevelMapBuilder<'_, 'tcx> {
|
2020-01-07 10:25:33 -06:00
|
|
|
type Map = Map<'tcx>;
|
|
|
|
|
2020-02-09 08:32:00 -06:00
|
|
|
fn nested_visit_map(&mut self) -> intravisit::NestedVisitorMap<Self::Map> {
|
|
|
|
intravisit::NestedVisitorMap::All(self.tcx.hir())
|
2019-12-30 12:02:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_param(&mut self, param: &'tcx hir::Param<'tcx>) {
|
|
|
|
self.with_lint_attrs(param.hir_id, ¶m.attrs, |builder| {
|
|
|
|
intravisit::walk_param(builder, param);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_item(&mut self, it: &'tcx hir::Item<'tcx>) {
|
2021-01-30 10:47:51 -06:00
|
|
|
self.with_lint_attrs(it.hir_id(), &it.attrs, |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>) {
|
|
|
|
self.with_lint_attrs(it.hir_id, &it.attrs, |builder| {
|
|
|
|
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>) {
|
|
|
|
self.with_lint_attrs(e.hir_id, &e.attrs, |builder| {
|
|
|
|
intravisit::walk_expr(builder, e);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_struct_field(&mut self, s: &'tcx hir::StructField<'tcx>) {
|
|
|
|
self.with_lint_attrs(s.hir_id, &s.attrs, |builder| {
|
|
|
|
intravisit::walk_struct_field(builder, s);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_variant(
|
|
|
|
&mut self,
|
|
|
|
v: &'tcx hir::Variant<'tcx>,
|
|
|
|
g: &'tcx hir::Generics<'tcx>,
|
|
|
|
item_id: hir::HirId,
|
|
|
|
) {
|
|
|
|
self.with_lint_attrs(v.id, &v.attrs, |builder| {
|
|
|
|
intravisit::walk_variant(builder, v, g, item_id);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_local(&mut self, l: &'tcx hir::Local<'tcx>) {
|
|
|
|
self.with_lint_attrs(l.hir_id, &l.attrs, |builder| {
|
|
|
|
intravisit::walk_local(builder, l);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_arm(&mut self, a: &'tcx hir::Arm<'tcx>) {
|
|
|
|
self.with_lint_attrs(a.hir_id, &a.attrs, |builder| {
|
|
|
|
intravisit::walk_arm(builder, a);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_trait_item(&mut self, trait_item: &'tcx hir::TraitItem<'tcx>) {
|
2021-01-30 13:46:50 -06:00
|
|
|
self.with_lint_attrs(trait_item.hir_id(), &trait_item.attrs, |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>) {
|
2021-01-30 16:25:03 -06:00
|
|
|
self.with_lint_attrs(impl_item.hir_id(), &impl_item.attrs, |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;
|
|
|
|
}
|