Skip to content

Commit

Permalink
implement dbg_macro rule (fixes #3721)
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysd committed Jan 30, 2019
1 parent 6ce78d1 commit f894adc
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
51 changes: 51 additions & 0 deletions clippy_lints/src/dbg_macro.rs
Original file line number Diff line number Diff line change
@@ -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",
);
}
}
}
4 changes: 4 additions & 0 deletions clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions tests/ui/dbg_macro.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
dbg!(42);
}
10 changes: 10 additions & 0 deletions tests/ui/dbg_macro.stderr
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit f894adc

Please sign in to comment.