]> git.lizzy.rs Git - rust.git/blob - src/librustc/lint/mod.rs
Auto merge of #22517 - brson:relnotes, r=Gankro
[rust.git] / src / librustc / lint / mod.rs
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.
4 //
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.
10
11 //! Lints, aka compiler warnings.
12 //!
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.
18 //!
19 //! Most lints can be written as `LintPass` instances. These run just before
20 //! translation to LLVM bytecode. 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.
23 //!
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 trans, for
28 //! example) requires more effort. See `emit_lint` and `GatherNodeLevels`
29 //! in `context.rs`.
30
31 pub use self::Level::*;
32 pub use self::LintSource::*;
33
34 use std::hash;
35 use std::ascii::AsciiExt;
36 use syntax::codemap::Span;
37 use syntax::visit::FnKind;
38 use syntax::ast;
39
40 pub use lint::context::{Context, LintStore, raw_emit_lint, check_crate, gather_attrs};
41
42 /// Specification of a single lint.
43 #[derive(Copy, Debug)]
44 pub struct Lint {
45     /// A string identifier for the lint.
46     ///
47     /// This identifies the lint in attributes and in command-line arguments.
48     /// In those contexts it is always lowercase, but this field is compared
49     /// in a way which is case-insensitive for ASCII characters. This allows
50     /// `declare_lint!()` invocations to follow the convention of upper-case
51     /// statics without repeating the name.
52     ///
53     /// The name is written with underscores, e.g. "unused_imports".
54     /// On the command line, underscores become dashes.
55     pub name: &'static str,
56
57     /// Default level for the lint.
58     pub default_level: Level,
59
60     /// Description of the lint or the issue it detects.
61     ///
62     /// e.g. "imports that are never used"
63     pub desc: &'static str,
64 }
65
66 impl Lint {
67     /// Get the lint's name, with ASCII letters converted to lowercase.
68     pub fn name_lower(&self) -> String {
69         self.name.to_ascii_lowercase()
70     }
71 }
72
73 /// Build a `Lint` initializer.
74 #[macro_export]
75 macro_rules! lint_initializer {
76     ($name:ident, $level:ident, $desc:expr) => (
77         ::rustc::lint::Lint {
78             name: stringify!($name),
79             default_level: ::rustc::lint::$level,
80             desc: $desc,
81         }
82     )
83 }
84
85 /// Declare a static item of type `&'static Lint`.
86 #[macro_export]
87 macro_rules! declare_lint {
88     // FIXME(#14660): deduplicate
89     (pub $name:ident, $level:ident, $desc:expr) => (
90         pub static $name: &'static ::rustc::lint::Lint
91             = &lint_initializer!($name, $level, $desc);
92     );
93     ($name:ident, $level:ident, $desc:expr) => (
94         static $name: &'static ::rustc::lint::Lint
95             = &lint_initializer!($name, $level, $desc);
96     );
97 }
98
99 /// Declare a static `LintArray` and return it as an expression.
100 #[macro_export]
101 macro_rules! lint_array { ($( $lint:expr ),*) => (
102     {
103         #[allow(non_upper_case_globals)]
104         static array: LintArray = &[ $( &$lint ),* ];
105         array
106     }
107 ) }
108
109 pub type LintArray = &'static [&'static &'static Lint];
110
111 /// Trait for types providing lint checks.
112 ///
113 /// Each `check` method checks a single syntax node, and should not
114 /// invoke methods recursively (unlike `Visitor`). By default they
115 /// do nothing.
116 //
117 // FIXME: eliminate the duplication with `Visitor`. But this also
118 // contains a few lint-specific methods with no equivalent in `Visitor`.
119 pub trait LintPass {
120     /// Get descriptions of the lints this `LintPass` object can emit.
121     ///
122     /// NB: there is no enforcement that the object only emits lints it registered.
123     /// And some `rustc` internal `LintPass`es register lints to be emitted by other
124     /// parts of the compiler. If you want enforced access restrictions for your
125     /// `Lint`, make it a private `static` item in its own module.
126     fn get_lints(&self) -> LintArray;
127
128     fn check_crate(&mut self, _: &Context, _: &ast::Crate) { }
129     fn check_ident(&mut self, _: &Context, _: Span, _: ast::Ident) { }
130     fn check_mod(&mut self, _: &Context, _: &ast::Mod, _: Span, _: ast::NodeId) { }
131     fn check_foreign_item(&mut self, _: &Context, _: &ast::ForeignItem) { }
132     fn check_item(&mut self, _: &Context, _: &ast::Item) { }
133     fn check_local(&mut self, _: &Context, _: &ast::Local) { }
134     fn check_block(&mut self, _: &Context, _: &ast::Block) { }
135     fn check_stmt(&mut self, _: &Context, _: &ast::Stmt) { }
136     fn check_arm(&mut self, _: &Context, _: &ast::Arm) { }
137     fn check_pat(&mut self, _: &Context, _: &ast::Pat) { }
138     fn check_decl(&mut self, _: &Context, _: &ast::Decl) { }
139     fn check_expr(&mut self, _: &Context, _: &ast::Expr) { }
140     fn check_expr_post(&mut self, _: &Context, _: &ast::Expr) { }
141     fn check_ty(&mut self, _: &Context, _: &ast::Ty) { }
142     fn check_generics(&mut self, _: &Context, _: &ast::Generics) { }
143     fn check_fn(&mut self, _: &Context,
144         _: FnKind, _: &ast::FnDecl, _: &ast::Block, _: Span, _: ast::NodeId) { }
145     fn check_ty_method(&mut self, _: &Context, _: &ast::TypeMethod) { }
146     fn check_trait_method(&mut self, _: &Context, _: &ast::TraitItem) { }
147     fn check_struct_def(&mut self, _: &Context,
148         _: &ast::StructDef, _: ast::Ident, _: &ast::Generics, _: ast::NodeId) { }
149     fn check_struct_def_post(&mut self, _: &Context,
150         _: &ast::StructDef, _: ast::Ident, _: &ast::Generics, _: ast::NodeId) { }
151     fn check_struct_field(&mut self, _: &Context, _: &ast::StructField) { }
152     fn check_variant(&mut self, _: &Context, _: &ast::Variant, _: &ast::Generics) { }
153     fn check_variant_post(&mut self, _: &Context, _: &ast::Variant, _: &ast::Generics) { }
154     fn check_opt_lifetime_ref(&mut self, _: &Context, _: Span, _: &Option<ast::Lifetime>) { }
155     fn check_lifetime_ref(&mut self, _: &Context, _: &ast::Lifetime) { }
156     fn check_lifetime_def(&mut self, _: &Context, _: &ast::LifetimeDef) { }
157     fn check_explicit_self(&mut self, _: &Context, _: &ast::ExplicitSelf) { }
158     fn check_mac(&mut self, _: &Context, _: &ast::Mac) { }
159     fn check_path(&mut self, _: &Context, _: &ast::Path, _: ast::NodeId) { }
160     fn check_attribute(&mut self, _: &Context, _: &ast::Attribute) { }
161
162     /// Called when entering a syntax node that can have lint attributes such
163     /// as `#[allow(...)]`. Called with *all* the attributes of that node.
164     fn enter_lint_attrs(&mut self, _: &Context, _: &[ast::Attribute]) { }
165
166     /// Counterpart to `enter_lint_attrs`.
167     fn exit_lint_attrs(&mut self, _: &Context, _: &[ast::Attribute]) { }
168 }
169
170 /// A lint pass boxed up as a trait object.
171 pub type LintPassObject = Box<LintPass + 'static>;
172
173 /// Identifies a lint known to the compiler.
174 #[derive(Clone, Copy)]
175 pub struct LintId {
176     // Identity is based on pointer equality of this field.
177     lint: &'static Lint,
178 }
179
180 impl PartialEq for LintId {
181     fn eq(&self, other: &LintId) -> bool {
182         (self.lint as *const Lint) == (other.lint as *const Lint)
183     }
184 }
185
186 impl Eq for LintId { }
187
188 impl<S: hash::Writer + hash::Hasher> hash::Hash<S> for LintId {
189     fn hash(&self, state: &mut S) {
190         let ptr = self.lint as *const Lint;
191         ptr.hash(state);
192     }
193 }
194
195 impl LintId {
196     /// Get the `LintId` for a `Lint`.
197     pub fn of(lint: &'static Lint) -> LintId {
198         LintId {
199             lint: lint,
200         }
201     }
202
203     /// Get the name of the lint.
204     pub fn as_str(&self) -> String {
205         self.lint.name_lower()
206     }
207 }
208
209 /// Setting for how to handle a lint.
210 #[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Debug)]
211 pub enum Level {
212     Allow, Warn, Deny, Forbid
213 }
214
215 impl Level {
216     /// Convert a level to a lower-case string.
217     pub fn as_str(self) -> &'static str {
218         match self {
219             Allow => "allow",
220             Warn => "warn",
221             Deny => "deny",
222             Forbid => "forbid",
223         }
224     }
225
226     /// Convert a lower-case string to a level.
227     pub fn from_str(x: &str) -> Option<Level> {
228         match x {
229             "allow" => Some(Allow),
230             "warn" => Some(Warn),
231             "deny" => Some(Deny),
232             "forbid" => Some(Forbid),
233             _ => None,
234         }
235     }
236 }
237
238 /// How a lint level was set.
239 #[derive(Clone, Copy, PartialEq, Eq)]
240 pub enum LintSource {
241     /// Lint is at the default level as declared
242     /// in rustc or a plugin.
243     Default,
244
245     /// Lint level was set by an attribute.
246     Node(Span),
247
248     /// Lint level was set by a command-line flag.
249     CommandLine,
250
251     /// Lint level was set by the release channel.
252     ReleaseChannel
253 }
254
255 pub type LevelSource = (Level, LintSource);
256
257 pub mod builtin;
258
259 mod context;