Skip to content

Commit efceda2

Browse files
committed
Auto merge of #44104 - llogiq:lowercase-lints, r=nikomatsakis
add a lowercase suggestion to unknown_lints I recently wrote some tests for a clippy lint, copied the (uppercase) lint name into my test file and forgot to toggle the case. This PR adds a suggestion that would have saved me 10 minutes of debugging, so it's likely a net win 🙂 . Also it adds a UI test for the `unknown_lints` lint.
2 parents f861b6e + ba643fa commit efceda2

File tree

3 files changed

+58
-3
lines changed

3 files changed

+58
-3
lines changed

src/librustc/lint/levels.rs

+17-3
Original file line numberDiff line numberDiff line change
@@ -247,13 +247,27 @@ impl<'a> LintLevelsBuilder<'a> {
247247
self.cur,
248248
Some(&specs));
249249
let msg = format!("unknown lint: `{}`", name);
250-
lint::struct_lint_level(self.sess,
250+
let mut db = lint::struct_lint_level(self.sess,
251251
lint,
252252
level,
253253
src,
254254
Some(li.span.into()),
255-
&msg)
256-
.emit();
255+
&msg);
256+
if name.as_str().chars().any(|c| c.is_uppercase()) {
257+
let name_lower = name.as_str().to_lowercase();
258+
if let CheckLintNameResult::NoLint =
259+
store.check_lint_name(&name_lower) {
260+
db.emit();
261+
} else {
262+
db.span_suggestion(
263+
li.span,
264+
"lowercase the lint name",
265+
name_lower
266+
).emit();
267+
}
268+
} else {
269+
db.emit();
270+
}
257271
}
258272
}
259273
}

src/test/ui/lint/not_found.rs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2014–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+
// this tests the `unknown_lint` lint, especially the suggestions
12+
13+
// the suggestion only appears if a lint with the lowercase name exists
14+
#[allow(FOO_BAR)]
15+
// the suggestion appears on all-uppercase names
16+
#[warn(DEAD_CODE)]
17+
// the suggestion appears also on mixed-case names
18+
#[deny(Warnings)]
19+
fn main() {
20+
unimplemented!();
21+
}

src/test/ui/lint/not_found.stderr

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
warning: unknown lint: `FOO_BAR`
2+
--> $DIR/not_found.rs:14:9
3+
|
4+
14 | #[allow(FOO_BAR)]
5+
| ^^^^^^^
6+
|
7+
= note: #[warn(unknown_lints)] on by default
8+
9+
warning: unknown lint: `DEAD_CODE`
10+
--> $DIR/not_found.rs:16:8
11+
|
12+
16 | #[warn(DEAD_CODE)]
13+
| ^^^^^^^^^ help: lowercase the lint name: `dead_code`
14+
15+
warning: unknown lint: `Warnings`
16+
--> $DIR/not_found.rs:18:8
17+
|
18+
18 | #[deny(Warnings)]
19+
| ^^^^^^^^ help: lowercase the lint name: `warnings`
20+

0 commit comments

Comments
 (0)