Skip to content

Commit f6d5786

Browse files
committed
Add new lint: unknwon_clippy_lintsg
1 parent 6256892 commit f6d5786

File tree

2 files changed

+87
-7
lines changed

2 files changed

+87
-7
lines changed

clippy_lints/src/attrs.rs

Lines changed: 85 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,20 @@
1212
1313
use crate::reexport::*;
1414
use crate::utils::{
15-
in_macro, last_line_of_span, match_def_path, opt_def_id, paths, snippet_opt, span_lint, span_lint_and_then,
16-
without_block_comments,
15+
in_macro, last_line_of_span, match_def_path, opt_def_id, paths, snippet_opt, span_lint,
16+
span_lint_and_then, without_block_comments,
1717
};
18-
use crate::rustc::hir::*;
19-
use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
20-
use crate::rustc::{declare_tool_lint, lint_array};
2118
use if_chain::if_chain;
19+
use crate::rustc::hir::*;
20+
use crate::rustc::lint::{
21+
CheckLintNameResult, LateContext, LateLintPass, LintArray, LintContext, LintPass,
22+
};
2223
use crate::rustc::ty::{self, TyCtxt};
24+
use crate::rustc::{declare_tool_lint, lint_array};
2325
use semver::Version;
24-
use crate::syntax::ast::{AttrStyle, Attribute, Lit, LitKind, MetaItemKind, NestedMetaItem, NestedMetaItemKind};
26+
use crate::syntax::ast::{
27+
AttrStyle, Attribute, Lit, LitKind, MetaItemKind, NestedMetaItem, NestedMetaItemKind,
28+
};
2529
use crate::syntax::source_map::Span;
2630
use crate::rustc_errors::Applicability;
2731

@@ -138,6 +142,33 @@ declare_clippy_lint! {
138142
"empty line after outer attribute"
139143
}
140144

145+
/// **What it does:** Checks for `allow`/`warn`/`deny`/`forbid` attributes with scoped clippy
146+
/// lints and if those lints exist in clippy. If there is a uppercase letter in the lint name
147+
/// (not the tool name) and a lowercase version of this lint exists, it will suggest to lowercase
148+
/// the lint name.
149+
///
150+
/// **Why is this bad?** An lint attribute with a misstyped lint name won't have an effect.
151+
///
152+
/// **Known problems:** None.
153+
///
154+
/// **Example:**
155+
/// Bad:
156+
/// ```rust
157+
/// #![warn(if_not_els)]
158+
/// #![deny(clippy::All)]
159+
/// ```
160+
///
161+
/// Good:
162+
/// ```rust
163+
/// #![warn(if_not_else)]
164+
/// #![deny(clippy::all)]
165+
/// ```
166+
declare_clippy_lint! {
167+
pub UNKNOWN_CLIPPY_LINTS,
168+
style,
169+
"unknown_lints for scoped Clippy lints"
170+
}
171+
141172
#[derive(Copy, Clone)]
142173
pub struct AttrPass;
143174

@@ -147,14 +178,21 @@ impl LintPass for AttrPass {
147178
INLINE_ALWAYS,
148179
DEPRECATED_SEMVER,
149180
USELESS_ATTRIBUTE,
150-
EMPTY_LINE_AFTER_OUTER_ATTR
181+
EMPTY_LINE_AFTER_OUTER_ATTR,
182+
UNKNOWN_CLIPPY_LINTS,
151183
)
152184
}
153185
}
154186

155187
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AttrPass {
156188
fn check_attribute(&mut self, cx: &LateContext<'a, 'tcx>, attr: &'tcx Attribute) {
157189
if let Some(ref items) = attr.meta_item_list() {
190+
match &*attr.name().as_str() {
191+
"allow" | "warn" | "deny" | "forbid" => {
192+
check_clippy_lint_names(cx, items);
193+
}
194+
_ => {}
195+
}
158196
if items.is_empty() || attr.name() != "deprecated" {
159197
return;
160198
}
@@ -247,6 +285,46 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AttrPass {
247285
}
248286
}
249287

288+
fn check_clippy_lint_names(cx: &LateContext<'_, '_>, items: &Vec<NestedMetaItem>) {
289+
let lint_store = cx.lints();
290+
for lint in items {
291+
if_chain! {
292+
if let Some(word) = lint.word();
293+
if let Some(tool_name) = word.is_scoped();
294+
if tool_name.as_str() == "clippy";
295+
let name = word.name();
296+
if let CheckLintNameResult::Tool(Err((None, _))) = lint_store.check_lint_name(
297+
&name.as_str(),
298+
Some(tool_name.as_str()),
299+
);
300+
then {
301+
span_lint_and_then(
302+
cx,
303+
UNKNOWN_CLIPPY_LINTS,
304+
lint.span,
305+
&format!("unknwon clippy lint: clippy::{}", name),
306+
|db| {
307+
if name.as_str().chars().any(|c| c.is_uppercase()) {
308+
let name_lower = name.as_str().to_lowercase().to_string();
309+
match lint_store.check_lint_name(
310+
&name_lower,
311+
Some(tool_name.as_str())
312+
) {
313+
CheckLintNameResult::NoLint => {}
314+
_ => {
315+
db.span_suggestion(lint.span,
316+
"lowercase the lint name",
317+
name_lower);
318+
}
319+
}
320+
}
321+
}
322+
);
323+
}
324+
};
325+
}
326+
}
327+
250328
fn is_relevant_item(tcx: TyCtxt<'_, '_, '_>, item: &Item) -> bool {
251329
if let ItemKind::Fn(_, _, _, eid) = item.node {
252330
is_relevant_expr(tcx, tcx.body_tables(eid), &tcx.hir.body(eid).value)

clippy_lints/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
533533
assign_ops::ASSIGN_OP_PATTERN,
534534
assign_ops::MISREFACTORED_ASSIGN_OP,
535535
attrs::DEPRECATED_SEMVER,
536+
attrs::UNKNOWN_CLIPPY_LINTS,
536537
attrs::USELESS_ATTRIBUTE,
537538
bit_mask::BAD_BIT_MASK,
538539
bit_mask::INEFFECTIVE_BIT_MASK,
@@ -749,6 +750,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
749750

750751
reg.register_lint_group("clippy::style", Some("clippy_style"), vec![
751752
assign_ops::ASSIGN_OP_PATTERN,
753+
attrs::UNKNOWN_CLIPPY_LINTS,
752754
bit_mask::VERBOSE_BIT_MASK,
753755
blacklisted_name::BLACKLISTED_NAME,
754756
block_in_if_condition::BLOCK_IN_IF_CONDITION_EXPR,

0 commit comments

Comments
 (0)