Skip to content

Commit

Permalink
Add cfg_attr lint
Browse files Browse the repository at this point in the history
  • Loading branch information
flip1995 committed Nov 1, 2018
1 parent 6a165e5 commit 1f9c0a9
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,7 @@ All notable changes to this project will be documented in this file.
[`decimal_literal_representation`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#decimal_literal_representation
[`declare_interior_mutable_const`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#declare_interior_mutable_const
[`default_trait_access`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#default_trait_access
[`deprecated_cfg_attr`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#deprecated_cfg_attr
[`deprecated_semver`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#deprecated_semver
[`deref_addrof`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#deref_addrof
[`derive_hash_xor_eq`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#derive_hash_xor_eq
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ We are currently in the process of discussing Clippy 1.0 via the RFC process in

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

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

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

Expand Down
77 changes: 75 additions & 2 deletions clippy_lints/src/attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
use crate::reexport::*;
use crate::utils::{
in_macro, last_line_of_span, match_def_path, opt_def_id, paths, snippet_opt, span_lint, span_lint_and_then,
without_block_comments,
span_lint_and_sugg, without_block_comments,
};
use crate::rustc::hir::*;
use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
use crate::rustc::lint::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintArray, LintPass};
use crate::rustc::{declare_tool_lint, lint_array};
use if_chain::if_chain;
use crate::rustc::ty::{self, TyCtxt};
Expand Down Expand Up @@ -138,6 +138,19 @@ declare_clippy_lint! {
"empty line after outer attribute"
}

/// **What it does:**
///
/// **Why is this bad?**
///
/// **Known problems:**
///
/// **Example:**
declare_clippy_lint! {
pub DEPRECATED_CFG_ATTR,
complexity,
"usage of `cfg_attr` instead of `tool_lints`"
}

#[derive(Copy, Clone)]
pub struct AttrPass;

Expand Down Expand Up @@ -387,3 +400,63 @@ fn is_present_in_source(cx: &LateContext<'_, '_>, span: Span) -> bool {
}
true
}

#[derive(Copy, Clone)]
pub struct CfgAttrPass;

impl LintPass for CfgAttrPass {
fn get_lints(&self) -> LintArray {
lint_array!(
DEPRECATED_CFG_ATTR,
)
}
}

impl EarlyLintPass for CfgAttrPass {
fn check_attribute(&mut self, cx: &EarlyContext<'_>, attr: &Attribute) {
if_chain! {
// check cfg_attr
if attr.name() == "cfg_attr";
if let Some(ref items) = attr.meta_item_list();
if items.len() == 2;
// check for `feature = "cargo-clippy"`
if let Some(feature_item) = items[0].meta_item();
if let Some(value) = feature_item.value_str();
if value == "cargo-clippy";
// check for `allow(..)`/... and retrieve the lints
let level = &items[1];
if let Some(name) = level.name();
if name == "allow" || name == "warn" || name == "deny" || name == "forbid";
if let Some(level_items) = level.meta_item_list();
if level_items.iter().all(|item| {
if let Some(meta_item) = item.meta_item() {
meta_item.is_word()
} else {
false
}
});
then {
let attr_style = match attr.style {
AttrStyle::Outer => "#[",
AttrStyle::Inner => "#![",
};
let level = name.as_str();
let mut lints = level_items.iter().fold(String::new(), |acc, item| {
acc + &format!("clippy::{}, ", item.name().unwrap().as_str())
});
let lints_len = lints.len();
lints.truncate(lints_len - 2);
let sugg = format!("{}{}({})]", attr_style, level, lints);
span_lint_and_sugg(
cx,
DEPRECATED_CFG_ATTR,
attr.span,
"`cfg_attr` is deprecated for clippy and got replaced by tool_lints",
"use",
sugg,
);
}
}
}
}

3 changes: 3 additions & 0 deletions clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ pub fn register_pre_expansion_lints(session: &rustc::session::Session, store: &m
store.register_pre_expansion_pass(Some(session), box non_expressive_names::NonExpressiveNames {
single_char_binding_names_threshold: conf.single_char_binding_names_threshold,
});
store.register_pre_expansion_pass(Some(session), box attrs::CfgAttrPass);
}

pub fn read_conf(reg: &rustc_plugin::Registry<'_>) -> Conf {
Expand Down Expand Up @@ -531,6 +532,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
approx_const::APPROX_CONSTANT,
assign_ops::ASSIGN_OP_PATTERN,
assign_ops::MISREFACTORED_ASSIGN_OP,
attrs::DEPRECATED_CFG_ATTR,
attrs::DEPRECATED_SEMVER,
attrs::USELESS_ATTRIBUTE,
bit_mask::BAD_BIT_MASK,
Expand Down Expand Up @@ -833,6 +835,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {

reg.register_lint_group("clippy::complexity", Some("clippy_complexity"), vec![
assign_ops::MISREFACTORED_ASSIGN_OP,
attrs::DEPRECATED_CFG_ATTR,
booleans::NONMINIMAL_BOOL,
cyclomatic_complexity::CYCLOMATIC_COMPLEXITY,
double_comparison::DOUBLE_COMPARISONS,
Expand Down

0 comments on commit 1f9c0a9

Please sign in to comment.