-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Implement dbg_macro rule #3723
Implement dbg_macro rule #3723
Conversation
If somebody is using Clippy during development (through RLS for an example) this warning will appear all the time. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is there any way to detect debug or release build from within Clippy?
That's a little problematic, the compiler itself only has a notion of optimization levels and debug info, not release/debug. The latter is solely a cargo concept. What we can do is ask which cfg
s have been set by looking at cx.tcx.sess.parse_sess.config
. Maybe there are cfgs for release/debug?
clippy_lints/src/dbg_macro.rs
Outdated
/// ``` | ||
declare_clippy_lint! { | ||
pub DBG_MACRO, | ||
style, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we need a whole new category for such lints at some point. We already have things like linting println!
calls.
Thank you for the nice point. I found following SO question/answer and
But I'm not sure actually. What do you think? |
This lint is similar to use_debug and print_stdout whose purpose is "to catch debugging remnants". Based on that, I'd expect it to be a restriction lint that you enable on CI. Also, maybe all three lints could be combined into a |
I change my mind about my second point. You definitely wouldn't want to combine them. |
I changed category to 'restriction' and used suggestion. Though applicability is not machine-applicable. Could you review 7ec5528?
|
Regarding to category, I'm having two options
I think 2. is potentially confusing since users would not expect that |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, the restriction
category fits best for now
By adding new tests, I found two problems in current implementation. At first, the token stream inserts a space between token and token:
At second, if
I think I should set |
I also have found empty suggestion message does not work. When I tried to set
|
Instead of serializing the tokens you can use the snippet function on the span of the tokenstream. Then you get the actual text without the spaces |
clippy_lints/src/dbg_macro.rs
Outdated
mac.span, | ||
"`dbg!` macro is intended as a debugging tool", | ||
"ensure to avoid having uses of it in version control", | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still used span_help_and_lint
when snippet cannot be made from the token stream.
@oli-obk Thank you for your suggestion. I read |
@bors r+ Thanks! this looks great to me now. Sorry about the tokentree thing, I wrongly thought there was a method to give you the span. |
📌 Commit 83d620b has been approved by |
Implement dbg_macro rule Fixes #3721 This patch adds new `dbg_macro` rule to check `dbg!` macro use. Since this is my first patch to clippy, I'm not confident about following points: - ~~Currently only checks `dbg!` span. Is it possible to check if the `dbg!` macro is provided by standard library or user-defined? If it's possible, I can make the check more strict.~~ Resolved as #3723 (comment) - ~~Is category `style` correct for this rule?~~'restriction' is used instead - ~~Should I use `span_lint_and_sugg` instead of `span_lint`? Currently entire message is put as `msg`. But later part `ensure to avoid having uses of it in version control` may be put as suggestion.~~ Done - I'm not native English speaker. The message and doc may not be natural as English.
☀️ Test successful - checks-travis, status-appveyor |
@oli-obk No problem. It was a good chance to read a source of rustc compiler for me. Thank you for many code reviews! |
Fixes #3721
This patch adds new
dbg_macro
rule to checkdbg!
macro use.Since this is my first patch to clippy, I'm not confident about following points:
Currently only checksResolved as Implement dbg_macro rule #3723 (comment)dbg!
span. Is it possible to check if thedbg!
macro is provided by standard library or user-defined? If it's possible, I can make the check more strict.Is category'restriction' is used insteadstyle
correct for this rule?Should I useDonespan_lint_and_sugg
instead ofspan_lint
? Currently entire message is put asmsg
. But later partensure to avoid having uses of it in version control
may be put as suggestion.