]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/dbg_macro.rs
Auto merge of #4478 - tsurai:master, r=flip1995
[rust.git] / clippy_lints / src / dbg_macro.rs
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_lint_pass, declare_tool_lint};
4 use rustc_errors::Applicability;
5 use syntax::ast;
6 use syntax::source_map::Span;
7 use syntax::tokenstream::TokenStream;
8
9 declare_clippy_lint! {
10     /// **What it does:** Checks for usage of dbg!() macro.
11     ///
12     /// **Why is this bad?** `dbg!` macro is intended as a debugging tool. It
13     /// should not be in version control.
14     ///
15     /// **Known problems:** None.
16     ///
17     /// **Example:**
18     /// ```rust,ignore
19     /// // Bad
20     /// dbg!(true)
21     ///
22     /// // Good
23     /// true
24     /// ```
25     pub DBG_MACRO,
26     restriction,
27     "`dbg!` macro is intended as a debugging tool"
28 }
29
30 declare_lint_pass!(DbgMacro => [DBG_MACRO]);
31
32 impl EarlyLintPass for DbgMacro {
33     fn check_mac(&mut self, cx: &EarlyContext<'_>, mac: &ast::Mac) {
34         if mac.path == sym!(dbg) {
35             if let Some(sugg) = tts_span(mac.tts.clone()).and_then(|span| snippet_opt(cx, span)) {
36                 span_lint_and_sugg(
37                     cx,
38                     DBG_MACRO,
39                     mac.span,
40                     "`dbg!` macro is intended as a debugging tool",
41                     "ensure to avoid having uses of it in version control",
42                     sugg,
43                     Applicability::MaybeIncorrect,
44                 );
45             } else {
46                 span_help_and_lint(
47                     cx,
48                     DBG_MACRO,
49                     mac.span,
50                     "`dbg!` macro is intended as a debugging tool",
51                     "ensure to avoid having uses of it in version control",
52                 );
53             }
54         }
55     }
56 }
57
58 // Get span enclosing entire the token stream.
59 fn tts_span(tts: TokenStream) -> Option<Span> {
60     let mut cursor = tts.into_trees();
61     let first = cursor.next()?.span();
62     let span = cursor.last().map_or(first, |tree| first.to(tree.span()));
63     Some(span)
64 }