1 use crate::utils::{snippet_opt, span_help_and_lint, span_lint_and_sugg};
2 use rustc::declare_lint_pass;
3 use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
4 use rustc_errors::Applicability;
5 use rustc_session::declare_tool_lint;
7 use syntax::source_map::Span;
8 use syntax::tokenstream::TokenStream;
10 declare_clippy_lint! {
11 /// **What it does:** Checks for usage of dbg!() macro.
13 /// **Why is this bad?** `dbg!` macro is intended as a debugging tool. It
14 /// should not be in version control.
16 /// **Known problems:** None.
28 "`dbg!` macro is intended as a debugging tool"
31 declare_lint_pass!(DbgMacro => [DBG_MACRO]);
33 impl EarlyLintPass for DbgMacro {
34 fn check_mac(&mut self, cx: &EarlyContext<'_>, mac: &ast::Mac) {
35 if mac.path == sym!(dbg) {
36 if let Some(sugg) = tts_span(mac.args.inner_tokens()).and_then(|span| snippet_opt(cx, span)) {
41 "`dbg!` macro is intended as a debugging tool",
42 "ensure to avoid having uses of it in version control",
44 Applicability::MaybeIncorrect,
51 "`dbg!` macro is intended as a debugging tool",
52 "ensure to avoid having uses of it in version control",
59 // Get span enclosing entire the token stream.
60 fn tts_span(tts: TokenStream) -> Option<Span> {
61 let mut cursor = tts.into_trees();
62 let first = cursor.next()?.span();
63 let span = cursor.last().map_or(first, |tree| first.to(tree.span()));