about summary refs log tree commit diff
diff options
context:
space:
mode:
authorEric Holk <ericholk@microsoft.com>2021-06-24 16:38:32 -0700
committerEric Holk <ericholk@microsoft.com>2021-07-06 15:50:10 -0700
commit1e0db4cfedcb9ecdc8df62aac22bec00a5c3c1b5 (patch)
tree1a1f730f67b7091eb04266b16f4b52704f4a4dbf
parent65b28a987bfce11c37634fff2be129ffc16096d5 (diff)
downloadrust-1e0db4cfedcb9ecdc8df62aac22bec00a5c3c1b5.tar.gz
rust-1e0db4cfedcb9ecdc8df62aac22bec00a5c3c1b5.zip
Parse tool name for command line lint options
-rw-r--r--compiler/rustc_lint/src/context.rs20
-rw-r--r--compiler/rustc_lint/src/lib.rs3
-rw-r--r--compiler/rustc_lint/src/tests.rs24
3 files changed, 44 insertions, 3 deletions
diff --git a/compiler/rustc_lint/src/context.rs b/compiler/rustc_lint/src/context.rs
index 00869ac3c9a..76ac4cfdf84 100644
--- a/compiler/rustc_lint/src/context.rs
+++ b/compiler/rustc_lint/src/context.rs
@@ -334,9 +334,16 @@ impl LintStore {
         }
     }
 
-    /// Checks the validity of lint names derived from the command line
-    pub fn check_lint_name_cmdline(&self, sess: &Session, lint_name: &str, level: Level) {
-        let db = match self.check_lint_name(lint_name, None) {
+    /// Checks the validity of lint names derived from the command line. Returns
+    /// true if the lint is valid, false otherwise.
+    pub fn check_lint_name_cmdline(
+        &self,
+        sess: &Session,
+        lint_name: &str,
+        level: Option<Level>,
+    ) -> bool {
+        let (tool_name, lint_name) = parse_lint_and_tool_name(lint_name);
+        let db = match self.check_lint_name(lint_name, tool_name) {
             CheckLintNameResult::Ok(_) => None,
             CheckLintNameResult::Warning(ref msg, _) => Some(sess.struct_warn(msg)),
             CheckLintNameResult::NoLint(suggestion) => {
@@ -1018,3 +1025,10 @@ impl<'tcx> LayoutOf for LateContext<'tcx> {
         self.tcx.layout_of(self.param_env.and(ty))
     }
 }
+
+pub fn parse_lint_and_tool_name(lint_name: &str) -> (Option<Symbol>, &str) {
+    match lint_name.split_once("::") {
+        Some((tool_name, lint_name)) => (Some(Symbol::intern(tool_name)), lint_name),
+        None => (None, lint_name),
+    }
+}
diff --git a/compiler/rustc_lint/src/lib.rs b/compiler/rustc_lint/src/lib.rs
index 89f9809d643..6f0cedca90c 100644
--- a/compiler/rustc_lint/src/lib.rs
+++ b/compiler/rustc_lint/src/lib.rs
@@ -494,3 +494,6 @@ fn register_internals(store: &mut LintStore) {
         ],
     );
 }
+
+#[cfg(test)]
+mod tests;
diff --git a/compiler/rustc_lint/src/tests.rs b/compiler/rustc_lint/src/tests.rs
new file mode 100644
index 00000000000..a50c88aa0f7
--- /dev/null
+++ b/compiler/rustc_lint/src/tests.rs
@@ -0,0 +1,24 @@
+use crate::context::parse_lint_and_tool_name;
+use rustc_span::{with_default_session_globals, Symbol};
+
+#[test]
+fn parse_lint_no_tool() {
+    with_default_session_globals(|| assert_eq!(parse_lint_and_tool_name("foo"), (None, "foo")));
+}
+
+#[test]
+fn parse_lint_with_tool() {
+    with_default_session_globals(|| {
+        assert_eq!(parse_lint_and_tool_name("clippy::foo"), (Some(Symbol::intern("clippy")), "foo"))
+    });
+}
+
+#[test]
+fn parse_lint_multiple_path() {
+    with_default_session_globals(|| {
+        assert_eq!(
+            parse_lint_and_tool_name("clippy::foo::bar"),
+            (Some(Symbol::intern("clippy")), "foo::bar")
+        )
+    });
+}