-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Implement dbg_macro rule #3723
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Implement dbg_macro rule #3723
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f894adc
implement dbg_macro rule (fixes #3721)
rhysd 7ec5528
fix category and use suggestion
rhysd 9d130a5
add dbg_macro rule to CHANGELOG.md and update count in README
rhysd 06e4e9c
remove TODO comment which was already done
rhysd b52a9bd
`cargo +nightly fmt` at clippy_lints/
rhysd 268ff85
use span_help_and_lint() instead of span_lint_and_sugg()
rhysd 54d49af
add more test cases for dbg_macro rule
rhysd 3100fec
use snippet for making a suggestion if possible
rhysd 60f723f
prefer `if` to `match`
rhysd 4b736ff
Merge branch 'master' into issue3721
rhysd 83d620b
run `util/dev update_lints` and `cargo fmt --all`
rhysd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
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 rustc_errors::Applicability; | ||
use syntax::ast; | ||
use syntax::source_map::Span; | ||
use syntax::tokenstream::TokenStream; | ||
|
||
/// **What it does:** Checks for usage of dbg!() macro. | ||
/// | ||
/// **Why is this bad?** `dbg!` macro is intended as a debugging tool. It | ||
/// should not be in version control. | ||
/// | ||
/// **Known problems:** None. | ||
/// | ||
/// **Example:** | ||
/// ```rust,ignore | ||
/// // Bad | ||
/// dbg!(true) | ||
/// | ||
/// // Good | ||
/// true | ||
/// ``` | ||
declare_clippy_lint! { | ||
pub DBG_MACRO, | ||
restriction, | ||
"`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" { | ||
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", | ||
); | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Get span enclosing entire the token stream. | ||
fn tts_span(tts: TokenStream) -> Option<Span> { | ||
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) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +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); | ||
rhysd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
dbg!(dbg!(dbg!(42))); | ||
foo(3) + dbg!(factorial(4)); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
error: `dbg!` macro is intended as a debugging tool | ||
--> $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 | ||
| | ||
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 | ||
| | ||
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 | ||
| | ||
|
||
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) | ||
| | ||
|
||
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 | ||
| | ||
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 | ||
| | ||
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 | ||
| | ||
LL | foo(3) + factorial(4); | ||
| ^^^^^^^^^^^^ | ||
|
||
error: aborting due to 7 previous errors | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.