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;
6 use syntax::source_map::Span;
7 use syntax::tokenstream::TokenStream;
9 /// **What it does:** Checks for usage of dbg!() macro.
11 /// **Why is this bad?** `dbg!` macro is intended as a debugging tool. It
12 /// should not be in version control.
14 /// **Known problems:** None.
24 declare_clippy_lint! {
27 "`dbg!` macro is intended as a debugging tool"
30 #[derive(Copy, Clone, Debug)]
33 impl LintPass for Pass {
34 fn get_lints(&self) -> LintArray {
35 lint_array!(DBG_MACRO)
38 fn name(&self) -> &'static str {
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)) {
51 "`dbg!` macro is intended as a debugging tool",
52 "ensure to avoid having uses of it in version control",
54 Applicability::MaybeIncorrect,
61 "`dbg!` macro is intended as a debugging tool",
62 "ensure to avoid having uses of it in version control",
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()),