From f894adce8cd93a9edf3ce0908920e514d837e0dd Mon Sep 17 00:00:00 2001 From: rhysd Date: Thu, 31 Jan 2019 02:39:38 +0900 Subject: [PATCH 01/10] implement dbg_macro rule (fixes #3721) --- clippy_lints/src/dbg_macro.rs | 51 +++++++++++++++++++++++++++++++++++ clippy_lints/src/lib.rs | 4 +++ tests/ui/dbg_macro.rs | 3 +++ tests/ui/dbg_macro.stderr | 10 +++++++ 4 files changed, 68 insertions(+) create mode 100644 clippy_lints/src/dbg_macro.rs create mode 100644 tests/ui/dbg_macro.rs create mode 100644 tests/ui/dbg_macro.stderr diff --git a/clippy_lints/src/dbg_macro.rs b/clippy_lints/src/dbg_macro.rs new file mode 100644 index 000000000000..fad012bf7a25 --- /dev/null +++ b/clippy_lints/src/dbg_macro.rs @@ -0,0 +1,51 @@ +use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass}; +use rustc::{declare_tool_lint, lint_array}; +use crate::utils::span_lint; +use syntax::ast; + +/// **What it does:** Checks for usage of dbg!() macro not to have it in +/// version control. +/// +/// **Why is this bad?** `dbg!` macro is intended as a debugging tool. +/// +/// **Known problems:** None. +/// +/// **Example:** +/// ```rust,ignore +/// // Bad +/// dbg!(true) +/// +/// // Good +/// true +/// ``` +declare_clippy_lint! { + pub DBG_MACRO, + style, + "`dbg!` macro is intended as a debugging tool" +} + +#[derive(Copy, Clone, Debug)] +pub struct Pass; + +impl LintPass for Pass { + fn get_lints(&self) -> LintArray { + lint_array!(DBG_MACRO) + } + + fn name(&self) -> &'static str { + "DbgMacro" + } +} + +impl EarlyLintPass for Pass { + fn check_mac(&mut self, cx: &EarlyContext<'_>, mac: &ast::Mac) { + if mac.node.path == "dbg" { + span_lint( + cx, + DBG_MACRO, + mac.span, + "`dbg!` macro is intended as a debugging tool. ensure to avoid having uses of it in version control", + ); + } + } +} diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index 52cc2a88da4a..9681e58914d0 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -94,6 +94,7 @@ pub mod const_static_lifetime; pub mod copies; pub mod copy_iterator; pub mod cyclomatic_complexity; +pub mod dbg_macro; pub mod default_trait_access; pub mod derive; pub mod doc; @@ -231,6 +232,7 @@ pub fn register_pre_expansion_lints( }, ); store.register_pre_expansion_pass(Some(session), true, false, box attrs::CfgAttrPass); + store.register_pre_expansion_pass(Some(session), true, false, box dbg_macro::Pass); } pub fn read_conf(reg: &rustc_plugin::Registry<'_>) -> Conf { @@ -589,6 +591,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) { copies::IFS_SAME_COND, copies::IF_SAME_THEN_ELSE, cyclomatic_complexity::CYCLOMATIC_COMPLEXITY, + dbg_macro::DBG_MACRO, derive::DERIVE_HASH_XOR_EQ, double_comparison::DOUBLE_COMPARISONS, double_parens::DOUBLE_PARENS, @@ -800,6 +803,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) { block_in_if_condition::BLOCK_IN_IF_CONDITION_STMT, collapsible_if::COLLAPSIBLE_IF, const_static_lifetime::CONST_STATIC_LIFETIME, + dbg_macro::DBG_MACRO, enum_variants::ENUM_VARIANT_NAMES, enum_variants::MODULE_INCEPTION, eq_op::OP_REF, diff --git a/tests/ui/dbg_macro.rs b/tests/ui/dbg_macro.rs new file mode 100644 index 000000000000..cf113050c267 --- /dev/null +++ b/tests/ui/dbg_macro.rs @@ -0,0 +1,3 @@ +fn main() { + dbg!(42); +} diff --git a/tests/ui/dbg_macro.stderr b/tests/ui/dbg_macro.stderr new file mode 100644 index 000000000000..cb5389c6e765 --- /dev/null +++ b/tests/ui/dbg_macro.stderr @@ -0,0 +1,10 @@ +error: `dbg!` macro is intended as a debugging tool. ensure to avoid having uses of it in version control + --> $DIR/dbg_macro.rs:2:5 + | +LL | dbg!(42); + | ^^^^^^^^ + | + = note: `-D clippy::dbg-macro` implied by `-D warnings` + +error: aborting due to previous error + From 7ec5528e0c92f15f74c1e8b48502ce3a07a419cd Mon Sep 17 00:00:00 2001 From: rhysd Date: Fri, 1 Feb 2019 09:23:40 +0900 Subject: [PATCH 02/10] fix category and use suggestion --- clippy_lints/src/dbg_macro.rs | 18 +++++++++++------- tests/ui/dbg_macro.rs | 2 ++ tests/ui/dbg_macro.stderr | 8 ++++++-- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/clippy_lints/src/dbg_macro.rs b/clippy_lints/src/dbg_macro.rs index fad012bf7a25..3dce8189b711 100644 --- a/clippy_lints/src/dbg_macro.rs +++ b/clippy_lints/src/dbg_macro.rs @@ -1,12 +1,13 @@ use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass}; use rustc::{declare_tool_lint, lint_array}; -use crate::utils::span_lint; +use crate::utils::span_lint_and_sugg; use syntax::ast; +use rustc_errors::Applicability; -/// **What it does:** Checks for usage of dbg!() macro not to have it in -/// version control. +/// **What it does:** Checks for usage of dbg!() macro. /// -/// **Why is this bad?** `dbg!` macro is intended as a debugging tool. +/// **Why is this bad?** `dbg!` macro is intended as a debugging tool. It +/// should not be in version control. /// /// **Known problems:** None. /// @@ -20,7 +21,7 @@ use syntax::ast; /// ``` declare_clippy_lint! { pub DBG_MACRO, - style, + restriction, "`dbg!` macro is intended as a debugging tool" } @@ -40,11 +41,14 @@ impl LintPass for Pass { impl EarlyLintPass for Pass { fn check_mac(&mut self, cx: &EarlyContext<'_>, mac: &ast::Mac) { if mac.node.path == "dbg" { - span_lint( + 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", + "`dbg!` macro is intended as a debugging tool", + "ensure to avoid having uses of it in version control", + mac.node.tts.to_string(), // TODO: to string + Applicability::MaybeIncorrect, ); } } diff --git a/tests/ui/dbg_macro.rs b/tests/ui/dbg_macro.rs index cf113050c267..dc96c7da0ace 100644 --- a/tests/ui/dbg_macro.rs +++ b/tests/ui/dbg_macro.rs @@ -1,3 +1,5 @@ +#![warn(clippy::dbg_macro)] + fn main() { dbg!(42); } diff --git a/tests/ui/dbg_macro.stderr b/tests/ui/dbg_macro.stderr index cb5389c6e765..4b8501462ff5 100644 --- a/tests/ui/dbg_macro.stderr +++ b/tests/ui/dbg_macro.stderr @@ -1,10 +1,14 @@ -error: `dbg!` macro is intended as a debugging tool. ensure to avoid having uses of it in version control - --> $DIR/dbg_macro.rs:2:5 +error: `dbg!` macro is intended as a debugging tool + --> $DIR/dbg_macro.rs:4:5 | LL | dbg!(42); | ^^^^^^^^ | = note: `-D clippy::dbg-macro` implied by `-D warnings` +help: ensure to avoid having uses of it in version control + | +LL | 42; + | ^^ error: aborting due to previous error From 9d130a546fbf448680848809a271687c8a671c9d Mon Sep 17 00:00:00 2001 From: rhysd Date: Fri, 1 Feb 2019 11:25:33 +0900 Subject: [PATCH 03/10] add dbg_macro rule to CHANGELOG.md and update count in README --- CHANGELOG.md | 1 + README.md | 2 +- clippy_lints/src/lib.rs | 3 +-- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 71066aadfcbd..44616a556105 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -772,6 +772,7 @@ All notable changes to this project will be documented in this file. [`copy_iterator`]: https://rust-lang.github.io/rust-clippy/master/index.html#copy_iterator [`crosspointer_transmute`]: https://rust-lang.github.io/rust-clippy/master/index.html#crosspointer_transmute [`cyclomatic_complexity`]: https://rust-lang.github.io/rust-clippy/master/index.html#cyclomatic_complexity +[`dbg_macro`]: https://rust-lang.github.io/rust-clippy/master/index.html#dbg_macro [`decimal_literal_representation`]: https://rust-lang.github.io/rust-clippy/master/index.html#decimal_literal_representation [`declare_interior_mutable_const`]: https://rust-lang.github.io/rust-clippy/master/index.html#declare_interior_mutable_const [`default_trait_access`]: https://rust-lang.github.io/rust-clippy/master/index.html#default_trait_access diff --git a/README.md b/README.md index c1f457a956eb..8519fe55a9f4 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ A collection of lints to catch common mistakes and improve your [Rust](https://github.com/rust-lang/rust) code. -[There are 294 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html) +[There are 295 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html) We have a bunch of lint categories to allow you to choose how much Clippy is supposed to ~~annoy~~ help you: diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index 9681e58914d0..0ec872c80bea 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -496,6 +496,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) { reg.register_lint_group("clippy::restriction", Some("clippy_restriction"), vec![ arithmetic::FLOAT_ARITHMETIC, arithmetic::INTEGER_ARITHMETIC, + dbg_macro::DBG_MACRO, else_if_without_else::ELSE_IF_WITHOUT_ELSE, implicit_return::IMPLICIT_RETURN, indexing_slicing::INDEXING_SLICING, @@ -591,7 +592,6 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) { copies::IFS_SAME_COND, copies::IF_SAME_THEN_ELSE, cyclomatic_complexity::CYCLOMATIC_COMPLEXITY, - dbg_macro::DBG_MACRO, derive::DERIVE_HASH_XOR_EQ, double_comparison::DOUBLE_COMPARISONS, double_parens::DOUBLE_PARENS, @@ -803,7 +803,6 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) { block_in_if_condition::BLOCK_IN_IF_CONDITION_STMT, collapsible_if::COLLAPSIBLE_IF, const_static_lifetime::CONST_STATIC_LIFETIME, - dbg_macro::DBG_MACRO, enum_variants::ENUM_VARIANT_NAMES, enum_variants::MODULE_INCEPTION, eq_op::OP_REF, From 06e4e9cf27c7aa5054068ad1aaf26a1b4ead97a1 Mon Sep 17 00:00:00 2001 From: rhysd Date: Fri, 1 Feb 2019 11:39:35 +0900 Subject: [PATCH 04/10] remove TODO comment which was already done --- clippy_lints/src/dbg_macro.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clippy_lints/src/dbg_macro.rs b/clippy_lints/src/dbg_macro.rs index 3dce8189b711..1759db1ca3a1 100644 --- a/clippy_lints/src/dbg_macro.rs +++ b/clippy_lints/src/dbg_macro.rs @@ -47,7 +47,7 @@ impl EarlyLintPass for Pass { mac.span, "`dbg!` macro is intended as a debugging tool", "ensure to avoid having uses of it in version control", - mac.node.tts.to_string(), // TODO: to string + mac.node.tts.to_string(), Applicability::MaybeIncorrect, ); } From b52a9bd9665218207015092ce574de86e3610df3 Mon Sep 17 00:00:00 2001 From: rhysd Date: Fri, 1 Feb 2019 12:03:13 +0900 Subject: [PATCH 05/10] `cargo +nightly fmt` at clippy_lints/ --- clippy_lints/src/dbg_macro.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/clippy_lints/src/dbg_macro.rs b/clippy_lints/src/dbg_macro.rs index 1759db1ca3a1..0bcca015c06a 100644 --- a/clippy_lints/src/dbg_macro.rs +++ b/clippy_lints/src/dbg_macro.rs @@ -1,8 +1,8 @@ +use crate::utils::span_lint_and_sugg; use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass}; use rustc::{declare_tool_lint, lint_array}; -use crate::utils::span_lint_and_sugg; -use syntax::ast; use rustc_errors::Applicability; +use syntax::ast; /// **What it does:** Checks for usage of dbg!() macro. /// From 268ff853263da6d518e233cf2a514bde50975f1d Mon Sep 17 00:00:00 2001 From: rhysd Date: Sat, 2 Feb 2019 04:52:21 +0900 Subject: [PATCH 06/10] use span_help_and_lint() instead of span_lint_and_sugg() --- clippy_lints/src/dbg_macro.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/clippy_lints/src/dbg_macro.rs b/clippy_lints/src/dbg_macro.rs index 0bcca015c06a..93d2007d3a55 100644 --- a/clippy_lints/src/dbg_macro.rs +++ b/clippy_lints/src/dbg_macro.rs @@ -1,4 +1,4 @@ -use crate::utils::span_lint_and_sugg; +use crate::utils::span_help_and_lint; use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass}; use rustc::{declare_tool_lint, lint_array}; use rustc_errors::Applicability; @@ -41,14 +41,12 @@ impl LintPass for Pass { impl EarlyLintPass for Pass { fn check_mac(&mut self, cx: &EarlyContext<'_>, mac: &ast::Mac) { if mac.node.path == "dbg" { - span_lint_and_sugg( + 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", - mac.node.tts.to_string(), - Applicability::MaybeIncorrect, ); } } From 54d49af3ff1f23e8e8b580483e6b3a4f708285bd Mon Sep 17 00:00:00 2001 From: rhysd Date: Sat, 2 Feb 2019 04:54:51 +0900 Subject: [PATCH 07/10] add more test cases for dbg_macro rule --- clippy_lints/src/dbg_macro.rs | 1 - tests/ui/dbg_macro.rs | 18 +++++++++++ tests/ui/dbg_macro.stderr | 57 +++++++++++++++++++++++++++++++---- 3 files changed, 69 insertions(+), 7 deletions(-) diff --git a/clippy_lints/src/dbg_macro.rs b/clippy_lints/src/dbg_macro.rs index 93d2007d3a55..0b8f0c3f5fc4 100644 --- a/clippy_lints/src/dbg_macro.rs +++ b/clippy_lints/src/dbg_macro.rs @@ -1,7 +1,6 @@ use crate::utils::span_help_and_lint; use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass}; use rustc::{declare_tool_lint, lint_array}; -use rustc_errors::Applicability; use syntax::ast; /// **What it does:** Checks for usage of dbg!() macro. diff --git a/tests/ui/dbg_macro.rs b/tests/ui/dbg_macro.rs index dc96c7da0ace..d2df7fbd3e84 100644 --- a/tests/ui/dbg_macro.rs +++ b/tests/ui/dbg_macro.rs @@ -1,5 +1,23 @@ #![warn(clippy::dbg_macro)] +fn foo(n: u32) -> u32 { + if let Some(n) = dbg!(n.checked_sub(4)) { + n + } else { + n + } +} + +fn factorial(n: u32) -> u32 { + if dbg!(n <= 1) { + dbg!(1) + } else { + dbg!(n * factorial(n - 1)) + } +} + fn main() { dbg!(42); + dbg!(dbg!(dbg!(42))); + foo(3) + dbg!(factorial(4)); } diff --git a/tests/ui/dbg_macro.stderr b/tests/ui/dbg_macro.stderr index 4b8501462ff5..28e59f4c11b3 100644 --- a/tests/ui/dbg_macro.stderr +++ b/tests/ui/dbg_macro.stderr @@ -1,14 +1,59 @@ error: `dbg!` macro is intended as a debugging tool - --> $DIR/dbg_macro.rs:4:5 + --> $DIR/dbg_macro.rs:4:22 + | +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 + +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 + +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 + +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 + +error: `dbg!` macro is intended as a debugging tool + --> $DIR/dbg_macro.rs:20:5 | LL | dbg!(42); | ^^^^^^^^ | - = 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 + +error: `dbg!` macro is intended as a debugging tool + --> $DIR/dbg_macro.rs:21:5 | -LL | 42; - | ^^ +LL | dbg!(dbg!(dbg!(42))); + | ^^^^^^^^^^^^^^^^^^^^ + | + = help: ensure to avoid having uses of it in version control -error: aborting due to previous error +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 + +error: aborting due to 7 previous errors From 3100fecb99782941a2512d58e7021fd93a57b619 Mon Sep 17 00:00:00 2001 From: rhysd Date: Sun, 3 Feb 2019 18:28:42 +0900 Subject: [PATCH 08/10] use snippet for making a suggestion if possible --- clippy_lints/src/dbg_macro.rs | 45 ++++++++++++++++++++++++++++------- tests/ui/dbg_macro.stderr | 29 ++++++++++++++++------ 2 files changed, 59 insertions(+), 15 deletions(-) diff --git a/clippy_lints/src/dbg_macro.rs b/clippy_lints/src/dbg_macro.rs index 0b8f0c3f5fc4..228d31bd554b 100644 --- a/clippy_lints/src/dbg_macro.rs +++ b/clippy_lints/src/dbg_macro.rs @@ -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. /// @@ -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 { + 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) +} diff --git a/tests/ui/dbg_macro.stderr b/tests/ui/dbg_macro.stderr index 28e59f4c11b3..43a60180f964 100644 --- a/tests/ui/dbg_macro.stderr +++ b/tests/ui/dbg_macro.stderr @@ -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 From 60f723fba42756138f3a9386b8376e2ed3e7dda1 Mon Sep 17 00:00:00 2001 From: rhysd Date: Sun, 3 Feb 2019 18:50:00 +0900 Subject: [PATCH 09/10] prefer `if` to `match` --- clippy_lints/src/dbg_macro.rs | 41 ++++++++++++++++------------------- 1 file changed, 19 insertions(+), 22 deletions(-) diff --git a/clippy_lints/src/dbg_macro.rs b/clippy_lints/src/dbg_macro.rs index 228d31bd554b..ae551a0792d7 100644 --- a/clippy_lints/src/dbg_macro.rs +++ b/clippy_lints/src/dbg_macro.rs @@ -43,28 +43,25 @@ impl LintPass for Pass { impl EarlyLintPass for Pass { fn check_mac(&mut self, cx: &EarlyContext<'_>, mac: &ast::Mac) { if mac.node.path == "dbg" { - 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", - ); - } - }; + if let Some(sugg) = tts_span(mac.node.tts.clone()).and_then(|span| snippet_opt(cx, span)) { + 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, + ); + } else { + 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", + ); + } } } } From 83d620b8246094ff54c90888f53015b1ef5a2bf8 Mon Sep 17 00:00:00 2001 From: rhysd Date: Sun, 3 Feb 2019 21:28:43 +0900 Subject: [PATCH 10/10] run `util/dev update_lints` and `cargo fmt --all` --- README.md | 2 +- clippy_lints/src/dbg_macro.rs | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 8519fe55a9f4..d3d82eb0752e 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ A collection of lints to catch common mistakes and improve your [Rust](https://github.com/rust-lang/rust) code. -[There are 295 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html) +[There are 296 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html) We have a bunch of lint categories to allow you to choose how much Clippy is supposed to ~~annoy~~ help you: diff --git a/clippy_lints/src/dbg_macro.rs b/clippy_lints/src/dbg_macro.rs index ae551a0792d7..d75970ce50a2 100644 --- a/clippy_lints/src/dbg_macro.rs +++ b/clippy_lints/src/dbg_macro.rs @@ -1,10 +1,10 @@ -use crate::utils::{span_help_and_lint, span_lint_and_sugg, snippet_opt}; +use crate::utils::{snippet_opt, span_help_and_lint, span_lint_and_sugg}; 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::ast; use syntax::source_map::Span; +use syntax::tokenstream::TokenStream; /// **What it does:** Checks for usage of dbg!() macro. ///