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