about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAlex Crichton <alex@alexcrichton.com>2017-08-13 05:37:24 -0700
committerAlex Crichton <alex@alexcrichton.com>2017-08-13 08:09:46 -0700
commit8b2bdc56d088d39e31100c182b3857dd7dc75f24 (patch)
tree67f51f84ecd35d0087ed69cc39b140b774c64237
parentf774bced502cf190426f778001a342715acb1d47 (diff)
downloadrust-8b2bdc56d088d39e31100c182b3857dd7dc75f24.tar.gz
rust-8b2bdc56d088d39e31100c182b3857dd7dc75f24.zip
rustc: Fix `unknown_lints` next to an unknown lint
The lint refactoring in #43522 didn't account for `#[allow(unknown_lints)]`
happening at the same node as an unknown lint itself, so this commit updates the
handling to ensure that the local set of lint configuration being built is
queried before looking at the chain of lint levels.

Closes #43809
-rw-r--r--src/librustc/lint/levels.rs65
-rw-r--r--src/test/run-pass/lint-unknown-lints-at-crate-level.rs16
2 files changed, 62 insertions, 19 deletions
diff --git a/src/librustc/lint/levels.rs b/src/librustc/lint/levels.rs
index ab9d4f75597..ab086e5b8e9 100644
--- a/src/librustc/lint/levels.rs
+++ b/src/librustc/lint/levels.rs
@@ -83,10 +83,13 @@ impl LintLevelSets {
         });
     }
 
-    fn get_lint_level(&self, lint: &'static Lint, idx: u32)
+    fn get_lint_level(&self,
+                      lint: &'static Lint,
+                      idx: u32,
+                      aux: Option<&FxHashMap<LintId, (Level, LintSource)>>)
         -> (Level, LintSource)
     {
-        let (level, mut src) = self.get_lint_id_level(LintId::of(lint), idx);
+        let (level, mut src) = self.get_lint_id_level(LintId::of(lint), idx, aux);
 
         // If `level` is none then we actually assume the default level for this
         // lint.
@@ -97,7 +100,9 @@ impl LintLevelSets {
         // `allow(warnings)` in scope then we want to respect that instead.
         if level == Level::Warn {
             let (warnings_level, warnings_src) =
-                self.get_lint_id_level(LintId::of(lint::builtin::WARNINGS), idx);
+                self.get_lint_id_level(LintId::of(lint::builtin::WARNINGS),
+                                       idx,
+                                       aux);
             if let Some(configured_warning_level) = warnings_level {
                 if configured_warning_level != Level::Warn {
                     level = configured_warning_level;
@@ -112,9 +117,17 @@ impl LintLevelSets {
         return (level, src)
     }
 
-    fn get_lint_id_level(&self, id: LintId, mut idx: u32)
+    fn get_lint_id_level(&self,
+                         id: LintId,
+                         mut idx: u32,
+                         aux: Option<&FxHashMap<LintId, (Level, LintSource)>>)
         -> (Option<Level>, LintSource)
     {
+        if let Some(specs) = aux {
+            if let Some(&(level, src)) = specs.get(&id) {
+                return (Some(level), src)
+            }
+        }
         loop {
             match self.list[idx as usize] {
                 LintSet::CommandLine { ref specs } => {
@@ -212,21 +225,35 @@ impl<'a> LintLevelsBuilder<'a> {
                             specs.insert(*id, (level, src));
                         }
                     }
+
+                    _ if !self.warn_about_weird_lints => {}
+
                     CheckLintNameResult::Warning(ref msg) => {
-                        if self.warn_about_weird_lints {
-                            self.struct_lint(builtin::RENAMED_AND_REMOVED_LINTS,
-                                             Some(li.span.into()),
-                                             msg)
-                                .emit();
-                        }
+                        let lint = builtin::RENAMED_AND_REMOVED_LINTS;
+                        let (level, src) = self.sets.get_lint_level(lint,
+                                                                    self.cur,
+                                                                    Some(&specs));
+                        lint::struct_lint_level(self.sess,
+                                                lint,
+                                                level,
+                                                src,
+                                                Some(li.span.into()),
+                                                msg)
+                            .emit();
                     }
                     CheckLintNameResult::NoLint => {
-                        if self.warn_about_weird_lints {
-                            self.struct_lint(builtin::UNKNOWN_LINTS,
-                                             Some(li.span.into()),
-                                             &format!("unknown lint: `{}`", name))
-                                .emit();
-                        }
+                        let lint = builtin::UNKNOWN_LINTS;
+                        let (level, src) = self.sets.get_lint_level(lint,
+                                                                    self.cur,
+                                                                    Some(&specs));
+                        let msg = format!("unknown lint: `{}`", name);
+                        lint::struct_lint_level(self.sess,
+                                                lint,
+                                                level,
+                                                src,
+                                                Some(li.span.into()),
+                                                &msg)
+                            .emit();
                     }
                 }
             }
@@ -236,7 +263,7 @@ impl<'a> LintLevelsBuilder<'a> {
             if level == Level::Forbid {
                 continue
             }
-            let forbid_src = match self.sets.get_lint_id_level(*id, self.cur) {
+            let forbid_src = match self.sets.get_lint_id_level(*id, self.cur, None) {
                 (Some(Level::Forbid), src) => src,
                 _ => continue,
             };
@@ -298,7 +325,7 @@ impl<'a> LintLevelsBuilder<'a> {
                        msg: &str)
         -> DiagnosticBuilder<'a>
     {
-        let (level, src) = self.sets.get_lint_level(lint, self.cur);
+        let (level, src) = self.sets.get_lint_level(lint, self.cur, None);
         lint::struct_lint_level(self.sess, lint, level, src, span, msg)
     }
 
@@ -337,7 +364,7 @@ impl LintLevelMap {
         -> Option<(Level, LintSource)>
     {
         self.id_to_set.get(&id).map(|idx| {
-            self.sets.get_lint_level(lint, *idx)
+            self.sets.get_lint_level(lint, *idx, None)
         })
     }
 }
diff --git a/src/test/run-pass/lint-unknown-lints-at-crate-level.rs b/src/test/run-pass/lint-unknown-lints-at-crate-level.rs
new file mode 100644
index 00000000000..1e510c28e78
--- /dev/null
+++ b/src/test/run-pass/lint-unknown-lints-at-crate-level.rs
@@ -0,0 +1,16 @@
+// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+// compile-flags: -D warnings -D unknown-lints
+
+#![allow(unknown_lints)]
+#![allow(random_lint_name)]
+
+fn main() {}