2017-07-27 04:51:09 +00:00
|
|
|
use std::cmp;
|
|
|
|
|
2019-02-05 17:20:45 +00:00
|
|
|
use crate::ich::StableHashingContext;
|
2020-01-09 04:20:28 +00:00
|
|
|
use crate::lint;
|
2019-12-22 22:42:04 +00:00
|
|
|
use crate::lint::context::{CheckLintNameResult, LintStore};
|
2019-12-24 04:02:53 +00:00
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
2019-11-12 17:09:20 +00:00
|
|
|
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
|
2020-01-09 10:18:47 +00:00
|
|
|
use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder};
|
2020-01-05 01:37:57 +00:00
|
|
|
use rustc_hir::HirId;
|
2020-01-05 09:58:44 +00:00
|
|
|
use rustc_session::lint::{builtin, Level, Lint, LintId};
|
|
|
|
use rustc_session::Session;
|
2020-01-01 18:25:28 +00:00
|
|
|
use rustc_span::source_map::MultiSpan;
|
2020-01-01 18:30:57 +00:00
|
|
|
use rustc_span::symbol::{sym, Symbol};
|
2020-01-09 04:20:28 +00:00
|
|
|
use rustc_span::Span;
|
2017-07-27 04:51:09 +00:00
|
|
|
use syntax::ast;
|
|
|
|
use syntax::attr;
|
2019-10-08 20:17:46 +00:00
|
|
|
use syntax::print::pprust;
|
2020-01-02 11:42:42 +00:00
|
|
|
use syntax::sess::feature_err;
|
2017-07-27 04:51:09 +00:00
|
|
|
|
2019-11-11 21:46:56 +00:00
|
|
|
use rustc_error_codes::*;
|
|
|
|
|
2020-01-09 04:20:28 +00:00
|
|
|
/// How a lint level was set.
|
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, HashStable)]
|
|
|
|
pub enum LintSource {
|
|
|
|
/// Lint is at the default level as declared
|
|
|
|
/// in rustc or a plugin.
|
|
|
|
Default,
|
|
|
|
|
|
|
|
/// Lint level was set by an attribute.
|
|
|
|
Node(Symbol, Span, Option<Symbol> /* RFC 2383 reason */),
|
|
|
|
|
|
|
|
/// Lint level was set by a command-line flag.
|
|
|
|
CommandLine(Symbol),
|
|
|
|
}
|
|
|
|
|
|
|
|
pub type LevelSource = (Level, LintSource);
|
|
|
|
|
2017-07-27 04:51:09 +00:00
|
|
|
pub struct LintLevelSets {
|
|
|
|
list: Vec<LintSet>,
|
|
|
|
lint_cap: Level,
|
|
|
|
}
|
|
|
|
|
|
|
|
enum LintSet {
|
|
|
|
CommandLine {
|
|
|
|
// -A,-W,-D flags, a `Symbol` for the flag itself and `Level` for which
|
|
|
|
// flag.
|
2020-01-09 04:20:28 +00:00
|
|
|
specs: FxHashMap<LintId, LevelSource>,
|
2017-07-27 04:51:09 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
Node {
|
2020-01-09 04:20:28 +00:00
|
|
|
specs: FxHashMap<LintId, LevelSource>,
|
2017-07-27 04:51:09 +00:00
|
|
|
parent: u32,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LintLevelSets {
|
2020-01-09 04:20:28 +00:00
|
|
|
pub fn new() -> Self {
|
2020-01-09 03:06:33 +00:00
|
|
|
LintLevelSets { list: Vec::new(), lint_cap: Level::Forbid }
|
2017-07-27 04:51:09 +00:00
|
|
|
}
|
|
|
|
|
2020-01-09 04:20:28 +00:00
|
|
|
pub fn get_lint_level(
|
2019-12-22 22:42:04 +00:00
|
|
|
&self,
|
|
|
|
lint: &'static Lint,
|
|
|
|
idx: u32,
|
2020-01-09 04:20:28 +00:00
|
|
|
aux: Option<&FxHashMap<LintId, LevelSource>>,
|
2019-12-22 22:42:04 +00:00
|
|
|
sess: &Session,
|
2020-01-09 04:20:28 +00:00
|
|
|
) -> LevelSource {
|
2017-08-13 12:37:24 +00:00
|
|
|
let (level, mut src) = self.get_lint_id_level(LintId::of(lint), idx, aux);
|
2017-07-27 04:51:09 +00:00
|
|
|
|
|
|
|
// If `level` is none then we actually assume the default level for this
|
|
|
|
// lint.
|
2019-11-12 16:52:26 +00:00
|
|
|
let mut level = level.unwrap_or_else(|| lint.default_level(sess.edition()));
|
2017-07-27 04:51:09 +00:00
|
|
|
|
|
|
|
// If we're about to issue a warning, check at the last minute for any
|
|
|
|
// directives against the warnings "lint". If, for example, there's an
|
|
|
|
// `allow(warnings)` in scope then we want to respect that instead.
|
|
|
|
if level == Level::Warn {
|
|
|
|
let (warnings_level, warnings_src) =
|
2020-01-09 04:20:28 +00:00
|
|
|
self.get_lint_id_level(LintId::of(builtin::WARNINGS), idx, aux);
|
2017-07-27 04:51:09 +00:00
|
|
|
if let Some(configured_warning_level) = warnings_level {
|
|
|
|
if configured_warning_level != Level::Warn {
|
|
|
|
level = configured_warning_level;
|
|
|
|
src = warnings_src;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that we never exceed the `--cap-lints` argument.
|
|
|
|
level = cmp::min(level, self.lint_cap);
|
|
|
|
|
2018-06-30 22:27:44 +00:00
|
|
|
if let Some(driver_level) = sess.driver_lint_caps.get(&LintId::of(lint)) {
|
|
|
|
// Ensure that we never exceed driver level.
|
|
|
|
level = cmp::min(*driver_level, level);
|
|
|
|
}
|
|
|
|
|
2019-12-22 22:42:04 +00:00
|
|
|
return (level, src);
|
2017-07-27 04:51:09 +00:00
|
|
|
}
|
|
|
|
|
2020-01-09 04:20:28 +00:00
|
|
|
pub fn get_lint_id_level(
|
2019-12-22 22:42:04 +00:00
|
|
|
&self,
|
|
|
|
id: LintId,
|
|
|
|
mut idx: u32,
|
2020-01-09 04:20:28 +00:00
|
|
|
aux: Option<&FxHashMap<LintId, LevelSource>>,
|
2019-12-22 22:42:04 +00:00
|
|
|
) -> (Option<Level>, LintSource) {
|
2017-08-13 12:37:24 +00:00
|
|
|
if let Some(specs) = aux {
|
|
|
|
if let Some(&(level, src)) = specs.get(&id) {
|
2019-12-22 22:42:04 +00:00
|
|
|
return (Some(level), src);
|
2017-08-13 12:37:24 +00:00
|
|
|
}
|
|
|
|
}
|
2017-07-27 04:51:09 +00:00
|
|
|
loop {
|
|
|
|
match self.list[idx as usize] {
|
|
|
|
LintSet::CommandLine { ref specs } => {
|
|
|
|
if let Some(&(level, src)) = specs.get(&id) {
|
2019-12-22 22:42:04 +00:00
|
|
|
return (Some(level), src);
|
2017-07-27 04:51:09 +00:00
|
|
|
}
|
2019-12-22 22:42:04 +00:00
|
|
|
return (None, LintSource::Default);
|
2017-07-27 04:51:09 +00:00
|
|
|
}
|
|
|
|
LintSet::Node { ref specs, parent } => {
|
|
|
|
if let Some(&(level, src)) = specs.get(&id) {
|
2019-12-22 22:42:04 +00:00
|
|
|
return (Some(level), src);
|
2017-07-27 04:51:09 +00:00
|
|
|
}
|
|
|
|
idx = parent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct LintLevelsBuilder<'a> {
|
|
|
|
sess: &'a Session,
|
|
|
|
sets: LintLevelSets,
|
|
|
|
id_to_set: FxHashMap<HirId, u32>,
|
|
|
|
cur: u32,
|
|
|
|
warn_about_weird_lints: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct BuilderPush {
|
|
|
|
prev: u32,
|
2019-12-30 18:02:52 +00:00
|
|
|
pub changed: bool,
|
2017-07-27 04:51:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> LintLevelsBuilder<'a> {
|
2020-01-09 03:06:33 +00:00
|
|
|
pub fn new(sess: &'a Session, warn_about_weird_lints: bool, store: &LintStore) -> Self {
|
|
|
|
let mut builder = LintLevelsBuilder {
|
2017-07-27 04:51:09 +00:00
|
|
|
sess,
|
2020-01-09 03:06:33 +00:00
|
|
|
sets: LintLevelSets::new(),
|
2017-07-27 04:51:09 +00:00
|
|
|
cur: 0,
|
2018-10-16 14:57:53 +00:00
|
|
|
id_to_set: Default::default(),
|
2019-10-25 17:41:51 +00:00
|
|
|
warn_about_weird_lints,
|
2020-01-09 03:06:33 +00: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);
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
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 {
|
|
|
|
let src = LintSource::CommandLine(lint_flag_val);
|
|
|
|
specs.insert(id, (level, src));
|
|
|
|
}
|
2017-07-27 04:51:09 +00:00
|
|
|
}
|
2020-01-09 03:06:33 +00:00
|
|
|
|
|
|
|
self.sets.list.push(LintSet::CommandLine { specs });
|
2017-07-27 04:51:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Pushes a list of AST lint attributes onto this context.
|
|
|
|
///
|
2019-01-03 02:06:49 +00:00
|
|
|
/// This function will return a `BuilderPush` object which should be passed
|
|
|
|
/// to `pop` when this scope for the attributes provided is exited.
|
2017-07-27 04:51:09 +00:00
|
|
|
///
|
|
|
|
/// This function will perform a number of tasks:
|
|
|
|
///
|
|
|
|
/// * It'll validate all lint-related attributes in `attrs`
|
2018-11-12 18:05:20 +00:00
|
|
|
/// * It'll mark all lint-related attributes as used
|
2017-07-27 04:51:09 +00:00
|
|
|
/// * Lint levels will be updated based on the attributes provided
|
2018-11-27 02:59:49 +00:00
|
|
|
/// * Lint attributes are validated, e.g., a #[forbid] can't be switched to
|
2017-07-27 04:51:09 +00:00
|
|
|
/// #[allow]
|
|
|
|
///
|
|
|
|
/// Don't forget to call `pop`!
|
2019-10-09 12:46:11 +00:00
|
|
|
pub fn push(&mut self, attrs: &[ast::Attribute], store: &LintStore) -> BuilderPush {
|
2018-10-16 08:44:26 +00:00
|
|
|
let mut specs = FxHashMap::default();
|
2017-07-27 04:51:09 +00:00
|
|
|
let sess = self.sess;
|
2019-12-22 22:42:04 +00:00
|
|
|
let bad_attr = |span| struct_span_err!(sess, span, E0452, "malformed lint attribute input");
|
2017-07-27 04:51:09 +00:00
|
|
|
for attr in attrs {
|
2019-05-08 04:33:06 +00:00
|
|
|
let level = match Level::from_symbol(attr.name_or_empty()) {
|
2017-07-27 04:51:09 +00:00
|
|
|
None => continue,
|
|
|
|
Some(lvl) => lvl,
|
|
|
|
};
|
|
|
|
|
|
|
|
let meta = unwrap_or!(attr.meta(), continue);
|
|
|
|
attr::mark_used(attr);
|
|
|
|
|
2019-10-21 06:18:09 +00:00
|
|
|
let mut metas = unwrap_or!(meta.meta_item_list(), continue);
|
2017-07-27 04:51:09 +00:00
|
|
|
|
2018-10-16 06:35:58 +00:00
|
|
|
if metas.is_empty() {
|
|
|
|
// FIXME (#55112): issue unused-attributes lint for `#[level()]`
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-10-12 07:21:21 +00:00
|
|
|
// Before processing the lint names, look for a reason (RFC 2383)
|
|
|
|
// at the end.
|
2018-09-30 00:25:26 +00:00
|
|
|
let mut reason = None;
|
2019-12-22 22:42:04 +00:00
|
|
|
let tail_li = &metas[metas.len() - 1];
|
2018-10-12 07:21:21 +00:00
|
|
|
if let Some(item) = tail_li.meta_item() {
|
2019-09-26 17:04:05 +00:00
|
|
|
match item.kind {
|
2019-12-22 22:42:04 +00:00
|
|
|
ast::MetaItemKind::Word => {} // actual lint names handled later
|
2018-10-12 07:21:21 +00:00
|
|
|
ast::MetaItemKind::NameValue(ref name_value) => {
|
2019-05-07 06:03:44 +00:00
|
|
|
if item.path == sym::reason {
|
2018-10-12 07:21:21 +00:00
|
|
|
// found reason, reslice meta list to exclude it
|
2019-12-22 22:42:04 +00:00
|
|
|
metas = &metas[0..metas.len() - 1];
|
2018-10-16 06:35:58 +00:00
|
|
|
// FIXME (#55112): issue unused-attributes lint if we thereby
|
|
|
|
// don't have any lint names (`#[level(reason = "foo")]`)
|
2019-09-26 15:56:53 +00:00
|
|
|
if let ast::LitKind::Str(rationale, _) = name_value.kind {
|
2018-12-20 22:47:03 +00:00
|
|
|
if !self.sess.features_untracked().lint_reasons {
|
2020-01-02 11:42:42 +00:00
|
|
|
feature_err(
|
2018-10-12 07:21:21 +00:00
|
|
|
&self.sess.parse_sess,
|
2019-05-08 03:21:18 +00:00
|
|
|
sym::lint_reasons,
|
2018-10-12 07:21:21 +00:00
|
|
|
item.span,
|
2019-12-22 22:42:04 +00:00
|
|
|
"lint reasons are experimental",
|
2019-11-30 06:40:28 +00:00
|
|
|
)
|
|
|
|
.emit();
|
2018-09-30 00:25:26 +00:00
|
|
|
}
|
2018-12-20 22:47:03 +00:00
|
|
|
reason = Some(rationale);
|
2018-09-30 00:25:26 +00:00
|
|
|
} else {
|
2019-05-22 00:47:23 +00:00
|
|
|
bad_attr(name_value.span)
|
|
|
|
.span_label(name_value.span, "reason must be a string literal")
|
|
|
|
.emit();
|
2018-09-30 00:25:26 +00:00
|
|
|
}
|
2018-10-12 07:21:21 +00:00
|
|
|
} else {
|
2019-05-22 00:47:23 +00:00
|
|
|
bad_attr(item.span)
|
|
|
|
.span_label(item.span, "bad attribute argument")
|
|
|
|
.emit();
|
2018-09-30 00:25:26 +00:00
|
|
|
}
|
2019-12-22 22:42:04 +00:00
|
|
|
}
|
2018-10-12 07:21:21 +00:00
|
|
|
ast::MetaItemKind::List(_) => {
|
2019-12-22 22:42:04 +00:00
|
|
|
bad_attr(item.span).span_label(item.span, "bad attribute argument").emit();
|
2018-09-30 00:25:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-27 04:51:09 +00:00
|
|
|
for li in metas {
|
2019-02-28 06:17:24 +00:00
|
|
|
let meta_item = match li.meta_item() {
|
|
|
|
Some(meta_item) if meta_item.is_word() => meta_item,
|
|
|
|
_ => {
|
2019-05-22 00:47:23 +00:00
|
|
|
let sp = li.span();
|
|
|
|
let mut err = bad_attr(sp);
|
|
|
|
let mut add_label = true;
|
2018-10-12 07:21:21 +00:00
|
|
|
if let Some(item) = li.meta_item() {
|
2019-09-26 17:04:05 +00:00
|
|
|
if let ast::MetaItemKind::NameValue(_) = item.kind {
|
2019-05-07 06:03:44 +00:00
|
|
|
if item.path == sym::reason {
|
2019-05-22 00:47:23 +00:00
|
|
|
err.span_label(sp, "reason in lint attribute must come last");
|
|
|
|
add_label = false;
|
2018-10-12 07:21:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-22 00:47:23 +00:00
|
|
|
if add_label {
|
|
|
|
err.span_label(sp, "bad attribute argument");
|
|
|
|
}
|
2018-10-12 07:21:21 +00:00
|
|
|
err.emit();
|
|
|
|
continue;
|
|
|
|
}
|
2017-07-27 04:51:09 +00:00
|
|
|
};
|
2019-03-02 16:15:26 +00:00
|
|
|
let tool_name = if meta_item.path.segments.len() > 1 {
|
|
|
|
let tool_ident = meta_item.path.segments[0].ident;
|
2019-02-28 06:17:24 +00:00
|
|
|
if !attr::is_known_lint_tool(tool_ident) {
|
2019-12-31 20:25:16 +00:00
|
|
|
struct_span_err!(
|
2018-07-03 11:50:48 +00:00
|
|
|
sess,
|
2019-02-28 06:17:24 +00:00
|
|
|
tool_ident.span,
|
2018-07-03 11:50:48 +00:00
|
|
|
E0710,
|
2018-07-04 12:25:33 +00:00
|
|
|
"an unknown tool name found in scoped lint: `{}`",
|
2019-10-08 20:17:46 +00:00
|
|
|
pprust::path_to_string(&meta_item.path),
|
2019-12-31 20:25:16 +00:00
|
|
|
)
|
|
|
|
.emit();
|
2018-08-27 21:25:31 +00:00
|
|
|
continue;
|
2018-07-30 09:29:23 +00:00
|
|
|
}
|
|
|
|
|
2019-09-03 06:02:32 +00:00
|
|
|
Some(tool_ident.name)
|
2018-07-30 09:29:23 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2019-03-02 16:15:26 +00:00
|
|
|
let name = meta_item.path.segments.last().expect("empty lint name").ident.name;
|
2018-07-30 09:29:23 +00:00
|
|
|
match store.check_lint_name(&name.as_str(), tool_name) {
|
2017-07-27 04:51:09 +00:00
|
|
|
CheckLintNameResult::Ok(ids) => {
|
2019-03-03 17:56:24 +00:00
|
|
|
let src = LintSource::Node(name, li.span(), reason);
|
2017-07-27 04:51:09 +00:00
|
|
|
for id in ids {
|
|
|
|
specs.insert(*id, (level, src));
|
|
|
|
}
|
|
|
|
}
|
2017-08-13 12:37:24 +00:00
|
|
|
|
2018-07-30 09:29:23 +00:00
|
|
|
CheckLintNameResult::Tool(result) => {
|
2018-08-27 21:25:31 +00:00
|
|
|
match result {
|
|
|
|
Ok(ids) => {
|
|
|
|
let complete_name = &format!("{}::{}", tool_name.unwrap(), name);
|
2018-09-30 00:25:26 +00:00
|
|
|
let src = LintSource::Node(
|
2019-12-22 22:42:04 +00:00
|
|
|
Symbol::intern(complete_name),
|
|
|
|
li.span(),
|
|
|
|
reason,
|
2018-09-30 00:25:26 +00:00
|
|
|
);
|
2018-08-27 21:25:31 +00:00
|
|
|
for id in ids {
|
|
|
|
specs.insert(*id, (level, src));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err((Some(ids), new_lint_name)) => {
|
|
|
|
let lint = builtin::RENAMED_AND_REMOVED_LINTS;
|
|
|
|
let (lvl, src) =
|
2019-12-22 22:42:04 +00:00
|
|
|
self.sets.get_lint_level(lint, self.cur, Some(&specs), &sess);
|
2018-08-29 22:12:47 +00:00
|
|
|
let msg = format!(
|
|
|
|
"lint name `{}` is deprecated \
|
2018-08-30 08:28:18 +00:00
|
|
|
and may not have an effect in the future. \
|
2018-08-29 22:12:47 +00:00
|
|
|
Also `cfg_attr(cargo-clippy)` won't be necessary anymore",
|
|
|
|
name
|
|
|
|
);
|
2019-05-22 00:47:23 +00:00
|
|
|
lint::struct_lint_level(
|
2018-08-27 21:25:31 +00:00
|
|
|
self.sess,
|
|
|
|
lint,
|
|
|
|
lvl,
|
|
|
|
src,
|
2019-03-03 17:56:24 +00:00
|
|
|
Some(li.span().into()),
|
2018-08-29 22:12:47 +00:00
|
|
|
&msg,
|
2019-12-22 22:42:04 +00:00
|
|
|
)
|
|
|
|
.span_suggestion(
|
2019-03-03 17:56:24 +00:00
|
|
|
li.span(),
|
2018-08-27 21:25:31 +00:00
|
|
|
"change it to",
|
|
|
|
new_lint_name.to_string(),
|
|
|
|
Applicability::MachineApplicable,
|
2019-12-22 22:42:04 +00:00
|
|
|
)
|
|
|
|
.emit();
|
2018-08-29 22:12:47 +00:00
|
|
|
|
2018-09-30 00:25:26 +00:00
|
|
|
let src = LintSource::Node(
|
2019-12-22 22:42:04 +00:00
|
|
|
Symbol::intern(&new_lint_name),
|
|
|
|
li.span(),
|
|
|
|
reason,
|
2018-09-30 00:25:26 +00:00
|
|
|
);
|
2018-08-27 21:25:31 +00:00
|
|
|
for id in ids {
|
|
|
|
specs.insert(*id, (level, src));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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.
|
2018-07-30 09:29:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-13 12:37:24 +00:00
|
|
|
_ if !self.warn_about_weird_lints => {}
|
|
|
|
|
2018-07-15 03:52:40 +00:00
|
|
|
CheckLintNameResult::Warning(msg, renamed) => {
|
2017-08-13 12:37:24 +00:00
|
|
|
let lint = builtin::RENAMED_AND_REMOVED_LINTS;
|
2019-12-22 22:42:04 +00:00
|
|
|
let (level, src) =
|
|
|
|
self.sets.get_lint_level(lint, self.cur, Some(&specs), &sess);
|
|
|
|
let mut err = lint::struct_lint_level(
|
|
|
|
self.sess,
|
|
|
|
lint,
|
|
|
|
level,
|
|
|
|
src,
|
|
|
|
Some(li.span().into()),
|
|
|
|
&msg,
|
|
|
|
);
|
2018-07-15 03:52:40 +00:00
|
|
|
if let Some(new_name) = renamed {
|
2019-01-25 21:03:27 +00:00
|
|
|
err.span_suggestion(
|
2019-03-03 17:56:24 +00:00
|
|
|
li.span(),
|
2018-07-15 03:52:40 +00:00
|
|
|
"use the new name",
|
|
|
|
new_name,
|
2019-12-22 22:42:04 +00:00
|
|
|
Applicability::MachineApplicable,
|
2018-07-15 03:52:40 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
err.emit();
|
2017-07-27 04:51:09 +00:00
|
|
|
}
|
2018-12-19 18:51:52 +00:00
|
|
|
CheckLintNameResult::NoLint(suggestion) => {
|
2017-08-13 12:37:24 +00:00
|
|
|
let lint = builtin::UNKNOWN_LINTS;
|
2019-12-22 22:42:04 +00:00
|
|
|
let (level, src) =
|
|
|
|
self.sets.get_lint_level(lint, self.cur, Some(&specs), self.sess);
|
2017-08-13 12:37:24 +00:00
|
|
|
let msg = format!("unknown lint: `{}`", name);
|
2019-12-22 22:42:04 +00:00
|
|
|
let mut db = lint::struct_lint_level(
|
|
|
|
self.sess,
|
|
|
|
lint,
|
|
|
|
level,
|
|
|
|
src,
|
|
|
|
Some(li.span().into()),
|
|
|
|
&msg,
|
|
|
|
);
|
2018-12-19 18:51:52 +00:00
|
|
|
|
|
|
|
if let Some(suggestion) = suggestion {
|
2019-01-25 21:03:27 +00:00
|
|
|
db.span_suggestion(
|
2019-03-03 17:56:24 +00:00
|
|
|
li.span(),
|
2018-12-19 18:51:52 +00:00
|
|
|
"did you mean",
|
|
|
|
suggestion.to_string(),
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
2017-08-26 20:18:04 +00:00
|
|
|
}
|
2018-12-19 18:51:52 +00:00
|
|
|
|
|
|
|
db.emit();
|
2017-07-27 04:51:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (id, &(level, ref src)) in specs.iter() {
|
|
|
|
if level == Level::Forbid {
|
2019-12-22 22:42:04 +00:00
|
|
|
continue;
|
2017-07-27 04:51:09 +00:00
|
|
|
}
|
2017-08-13 12:37:24 +00:00
|
|
|
let forbid_src = match self.sets.get_lint_id_level(*id, self.cur, None) {
|
2017-07-27 04:51:09 +00:00
|
|
|
(Some(Level::Forbid), src) => src,
|
|
|
|
_ => continue,
|
|
|
|
};
|
|
|
|
let forbidden_lint_name = match forbid_src {
|
|
|
|
LintSource::Default => id.to_string(),
|
2018-09-30 00:25:26 +00:00
|
|
|
LintSource::Node(name, _, _) => name.to_string(),
|
2017-07-27 04:51:09 +00:00
|
|
|
LintSource::CommandLine(name) => name.to_string(),
|
|
|
|
};
|
|
|
|
let (lint_attr_name, lint_attr_span) = match *src {
|
2018-09-30 00:25:26 +00:00
|
|
|
LintSource::Node(name, span, _) => (name, span),
|
2017-07-27 04:51:09 +00:00
|
|
|
_ => continue,
|
|
|
|
};
|
2019-12-22 22:42:04 +00:00
|
|
|
let mut diag_builder = struct_span_err!(
|
|
|
|
self.sess,
|
|
|
|
lint_attr_span,
|
|
|
|
E0453,
|
|
|
|
"{}({}) overruled by outer forbid({})",
|
|
|
|
level.as_str(),
|
|
|
|
lint_attr_name,
|
|
|
|
forbidden_lint_name
|
|
|
|
);
|
2017-07-27 04:51:09 +00:00
|
|
|
diag_builder.span_label(lint_attr_span, "overruled by previous forbid");
|
|
|
|
match forbid_src {
|
2019-12-22 22:42:04 +00:00
|
|
|
LintSource::Default => {}
|
2018-09-30 00:25:26 +00:00
|
|
|
LintSource::Node(_, forbid_source_span, reason) => {
|
2019-12-22 22:42:04 +00:00
|
|
|
diag_builder.span_label(forbid_source_span, "`forbid` level set here");
|
2018-09-30 00:25:26 +00:00
|
|
|
if let Some(rationale) = reason {
|
|
|
|
diag_builder.note(&rationale.as_str());
|
|
|
|
}
|
2019-12-22 22:42:04 +00:00
|
|
|
}
|
2017-07-27 04:51:09 +00:00
|
|
|
LintSource::CommandLine(_) => {
|
2018-09-30 00:25:26 +00:00
|
|
|
diag_builder.note("`forbid` lint level was set on command line");
|
2017-07-27 04:51:09 +00:00
|
|
|
}
|
2018-09-30 00:25:26 +00:00
|
|
|
}
|
|
|
|
diag_builder.emit();
|
2017-07-27 04:51:09 +00:00
|
|
|
// don't set a separate error for every lint in the group
|
2019-12-22 22:42:04 +00:00
|
|
|
break;
|
2017-07-27 04:51:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let prev = self.cur;
|
|
|
|
if specs.len() > 0 {
|
|
|
|
self.cur = self.sets.list.len() as u32;
|
2019-12-22 22:42:04 +00:00
|
|
|
self.sets.list.push(LintSet::Node { specs: specs, parent: prev });
|
2017-07-27 04:51:09 +00:00
|
|
|
}
|
|
|
|
|
2019-12-22 22:42:04 +00:00
|
|
|
BuilderPush { prev: prev, changed: prev != self.cur }
|
2017-07-27 04:51:09 +00: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Used to emit a lint-related diagnostic based on the current state of
|
|
|
|
/// this lint context.
|
2019-12-22 22:42:04 +00:00
|
|
|
pub fn struct_lint(
|
|
|
|
&self,
|
|
|
|
lint: &'static Lint,
|
|
|
|
span: Option<MultiSpan>,
|
|
|
|
msg: &str,
|
|
|
|
) -> DiagnosticBuilder<'a> {
|
2018-02-23 00:51:42 +00:00
|
|
|
let (level, src) = self.sets.get_lint_level(lint, self.cur, None, self.sess);
|
2017-07-27 04:51:09 +00:00
|
|
|
lint::struct_lint_level(self.sess, lint, level, src, span, msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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 {
|
2019-12-22 22:42:04 +00:00
|
|
|
LintLevelMap { sets: self.sets, id_to_set: self.id_to_set }
|
2017-07-27 04:51:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct LintLevelMap {
|
|
|
|
sets: LintLevelSets,
|
|
|
|
id_to_set: FxHashMap<HirId, u32>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LintLevelMap {
|
|
|
|
/// If the `id` was previously registered with `register_id` when building
|
|
|
|
/// this `LintLevelMap` this returns the corresponding lint level and source
|
|
|
|
/// of the lint level for the lint provided.
|
|
|
|
///
|
|
|
|
/// If the `id` was not previously registered, returns `None`. If `None` is
|
|
|
|
/// returned then the parent of `id` should be acquired and this function
|
|
|
|
/// should be called again.
|
2019-12-22 22:42:04 +00:00
|
|
|
pub fn level_and_source(
|
|
|
|
&self,
|
|
|
|
lint: &'static Lint,
|
|
|
|
id: HirId,
|
|
|
|
session: &Session,
|
2020-01-09 04:20:28 +00:00
|
|
|
) -> Option<LevelSource> {
|
2019-12-22 22:42:04 +00:00
|
|
|
self.id_to_set.get(&id).map(|idx| self.sets.get_lint_level(lint, *idx, None, session))
|
2017-07-27 04:51:09 +00:00
|
|
|
}
|
|
|
|
}
|
2017-08-14 16:19:42 +00:00
|
|
|
|
2018-01-16 09:16:38 +00:00
|
|
|
impl<'a> HashStable<StableHashingContext<'a>> for LintLevelMap {
|
2017-08-14 16:19:42 +00:00
|
|
|
#[inline]
|
2019-09-26 22:54:39 +00:00
|
|
|
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
|
2019-12-22 22:42:04 +00:00
|
|
|
let LintLevelMap { ref sets, ref id_to_set } = *self;
|
2017-08-14 16:19:42 +00:00
|
|
|
|
2017-09-13 16:20:27 +00:00
|
|
|
id_to_set.hash_stable(hcx, hasher);
|
2017-08-14 16:19:42 +00:00
|
|
|
|
2019-12-22 22:42:04 +00:00
|
|
|
let LintLevelSets { ref list, lint_cap } = *sets;
|
2017-08-14 16:19:42 +00:00
|
|
|
|
|
|
|
lint_cap.hash_stable(hcx, hasher);
|
|
|
|
|
|
|
|
hcx.while_hashing_spans(true, |hcx| {
|
|
|
|
list.len().hash_stable(hcx, hasher);
|
|
|
|
|
|
|
|
// We are working under the assumption here that the list of
|
|
|
|
// lint-sets is built in a deterministic order.
|
|
|
|
for lint_set in list {
|
|
|
|
::std::mem::discriminant(lint_set).hash_stable(hcx, hasher);
|
|
|
|
|
|
|
|
match *lint_set {
|
|
|
|
LintSet::CommandLine { ref specs } => {
|
2017-09-13 16:20:27 +00:00
|
|
|
specs.hash_stable(hcx, hasher);
|
2017-08-14 16:19:42 +00:00
|
|
|
}
|
|
|
|
LintSet::Node { ref specs, parent } => {
|
2017-09-13 16:20:27 +00:00
|
|
|
specs.hash_stable(hcx, hasher);
|
2017-08-14 16:19:42 +00:00
|
|
|
parent.hash_stable(hcx, hasher);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|