Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement dbg_macro rule #3723

Merged
merged 11 commits into from
Feb 3, 2019
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
rhysd marked this conversation as resolved.
Show resolved Hide resolved
/// 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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we need a whole new category for such lints at some point. We already have things like linting println! calls.

"`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" {
rhysd marked this conversation as resolved.
Show resolved Hide resolved
span_lint(
rhysd marked this conversation as resolved.
Show resolved Hide resolved
cx,
DBG_MACRO,
mac.span,
"`dbg!` macro is intended as a debugging tool. ensure to avoid having uses of it in version control",
rhysd marked this conversation as resolved.
Show resolved Hide resolved
);
}
}
}
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);
rhysd marked this conversation as resolved.
Show resolved Hide resolved
}
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