Skip to content

Commit 57c7742

Browse files
committed
Check if the lint_name is from a tool and if the tool_lint exists
1 parent 4b466ee commit 57c7742

File tree

2 files changed

+71
-29
lines changed

2 files changed

+71
-29
lines changed

src/librustc/lint/context.rs

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ use util::nodemap::FxHashMap;
4242
use std::default::Default as StdDefault;
4343
use syntax::ast;
4444
use syntax::edition;
45-
use syntax_pos::{MultiSpan, Span};
45+
use syntax_pos::{MultiSpan, Span, symbol::LocalInternedString};
4646
use errors::DiagnosticBuilder;
4747
use hir;
4848
use hir::def_id::LOCAL_CRATE;
@@ -133,6 +133,12 @@ pub enum CheckLintNameResult<'a> {
133133
/// The lint is either renamed or removed. This is the warning
134134
/// message, and an optional new name (`None` if removed).
135135
Warning(String, Option<String>),
136+
/// The lint is from a tool. If the Option is None, then either
137+
/// the lint does not exist in the tool or the code was not
138+
/// compiled with the tool and therefore the lint was never
139+
/// added to the `LintStore`. Otherwise the `LintId` will be
140+
/// returned as if it where a rustc lint.
141+
Tool(Option<&'a [LintId]>),
136142
}
137143

