Skip to content

Commit 1e0db4c

Browse files
committed
Parse tool name for command line lint options
1 parent 65b28a9 commit 1e0db4c

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

compiler/rustc_lint/src/context.rs

+17-3
Original file line numberDiff line numberDiff line change
@@ -334,9 +334,16 @@ impl LintStore {
334334
}
335335
}
336336

337-
/// Checks the validity of lint names derived from the command line
338-
pub fn check_lint_name_cmdline(&self, sess: &Session, lint_name: &str, level: Level) {
339-
let db = match self.check_lint_name(lint_name, None) {
337+
/// Checks the validity of lint names derived from the command line. Returns
338+
/// true if the lint is valid, false otherwise.
339+
pub fn check_lint_name_cmdline(
340+
&self,
341+
sess: &Session,
342+
lint_name: &str,
343+
level: Option<Level>,
344+
) -> bool {
345+
let (tool_name, lint_name) = parse_lint_and_tool_name(lint_name);
346+
let db = match self.check_lint_name(lint_name, tool_name) {
340347
CheckLintNameResult::Ok(_) => None,
341348
CheckLintNameResult::Warning(ref msg, _) => Some(sess.struct_warn(msg)),
342349
CheckLintNameResult::NoLint(suggestion) => {
@@ -1018,3 +1025,10 @@ impl<'tcx> LayoutOf for LateContext<'tcx> {
10181025
self.tcx.layout_of(self.param_env.and(ty))
10191026
}
10201027
}
1028+
1029+
pub fn parse_lint_and_tool_name(lint_name: &str) -> (Option<Symbol>, &str) {
1030+
match lint_name.split_once("::") {
1031+
Some((tool_name, lint_name)) => (Some(Symbol::intern(tool_name)), lint_name),
1032+
None => (None, lint_name),
1033+
}
1034+
}

compiler/rustc_lint/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -494,3 +494,6 @@ fn register_internals(store: &mut LintStore) {
494494
],
495495
);
496496
}
497+
498+
#[cfg(test)]
499+
mod tests;

compiler/rustc_lint/src/tests.rs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use crate::context::parse_lint_and_tool_name;
2+
use rustc_span::{with_default_session_globals, Symbol};
3+
4+
#[test]
5+
fn parse_lint_no_tool() {
6+
with_default_session_globals(|| assert_eq!(parse_lint_and_tool_name("foo"), (None, "foo")));
7+
}
8+
9+
#[test]
10+
fn parse_lint_with_tool() {
11+
with_default_session_globals(|| {
12+
assert_eq!(parse_lint_and_tool_name("clippy::foo"), (Some(Symbol::intern("clippy")), "foo"))
13+
});
14+
}
15+
16+
#[test]
17+
fn parse_lint_multiple_path() {
18+
with_default_session_globals(|| {
19+
assert_eq!(
20+
parse_lint_and_tool_name("clippy::foo::bar"),
21+
(Some(Symbol::intern("clippy")), "foo::bar")
22+
)
23+
});
24+
}

0 commit comments

Comments
 (0)