implement dbg_macro rule (fixes #3721)

This commit is contained in:
rhysd 2019-01-31 02:39:38 +09:00
parent 6ce78d1257
commit f894adce8c
4 changed files with 68 additions and 0 deletions

View File

@ -0,0 +1,51 @@
use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
use rustc::{declare_tool_lint, lint_array};
use crate::utils::span_lint;
use syntax::ast;
/// **What it does:** Checks for usage of dbg!() macro not to have it in
/// version control.
///
/// **Why is this bad?** `dbg!` macro is intended as a debugging tool.
///
/// **Known problems:** None.
///
/// **Example:**
/// ```rust,ignore
/// // Bad
/// dbg!(true)
///
/// // Good
/// true
/// ```
declare_clippy_lint! {
pub DBG_MACRO,
style,
"`dbg!` macro is intended as a debugging tool"
}
#[derive(Copy, Clone, Debug)]
pub struct Pass;
impl LintPass for Pass {
fn get_lints(&self) -> LintArray {
lint_array!(DBG_MACRO)
}
fn name(&self) -> &'static str {
"DbgMacro"
}
}
impl EarlyLintPass for Pass {
fn check_mac(&mut self, cx: &EarlyContext<'_>, mac: &ast::Mac) {
if mac.node.path == "dbg" {
span_lint(
cx,
DBG_MACRO,
mac.span,
"`dbg!` macro is intended as a debugging tool. ensure to avoid having uses of it in version control",
);
}
}
}

View File

@ -94,6 +94,7 @@ pub mod const_static_lifetime;
pub mod copies;
pub mod copy_iterator;
pub mod cyclomatic_complexity;
pub mod dbg_macro;
pub mod default_trait_access;
pub mod derive;
pub mod doc;
@ -231,6 +232,7 @@ pub fn register_pre_expansion_lints(
},
);
store.register_pre_expansion_pass(Some(session), true, false, box attrs::CfgAttrPass);
store.register_pre_expansion_pass(Some(session), true, false, box dbg_macro::Pass);
}
pub fn read_conf(reg: &rustc_plugin::Registry<'_>) -> Conf {
@ -589,6 +591,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
copies::IFS_SAME_COND,
copies::IF_SAME_THEN_ELSE,
cyclomatic_complexity::CYCLOMATIC_COMPLEXITY,
dbg_macro::DBG_MACRO,
derive::DERIVE_HASH_XOR_EQ,
double_comparison::DOUBLE_COMPARISONS,
double_parens::DOUBLE_PARENS,
@ -800,6 +803,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
block_in_if_condition::BLOCK_IN_IF_CONDITION_STMT,
collapsible_if::COLLAPSIBLE_IF,
const_static_lifetime::CONST_STATIC_LIFETIME,
dbg_macro::DBG_MACRO,
enum_variants::ENUM_VARIANT_NAMES,
enum_variants::MODULE_INCEPTION,
eq_op::OP_REF,

3
tests/ui/dbg_macro.rs Normal file
View File

@ -0,0 +1,3 @@
fn main() {
dbg!(42);
}

10
tests/ui/dbg_macro.stderr Normal file
View File

@ -0,0 +1,10 @@
error: `dbg!` macro is intended as a debugging tool. ensure to avoid having uses of it in version control
--> $DIR/dbg_macro.rs:2:5
|
LL | dbg!(42);
| ^^^^^^^^
|
= note: `-D clippy::dbg-macro` implied by `-D warnings`
error: aborting due to previous error