Skip to content

Commit 273b7f7

Browse files
committed
Auto merge of #3723 - rhysd:issue3721, r=oli-obk
Implement dbg_macro rule Fixes #3721 This patch adds new `dbg_macro` rule to check `dbg!` macro use. Since this is my first patch to clippy, I'm not confident about following points: - ~~Currently only checks `dbg!` span. Is it possible to check if the `dbg!` macro is provided by standard library or user-defined? If it's possible, I can make the check more strict.~~ Resolved as #3723 (comment) - ~~Is category `style` correct for this rule?~~'restriction' is used instead - ~~Should I use `span_lint_and_sugg` instead of `span_lint`? Currently entire message is put as `msg`. But later part `ensure to avoid having uses of it in version control` may be put as suggestion.~~ Done - I'm not native English speaker. The message and doc may not be natural as English.
2 parents 3bda548 + 83d620b commit 273b7f7

File tree

6 files changed

+180
-1
lines changed

6 files changed

+180
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -772,6 +772,7 @@ All notable changes to this project will be documented in this file.
772772
[`copy_iterator`]: https://rust-lang.github.io/rust-clippy/master/index.html#copy_iterator
773773
[`crosspointer_transmute`]: https://rust-lang.github.io/rust-clippy/master/index.html#crosspointer_transmute
774774
[`cyclomatic_complexity`]: https://rust-lang.github.io/rust-clippy/master/index.html#cyclomatic_complexity
775+
[`dbg_macro`]: https://rust-lang.github.io/rust-clippy/master/index.html#dbg_macro
775776
[`decimal_literal_representation`]: https://rust-lang.github.io/rust-clippy/master/index.html#decimal_literal_representation
776777
[`declare_interior_mutable_const`]: https://rust-lang.github.io/rust-clippy/master/index.html#declare_interior_mutable_const
777778
[`default_trait_access`]: https://rust-lang.github.io/rust-clippy/master/index.html#default_trait_access

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

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

