2017-07-27 04:51:09 +00:00
|
|
|
use std::cmp;
|
|
|
|
|
2019-02-05 17:20:45 +00:00
|
|
|
use crate::hir::HirId;
|
|
|
|
use crate::ich::StableHashingContext;
|
|
|
|
use crate::lint::builtin;
|
|
|
|
use crate::lint::context::CheckLintNameResult;
|
|
|
|
use crate::lint::{self, Lint, LintId, Level, LintSource};
|
2019-02-09 02:24:02 +00:00
|
|
|
use crate::session::Session;
|
|
|
|
use crate::util::nodemap::FxHashMap;
|
|
|
|
use errors::{Applicability, DiagnosticBuilder};
|
2017-09-13 16:20:27 +00:00
|
|
|
use rustc_data_structures::stable_hasher::{HashStable, ToStableHashKey,
|
|
|
|
StableHasher, StableHasherResult};
|
2017-07-27 04:51:09 +00:00
|
|
|
use syntax::ast;
|
|
|
|
use syntax::attr;
|
2018-10-12 07:21:21 +00:00
|
|
|
use syntax::feature_gate;
|
2018-08-18 10:14:03 +00:00
|
|
|
use syntax::source_map::MultiSpan;
|
2019-05-08 03:21:18 +00:00
|
|
|
use syntax::symbol::{Symbol, sym};
|
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.
|
|
|
|
specs: FxHashMap<LintId, (Level, LintSource)>,
|
|
|
|
},
|
|
|
|
|
|
|
|
Node {
|
|
|
|
specs: FxHashMap<LintId, (Level, LintSource)>,
|
|
|
|
parent: u32,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LintLevelSets {
|
|
|
|
pub fn new(sess: &Session) -> LintLevelSets {
|
|
|
|
let mut me = LintLevelSets {
|
|
|
|
list: Vec::new(),
|
|
|
|
lint_cap: Level::Forbid,
|
|
|
|
};
|
|
|
|
me.process_command_line(sess);
|
|
|
|
return me
|
|
|
|
}
|
|
|
|
|
2018-08-30 05:02:42 +00:00
|
|
|
pub fn builder(sess: &Session) -> LintLevelsBuilder<'_> {
|
2017-07-27 04:51:09 +00:00
|
|
|
LintLevelsBuilder::new(sess, LintLevelSets::new(sess))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn process_command_line(&mut self, sess: &Session) {
|
|
|
|
let store = sess.lint_store.borrow();
|
2018-10-16 08:44:26 +00:00
|
|
|
let mut specs = FxHashMap::default();
|
2017-07-27 04:51:09 +00:00
|
|
|
self.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);
|
|
|
|
|
2018-11-27 02:59:49 +00:00
|
|
|
// If the cap is less than this specified level, e.g., if we've got
|
2017-07-27 04:51:09 +00:00
|
|
|
// `--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.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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.list.push(LintSet::CommandLine {
|
|
|
|
specs: specs,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-08-13 12:37:24 +00:00
|
|
|
fn get_lint_level(&self,
|
|
|
|
lint: &'static Lint,
|
|
|
|
idx: u32,
|
2018-02-23 00:51:42 +00:00
|
|
|
aux: Option<&FxHashMap<LintId, (Level, LintSource)>>,
|
|
|
|
sess: &Session)
|
2017-07-27 04:51:09 +00:00
|
|
|
-> (Level, LintSource)
|
|
|
|
{
|
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.
|
2018-10-12 14:16:00 +00:00
|
|
|
let mut level = level.unwrap_or_else(|| lint.default_level(sess));
|
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) =
|
2017-08-13 12:37:24 +00:00
|
|
|
self.get_lint_id_level(LintId::of(lint::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);
|
|
|
|
}
|
|
|
|
|
2017-07-27 04:51:09 +00:00
|
|
|
return (level, src)
|
|
|
|
}
|
|
|
|
|
2017-08-13 12:37:24 +00:00
|
|
|
fn get_lint_id_level(&self,
|
|
|
|
id: LintId,
|
|
|
|
mut idx: u32,
|
|
|
|
aux: Option<&FxHashMap<LintId, (Level, LintSource)>>)
|
2017-07-27 04:51:09 +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) {
|
|
|
|
return (Some(level), src)
|
|
|
|
}
|
|
|
|
}
|
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) {
|
|
|
|
return (Some(level), src)
|
|
|
|
}
|
|
|
|
return (None, LintSource::Default)
|
|
|
|
}
|
|
|
|
LintSet::Node { ref specs, parent } => {
|
|
|
|
if let Some(&(level, src)) = specs.get(&id) {
|
|
|
|
return (Some(level), src)
|
|
|
|
}
|
|
|
|
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-01-31 22:11:29 +00:00
|
|
|
pub(super) changed: bool,
|
2017-07-27 04:51:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> LintLevelsBuilder<'a> {
|
|
|
|
pub fn new(sess: &'a Session, sets: LintLevelSets) -> LintLevelsBuilder<'a> {
|
|
|
|
assert_eq!(sets.list.len(), 1);
|
|
|
|
LintLevelsBuilder {
|
|
|
|
sess,
|
|
|
|
sets,
|
|
|
|
cur: 0,
|
2018-10-16 14:57:53 +00:00
|
|
|
id_to_set: Default::default(),
|
2017-07-27 04:51:09 +00:00
|
|
|
warn_about_weird_lints: sess.buffered_lints.borrow().is_some(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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`!
|
|
|
|
pub fn push(&mut self, attrs: &[ast::Attribute]) -> BuilderPush {
|
2018-10-16 08:44:26 +00:00
|
|
|
let mut specs = FxHashMap::default();
|
2017-07-27 04:51:09 +00:00
|
|
|
let store = self.sess.lint_store.borrow();
|
|
|
|
let sess = self.sess;
|
|
|
|
let bad_attr = |span| {
|
2019-05-22 00:47:23 +00:00
|
|
|
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);
|
|
|
|
|
2018-10-12 07:21:21 +00:00
|
|
|
let mut metas = if let Some(metas) = meta.meta_item_list() {
|
2017-07-27 04:51:09 +00:00
|
|
|
metas
|
|
|
|
} else {
|
2018-10-16 06:35:58 +00:00
|
|
|
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;
|
2018-10-12 07:21:21 +00:00
|
|
|
let tail_li = &metas[metas.len()-1];
|
|
|
|
if let Some(item) = tail_li.meta_item() {
|
|
|
|
match item.node {
|
|
|
|
ast::MetaItemKind::Word => {} // actual lint names handled later
|
|
|
|
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
|
|
|
|
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 {
|
2018-10-12 07:21:21 +00:00
|
|
|
feature_gate::emit_feature_err(
|
|
|
|
&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,
|
|
|
|
feature_gate::GateIssue::Language,
|
|
|
|
"lint reasons are experimental"
|
|
|
|
);
|
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
|
|
|
}
|
2018-10-12 07:21:21 +00:00
|
|
|
},
|
|
|
|
ast::MetaItemKind::List(_) => {
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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() {
|
|
|
|
if let ast::MetaItemKind::NameValue(_) = item.node {
|
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) {
|
2018-07-03 11:50:48 +00:00
|
|
|
span_err!(
|
|
|
|
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-03-02 16:15:26 +00:00
|
|
|
meta_item.path
|
2018-07-03 11:50:48 +00:00
|
|
|
);
|
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-03-03 17:56:24 +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) =
|
|
|
|
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-05-22 00:47:23 +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,
|
2018-10-02 16:15:13 +00:00
|
|
|
).emit();
|
2018-08-29 22:12:47 +00:00
|
|
|
|
2018-09-30 00:25:26 +00:00
|
|
|
let src = LintSource::Node(
|
2019-03-03 17:56:24 +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;
|
|
|
|
let (level, src) = self.sets.get_lint_level(lint,
|
|
|
|
self.cur,
|
2018-02-23 00:51:42 +00:00
|
|
|
Some(&specs),
|
|
|
|
&sess);
|
2018-07-15 03:52:40 +00:00
|
|
|
let mut err = lint::struct_lint_level(self.sess,
|
|
|
|
lint,
|
|
|
|
level,
|
|
|
|
src,
|
2019-03-03 17:56:24 +00:00
|
|
|
Some(li.span().into()),
|
2018-07-15 03:52:40 +00:00
|
|
|
&msg);
|
|
|
|
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,
|
|
|
|
Applicability::MachineApplicable
|
|
|
|
);
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
let (level, src) = self.sets.get_lint_level(lint,
|
|
|
|
self.cur,
|
2018-02-23 00:51:42 +00:00
|
|
|
Some(&specs),
|
|
|
|
self.sess);
|
2017-08-13 12:37:24 +00:00
|
|
|
let msg = format!("unknown lint: `{}`", name);
|
2017-08-26 20:18:04 +00:00
|
|
|
let mut db = lint::struct_lint_level(self.sess,
|
2017-08-13 12:37:24 +00:00
|
|
|
lint,
|
|
|
|
level,
|
|
|
|
src,
|
2019-03-03 17:56:24 +00:00
|
|
|
Some(li.span().into()),
|
2017-08-26 20:18:04 +00:00
|
|
|
&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 {
|
|
|
|
continue
|
|
|
|
}
|
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,
|
|
|
|
};
|
|
|
|
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);
|
|
|
|
diag_builder.span_label(lint_attr_span, "overruled by previous forbid");
|
|
|
|
match forbid_src {
|
2018-09-30 00:25:26 +00:00
|
|
|
LintSource::Default => {},
|
|
|
|
LintSource::Node(_, forbid_source_span, reason) => {
|
2017-07-27 04:51:09 +00:00
|
|
|
diag_builder.span_label(forbid_source_span,
|
2018-09-30 00:25:26 +00:00
|
|
|
"`forbid` level set here");
|
|
|
|
if let Some(rationale) = reason {
|
|
|
|
diag_builder.note(&rationale.as_str());
|
|
|
|
}
|
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
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
let prev = self.cur;
|
|
|
|
if specs.len() > 0 {
|
|
|
|
self.cur = self.sets.list.len() as u32;
|
|
|
|
self.sets.list.push(LintSet::Node {
|
|
|
|
specs: specs,
|
|
|
|
parent: prev,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
BuilderPush {
|
|
|
|
prev: prev,
|
2019-01-31 22:11:29 +00:00
|
|
|
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.
|
|
|
|
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 {
|
|
|
|
LintLevelMap {
|
|
|
|
sets: self.sets,
|
|
|
|
id_to_set: self.id_to_set,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2018-02-23 00:51:42 +00:00
|
|
|
pub fn level_and_source(&self, lint: &'static Lint, id: HirId, session: &Session)
|
2017-07-27 04:51:09 +00:00
|
|
|
-> Option<(Level, LintSource)>
|
|
|
|
{
|
|
|
|
self.id_to_set.get(&id).map(|idx| {
|
2018-02-23 00:51:42 +00:00
|
|
|
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]
|
|
|
|
fn hash_stable<W: StableHasherResult>(&self,
|
2018-01-16 09:16:38 +00:00
|
|
|
hcx: &mut StableHashingContext<'a>,
|
2017-08-14 16:19:42 +00:00
|
|
|
hasher: &mut StableHasher<W>) {
|
|
|
|
let LintLevelMap {
|
|
|
|
ref sets,
|
|
|
|
ref id_to_set,
|
|
|
|
} = *self;
|
|
|
|
|
2017-09-13 16:20:27 +00:00
|
|
|
id_to_set.hash_stable(hcx, hasher);
|
2017-08-14 16:19:42 +00:00
|
|
|
|
|
|
|
let LintLevelSets {
|
|
|
|
ref list,
|
|
|
|
lint_cap,
|
|
|
|
} = *sets;
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-13 16:20:27 +00:00
|
|
|
impl<HCX> HashStable<HCX> for LintId {
|
2017-08-14 16:19:42 +00:00
|
|
|
#[inline]
|
|
|
|
fn hash_stable<W: StableHasherResult>(&self,
|
2017-09-13 16:20:27 +00:00
|
|
|
hcx: &mut HCX,
|
2017-08-14 16:19:42 +00:00
|
|
|
hasher: &mut StableHasher<W>) {
|
|
|
|
self.lint_name_raw().hash_stable(hcx, hasher);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-13 16:20:27 +00:00
|
|
|
impl<HCX> ToStableHashKey<HCX> for LintId {
|
|
|
|
type KeyType = &'static str;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn to_stable_hash_key(&self, _: &HCX) -> &'static str {
|
|
|
|
self.lint_name_raw()
|
|
|
|
}
|
|
|
|
}
|