]> git.lizzy.rs Git - rust.git/blob - src/librustc/lint/levels.rs
Various minor/cosmetic improvements to code
[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::{Applicability, DiagnosticBuilder};
14 use hir::HirId;
15 use ich::StableHashingContext;
16 use lint::builtin;
17 use lint::context::CheckLintNameResult;
18 use lint::{self, Lint, LintId, Level, LintSource};
19 use rustc_data_structures::stable_hasher::{HashStable, ToStableHashKey,
20                                            StableHasher, StableHasherResult};
21 use session::Session;
22 use syntax::ast;
23 use syntax::attr;
24 use syntax::feature_gate;
25 use syntax::source_map::MultiSpan;
26 use syntax::symbol::Symbol;
27 use util::nodemap::FxHashMap;
28
29 pub struct LintLevelSets {
30     list: Vec<LintSet>,
31     lint_cap: Level,
32 }
33
34 enum LintSet {
35     CommandLine {
36         // -A,-W,-D flags, a `Symbol` for the flag itself and `Level` for which
37         // flag.
38         specs: FxHashMap<LintId, (Level, LintSource)>,
39     },
40
41     Node {
42         specs: FxHashMap<LintId, (Level, LintSource)>,
43         parent: u32,
44     },
45 }
46
47 impl LintLevelSets {
48     pub fn new(sess: &Session) -> LintLevelSets {
49         let mut me = LintLevelSets {
50             list: Vec::new(),
51             lint_cap: Level::Forbid,
52         };
53         me.process_command_line(sess);
54         return me
55     }
56
57     pub fn builder(sess: &Session) -> LintLevelsBuilder<'_> {
58         LintLevelsBuilder::new(sess, LintLevelSets::new(sess))
59     }
60
61     fn process_command_line(&mut self, sess: &Session) {
62         let store = sess.lint_store.borrow();
63         let mut specs = FxHashMap::default();
64         self.lint_cap = sess.opts.lint_cap.unwrap_or(Level::Forbid);
65
66         for &(ref lint_name, level) in &sess.opts.lint_opts {
67             store.check_lint_name_cmdline(sess, &lint_name, level);
68
69             // If the cap is less than this specified level, e.g., if we've got
70             // `--cap-lints allow` but we've also got `-D foo` then we ignore
71             // this specification as the lint cap will set it to allow anyway.
72             let level = cmp::min(level, self.lint_cap);
73
74             let lint_flag_val = Symbol::intern(lint_name);
75             let ids = match store.find_lints(&lint_name) {
76                 Ok(ids) => ids,
77                 Err(_) => continue, // errors handled in check_lint_name_cmdline above
78             };
79             for id in ids {
80                 let src = LintSource::CommandLine(lint_flag_val);
81                 specs.insert(id, (level, src));
82             }
83         }
84
85         self.list.push(LintSet::CommandLine {
86             specs: specs,
87         });
88     }
89
90     fn get_lint_level(&self,
91                       lint: &'static Lint,
92                       idx: u32,
93                       aux: Option<&FxHashMap<LintId, (Level, LintSource)>>,
94                       sess: &Session)
95         -> (Level, LintSource)
96     {
97         let (level, mut src) = self.get_lint_id_level(LintId::of(lint), idx, aux);
98
99         // If `level` is none then we actually assume the default level for this
100         // lint.
101         let mut level = level.unwrap_or_else(|| lint.default_level(sess));
102
103         // If we're about to issue a warning, check at the last minute for any
104         // directives against the warnings "lint". If, for example, there's an
105         // `allow(warnings)` in scope then we want to respect that instead.
106         if level == Level::Warn {
107             let (warnings_level, warnings_src) =
108                 self.get_lint_id_level(LintId::of(lint::builtin::WARNINGS),
109                                        idx,
110                                        aux);
111             if let Some(configured_warning_level) = warnings_level {
112                 if configured_warning_level != Level::Warn {
113                     level = configured_warning_level;
114                     src = warnings_src;
115                 }
116             }
117         }
118
119         // Ensure that we never exceed the `--cap-lints` argument.
120         level = cmp::min(level, self.lint_cap);
121
122         if let Some(driver_level) = sess.driver_lint_caps.get(&LintId::of(lint)) {
123             // Ensure that we never exceed driver level.
124             level = cmp::min(*driver_level, level);
125         }
126
127         return (level, src)
128     }
129
130     fn get_lint_id_level(&self,
131                          id: LintId,
132                          mut idx: u32,
133                          aux: Option<&FxHashMap<LintId, (Level, LintSource)>>)
134         -> (Option<Level>, LintSource)
135     {
136         if let Some(specs) = aux {
137             if let Some(&(level, src)) = specs.get(&id) {
138                 return (Some(level), src)
139             }
140         }
141         loop {
142             match self.list[idx as usize] {
143                 LintSet::CommandLine { ref specs } => {
144                     if let Some(&(level, src)) = specs.get(&id) {
145                         return (Some(level), src)
146                     }
147                     return (None, LintSource::Default)
148                 }
149                 LintSet::Node { ref specs, parent } => {
150                     if let Some(&(level, src)) = specs.get(&id) {
151                         return (Some(level), src)
152                     }
153                     idx = parent;
154                 }
155             }
156         }
157     }
158 }
159
160 pub struct LintLevelsBuilder<'a> {
161     sess: &'a Session,
162     sets: LintLevelSets,
163     id_to_set: FxHashMap<HirId, u32>,
164     cur: u32,
165     warn_about_weird_lints: bool,
166 }
167
168 pub struct BuilderPush {
169     prev: u32,
170 }
171
172 impl<'a> LintLevelsBuilder<'a> {
173     pub fn new(sess: &'a Session, sets: LintLevelSets) -> LintLevelsBuilder<'a> {
174         assert_eq!(sets.list.len(), 1);
175         LintLevelsBuilder {
176             sess,
177             sets,
178             cur: 0,
179             id_to_set: Default::default(),
180             warn_about_weird_lints: sess.buffered_lints.borrow().is_some(),
181         }
182     }
183
184     /// Pushes a list of AST lint attributes onto this context.
185     ///
186     /// This function will return a `BuilderPush` object which should be be
187     /// passed to `pop` when this scope for the attributes provided is exited.
188     ///
189     /// This function will perform a number of tasks:
190     ///
191     /// * It'll validate all lint-related attributes in `attrs`
192     /// * It'll mark all lint-related attributes as used
193     /// * Lint levels will be updated based on the attributes provided
194     /// * Lint attributes are validated, e.g., a #[forbid] can't be switched to
195     ///   #[allow]
196     ///
197     /// Don't forget to call `pop`!
198     pub fn push(&mut self, attrs: &[ast::Attribute]) -> BuilderPush {
199         let mut specs = FxHashMap::default();
200         let store = self.sess.lint_store.borrow();
201         let sess = self.sess;
202         let bad_attr = |span| {
203             struct_span_err!(sess, span, E0452, "malformed lint attribute")
204         };
205         for attr in attrs {
206             let level = match Level::from_str(&attr.name().as_str()) {
207                 None => continue,
208                 Some(lvl) => lvl,
209             };
210
211             let meta = unwrap_or!(attr.meta(), continue);
212             attr::mark_used(attr);
213
214             let mut metas = if let Some(metas) = meta.meta_item_list() {
215                 metas
216             } else {
217                 let mut err = bad_attr(meta.span);
218                 err.emit();
219                 continue;
220             };
221
222             if metas.is_empty() {
223                 // FIXME (#55112): issue unused-attributes lint for `#[level()]`
224                 continue;
225             }
226
227             // Before processing the lint names, look for a reason (RFC 2383)
228             // at the end.
229             let mut reason = None;
230             let tail_li = &metas[metas.len()-1];
231             if let Some(item) = tail_li.meta_item() {
232                 match item.node {
233                     ast::MetaItemKind::Word => {}  // actual lint names handled later
234                     ast::MetaItemKind::NameValue(ref name_value) => {
235                         let gate_reasons = !self.sess.features_untracked().lint_reasons;
236                         if item.ident == "reason" {
237                             // found reason, reslice meta list to exclude it
238                             metas = &metas[0..metas.len()-1];
239                             // FIXME (#55112): issue unused-attributes lint if we thereby
240                             // don't have any lint names (`#[level(reason = "foo")]`)
241                             if let ast::LitKind::Str(rationale, _) = name_value.node {
242                                 if gate_reasons {
243                                     feature_gate::emit_feature_err(
244                                         &self.sess.parse_sess,
245                                         "lint_reasons",
246                                         item.span,
247                                         feature_gate::GateIssue::Language,
248                                         "lint reasons are experimental"
249                                     );
250                                 } else {
251                                     reason = Some(rationale);
252                                 }
253                             } else {
254                                 let mut err = bad_attr(name_value.span);
255                                 err.help("reason must be a string literal");
256                                 err.emit();
257                             }
258                         } else {
259                             let mut err = bad_attr(item.span);
260                             err.emit();
261                         }
262                     },
263                     ast::MetaItemKind::List(_) => {
264                         let mut err = bad_attr(item.span);
265                         err.emit();
266                     }
267                 }
268             }
269
270             for li in metas {
271                 let word = match li.word() {
272                     Some(word) => word,
273                     None => {
274                         let mut err = bad_attr(li.span);
275                         if let Some(item) = li.meta_item() {
276                             if let ast::MetaItemKind::NameValue(_) = item.node {
277                                 if item.ident == "reason" {
278                                     err.help("reason in lint attribute must come last");
279                                 }
280                             }
281                         }
282                         err.emit();
283                         continue;
284                     }
285                 };
286                 let tool_name = if let Some(lint_tool) = word.is_scoped() {
287                     if !attr::is_known_lint_tool(lint_tool) {
288                         span_err!(
289                             sess,
290                             lint_tool.span,
291                             E0710,
292                             "an unknown tool name found in scoped lint: `{}`",
293                             word.ident
294                         );
295                         continue;
296                     }
297
298                     Some(lint_tool.as_str())
299                 } else {
300                     None
301                 };
302                 let name = word.name();
303                 match store.check_lint_name(&name.as_str(), tool_name) {
304                     CheckLintNameResult::Ok(ids) => {
305                         let src = LintSource::Node(name, li.span, reason);
306                         for id in ids {
307                             specs.insert(*id, (level, src));
308                         }
309                     }
310
311                     CheckLintNameResult::Tool(result) => {
312                         match result {
313                             Ok(ids) => {
314                                 let complete_name = &format!("{}::{}", tool_name.unwrap(), name);
315                                 let src = LintSource::Node(
316                                     Symbol::intern(complete_name), li.span, reason
317                                 );
318                                 for id in ids {
319                                     specs.insert(*id, (level, src));
320                                 }
321                             }
322                             Err((Some(ids), new_lint_name)) => {
323                                 let lint = builtin::RENAMED_AND_REMOVED_LINTS;
324                                 let (lvl, src) =
325                                     self.sets
326                                         .get_lint_level(lint, self.cur, Some(&specs), &sess);
327                                 let msg = format!(
328                                     "lint name `{}` is deprecated \
329                                      and may not have an effect in the future. \
330                                      Also `cfg_attr(cargo-clippy)` won't be necessary anymore",
331                                     name
332                                 );
333                                 let mut err = lint::struct_lint_level(
334                                     self.sess,
335                                     lint,
336                                     lvl,
337                                     src,
338                                     Some(li.span.into()),
339                                     &msg,
340                                 );
341                                 err.span_suggestion_with_applicability(
342                                     li.span,
343                                     "change it to",
344                                     new_lint_name.to_string(),
345                                     Applicability::MachineApplicable,
346                                 ).emit();
347
348                                 let src = LintSource::Node(
349                                     Symbol::intern(&new_lint_name), li.span, reason
350                                 );
351                                 for id in ids {
352                                     specs.insert(*id, (level, src));
353                                 }
354                             }
355                             Err((None, _)) => {
356                                 // If Tool(Err(None, _)) is returned, then either the lint does not
357                                 // exist in the tool or the code was not compiled with the tool and
358                                 // therefore the lint was never added to the `LintStore`. To detect
359                                 // this is the responsibility of the lint tool.
360                             }
361                         }
362                     }
363
364                     _ if !self.warn_about_weird_lints => {}
365
366                     CheckLintNameResult::Warning(msg, renamed) => {
367                         let lint = builtin::RENAMED_AND_REMOVED_LINTS;
368                         let (level, src) = self.sets.get_lint_level(lint,
369                                                                     self.cur,
370                                                                     Some(&specs),
371                                                                     &sess);
372                         let mut err = lint::struct_lint_level(self.sess,
373                                                               lint,
374                                                               level,
375                                                               src,
376                                                               Some(li.span.into()),
377                                                               &msg);
378                         if let Some(new_name) = renamed {
379                             err.span_suggestion_with_applicability(
380                                 li.span,
381                                 "use the new name",
382                                 new_name,
383                                 Applicability::MachineApplicable
384                             );
385                         }
386                         err.emit();
387                     }
388                     CheckLintNameResult::NoLint => {
389                         let lint = builtin::UNKNOWN_LINTS;
390                         let (level, src) = self.sets.get_lint_level(lint,
391                                                                     self.cur,
392                                                                     Some(&specs),
393                                                                     self.sess);
394                         let msg = format!("unknown lint: `{}`", name);
395                         let mut db = lint::struct_lint_level(self.sess,
396                                                 lint,
397                                                 level,
398                                                 src,
399                                                 Some(li.span.into()),
400                                                 &msg);
401                         if name.as_str().chars().any(|c| c.is_uppercase()) {
402                             let name_lower = name.as_str().to_lowercase().to_string();
403                             if let CheckLintNameResult::NoLint =
404                                     store.check_lint_name(&name_lower, tool_name) {
405                                 db.emit();
406                             } else {
407                                 db.span_suggestion_with_applicability(
408                                     li.span,
409                                     "lowercase the lint name",
410                                     name_lower,
411                                     Applicability::MachineApplicable
412                                 ).emit();
413                             }
414                         } else {
415                             db.emit();
416                         }
417                     }
418                 }
419             }
420         }
421
422         for (id, &(level, ref src)) in specs.iter() {
423             if level == Level::Forbid {
424                 continue
425             }
426             let forbid_src = match self.sets.get_lint_id_level(*id, self.cur, None) {
427                 (Some(Level::Forbid), src) => src,
428                 _ => continue,
429             };
430             let forbidden_lint_name = match forbid_src {
431                 LintSource::Default => id.to_string(),
432                 LintSource::Node(name, _, _) => name.to_string(),
433                 LintSource::CommandLine(name) => name.to_string(),
434             };
435             let (lint_attr_name, lint_attr_span) = match *src {
436                 LintSource::Node(name, span, _) => (name, span),
437                 _ => continue,
438             };
439             let mut diag_builder = struct_span_err!(self.sess,
440                                                     lint_attr_span,
441                                                     E0453,
442                                                     "{}({}) overruled by outer forbid({})",
443                                                     level.as_str(),
444                                                     lint_attr_name,
445                                                     forbidden_lint_name);
446             diag_builder.span_label(lint_attr_span, "overruled by previous forbid");
447             match forbid_src {
448                 LintSource::Default => {},
449                 LintSource::Node(_, forbid_source_span, reason) => {
450                     diag_builder.span_label(forbid_source_span,
451                                             "`forbid` level set here");
452                     if let Some(rationale) = reason {
453                         diag_builder.note(&rationale.as_str());
454                     }
455                 },
456                 LintSource::CommandLine(_) => {
457                     diag_builder.note("`forbid` lint level was set on command line");
458                 }
459             }
460             diag_builder.emit();
461             // don't set a separate error for every lint in the group
462             break
463         }
464
465         let prev = self.cur;
466         if specs.len() > 0 {
467             self.cur = self.sets.list.len() as u32;
468             self.sets.list.push(LintSet::Node {
469                 specs: specs,
470                 parent: prev,
471             });
472         }
473
474         BuilderPush {
475             prev: prev,
476         }
477     }
478
479     /// Called after `push` when the scope of a set of attributes are exited.
480     pub fn pop(&mut self, push: BuilderPush) {
481         self.cur = push.prev;
482     }
483
484     /// Used to emit a lint-related diagnostic based on the current state of
485     /// this lint context.
486     pub fn struct_lint(&self,
487                        lint: &'static Lint,
488                        span: Option<MultiSpan>,
489                        msg: &str)
490         -> DiagnosticBuilder<'a>
491     {
492         let (level, src) = self.sets.get_lint_level(lint, self.cur, None, self.sess);
493         lint::struct_lint_level(self.sess, lint, level, src, span, msg)
494     }
495
496     /// Registers the ID provided with the current set of lints stored in
497     /// this context.
498     pub fn register_id(&mut self, id: HirId) {
499         self.id_to_set.insert(id, self.cur);
500     }
501
502     pub fn build(self) -> LintLevelSets {
503         self.sets
504     }
505
506     pub fn build_map(self) -> LintLevelMap {
507         LintLevelMap {
508             sets: self.sets,
509             id_to_set: self.id_to_set,
510         }
511     }
512 }
513
514 pub struct LintLevelMap {
515     sets: LintLevelSets,
516     id_to_set: FxHashMap<HirId, u32>,
517 }
518
519 impl LintLevelMap {
520     /// If the `id` was previously registered with `register_id` when building
521     /// this `LintLevelMap` this returns the corresponding lint level and source
522     /// of the lint level for the lint provided.
523     ///
524     /// If the `id` was not previously registered, returns `None`. If `None` is
525     /// returned then the parent of `id` should be acquired and this function
526     /// should be called again.
527     pub fn level_and_source(&self, lint: &'static Lint, id: HirId, session: &Session)
528         -> Option<(Level, LintSource)>
529     {
530         self.id_to_set.get(&id).map(|idx| {
531             self.sets.get_lint_level(lint, *idx, None, session)
532         })
533     }
534
535     /// Returns if this `id` has lint level information.
536     pub fn lint_level_set(&self, id: HirId) -> Option<u32> {
537         self.id_to_set.get(&id).cloned()
538     }
539 }
540
541 impl<'a> HashStable<StableHashingContext<'a>> for LintLevelMap {
542     #[inline]
543     fn hash_stable<W: StableHasherResult>(&self,
544                                           hcx: &mut StableHashingContext<'a>,
545                                           hasher: &mut StableHasher<W>) {
546         let LintLevelMap {
547             ref sets,
548             ref id_to_set,
549         } = *self;
550
551         id_to_set.hash_stable(hcx, hasher);
552
553         let LintLevelSets {
554             ref list,
555             lint_cap,
556         } = *sets;
557
558         lint_cap.hash_stable(hcx, hasher);
559
560         hcx.while_hashing_spans(true, |hcx| {
561             list.len().hash_stable(hcx, hasher);
562
563             // We are working under the assumption here that the list of
564             // lint-sets is built in a deterministic order.
565             for lint_set in list {
566                 ::std::mem::discriminant(lint_set).hash_stable(hcx, hasher);
567
568                 match *lint_set {
569                     LintSet::CommandLine { ref specs } => {
570                         specs.hash_stable(hcx, hasher);
571                     }
572                     LintSet::Node { ref specs, parent } => {
573                         specs.hash_stable(hcx, hasher);
574                         parent.hash_stable(hcx, hasher);
575                     }
576                 }
577             }
578         })
579     }
580 }
581
582 impl<HCX> HashStable<HCX> for LintId {
583     #[inline]
584     fn hash_stable<W: StableHasherResult>(&self,
585                                           hcx: &mut HCX,
586                                           hasher: &mut StableHasher<W>) {
587         self.lint_name_raw().hash_stable(hcx, hasher);
588     }
589 }
590
591 impl<HCX> ToStableHashKey<HCX> for LintId {
592     type KeyType = &'static str;
593
594     #[inline]
595     fn to_stable_hash_key(&self, _: &HCX) -> &'static str {
596         self.lint_name_raw()
597     }
598 }