Skip to content

Commit

Permalink
use snippet for making a suggestion if possible
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysd committed Feb 3, 2019
1 parent 54d49af commit 3100fec
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 15 deletions.
45 changes: 37 additions & 8 deletions clippy_lints/src/dbg_macro.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use crate::utils::span_help_and_lint;
use crate::utils::{span_help_and_lint, span_lint_and_sugg, snippet_opt};
use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
use rustc::{declare_tool_lint, lint_array};
use syntax::ast;
use rustc_errors::Applicability;
use syntax::tokenstream::TokenStream;
use syntax::source_map::Span;

/// **What it does:** Checks for usage of dbg!() macro.
///
Expand Down Expand Up @@ -40,13 +43,39 @@ impl LintPass for Pass {
impl EarlyLintPass for Pass {
fn check_mac(&mut self, cx: &EarlyContext<'_>, mac: &ast::Mac) {
if mac.node.path == "dbg" {
span_help_and_lint(
cx,
DBG_MACRO,
mac.span,
"`dbg!` macro is intended as a debugging tool",
"ensure to avoid having uses of it in version control",
);
match tts_span(mac.node.tts.clone()).and_then(|span| snippet_opt(cx, span)) {
Some(sugg) => {
span_lint_and_sugg(
cx,
DBG_MACRO,
mac.span,
"`dbg!` macro is intended as a debugging tool",
"ensure to avoid having uses of it in version control",
sugg,
Applicability::MaybeIncorrect,
);
}
None => {
span_help_and_lint(
cx,
DBG_MACRO,
mac.span,
"`dbg!` macro is intended as a debugging tool",
"ensure to avoid having uses of it in version control",
);
}
};
}
}
}

// Get span enclosing entire the token stream.
fn tts_span(tts: TokenStream) -> Option<Span> {
let mut cursor = tts.into_trees();
let first = cursor.next()?.span();
let span = match cursor.last() {
Some(tree) => first.to(tree.span()),
None => first,
};
Some(span)
}
29 changes: 22 additions & 7 deletions tests/ui/dbg_macro.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,55 +5,70 @@ LL | if let Some(n) = dbg!(n.checked_sub(4)) {
| ^^^^^^^^^^^^^^^^^^^^^^
|
= note: `-D clippy::dbg-macro` implied by `-D warnings`
= help: ensure to avoid having uses of it in version control
help: ensure to avoid having uses of it in version control
|
LL | if let Some(n) = n.checked_sub(4) {
| ^^^^^^^^^^^^^^^^

error: `dbg!` macro is intended as a debugging tool
--> $DIR/dbg_macro.rs:12:8
|
LL | if dbg!(n <= 1) {
| ^^^^^^^^^^^^
help: ensure to avoid having uses of it in version control
|
= help: ensure to avoid having uses of it in version control
LL | if n <= 1 {
| ^^^^^^

error: `dbg!` macro is intended as a debugging tool
--> $DIR/dbg_macro.rs:13:9
|
LL | dbg!(1)
| ^^^^^^^
help: ensure to avoid having uses of it in version control
|
LL | 1
|
= help: ensure to avoid having uses of it in version control

error: `dbg!` macro is intended as a debugging tool
--> $DIR/dbg_macro.rs:15:9
|
LL | dbg!(n * factorial(n - 1))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
help: ensure to avoid having uses of it in version control
|
LL | n * factorial(n - 1)
|
= help: ensure to avoid having uses of it in version control

error: `dbg!` macro is intended as a debugging tool
--> $DIR/dbg_macro.rs:20:5
|
LL | dbg!(42);
| ^^^^^^^^
help: ensure to avoid having uses of it in version control
|
= help: ensure to avoid having uses of it in version control
LL | 42;
| ^^

error: `dbg!` macro is intended as a debugging tool
--> $DIR/dbg_macro.rs:21:5
|
LL | dbg!(dbg!(dbg!(42)));
| ^^^^^^^^^^^^^^^^^^^^
help: ensure to avoid having uses of it in version control
|
= help: ensure to avoid having uses of it in version control
LL | dbg!(dbg!(42));
| ^^^^^^^^^^^^^^

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

error: aborting due to 7 previous errors

0 comments on commit 3100fec

Please sign in to comment.