]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/dbg_macro.rs
Auto merge of #4938 - flip1995:rustup, 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::declare_lint_pass;
3 use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
4 use rustc_errors::Applicability;
5 use rustc_session::declare_tool_lint;
6 use syntax::ast;
7 use syntax::source_map::Span;
8 use syntax::tokenstream::TokenStream;
9
10 declare_clippy_lint! {
11     /// **What it does:** Checks for usage of dbg!() macro.
12     ///
13     /// **Why is this bad?** `dbg!` macro is intended as a debugging tool. It
14     /// should not be in version control.
15     ///
16     /// **Known problems:** None.
17     ///
18     /// **Example:**
19     /// ```rust,ignore
20     /// // Bad
21     /// dbg!(true)
22     ///
23     /// // Good
24     /// true
25     /// ```
26     pub DBG_MACRO,
27     restriction,
28     "`dbg!` macro is intended as a debugging tool"
29 }
30
31 declare_lint_pass!(DbgMacro => [DBG_MACRO]);
32
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)) {
37                 span_lint_and_sugg(
38                     cx,
39                     DBG_MACRO,
40                     mac.span(),
41                     "`dbg!` macro is intended as a debugging tool",
42                     "ensure to avoid having uses of it in version control",
43                     sugg,
44                     Applicability::MaybeIncorrect,
45                 );
46             } else {
47                 span_help_and_lint(
48                     cx,
49                     DBG_MACRO,
50                     mac.span(),
51                     "`dbg!` macro is intended as a debugging tool",
52                     "ensure to avoid having uses of it in version control",
53                 );
54             }
55         }
56     }
57 }
58
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()));
64     Some(span)
65 }