Skip to content

Commit f894adc

Browse files
committed
implement dbg_macro rule (fixes rust-lang#3721)
1 parent 6ce78d1 commit f894adc

File tree

4 files changed

+68
-0
lines changed

4 files changed

+68
-0
lines changed

clippy_lints/src/dbg_macro.rs

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
2+
use rustc::{declare_tool_lint, lint_array};
3+
use crate::utils::span_lint;
4+
use syntax::ast;
5+
6+
/// **What it does:** Checks for usage of dbg!() macro not to have it in
7+
/// version control.
8+
///
9+
/// **Why is this bad?** `dbg!` macro is intended as a debugging tool.
10+
///
11+
/// **Known problems:** None.
12+
///
13+
/// **Example:**
14+
/// ```rust,ignore
15+
/// // Bad
16+
/// dbg!(true)
17+
///
18+
/// // Good
19+
/// true
20+
/// ```
21+
declare_clippy_lint! {
22+
pub DBG_MACRO,
23+
style,
24+
"`dbg!` macro is intended as a debugging tool"
25+
}
26+
27+
#[derive(Copy, Clone, Debug)]
28+
pub struct Pass;
29+
30+
impl LintPass for Pass {
31+
fn get_lints(&self) -> LintArray {
32+
lint_array!(DBG_MACRO)
33+
}
34+
35+
fn name(&self) -> &'static str {
36+
"DbgMacro"
37+
}
38+
}
39+
40+
impl EarlyLintPass for Pass {
41+
fn check_mac(&mut self, cx: &EarlyContext<'_>, mac: &ast::Mac) {
42+
if mac.node.path == "dbg" {
43+
span_lint(
44+
cx,
45+
DBG_MACRO,
46+
mac.span,
47+
"`dbg!` macro is intended as a debugging tool. ensure to avoid having uses of it in version control",
48+
);
49+
}
50+
}
51+
}

clippy_lints/src/lib.rs

+4
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ pub mod const_static_lifetime;
9494
pub mod copies;
9595
pub mod copy_iterator;
9696
pub mod cyclomatic_complexity;
97+
pub mod dbg_macro;
9798
pub mod default_trait_access;
9899
pub mod derive;
99100
pub mod doc;
@@ -231,6 +232,7 @@ pub fn register_pre_expansion_lints(
231232
},
232233
);
233234
store.register_pre_expansion_pass(Some(session), true, false, box attrs::CfgAttrPass);
235+
store.register_pre_expansion_pass(Some(session), true, false, box dbg_macro::Pass);
234236
}
235237

236238
pub fn read_conf(reg: &rustc_plugin::Registry<'_>) -> Conf {
@@ -589,6 +591,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
589591
copies::IFS_SAME_COND,
590592
copies::IF_SAME_THEN_ELSE,
591593
cyclomatic_complexity::CYCLOMATIC_COMPLEXITY,
594+
dbg_macro::DBG_MACRO,
592595
derive::DERIVE_HASH_XOR_EQ,
593596
double_comparison::DOUBLE_COMPARISONS,
594597
double_parens::DOUBLE_PARENS,
@@ -800,6 +803,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
800803
block_in_if_condition::BLOCK_IN_IF_CONDITION_STMT,
801804
collapsible_if::COLLAPSIBLE_IF,
802805
const_static_lifetime::CONST_STATIC_LIFETIME,
806+
dbg_macro::DBG_MACRO,
803807
enum_variants::ENUM_VARIANT_NAMES,
804808
enum_variants::MODULE_INCEPTION,
805809
eq_op::OP_REF,

tests/ui/dbg_macro.rs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
dbg!(42);
3+
}

tests/ui/dbg_macro.stderr

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

0 commit comments

Comments
 (0)