10-
[There are 295 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
10+
[There are 296 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
1111

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

clippy_lints/src/dbg_macro.rs

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
use crate::utils::{snippet_opt, span_help_and_lint, span_lint_and_sugg};
2+
use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
3+
use rustc::{declare_tool_lint, lint_array};
4+
use rustc_errors::Applicability;
5+
use syntax::ast;
6+
use syntax::source_map::Span;
7+
use syntax::tokenstream::TokenStream;
8+
9+
/// **What it does:** Checks for usage of dbg!() macro.
10+
///
11+
/// **Why is this bad?** `dbg!` macro is intended as a debugging tool. It
12+
/// should not be in version control.
13+
///
14+
/// **Known problems:** None.
15+
///
16+
/// **Example:**
17+
/// ```rust,ignore
18+
/// // Bad
19+
/// dbg!(true)
20+
///
21+
/// // Good
22+
/// true
23+
/// ```
24+
declare_clippy_lint! {
25+
pub DBG_MACRO,
26+
restriction,
27+
"`dbg!` macro is intended as a debugging tool"
28+
}
29+
30+
#[derive(Copy, Clone, Debug)]
31+
pub struct Pass;
32+
33+
impl LintPass for Pass {
34+
fn get_lints(&self) -> LintArray {
35+
lint_array!(DBG_MACRO)
36+
}
37+
38+
fn name(&self) -> &'static str {
39+
"DbgMacro"
40+
}
41+
}
42+
43+
impl EarlyLintPass for Pass {
44+
fn check_mac(&mut self, cx: &EarlyContext<'_>, mac: &ast::Mac) {
45+
if mac.node.path == "dbg" {
46+
if let Some(sugg) = tts_span(mac.node.tts.clone()).and_then(|span| snippet_opt(cx, span)) {
47+
span_lint_and_sugg(
48+
cx,
49+
DBG_MACRO,
50+
mac.span,
51+
"`dbg!` macro is intended as a debugging tool",
52+
"ensure to avoid having uses of it in version control",
53+
sugg,
54+
Applicability::MaybeIncorrect,
55+
);
56+
} else {
57+
span_help_and_lint(
58+
cx,
59+
DBG_MACRO,
60+
mac.span,
61+
"`dbg!` macro is intended as a debugging tool",
62+
"ensure to avoid having uses of it in version control",
63+
);
64+
}
65+
}
66+
}
67+
}
68+
69+
// Get span enclosing entire the token stream.
70+
fn tts_span(tts: TokenStream) -> Option<Span> {
71+
let mut cursor = tts.into_trees();
72+
let first = cursor.next()?.span();
73+
let span = match cursor.last() {
74+
Some(tree) => first.to(tree.span()),
75+
None => first,
76+
};
77+
Some(span)
78+
}

clippy_lints/src/lib.rs

+3
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 {
@@ -495,6 +497,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
495497
reg.register_lint_group("clippy::restriction", Some("clippy_restriction"), vec![
496498
arithmetic::FLOAT_ARITHMETIC,
497499
arithmetic::INTEGER_ARITHMETIC,
500+
dbg_macro::DBG_MACRO,
498501
else_if_without_else::ELSE_IF_WITHOUT_ELSE,
499502
implicit_return::IMPLICIT_RETURN,
500503
indexing_slicing::INDEXING_SLICING,

tests/ui/dbg_macro.rs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#![warn(clippy::dbg_macro)]
2+
3+
fn foo(n: u32) -> u32 {
4+
if let Some(n) = dbg!(n.checked_sub(4)) {
5+
n
6+
} else {
7+
n
8+
}
9+
}
10+
11+
fn factorial(n: u32) -> u32 {
12+
if dbg!(n <= 1) {
13+
dbg!(1)
14+
} else {
15+
dbg!(n * factorial(n - 1))
16+
}
17+
}
18+
19+
fn main() {
20+
dbg!(42);
21+
dbg!(dbg!(dbg!(42)));
22+
foo(3) + dbg!(factorial(4));
23+
}

tests/ui/dbg_macro.stderr

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
error: `dbg!` macro is intended as a debugging tool
2+
--> $DIR/dbg_macro.rs:4:22
3+
|
4+
LL | if let Some(n) = dbg!(n.checked_sub(4)) {
5+
| ^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: `-D clippy::dbg-macro` implied by `-D warnings`
8+
help: ensure to avoid having uses of it in version control
9+
|
10+
LL | if let Some(n) = n.checked_sub(4) {
11+
| ^^^^^^^^^^^^^^^^
12+
13+
error: `dbg!` macro is intended as a debugging tool
14+
--> $DIR/dbg_macro.rs:12:8
15+
|
16+
LL | if dbg!(n <= 1) {
17+
| ^^^^^^^^^^^^
18+
help: ensure to avoid having uses of it in version control
19+
|
20+
LL | if n <= 1 {
21+
| ^^^^^^
22+
23+
error: `dbg!` macro is intended as a debugging tool
24+
--> $DIR/dbg_macro.rs:13:9
25+
|
26+
LL | dbg!(1)
27+
| ^^^^^^^
28+
help: ensure to avoid having uses of it in version control
29+
|
30+
LL | 1
31+
|
32+
33+
error: `dbg!` macro is intended as a debugging tool
34+
--> $DIR/dbg_macro.rs:15:9
35+
|
36+
LL | dbg!(n * factorial(n - 1))
37+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
38+
help: ensure to avoid having uses of it in version control
39+
|
40+
LL | n * factorial(n - 1)
41+
|
42+
43+
error: `dbg!` macro is intended as a debugging tool
44+
--> $DIR/dbg_macro.rs:20:5
45+
|
46+
LL | dbg!(42);
47+
| ^^^^^^^^
48+
help: ensure to avoid having uses of it in version control
49+
|
50+
LL | 42;
51+
| ^^
52+
53+
error: `dbg!` macro is intended as a debugging tool
54+
--> $DIR/dbg_macro.rs:21:5
55+
|
56+
LL | dbg!(dbg!(dbg!(42)));
57+
| ^^^^^^^^^^^^^^^^^^^^
58+
help: ensure to avoid having uses of it in version control
59+
|
60+
LL | dbg!(dbg!(42));
61+
| ^^^^^^^^^^^^^^
62+
63+
error: `dbg!` macro is intended as a debugging tool
64+
--> $DIR/dbg_macro.rs:22:14
65+
|
66+
LL | foo(3) + dbg!(factorial(4));
67+
| ^^^^^^^^^^^^^^^^^^
68+
help: ensure to avoid having uses of it in version control
69+
|
70+
LL | foo(3) + factorial(4);
71+
| ^^^^^^^^^^^^
72+
73+
error: aborting due to 7 previous errors
74+

0 commit comments

Comments
 (0)