1 use crate::context::{CheckLintNameResult, LintStore};
2 use crate::late::unerased_lint_store;
3 use rustc::hir::map::Map;
4 use rustc::lint::struct_lint_level;
5 use rustc::lint::{LintLevelMap, LintLevelSets, LintSet, LintSource};
6 use rustc::ty::query::Providers;
8 use rustc_data_structures::fx::FxHashMap;
9 use rustc_error_codes::*;
10 use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder};
12 use rustc_hir::def_id::{CrateNum, LOCAL_CRATE};
13 use rustc_hir::hir_id::HirId;
14 use rustc_hir::intravisit;
15 use rustc_session::lint::{builtin, Level, Lint};
16 use rustc_session::Session;
17 use rustc_span::{sym, MultiSpan, Symbol};
20 use syntax::print::pprust;
21 use syntax::sess::feature_err;
22 use syntax::unwrap_or;
26 fn lint_levels(tcx: TyCtxt<'_>, cnum: CrateNum) -> &LintLevelMap {
27 assert_eq!(cnum, LOCAL_CRATE);
28 let store = unerased_lint_store(tcx);
29 let levels = LintLevelsBuilder::new(tcx.sess, false, &store);
30 let mut builder = LintLevelMapBuilder { levels, tcx, store };
31 let krate = tcx.hir().krate();
33 let push = builder.levels.push(&krate.attrs, &store);
34 builder.levels.register_id(hir::CRATE_HIR_ID);
35 for macro_def in krate.exported_macros {
36 builder.levels.register_id(macro_def.hir_id);
38 intravisit::walk_crate(&mut builder, krate);
39 builder.levels.pop(push);
41 tcx.arena.alloc(builder.levels.build_map())
44 pub struct LintLevelsBuilder<'a> {
47 id_to_set: FxHashMap<HirId, u32>,
49 warn_about_weird_lints: bool,
52 pub struct BuilderPush {
57 impl<'a> LintLevelsBuilder<'a> {
58 pub fn new(sess: &'a Session, warn_about_weird_lints: bool, store: &LintStore) -> Self {
59 let mut builder = LintLevelsBuilder {
61 sets: LintLevelSets::new(),
63 id_to_set: Default::default(),
64 warn_about_weird_lints,
66 builder.process_command_line(sess, store);
67 assert_eq!(builder.sets.list.len(), 1);
71 fn process_command_line(&mut self, sess: &Session, store: &LintStore) {
72 let mut specs = FxHashMap::default();
73 self.sets.lint_cap = sess.opts.lint_cap.unwrap_or(Level::Forbid);
75 for &(ref lint_name, level) in &sess.opts.lint_opts {
76 store.check_lint_name_cmdline(sess, &lint_name, level);
78 // If the cap is less than this specified level, e.g., if we've got
79 // `--cap-lints allow` but we've also got `-D foo` then we ignore
80 // this specification as the lint cap will set it to allow anyway.
81 let level = cmp::min(level, self.sets.lint_cap);
83 let lint_flag_val = Symbol::intern(lint_name);
84 let ids = match store.find_lints(&lint_name) {
86 Err(_) => continue, // errors handled in check_lint_name_cmdline above
89 let src = LintSource::CommandLine(lint_flag_val);
90 specs.insert(id, (level, src));
94 self.sets.list.push(LintSet::CommandLine { specs });
97 /// Pushes a list of AST lint attributes onto this context.
99 /// This function will return a `BuilderPush` object which should be passed
100 /// to `pop` when this scope for the attributes provided is exited.
102 /// This function will perform a number of tasks:
104 /// * It'll validate all lint-related attributes in `attrs`
105 /// * It'll mark all lint-related attributes as used
106 /// * Lint levels will be updated based on the attributes provided
107 /// * Lint attributes are validated, e.g., a #[forbid] can't be switched to
110 /// Don't forget to call `pop`!
111 pub fn push(&mut self, attrs: &[ast::Attribute], store: &LintStore) -> BuilderPush {
112 let mut specs = FxHashMap::default();
113 let sess = self.sess;
114 let bad_attr = |span| struct_span_err!(sess, span, E0452, "malformed lint attribute input");
116 let level = match Level::from_symbol(attr.name_or_empty()) {
121 let meta = unwrap_or!(attr.meta(), continue);
122 attr::mark_used(attr);
124 let mut metas = unwrap_or!(meta.meta_item_list(), continue);
126 if metas.is_empty() {
127 // FIXME (#55112): issue unused-attributes lint for `#[level()]`
131 // Before processing the lint names, look for a reason (RFC 2383)
133 let mut reason = None;
134 let tail_li = &metas[metas.len() - 1];
135 if let Some(item) = tail_li.meta_item() {
137 ast::MetaItemKind::Word => {} // actual lint names handled later
138 ast::MetaItemKind::NameValue(ref name_value) => {
139 if item.path == sym::reason {
140 // found reason, reslice meta list to exclude it
141 metas = &metas[0..metas.len() - 1];
142 // FIXME (#55112): issue unused-attributes lint if we thereby
143 // don't have any lint names (`#[level(reason = "foo")]`)
144 if let ast::LitKind::Str(rationale, _) = name_value.kind {
145 if !self.sess.features_untracked().lint_reasons {
147 &self.sess.parse_sess,
150 "lint reasons are experimental",
154 reason = Some(rationale);
156 bad_attr(name_value.span)
157 .span_label(name_value.span, "reason must be a string literal")
162 .span_label(item.span, "bad attribute argument")
166 ast::MetaItemKind::List(_) => {
167 bad_attr(item.span).span_label(item.span, "bad attribute argument").emit();
173 let meta_item = match li.meta_item() {
174 Some(meta_item) if meta_item.is_word() => meta_item,
177 let mut err = bad_attr(sp);
178 let mut add_label = true;
179 if let Some(item) = li.meta_item() {
180 if let ast::MetaItemKind::NameValue(_) = item.kind {
181 if item.path == sym::reason {
182 err.span_label(sp, "reason in lint attribute must come last");
188 err.span_label(sp, "bad attribute argument");
194 let tool_name = if meta_item.path.segments.len() > 1 {
195 let tool_ident = meta_item.path.segments[0].ident;
196 if !attr::is_known_lint_tool(tool_ident) {
201 "an unknown tool name found in scoped lint: `{}`",
202 pprust::path_to_string(&meta_item.path),
208 Some(tool_ident.name)
212 let name = meta_item.path.segments.last().expect("empty lint name").ident.name;
213 match store.check_lint_name(&name.as_str(), tool_name) {
214 CheckLintNameResult::Ok(ids) => {
215 let src = LintSource::Node(name, li.span(), reason);
217 specs.insert(*id, (level, src));
221 CheckLintNameResult::Tool(result) => {
224 let complete_name = &format!("{}::{}", tool_name.unwrap(), name);
225 let src = LintSource::Node(
226 Symbol::intern(complete_name),
231 specs.insert(*id, (level, src));
234 Err((Some(ids), new_lint_name)) => {
235 let lint = builtin::RENAMED_AND_REMOVED_LINTS;
237 self.sets.get_lint_level(lint, self.cur, Some(&specs), &sess);
239 "lint name `{}` is deprecated \
240 and may not have an effect in the future. \
241 Also `cfg_attr(cargo-clippy)` won't be necessary anymore",
249 Some(li.span().into()),
255 new_lint_name.to_string(),
256 Applicability::MachineApplicable,
260 let src = LintSource::Node(
261 Symbol::intern(&new_lint_name),
266 specs.insert(*id, (level, src));
270 // If Tool(Err(None, _)) is returned, then either the lint does not
271 // exist in the tool or the code was not compiled with the tool and
272 // therefore the lint was never added to the `LintStore`. To detect
273 // this is the responsibility of the lint tool.
278 _ if !self.warn_about_weird_lints => {}
280 CheckLintNameResult::Warning(msg, renamed) => {
281 let lint = builtin::RENAMED_AND_REMOVED_LINTS;
283 self.sets.get_lint_level(lint, self.cur, Some(&specs), &sess);
284 let mut err = struct_lint_level(
289 Some(li.span().into()),
292 if let Some(new_name) = renamed {
297 Applicability::MachineApplicable,
302 CheckLintNameResult::NoLint(suggestion) => {
303 let lint = builtin::UNKNOWN_LINTS;
305 self.sets.get_lint_level(lint, self.cur, Some(&specs), self.sess);
306 let msg = format!("unknown lint: `{}`", name);
307 let mut db = struct_lint_level(
312 Some(li.span().into()),
316 if let Some(suggestion) = suggestion {
320 suggestion.to_string(),
321 Applicability::MachineApplicable,
331 for (id, &(level, ref src)) in specs.iter() {
332 if level == Level::Forbid {
335 let forbid_src = match self.sets.get_lint_id_level(*id, self.cur, None) {
336 (Some(Level::Forbid), src) => src,
339 let forbidden_lint_name = match forbid_src {
340 LintSource::Default => id.to_string(),
341 LintSource::Node(name, _, _) => name.to_string(),
342 LintSource::CommandLine(name) => name.to_string(),
344 let (lint_attr_name, lint_attr_span) = match *src {
345 LintSource::Node(name, span, _) => (name, span),
348 let mut diag_builder = struct_span_err!(
352 "{}({}) overruled by outer forbid({})",
357 diag_builder.span_label(lint_attr_span, "overruled by previous forbid");
359 LintSource::Default => {}
360 LintSource::Node(_, forbid_source_span, reason) => {
361 diag_builder.span_label(forbid_source_span, "`forbid` level set here");
362 if let Some(rationale) = reason {
363 diag_builder.note(&rationale.as_str());
366 LintSource::CommandLine(_) => {
367 diag_builder.note("`forbid` lint level was set on command line");
371 // don't set a separate error for every lint in the group
377 self.cur = self.sets.list.len() as u32;
378 self.sets.list.push(LintSet::Node { specs: specs, parent: prev });
381 BuilderPush { prev: prev, changed: prev != self.cur }
384 /// Called after `push` when the scope of a set of attributes are exited.
385 pub fn pop(&mut self, push: BuilderPush) {
386 self.cur = push.prev;
389 /// Used to emit a lint-related diagnostic based on the current state of
390 /// this lint context.
394 span: Option<MultiSpan>,
396 ) -> DiagnosticBuilder<'a> {
397 let (level, src) = self.sets.get_lint_level(lint, self.cur, None, self.sess);
398 struct_lint_level(self.sess, lint, level, src, span, msg)
401 /// Registers the ID provided with the current set of lints stored in
403 pub fn register_id(&mut self, id: HirId) {
404 self.id_to_set.insert(id, self.cur);
407 pub fn build(self) -> LintLevelSets {
411 pub fn build_map(self) -> LintLevelMap {
412 LintLevelMap { sets: self.sets, id_to_set: self.id_to_set }
416 struct LintLevelMapBuilder<'a, 'tcx> {
417 levels: LintLevelsBuilder<'tcx>,
419 store: &'a LintStore,
422 impl LintLevelMapBuilder<'_, '_> {
423 fn with_lint_attrs<F>(&mut self, id: hir::HirId, attrs: &[ast::Attribute], f: F)
425 F: FnOnce(&mut Self),
427 let push = self.levels.push(attrs, self.store);
429 self.levels.register_id(id);
432 self.levels.pop(push);
436 impl<'tcx> intravisit::Visitor<'tcx> for LintLevelMapBuilder<'_, 'tcx> {
437 type Map = Map<'tcx>;
439 fn nested_visit_map<'this>(&'this mut self) -> intravisit::NestedVisitorMap<'this, Self::Map> {
440 intravisit::NestedVisitorMap::All(&self.tcx.hir())
443 fn visit_param(&mut self, param: &'tcx hir::Param<'tcx>) {
444 self.with_lint_attrs(param.hir_id, ¶m.attrs, |builder| {
445 intravisit::walk_param(builder, param);
449 fn visit_item(&mut self, it: &'tcx hir::Item<'tcx>) {
450 self.with_lint_attrs(it.hir_id, &it.attrs, |builder| {
451 intravisit::walk_item(builder, it);
455 fn visit_foreign_item(&mut self, it: &'tcx hir::ForeignItem<'tcx>) {
456 self.with_lint_attrs(it.hir_id, &it.attrs, |builder| {
457 intravisit::walk_foreign_item(builder, it);
461 fn visit_expr(&mut self, e: &'tcx hir::Expr<'tcx>) {
462 self.with_lint_attrs(e.hir_id, &e.attrs, |builder| {
463 intravisit::walk_expr(builder, e);
467 fn visit_struct_field(&mut self, s: &'tcx hir::StructField<'tcx>) {
468 self.with_lint_attrs(s.hir_id, &s.attrs, |builder| {
469 intravisit::walk_struct_field(builder, s);
475 v: &'tcx hir::Variant<'tcx>,
476 g: &'tcx hir::Generics<'tcx>,
479 self.with_lint_attrs(v.id, &v.attrs, |builder| {
480 intravisit::walk_variant(builder, v, g, item_id);
484 fn visit_local(&mut self, l: &'tcx hir::Local<'tcx>) {
485 self.with_lint_attrs(l.hir_id, &l.attrs, |builder| {
486 intravisit::walk_local(builder, l);
490 fn visit_arm(&mut self, a: &'tcx hir::Arm<'tcx>) {
491 self.with_lint_attrs(a.hir_id, &a.attrs, |builder| {
492 intravisit::walk_arm(builder, a);
496 fn visit_trait_item(&mut self, trait_item: &'tcx hir::TraitItem<'tcx>) {
497 self.with_lint_attrs(trait_item.hir_id, &trait_item.attrs, |builder| {
498 intravisit::walk_trait_item(builder, trait_item);
502 fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem<'tcx>) {
503 self.with_lint_attrs(impl_item.hir_id, &impl_item.attrs, |builder| {
504 intravisit::walk_impl_item(builder, impl_item);
509 pub fn provide(providers: &mut Providers<'_>) {
510 providers.lint_levels = lint_levels;