]> git.lizzy.rs Git - rust.git/blob - src/librustc/lint/levels.rs
Improve how warnings are displayed
[rust.git] / src / librustc / lint / levels.rs
1 // Copyright 2017 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 use std::cmp;
12
13 use errors::DiagnosticBuilder;
14 use hir::HirId;
15 use lint::builtin;
16 use lint::context::CheckLintNameResult;
17 use lint::{self, Lint, LintId, Level, LintSource};
18 use session::Session;
19 use syntax::ast;
20 use syntax::attr;
21 use syntax::codemap::MultiSpan;
22 use syntax::symbol::Symbol;
23 use util::nodemap::FxHashMap;
24
25 pub struct LintLevelSets {
26     list: Vec<LintSet>,
27     lint_cap: Level,
28 }
29
30 enum LintSet {
31     CommandLine {
32         // -A,-W,-D flags, a `Symbol` for the flag itself and `Level` for which
33         // flag.
34         specs: FxHashMap<LintId, (Level, LintSource)>,
35     },
36
37     Node {
38         specs: FxHashMap<LintId, (Level, LintSource)>,
39         parent: u32,
40     },
41 }
42
43 impl LintLevelSets {
44     pub fn new(sess: &Session) -> LintLevelSets {
45         let mut me = LintLevelSets {
46             list: Vec::new(),
47             lint_cap: Level::Forbid,
48         };
49         me.process_command_line(sess);
50         return me
51     }
52
53     pub fn builder(sess: &Session) -> LintLevelsBuilder {
54         LintLevelsBuilder::new(sess, LintLevelSets::new(sess))
55     }
56
57     fn process_command_line(&mut self, sess: &Session) {
58         let store = sess.lint_store.borrow();
59         let mut specs = FxHashMap();
60         self.lint_cap = sess.opts.lint_cap.unwrap_or(Level::Forbid);
61
62         for &(ref lint_name, level) in &sess.opts.lint_opts {
63             store.check_lint_name_cmdline(sess, &lint_name, level);
64
65             // If the cap is less than this specified level, e.g. if we've got
66             // `--cap-lints allow` but we've also got `-D foo` then we ignore
67             // this specification as the lint cap will set it to allow anyway.
68             let level = cmp::min(level, self.lint_cap);
69
70             let lint_flag_val = Symbol::intern(lint_name);
71             let ids = match store.find_lints(&lint_name) {
72                 Ok(ids) => ids,
73                 Err(_) => continue, // errors handled in check_lint_name_cmdline above
74             };
75             for id in ids {
76                 let src = LintSource::CommandLine(lint_flag_val);
77                 specs.insert(id, (level, src));
78             }
79         }
80
81         self.list.push(LintSet::CommandLine {
82             specs: specs,
83         });
84     }
85
86     fn get_lint_level(&self,
87                       lint: &'static Lint,
88                       idx: u32,
89                       aux: Option<&FxHashMap<LintId, (Level, LintSource)>>)
90         -> (Level, LintSource)
91     {
92         let (level, mut src) = self.get_lint_id_level(LintId::of(lint), idx, aux);
93
94         // If `level` is none then we actually assume the default level for this
95         // lint.
96         let mut level = level.unwrap_or(lint.default_level);
97
98         // If we're about to issue a warning, check at the last minute for any
99         // directives against the warnings "lint". If, for example, there's an
100         // `allow(warnings)` in scope then we want to respect that instead.
101         if level == Level::Warn {
102             let (warnings_level, warnings_src) =
103                 self.get_lint_id_level(LintId::of(lint::builtin::WARNINGS),
104                                        idx,
105                                        aux);
106             if let Some(configured_warning_level) = warnings_level {
107                 if configured_warning_level != Level::Warn {
108                     level = configured_warning_level;
109                     src = warnings_src;
110                 }
111             }
112         }
113
114         // Ensure that we never exceed the `--cap-lints` argument.
115         level = cmp::min(level, self.lint_cap);
116
117         return (level, src)
118     }
119
120     fn get_lint_id_level(&self,
121                          id: LintId,
122                          mut idx: u32,
123                          aux: Option<&FxHashMap<LintId, (Level, LintSource)>>)
124         -> (Option<Level>, LintSource)
125     {
126         if let Some(specs) = aux {
127             if let Some(&(level, src)) = specs.get(&id) {
128                 return (Some(level), src)
129             }
130         }
131         loop {
132             match self.list[idx as usize] {
133                 LintSet::CommandLine { ref specs } => {
134                     if let Some(&(level, src)) = specs.get(&id) {
135                         return (Some(level), src)
136                     }
137                     return (None, LintSource::Default)
138                 }
139                 LintSet::Node { ref specs, parent } => {
140                     if let Some(&(level, src)) = specs.get(&id) {
141                         return (Some(level), src)
142                     }
143                     idx = parent;
144                 }
145             }
146         }
147     }
148 }
149
150 pub struct LintLevelsBuilder<'a> {
151     sess: &'a Session,
152     sets: LintLevelSets,
153     id_to_set: FxHashMap<HirId, u32>,
154     cur: u32,
155     warn_about_weird_lints: bool,
156 }
157
158 pub struct BuilderPush {
159     prev: u32,
160 }
161
162 impl<'a> LintLevelsBuilder<'a> {
163     pub fn new(sess: &'a Session, sets: LintLevelSets) -> LintLevelsBuilder<'a> {
164         assert_eq!(sets.list.len(), 1);
165         LintLevelsBuilder {
166             sess,
167             sets,
168             cur: 0,
169             id_to_set: FxHashMap(),
170             warn_about_weird_lints: sess.buffered_lints.borrow().is_some(),
171         }
172     }
173
174     /// Pushes a list of AST lint attributes onto this context.
175     ///
176     /// This function will return a `BuilderPush` object which should be be
177     /// passed to `pop` when this scope for the attributes provided is exited.
178     ///
179     /// This function will perform a number of tasks:
180     ///
181     /// * It'll validate all lint-related attributes in `attrs`
182     /// * It'll mark all lint-related attriutes as used
183     /// * Lint levels will be updated based on the attributes provided
184     /// * Lint attributes are validated, e.g. a #[forbid] can't be switched to
185     ///   #[allow]
186     ///
187     /// Don't forget to call `pop`!
188     pub fn push(&mut self, attrs: &[ast::Attribute]) -> BuilderPush {
189         let mut specs = FxHashMap();
190         let store = self.sess.lint_store.borrow();
191         let sess = self.sess;
192         let bad_attr = |span| {
193             span_err!(sess, span, E0452,
194                       "malformed lint attribute");
195         };
196         for attr in attrs {
197             let level = match attr.name().and_then(|name| Level::from_str(&name.as_str())) {
198                 None => continue,
199                 Some(lvl) => lvl,
200             };
201
202             let meta = unwrap_or!(attr.meta(), continue);
203             attr::mark_used(attr);
204
205             let metas = if let Some(metas) = meta.meta_item_list() {
206                 metas
207             } else {
208                 bad_attr(meta.span);
209                 continue
210             };
211
212             for li in metas {
213                 let word = match li.word() {
214                     Some(word) => word,
215                     None => {
216                         bad_attr(li.span);
217                         continue
218                     }
219                 };
220                 let name = word.name();
221                 match store.check_lint_name(&name.as_str()) {
222                     CheckLintNameResult::Ok(ids) => {
223                         let src = LintSource::Node(name, li.span);
224                         for id in ids {
225                             specs.insert(*id, (level, src));
226                         }
227                     }
228
229                     _ if !self.warn_about_weird_lints => {}
230
231                     CheckLintNameResult::Warning(ref msg) => {
232                         let lint = builtin::RENAMED_AND_REMOVED_LINTS;
233                         let (level, src) = self.sets.get_lint_level(lint,
234                                                                     self.cur,
235                                                                     Some(&specs));
236                         lint::struct_lint_level(self.sess,
237                                                 lint,
238                                                 level,
239                                                 src,
240                                                 Some(li.span.into()),
241                                                 msg)
242                             .emit();
243                     }
244                     CheckLintNameResult::NoLint => {
245                         let lint = builtin::UNKNOWN_LINTS;
246                         let (level, src) = self.sets.get_lint_level(lint,
247                                                                     self.cur,
248                                                                     Some(&specs));
249                         let msg = format!("unknown lint: `{}`", name);
250                         let mut db = lint::struct_lint_level(self.sess,
251                                                 lint,
252                                                 level,
253                                                 src,
254                                                 Some(li.span.into()),
255                                                 &msg);
256                         if name.as_str().chars().any(|c| c.is_uppercase()) {
257                             let name_lower = name.as_str().to_lowercase();
258                             if let CheckLintNameResult::NoLint =
259                                     store.check_lint_name(&name_lower) {
260                                 db.emit();
261                             } else {
262                                 db.span_suggestion(
263                                     li.span,
264                                     "lowercase the lint name",
265                                     name_lower
266                                 ).emit();
267                             }
268                         } else {
269                             db.emit();
270                         }
271                     }
272                 }
273             }
274         }
275
276         for (id, &(level, ref src)) in specs.iter() {
277             if level == Level::Forbid {
278                 continue
279             }
280             let forbid_src = match self.sets.get_lint_id_level(*id, self.cur, None) {
281                 (Some(Level::Forbid), src) => src,
282                 _ => continue,
283             };
284             let forbidden_lint_name = match forbid_src {
285                 LintSource::Default => id.to_string(),
286                 LintSource::Node(name, _) => name.to_string(),
287                 LintSource::CommandLine(name) => name.to_string(),
288             };
289             let (lint_attr_name, lint_attr_span) = match *src {
290                 LintSource::Node(name, span) => (name, span),
291                 _ => continue,
292             };
293             let mut diag_builder = struct_span_err!(self.sess,
294                                                     lint_attr_span,
295                                                     E0453,
296                                                     "{}({}) overruled by outer forbid({})",
297                                                     level.as_str(),
298                                                     lint_attr_name,
299                                                     forbidden_lint_name);
300             diag_builder.span_label(lint_attr_span, "overruled by previous forbid");
301             match forbid_src {
302                 LintSource::Default => &mut diag_builder,
303                 LintSource::Node(_, forbid_source_span) => {
304                     diag_builder.span_label(forbid_source_span,
305                                             "`forbid` level set here")
306                 },
307                 LintSource::CommandLine(_) => {
308                     diag_builder.note("`forbid` lint level was set on command line")
309                 }
310             }.emit();
311             // don't set a separate error for every lint in the group
312             break
313         }
314
315         let prev = self.cur;
316         if specs.len() > 0 {
317             self.cur = self.sets.list.len() as u32;
318             self.sets.list.push(LintSet::Node {
319                 specs: specs,
320                 parent: prev,
321             });
322         }
323
324         BuilderPush {
325             prev: prev,
326         }
327     }
328
329     /// Called after `push` when the scope of a set of attributes are exited.
330     pub fn pop(&mut self, push: BuilderPush) {
331         self.cur = push.prev;
332     }
333
334     /// Used to emit a lint-related diagnostic based on the current state of
335     /// this lint context.
336     pub fn struct_lint(&self,
337                        lint: &'static Lint,
338                        span: Option<MultiSpan>,
339                        msg: &str)
340         -> DiagnosticBuilder<'a>
341     {
342         let (level, src) = self.sets.get_lint_level(lint, self.cur, None);
343         lint::struct_lint_level(self.sess, lint, level, src, span, msg)
344     }
345
346     /// Registers the ID provided with the current set of lints stored in
347     /// this context.
348     pub fn register_id(&mut self, id: HirId) {
349         self.id_to_set.insert(id, self.cur);
350     }
351
352     pub fn build(self) -> LintLevelSets {
353         self.sets
354     }
355
356     pub fn build_map(self) -> LintLevelMap {
357         LintLevelMap {
358             sets: self.sets,
359             id_to_set: self.id_to_set,
360         }
361     }
362 }
363
364 pub struct LintLevelMap {
365     sets: LintLevelSets,
366     id_to_set: FxHashMap<HirId, u32>,
367 }
368
369 impl LintLevelMap {
370     /// If the `id` was previously registered with `register_id` when building
371     /// this `LintLevelMap` this returns the corresponding lint level and source
372     /// of the lint level for the lint provided.
373     ///
374     /// If the `id` was not previously registered, returns `None`. If `None` is
375     /// returned then the parent of `id` should be acquired and this function
376     /// should be called again.
377     pub fn level_and_source(&self, lint: &'static Lint, id: HirId)
378         -> Option<(Level, LintSource)>
379     {
380         self.id_to_set.get(&id).map(|idx| {
381             self.sets.get_lint_level(lint, *idx, None)
382         })
383     }
384 }