Skip to content

Commit 3100fec

Browse files
committed
use snippet for making a suggestion if possible
1 parent 54d49af commit 3100fec

File tree

2 files changed

+59
-15
lines changed

2 files changed

+59
-15
lines changed

Diff for: clippy_lints/src/dbg_macro.rs

+37-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
use crate::utils::span_help_and_lint;
1+
use crate::utils::{span_help_and_lint, span_lint_and_sugg, snippet_opt};
22
use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
33
use rustc::{declare_tool_lint, lint_array};
44
use syntax::ast;
5+
use rustc_errors::Applicability;
6+
use syntax::tokenstream::TokenStream;
7+
use syntax::source_map::Span;
58

69
/// **What it does:** Checks for usage of dbg!() macro.
710
///
@@ -40,13 +43,39 @@ impl LintPass for Pass {
4043
impl EarlyLintPass for Pass {
4144
fn check_mac(&mut self, cx: &EarlyContext<'_>, mac: &ast::Mac) {
4245
if mac.node.path == "dbg" {
43-
span_help_and_lint(
44-
cx,
45-
DBG_MACRO,
46-
mac.span,
47-
"`dbg!` macro is intended as a debugging tool",
48-
"ensure to avoid having uses of it in version control",
49-
);
46+
match tts_span(mac.node.tts.clone()).and_then(|span| snippet_opt(cx, span)) {
47+
Some(sugg) => {
48+
span_lint_and_sugg(
49+
cx,
50+
DBG_MACRO,
51+
mac.span,
52+
"`dbg!` macro is intended as a debugging tool",
53+
"ensure to avoid having uses of it in version control",
54+
sugg,
55+
Applicability::MaybeIncorrect,
56+
);
57+
}
58+
None => {
59+
span_help_and_lint(
60+
cx,
61+
DBG_MACRO,
62+
mac.span,
63+
"`dbg!` macro is intended as a debugging tool",
64+
"ensure to avoid having uses of it in version control",
65+
);
66+
}
67+
};
5068
}
5169
}
5270
}
71+
72+
// Get span enclosing entire the token stream.
73+
fn tts_span(tts: TokenStream) -> Option<Span> {
74+
let mut cursor = tts.into_trees();
75+
let first = cursor.next()?.span();
76+
let span = match cursor.last() {
77+
Some(tree) => first.to(tree.span()),
78+
None => first,
79+
};
80+
Some(span)
81+
}

Diff for: tests/ui/dbg_macro.stderr

+22-7
Original file line numberDiff line numberDiff line change
@@ -5,55 +5,70 @@ LL | if let Some(n) = dbg!(n.checked_sub(4)) {
55
| ^^^^^^^^^^^^^^^^^^^^^^
66
|
77
= note: `-D clippy::dbg-macro` implied by `-D warnings`
8-
= help: ensure to avoid having uses of it in version control
8+
help: ensure to avoid having uses of it in version control
9+
|
10+
LL | if let Some(n) = n.checked_sub(4) {
11+
| ^^^^^^^^^^^^^^^^
912

1013
error: `dbg!` macro is intended as a debugging tool
1114
--> $DIR/dbg_macro.rs:12:8
1215
|
1316
LL | if dbg!(n <= 1) {
1417
| ^^^^^^^^^^^^
18+
help: ensure to avoid having uses of it in version control
1519
|
16-
= help: ensure to avoid having uses of it in version control
20+
LL | if n <= 1 {
21+
| ^^^^^^
1722

1823
error: `dbg!` macro is intended as a debugging tool
1924
--> $DIR/dbg_macro.rs:13:9
2025
|
2126
LL | dbg!(1)
2227
| ^^^^^^^
28+
help: ensure to avoid having uses of it in version control
29+
|
30+
LL | 1
2331
|
24-
= help: ensure to avoid having uses of it in version control
2532

2633
error: `dbg!` macro is intended as a debugging tool
2734
--> $DIR/dbg_macro.rs:15:9
2835
|
2936
LL | dbg!(n * factorial(n - 1))
3037
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
38+
help: ensure to avoid having uses of it in version control
39+
|
40+
LL | n * factorial(n - 1)
3141
|
32-
= help: ensure to avoid having uses of it in version control
3342

3443
error: `dbg!` macro is intended as a debugging tool
3544
--> $DIR/dbg_macro.rs:20:5
3645
|
3746
LL | dbg!(42);
3847
| ^^^^^^^^
48+
help: ensure to avoid having uses of it in version control
3949
|
40-
= help: ensure to avoid having uses of it in version control
50+
LL | 42;
51+
| ^^
4152

4253
error: `dbg!` macro is intended as a debugging tool
4354
--> $DIR/dbg_macro.rs:21:5
4455
|
4556
LL | dbg!(dbg!(dbg!(42)));
4657
| ^^^^^^^^^^^^^^^^^^^^
58+
help: ensure to avoid having uses of it in version control
4759
|
48-
= help: ensure to avoid having uses of it in version control
60+
LL | dbg!(dbg!(42));
61+
| ^^^^^^^^^^^^^^
4962

5063
error: `dbg!` macro is intended as a debugging tool
5164
--> $DIR/dbg_macro.rs:22:14
5265
|
5366
LL | foo(3) + dbg!(factorial(4));
5467
| ^^^^^^^^^^^^^^^^^^
68+
help: ensure to avoid having uses of it in version control
5569
|
56-
= help: ensure to avoid having uses of it in version control
70+
LL | foo(3) + factorial(4);
71+
| ^^^^^^^^^^^^
5772

5873
error: aborting due to 7 previous errors
5974

0 commit comments

Comments
 (0)