1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! Lints, aka compiler warnings.
13 //! A 'lint' check is a kind of miscellaneous constraint that a user _might_
14 //! want to enforce, but might reasonably want to permit as well, on a
15 //! module-by-module basis. They contrast with static constraints enforced by
16 //! other phases of the compiler, which are generally required to hold in order
17 //! to compile the program at all.
19 //! Most lints can be written as `LintPass` instances. These run after
20 //! all other analyses. The `LintPass`es built into rustc are defined
21 //! within `builtin.rs`, which has further comments on how to add such a lint.
22 //! rustc can also load user-defined lint plugins via the plugin mechanism.
24 //! Some of rustc's lints are defined elsewhere in the compiler and work by
25 //! calling `add_lint()` on the overall `Session` object. This works when
26 //! it happens before the main lint pass, which emits the lints stored by
27 //! `add_lint()`. To emit lints after the main lint pass (from codegen, for
28 //! example) requires more effort. See `emit_lint` and `GatherNodeLevels`
31 pub use self::Level::*;
32 pub use self::LintSource::*;
34 use rustc_data_structures::sync::{self, Lrc};
36 use errors::{DiagnosticBuilder, DiagnosticId};
37 use hir::def_id::{CrateNum, LOCAL_CRATE};
40 use lint::builtin::BuiltinLintDiagnostics;
41 use lint::builtin::parser::QUESTION_MARK_MACRO_SEP;
42 use session::{Session, DiagnosticMessageId};
45 use syntax::codemap::{MultiSpan, ExpnFormat};
46 use syntax::early_buffered_lints::BufferedEarlyLintId;
47 use syntax::edition::Edition;
48 use syntax::symbol::Symbol;
49 use syntax::visit as ast_visit;
52 use ty::query::Providers;
53 use util::nodemap::NodeMap;
55 pub use lint::context::{LateContext, EarlyContext, LintContext, LintStore,
56 check_crate, check_ast_crate,
57 FutureIncompatibleInfo, BufferedEarlyLint};
59 /// Specification of a single lint.
60 #[derive(Copy, Clone, Debug)]
62 /// A string identifier for the lint.
64 /// This identifies the lint in attributes and in command-line arguments.
65 /// In those contexts it is always lowercase, but this field is compared
66 /// in a way which is case-insensitive for ASCII characters. This allows
67 /// `declare_lint!()` invocations to follow the convention of upper-case
68 /// statics without repeating the name.
70 /// The name is written with underscores, e.g. "unused_imports".
71 /// On the command line, underscores become dashes.
72 pub name: &'static str,
74 /// Default level for the lint.
75 pub default_level: Level,
77 /// Description of the lint or the issue it detects.
79 /// e.g. "imports that are never used"
80 pub desc: &'static str,
82 /// Starting at the given edition, default to the given lint level. If this is `None`, then use
84 pub edition_lint_opts: Option<(Edition, Level)>,
86 /// Whether this lint is reported even inside expansions of external macros
87 pub report_in_external_macro: bool,
91 /// Returns the `rust::lint::Lint` for a `syntax::early_buffered_lints::BufferedEarlyLintId`.
92 pub fn from_parser_lint_id(lint_id: BufferedEarlyLintId) -> &'static Self {
94 BufferedEarlyLintId::QuestionMarkMacroSep => QUESTION_MARK_MACRO_SEP,
98 /// Get the lint's name, with ASCII letters converted to lowercase.
99 pub fn name_lower(&self) -> String {
100 self.name.to_ascii_lowercase()
103 pub fn default_level(&self, session: &Session) -> Level {
104 self.edition_lint_opts
105 .filter(|(e, _)| *e <= session.edition())
107 .unwrap_or(self.default_level)
111 /// Declare a static item of type `&'static Lint`.
113 macro_rules! declare_lint {
114 ($vis: vis $NAME: ident, $Level: ident, $desc: expr) => (
115 declare_lint!{$vis $NAME, $Level, $desc, false}
117 ($vis: vis $NAME: ident, $Level: ident, $desc: expr, report_in_external_macro: $rep: expr) => (
118 declare_lint!{$vis $NAME, $Level, $desc, $rep}
120 ($vis: vis $NAME: ident, $Level: ident, $desc: expr, $external: expr) => (
121 $vis static $NAME: &$crate::lint::Lint = &$crate::lint::Lint {
122 name: stringify!($NAME),
123 default_level: $crate::lint::$Level,
125 edition_lint_opts: None,
126 report_in_external_macro: $external,
129 ($vis: vis $NAME: ident, $Level: ident, $desc: expr,
130 $lint_edition: expr => $edition_level: ident
132 $vis static $NAME: &$crate::lint::Lint = &$crate::lint::Lint {
133 name: stringify!($NAME),
134 default_level: $crate::lint::$Level,
136 edition_lint_opts: Some(($lint_edition, $crate::lint::Level::$edition_level)),
137 report_in_external_macro: false,
142 /// Declare a static `LintArray` and return it as an expression.
144 macro_rules! lint_array {
145 ($( $lint:expr ),* ,) => { lint_array!( $($lint),* ) };
146 ($( $lint:expr ),*) => {{
151 pub type LintArray = Vec<&'static Lint>;
154 /// Get descriptions of the lints this `LintPass` object can emit.
156 /// NB: there is no enforcement that the object only emits lints it registered.
157 /// And some `rustc` internal `LintPass`es register lints to be emitted by other
158 /// parts of the compiler. If you want enforced access restrictions for your
159 /// `Lint`, make it a private `static` item in its own module.
160 fn get_lints(&self) -> LintArray;
164 macro_rules! late_lint_methods {
165 ($macro:path, $args:tt, [$hir:tt]) => (
166 $macro!($args, [$hir], [
167 fn check_body(a: &$hir hir::Body);
168 fn check_body_post(a: &$hir hir::Body);
169 fn check_name(a: Span, b: ast::Name);
170 fn check_crate(a: &$hir hir::Crate);
171 fn check_crate_post(a: &$hir hir::Crate);
172 fn check_mod(a: &$hir hir::Mod, b: Span, c: ast::NodeId);
173 fn check_mod_post(a: &$hir hir::Mod, b: Span, c: ast::NodeId);
174 fn check_foreign_item(a: &$hir hir::ForeignItem);
175 fn check_foreign_item_post(a: &$hir hir::ForeignItem);
176 fn check_item(a: &$hir hir::Item);
177 fn check_item_post(a: &$hir hir::Item);
178 fn check_local(a: &$hir hir::Local);
179 fn check_block(a: &$hir hir::Block);
180 fn check_block_post(a: &$hir hir::Block);
181 fn check_stmt(a: &$hir hir::Stmt);
182 fn check_arm(a: &$hir hir::Arm);
183 fn check_pat(a: &$hir hir::Pat);
184 fn check_decl(a: &$hir hir::Decl);
185 fn check_expr(a: &$hir hir::Expr);
186 fn check_expr_post(a: &$hir hir::Expr);
187 fn check_ty(a: &$hir hir::Ty);
188 fn check_generic_param(a: &$hir hir::GenericParam);
189 fn check_generics(a: &$hir hir::Generics);
190 fn check_where_predicate(a: &$hir hir::WherePredicate);
191 fn check_poly_trait_ref(a: &$hir hir::PolyTraitRef, b: hir::TraitBoundModifier);
193 a: hir::intravisit::FnKind<$hir>,
194 b: &$hir hir::FnDecl,
199 a: hir::intravisit::FnKind<$hir>,
200 b: &$hir hir::FnDecl,
205 fn check_trait_item(a: &$hir hir::TraitItem);
206 fn check_trait_item_post(a: &$hir hir::TraitItem);
207 fn check_impl_item(a: &$hir hir::ImplItem);
208 fn check_impl_item_post(a: &$hir hir::ImplItem);
210 a: &$hir hir::VariantData,
212 c: &$hir hir::Generics,
215 fn check_struct_def_post(
216 a: &$hir hir::VariantData,
218 c: &$hir hir::Generics,
221 fn check_struct_field(a: &$hir hir::StructField);
222 fn check_variant(a: &$hir hir::Variant, b: &$hir hir::Generics);
223 fn check_variant_post(a: &$hir hir::Variant, b: &$hir hir::Generics);
224 fn check_lifetime(a: &$hir hir::Lifetime);
225 fn check_path(a: &$hir hir::Path, b: ast::NodeId);
226 fn check_attribute(a: &$hir ast::Attribute);
228 /// Called when entering a syntax node that can have lint attributes such
229 /// as `#[allow(...)]`. Called with *all* the attributes of that node.
230 fn enter_lint_attrs(a: &$hir [ast::Attribute]);
232 /// Counterpart to `enter_lint_attrs`.
233 fn exit_lint_attrs(a: &$hir [ast::Attribute]);
238 /// Trait for types providing lint checks.
240 /// Each `check` method checks a single syntax node, and should not
241 /// invoke methods recursively (unlike `Visitor`). By default they
244 // FIXME: eliminate the duplication with `Visitor`. But this also
245 // contains a few lint-specific methods with no equivalent in `Visitor`.
247 macro_rules! expand_lint_pass_methods {
248 ($context:ty, [$($(#[$attr:meta])* fn $name:ident($($param:ident: $arg:ty),*);)*]) => (
249 $(#[inline(always)] fn $name(&mut self, $context, $(_: $arg),*) {})*
253 macro_rules! declare_late_lint_pass {
254 ([], [$hir:tt], [$($methods:tt)*]) => (
255 pub trait LateLintPass<'a, $hir>: LintPass {
256 expand_lint_pass_methods!(&LateContext<'a, $hir>, [$($methods)*]);
261 late_lint_methods!(declare_late_lint_pass, [], ['tcx]);
264 macro_rules! expand_combined_late_lint_pass_method {
265 ([$($passes:ident),*], $self: ident, $name: ident, $params:tt) => ({
266 $($self.$passes.$name $params;)*
271 macro_rules! expand_combined_late_lint_pass_methods {
272 ($passes:tt, [$($(#[$attr:meta])* fn $name:ident($($param:ident: $arg:ty),*);)*]) => (
273 $(fn $name(&mut self, context: &LateContext<'a, 'tcx>, $($param: $arg),*) {
274 expand_combined_late_lint_pass_method!($passes, self, $name, (context, $($param),*));
280 macro_rules! declare_combined_late_lint_pass {
281 ([$name:ident, [$($passes:ident: $constructor:expr,)*]], [$hir:tt], $methods:tt) => (
282 #[allow(non_snake_case)]
284 $($passes: $passes,)*
290 $($passes: $constructor,)*
295 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for $name {
296 expand_combined_late_lint_pass_methods!([$($passes),*], $methods);
299 impl LintPass for $name {
300 fn get_lints(&self) -> LintArray {
301 let mut lints = Vec::new();
302 $(lints.extend_from_slice(&self.$passes.get_lints());)*
309 pub trait EarlyLintPass: LintPass {
310 fn check_ident(&mut self, _: &EarlyContext, _: ast::Ident) { }
311 fn check_crate(&mut self, _: &EarlyContext, _: &ast::Crate) { }
312 fn check_crate_post(&mut self, _: &EarlyContext, _: &ast::Crate) { }
313 fn check_mod(&mut self, _: &EarlyContext, _: &ast::Mod, _: Span, _: ast::NodeId) { }
314 fn check_mod_post(&mut self, _: &EarlyContext, _: &ast::Mod, _: Span, _: ast::NodeId) { }
315 fn check_foreign_item(&mut self, _: &EarlyContext, _: &ast::ForeignItem) { }
316 fn check_foreign_item_post(&mut self, _: &EarlyContext, _: &ast::ForeignItem) { }
317 fn check_item(&mut self, _: &EarlyContext, _: &ast::Item) { }
318 fn check_item_post(&mut self, _: &EarlyContext, _: &ast::Item) { }
319 fn check_local(&mut self, _: &EarlyContext, _: &ast::Local) { }
320 fn check_block(&mut self, _: &EarlyContext, _: &ast::Block) { }
321 fn check_block_post(&mut self, _: &EarlyContext, _: &ast::Block) { }
322 fn check_stmt(&mut self, _: &EarlyContext, _: &ast::Stmt) { }
323 fn check_arm(&mut self, _: &EarlyContext, _: &ast::Arm) { }
324 fn check_pat(&mut self, _: &EarlyContext, _: &ast::Pat) { }
325 fn check_expr(&mut self, _: &EarlyContext, _: &ast::Expr) { }
326 fn check_expr_post(&mut self, _: &EarlyContext, _: &ast::Expr) { }
327 fn check_ty(&mut self, _: &EarlyContext, _: &ast::Ty) { }
328 fn check_generic_param(&mut self, _: &EarlyContext, _: &ast::GenericParam) { }
329 fn check_generics(&mut self, _: &EarlyContext, _: &ast::Generics) { }
330 fn check_where_predicate(&mut self, _: &EarlyContext, _: &ast::WherePredicate) { }
331 fn check_poly_trait_ref(&mut self, _: &EarlyContext, _: &ast::PolyTraitRef,
332 _: &ast::TraitBoundModifier) { }
333 fn check_fn(&mut self, _: &EarlyContext,
334 _: ast_visit::FnKind, _: &ast::FnDecl, _: Span, _: ast::NodeId) { }
335 fn check_fn_post(&mut self, _: &EarlyContext,
336 _: ast_visit::FnKind, _: &ast::FnDecl, _: Span, _: ast::NodeId) { }
337 fn check_trait_item(&mut self, _: &EarlyContext, _: &ast::TraitItem) { }
338 fn check_trait_item_post(&mut self, _: &EarlyContext, _: &ast::TraitItem) { }
339 fn check_impl_item(&mut self, _: &EarlyContext, _: &ast::ImplItem) { }
340 fn check_impl_item_post(&mut self, _: &EarlyContext, _: &ast::ImplItem) { }
341 fn check_struct_def(&mut self, _: &EarlyContext,
342 _: &ast::VariantData, _: ast::Ident, _: &ast::Generics, _: ast::NodeId) { }
343 fn check_struct_def_post(&mut self, _: &EarlyContext,
344 _: &ast::VariantData, _: ast::Ident, _: &ast::Generics, _: ast::NodeId) { }
345 fn check_struct_field(&mut self, _: &EarlyContext, _: &ast::StructField) { }
346 fn check_variant(&mut self, _: &EarlyContext, _: &ast::Variant, _: &ast::Generics) { }
347 fn check_variant_post(&mut self, _: &EarlyContext, _: &ast::Variant, _: &ast::Generics) { }
348 fn check_lifetime(&mut self, _: &EarlyContext, _: &ast::Lifetime) { }
349 fn check_path(&mut self, _: &EarlyContext, _: &ast::Path, _: ast::NodeId) { }
350 fn check_attribute(&mut self, _: &EarlyContext, _: &ast::Attribute) { }
351 fn check_mac_def(&mut self, _: &EarlyContext, _: &ast::MacroDef, _id: ast::NodeId) { }
352 fn check_mac(&mut self, _: &EarlyContext, _: &ast::Mac) { }
354 /// Called when entering a syntax node that can have lint attributes such
355 /// as `#[allow(...)]`. Called with *all* the attributes of that node.
356 fn enter_lint_attrs(&mut self, _: &EarlyContext, _: &[ast::Attribute]) { }
358 /// Counterpart to `enter_lint_attrs`.
359 fn exit_lint_attrs(&mut self, _: &EarlyContext, _: &[ast::Attribute]) { }
362 /// A lint pass boxed up as a trait object.
363 pub type EarlyLintPassObject = Box<dyn EarlyLintPass + sync::Send + sync::Sync + 'static>;
364 pub type LateLintPassObject = Box<dyn for<'a, 'tcx> LateLintPass<'a, 'tcx> + sync::Send
365 + sync::Sync + 'static>;
369 /// Identifies a lint known to the compiler.
370 #[derive(Clone, Copy, Debug)]
372 // Identity is based on pointer equality of this field.
376 impl PartialEq for LintId {
377 fn eq(&self, other: &LintId) -> bool {
378 ptr::eq(self.lint, other.lint)
382 impl Eq for LintId { }
384 impl hash::Hash for LintId {
385 fn hash<H: hash::Hasher>(&self, state: &mut H) {
386 let ptr = self.lint as *const Lint;
392 /// Get the `LintId` for a `Lint`.
393 pub fn of(lint: &'static Lint) -> LintId {
399 pub fn lint_name_raw(&self) -> &'static str {
403 /// Get the name of the lint.
404 pub fn to_string(&self) -> String {
405 self.lint.name_lower()
409 /// Setting for how to handle a lint.
410 #[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Debug, Hash)]
412 Allow, Warn, Deny, Forbid,
415 impl_stable_hash_for!(enum self::Level {
423 /// Convert a level to a lower-case string.
424 pub fn as_str(self) -> &'static str {
433 /// Convert a lower-case string to a level.
434 pub fn from_str(x: &str) -> Option<Level> {
436 "allow" => Some(Allow),
437 "warn" => Some(Warn),
438 "deny" => Some(Deny),
439 "forbid" => Some(Forbid),
445 /// How a lint level was set.
446 #[derive(Clone, Copy, PartialEq, Eq)]
447 pub enum LintSource {
448 /// Lint is at the default level as declared
449 /// in rustc or a plugin.
452 /// Lint level was set by an attribute.
453 Node(ast::Name, Span),
455 /// Lint level was set by a command-line flag.
459 impl_stable_hash_for!(enum self::LintSource {
465 pub type LevelSource = (Level, LintSource);
471 pub use self::levels::{LintLevelSets, LintLevelMap};
473 pub struct LintBuffer {
474 map: NodeMap<Vec<BufferedEarlyLint>>,
478 pub fn new() -> LintBuffer {
479 LintBuffer { map: NodeMap() }
482 pub fn add_lint(&mut self,
487 diagnostic: BuiltinLintDiagnostics) {
488 let early_lint = BufferedEarlyLint {
489 lint_id: LintId::of(lint),
492 msg: msg.to_string(),
495 let arr = self.map.entry(id).or_insert(Vec::new());
496 if !arr.contains(&early_lint) {
497 arr.push(early_lint);
501 pub fn take(&mut self, id: ast::NodeId) -> Vec<BufferedEarlyLint> {
502 self.map.remove(&id).unwrap_or(Vec::new())
505 pub fn get_any(&self) -> Option<&[BufferedEarlyLint]> {
506 let key = self.map.keys().next().map(|k| *k);
507 key.map(|k| &self.map[&k][..])
511 pub fn struct_lint_level<'a>(sess: &'a Session,
515 span: Option<MultiSpan>,
517 -> DiagnosticBuilder<'a>
519 let mut err = match (level, span) {
520 (Level::Allow, _) => return sess.diagnostic().struct_dummy(),
521 (Level::Warn, Some(span)) => sess.struct_span_warn(span, msg),
522 (Level::Warn, None) => sess.struct_warn(msg),
523 (Level::Deny, Some(span)) |
524 (Level::Forbid, Some(span)) => sess.struct_span_err(span, msg),
525 (Level::Deny, None) |
526 (Level::Forbid, None) => sess.struct_err(msg),
529 let name = lint.name_lower();
531 LintSource::Default => {
534 DiagnosticMessageId::from(lint),
535 &format!("#[{}({})] on by default", level.as_str(), name));
537 LintSource::CommandLine(lint_flag_val) => {
538 let flag = match level {
541 Level::Forbid => "-F",
542 Level::Allow => panic!(),
544 let hyphen_case_lint_name = name.replace("_", "-");
545 if lint_flag_val.as_str() == name {
548 DiagnosticMessageId::from(lint),
549 &format!("requested on the command line with `{} {}`",
550 flag, hyphen_case_lint_name));
552 let hyphen_case_flag_val = lint_flag_val.as_str().replace("_", "-");
555 DiagnosticMessageId::from(lint),
556 &format!("`{} {}` implied by `{} {}`",
557 flag, hyphen_case_lint_name, flag,
558 hyphen_case_flag_val));
561 LintSource::Node(lint_attr_name, src) => {
562 sess.diag_span_note_once(&mut err, DiagnosticMessageId::from(lint),
563 src, "lint level defined here");
564 if lint_attr_name.as_str() != name {
565 let level_str = level.as_str();
566 sess.diag_note_once(&mut err, DiagnosticMessageId::from(lint),
567 &format!("#[{}({})] implied by #[{}({})]",
568 level_str, name, level_str, lint_attr_name));
573 err.code(DiagnosticId::Lint(name));
575 // Check for future incompatibility lints and issue a stronger warning.
576 let lints = sess.lint_store.borrow();
577 let lint_id = LintId::of(lint);
578 if let Some(future_incompatible) = lints.future_incompatible(lint_id) {
579 const STANDARD_MESSAGE: &str =
580 "this was previously accepted by the compiler but is being phased out; \
581 it will become a hard error";
583 let explanation = if lint_id == LintId::of(::lint::builtin::UNSTABLE_NAME_COLLISIONS) {
584 "once this method is added to the standard library, \
585 the ambiguity may cause an error or change in behavior!"
587 } else if let Some(edition) = future_incompatible.edition {
588 format!("{} in the {} edition!", STANDARD_MESSAGE, edition)
590 format!("{} in a future release!", STANDARD_MESSAGE)
592 let citation = format!("for more information, see {}",
593 future_incompatible.reference);
594 err.warn(&explanation);
597 // If this lint is *not* a future incompatibility warning then we want to be
598 // sure to not be too noisy in some situations. If this code originates in a
599 // foreign macro, aka something that this crate did not itself author, then
600 // it's likely that there's nothing this crate can do about it. We probably
601 // want to skip the lint entirely.
603 // For some lints though (like unreachable code) there's clear actionable
604 // items to take care of (delete the macro invocation). As a result we have
605 // a few lints we whitelist here for allowing a lint even though it's in a
606 // foreign macro invocation.
607 } else if !lint.report_in_external_macro {
608 if err.span.primary_spans().iter().any(|s| in_external_macro(sess, *s)) {
616 fn lint_levels<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, cnum: CrateNum)
619 assert_eq!(cnum, LOCAL_CRATE);
620 let mut builder = LintLevelMapBuilder {
621 levels: LintLevelSets::builder(tcx.sess),
624 let krate = tcx.hir.krate();
626 builder.with_lint_attrs(ast::CRATE_NODE_ID, &krate.attrs, |builder| {
627 intravisit::walk_crate(builder, krate);
630 Lrc::new(builder.levels.build_map())
633 struct LintLevelMapBuilder<'a, 'tcx: 'a> {
634 levels: levels::LintLevelsBuilder<'tcx>,
635 tcx: TyCtxt<'a, 'tcx, 'tcx>,
638 impl<'a, 'tcx> LintLevelMapBuilder<'a, 'tcx> {
639 fn with_lint_attrs<F>(&mut self,
641 attrs: &[ast::Attribute],
643 where F: FnOnce(&mut Self)
645 let push = self.levels.push(attrs);
646 self.levels.register_id(self.tcx.hir.definitions().node_to_hir_id(id));
648 self.levels.pop(push);
652 impl<'a, 'tcx> intravisit::Visitor<'tcx> for LintLevelMapBuilder<'a, 'tcx> {
653 fn nested_visit_map<'this>(&'this mut self) -> intravisit::NestedVisitorMap<'this, 'tcx> {
654 intravisit::NestedVisitorMap::All(&self.tcx.hir)
657 fn visit_item(&mut self, it: &'tcx hir::Item) {
658 self.with_lint_attrs(it.id, &it.attrs, |builder| {
659 intravisit::walk_item(builder, it);
663 fn visit_foreign_item(&mut self, it: &'tcx hir::ForeignItem) {
664 self.with_lint_attrs(it.id, &it.attrs, |builder| {
665 intravisit::walk_foreign_item(builder, it);
669 fn visit_expr(&mut self, e: &'tcx hir::Expr) {
670 self.with_lint_attrs(e.id, &e.attrs, |builder| {
671 intravisit::walk_expr(builder, e);
675 fn visit_struct_field(&mut self, s: &'tcx hir::StructField) {
676 self.with_lint_attrs(s.id, &s.attrs, |builder| {
677 intravisit::walk_struct_field(builder, s);
681 fn visit_variant(&mut self,
682 v: &'tcx hir::Variant,
683 g: &'tcx hir::Generics,
684 item_id: ast::NodeId) {
685 self.with_lint_attrs(v.node.data.id(), &v.node.attrs, |builder| {
686 intravisit::walk_variant(builder, v, g, item_id);
690 fn visit_local(&mut self, l: &'tcx hir::Local) {
691 self.with_lint_attrs(l.id, &l.attrs, |builder| {
692 intravisit::walk_local(builder, l);
696 fn visit_trait_item(&mut self, trait_item: &'tcx hir::TraitItem) {
697 self.with_lint_attrs(trait_item.id, &trait_item.attrs, |builder| {
698 intravisit::walk_trait_item(builder, trait_item);
702 fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem) {
703 self.with_lint_attrs(impl_item.id, &impl_item.attrs, |builder| {
704 intravisit::walk_impl_item(builder, impl_item);
709 pub fn provide(providers: &mut Providers) {
710 providers.lint_levels = lint_levels;
713 /// Returns whether `span` originates in a foreign crate's external macro.
715 /// This is used to test whether a lint should be entirely aborted above.
716 pub fn in_external_macro(sess: &Session, span: Span) -> bool {
717 let info = match span.ctxt().outer().expn_info() {
719 // no ExpnInfo means this span doesn't come from a macro
720 None => return false,
724 ExpnFormat::MacroAttribute(..) => return true, // definitely a plugin
725 ExpnFormat::CompilerDesugaring(_) => return true, // well, it's "external"
726 ExpnFormat::MacroBang(..) => {} // check below
729 let def_site = match info.def_site {
731 // no span for the def_site means it's an external macro
735 match sess.codemap().span_to_snippet(def_site) {
736 Ok(code) => !code.starts_with("macro_rules"),
737 // no snippet = external macro or compiler-builtin expansion