Skip to content

Commit 1f9c0a9

Browse files
committed
Add cfg_attr lint
1 parent 6a165e5 commit 1f9c0a9

File tree

4 files changed

+80
-3
lines changed

4 files changed

+80
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,7 @@ All notable changes to this project will be documented in this file.
651651
[`decimal_literal_representation`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#decimal_literal_representation
652652
[`declare_interior_mutable_const`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#declare_interior_mutable_const
653653
[`default_trait_access`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#default_trait_access
654+
[`deprecated_cfg_attr`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#deprecated_cfg_attr
654655
[`deprecated_semver`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#deprecated_semver
655656
[`deref_addrof`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#deref_addrof
656657
[`derive_hash_xor_eq`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#derive_hash_xor_eq

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ We are currently in the process of discussing Clippy 1.0 via the RFC process in
99

1010
A collection of lints to catch common mistakes and improve your [Rust](https://github.com/rust-lang/rust) code.
1111

12-
[There are 283 lints included in this crate!](https://rust-lang-nursery.github.io/rust-clippy/master/index.html)
12+
[There are 284 lints included in this crate!](https://rust-lang-nursery.github.io/rust-clippy/master/index.html)
1313

1414
We have a bunch of lint categories to allow you to choose how much Clippy is supposed to ~~annoy~~ help you:
1515

clippy_lints/src/attrs.rs

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
use crate::reexport::*;
1414
use crate::utils::{
1515
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,
16+
span_lint_and_sugg, without_block_comments,
1717
};
1818
use crate::rustc::hir::*;
19-
use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
19+
use crate::rustc::lint::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintArray, LintPass};
2020
use crate::rustc::{declare_tool_lint, lint_array};
2121
use if_chain::if_chain;
2222
use crate::rustc::ty::{self, TyCtxt};
@@ -138,6 +138,19 @@ declare_clippy_lint! {
138138
"empty line after outer attribute"
139139
}
140140

141+
/// **What it does:**
142+
///
143+
/// **Why is this bad?**
144+
///
145+
/// **Known problems:**
146+
///
147+
/// **Example:**
148+
declare_clippy_lint! {
149+
pub DEPRECATED_CFG_ATTR,
150+
complexity,
151+
"usage of `cfg_attr` instead of `tool_lints`"
152+
}
153+
141154
#[derive(Copy, Clone)]
142155
pub struct AttrPass;
143156

@@ -387,3 +400,63 @@ fn is_present_in_source(cx: &LateContext<'_, '_>, span: Span) -> bool {
387400
}
388401
true
389402
}
403+
404+
#[derive(Copy, Clone)]
405+
pub struct CfgAttrPass;
406+
407+
impl LintPass for CfgAttrPass {
408+
fn get_lints(&self) -> LintArray {
409+
lint_array!(
410+
DEPRECATED_CFG_ATTR,
411+
)
412+
}
413+
}
414+
415+
impl EarlyLintPass for CfgAttrPass {
416+
fn check_attribute(&mut self, cx: &EarlyContext<'_>, attr: &Attribute) {
417+
if_chain! {
418+
// check cfg_attr
419+
if attr.name() == "cfg_attr";
420+
if let Some(ref items) = attr.meta_item_list();
421+
if items.len() == 2;
422+
// check for `feature = "cargo-clippy"`
423+
if let Some(feature_item) = items[0].meta_item();
424+
if let Some(value) = feature_item.value_str();
425+
if value == "cargo-clippy";
426+
// check for `allow(..)`/... and retrieve the lints
427+
let level = &items[1];
428+
if let Some(name) = level.name();
429+
if name == "allow" || name == "warn" || name == "deny" || name == "forbid";
430+
if let Some(level_items) = level.meta_item_list();
431+
if level_items.iter().all(|item| {
432+
if let Some(meta_item) = item.meta_item() {
433+
meta_item.is_word()
434+
} else {
435+
false
436+
}
437+
});
438+
then {
439+
let attr_style = match attr.style {
440+
AttrStyle::Outer => "#[",
441+
AttrStyle::Inner => "#![",
442+
};
443+
let level = name.as_str();
444+
let mut lints = level_items.iter().fold(String::new(), |acc, item| {
445+
acc + &format!("clippy::{}, ", item.name().unwrap().as_str())
446+
});
447+
let lints_len = lints.len();
448+
lints.truncate(lints_len - 2);
449+
let sugg = format!("{}{}({})]", attr_style, level, lints);
450+
span_lint_and_sugg(
451+
cx,
452+
DEPRECATED_CFG_ATTR,
453+
attr.span,
454+
"`cfg_attr` is deprecated for clippy and got replaced by tool_lints",
455+
"use",
456+
sugg,
457+
);
458+
}
459+
}
460+
}
461+
}
462+

clippy_lints/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ pub fn register_pre_expansion_lints(session: &rustc::session::Session, store: &m
220220
store.register_pre_expansion_pass(Some(session), box non_expressive_names::NonExpressiveNames {
221221
single_char_binding_names_threshold: conf.single_char_binding_names_threshold,
222222
});
223+
store.register_pre_expansion_pass(Some(session), box attrs::CfgAttrPass);
223224
}
224225

225226
pub fn read_conf(reg: &rustc_plugin::Registry<'_>) -> Conf {
@@ -531,6 +532,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
531532
approx_const::APPROX_CONSTANT,
532533
assign_ops::ASSIGN_OP_PATTERN,
533534
assign_ops::MISREFACTORED_ASSIGN_OP,
535+
attrs::DEPRECATED_CFG_ATTR,
534536
attrs::DEPRECATED_SEMVER,
535537
attrs::USELESS_ATTRIBUTE,
536538
bit_mask::BAD_BIT_MASK,
@@ -833,6 +835,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
833835

834836
reg.register_lint_group("clippy::complexity", Some("clippy_complexity"), vec![
835837
assign_ops::MISREFACTORED_ASSIGN_OP,
838+
attrs::DEPRECATED_CFG_ATTR,
836839
booleans::NONMINIMAL_BOOL,
837840
cyclomatic_complexity::CYCLOMATIC_COMPLEXITY,
838841
double_comparison::DOUBLE_COMPARISONS,

0 commit comments

Comments
 (0)