138144
impl LintStore {
@@ -288,14 +294,15 @@ impl LintStore {
288294
sess: &Session,
289295
lint_name: &str,
290296
level: Level) {
291-
let db = match self.check_lint_name(lint_name) {
297+
let db = match self.check_lint_name(lint_name, None) {
292298
CheckLintNameResult::Ok(_) => None,
293299
CheckLintNameResult::Warning(ref msg, _) => {
294300
Some(sess.struct_warn(msg))
295301
},
296302
CheckLintNameResult::NoLint => {
297303
Some(struct_err!(sess, E0602, "unknown lint: `{}`", lint_name))
298304
}
305+
CheckLintNameResult::Tool(_) => unreachable!(),
299306
};
300307

301308
if let Some(mut db) = db {
@@ -319,26 +326,41 @@ impl LintStore {
319326
/// it emits non-fatal warnings and there are *two* lint passes that
320327
/// inspect attributes, this is only run from the late pass to avoid
321328
/// printing duplicate warnings.
322-
pub fn check_lint_name(&self, lint_name: &str) -> CheckLintNameResult {
323-
match self.by_name.get(lint_name) {
324-
Some(&Renamed(ref new_name, _)) => {
325-
CheckLintNameResult::Warning(
326-
format!("lint `{}` has been renamed to `{}`", lint_name, new_name),
327-
Some(new_name.to_owned())
328-
)
329-
},
330-
Some(&Removed(ref reason)) => {
331-
CheckLintNameResult::Warning(
332-
format!("lint `{}` has been removed: `{}`", lint_name, reason),
333-
None
334-
)
335-
},
336-
None => {
337-
match self.lint_groups.get(lint_name) {
338-
None => CheckLintNameResult::NoLint,
339-
Some(ids) => CheckLintNameResult::Ok(&ids.0),
340-
}
329+
pub fn check_lint_name(
330+
&self,
331+
lint_name: &str,
332+
tool_name: Option<LocalInternedString>,
333+
) -> CheckLintNameResult {
334+
let complete_name = if let Some(tool_name) = tool_name {
335+
format!("{}::{}", tool_name, lint_name)
336+
} else {
337+
lint_name.to_string()
338+
};
339+
if let Some(_) = tool_name {
340+
match self.by_name.get(&complete_name) {
341+
None => match self.lint_groups.get(&*complete_name) {
342+
None => return CheckLintNameResult::Tool(None),
343+
Some(ids) => return CheckLintNameResult::Tool(Some(&ids.0)),
344+
},
345+
Some(&Id(ref id)) => return CheckLintNameResult::Tool(Some(slice::from_ref(id))),
346+
// If the lint was registered as removed or renamed by the lint tool, we don't need
347+
// to treat tool_lints and rustc lints different and can use the code below.
348+
_ => {}
341349
}
350+
}
351+
match self.by_name.get(&complete_name) {
352+
Some(&Renamed(ref new_name, _)) => CheckLintNameResult::Warning(
353+
format!("lint `{}` has been renamed to `{}`", lint_name, new_name),
354+
Some(new_name.to_owned()),
355+
),
356+
Some(&Removed(ref reason)) => CheckLintNameResult::Warning(
357+
format!("lint `{}` has been removed: `{}`", lint_name, reason),
358+
None,
359+
),
360+
None => match self.lint_groups.get(&*complete_name) {
361+
None => CheckLintNameResult::NoLint,
362+
Some(ids) => CheckLintNameResult::Ok(&ids.0),
363+
},
342364
Some(&Id(ref id)) => CheckLintNameResult::Ok(slice::from_ref(id)),
343365
}
344366
}

src/librustc/lint/levels.rs

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -227,17 +227,18 @@ impl<'a> LintLevelsBuilder<'a> {
227227
continue
228228
}
229229
};
230-
if let Some(lint_tool) = word.is_scoped() {
231-
if !self.sess.features_untracked().tool_lints {
230+
let tool_name = if let Some(lint_tool) = word.is_scoped() {
231+
let gate_feature = !self.sess.features_untracked().tool_lints;
232+
let known_tool = attr::is_known_lint_tool(lint_tool);
233+
if gate_feature {
232234
feature_gate::emit_feature_err(&sess.parse_sess,
233235
"tool_lints",
234236
word.span,
235237
feature_gate::GateIssue::Language,
236238
&format!("scoped lint `{}` is experimental",
237239
word.ident));
238240
}
239-
240-
if !attr::is_known_lint_tool(lint_tool) {
241+
if !known_tool {
241242
span_err!(
242243
sess,
243244
lint_tool.span,
@@ -247,17 +248,36 @@ impl<'a> LintLevelsBuilder<'a> {
247248
);
248249
}
249250

250-
continue
251-
}
251+
if gate_feature || !known_tool {
252+
continue
253+
}
254+
255+
Some(lint_tool.as_str())
256+
} else {
257+
None
258+
};
252259
let name = word.name();
253-
match store.check_lint_name(&name.as_str()) {
260+
match store.check_lint_name(&name.as_str(), tool_name) {
254261
CheckLintNameResult::Ok(ids) => {
255262
let src = LintSource::Node(name, li.span);
256263
for id in ids {
257264
specs.insert(*id, (level, src));
258265
}
259266
}
260267

268+
CheckLintNameResult::Tool(result) => {
269+
if let Some(ids) = result {
270+
let complete_name = &format!("{}::{}", tool_name.unwrap(), name);
271+
let src = LintSource::Node(Symbol::intern(complete_name), li.span);
272+
for id in ids {
273+
specs.insert(*id, (level, src));
274+
}
275+
}
276+
//FIXME: if Tool(None) is returned than the lint either does not exist in
277+
//the lint tool or the code doesn't get compiled with the lint tool and
278+
//therefore the lint cannot exist.
279+
}
280+
261281
_ if !self.warn_about_weird_lints => {}
262282

263283
CheckLintNameResult::Warning(msg, renamed) => {
@@ -298,7 +318,7 @@ impl<'a> LintLevelsBuilder<'a> {
298318
if name.as_str().chars().any(|c| c.is_uppercase()) {
299319
let name_lower = name.as_str().to_lowercase().to_string();
300320
if let CheckLintNameResult::NoLint =
301-
store.check_lint_name(&name_lower) {
321+
store.check_lint_name(&name_lower, tool_name) {
302322
db.emit();
303323
} else {
304324
db.span_suggestion_with_applicability(

0 commit comments

Comments
 (0)