Skip to content

Implement dbg_macro rule - #3723

Merged
bors merged 11 commits into
rust-lang:masterfrom
rhysd:issue3721
Feb 3, 2019
Merged

bors merged 11 commits into
rust-lang:masterfrom
rhysd:issue3721

Conversation

@rhysd

@rhysd rhysd commented Jan 30, 2019 •

Copy link
Copy Markdown
Contributor

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 Implement dbg_macro rule #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.
@mati865

mati865 commented Jan 30, 2019

Copy link
Copy Markdown
Member

If somebody is using Clippy during development (through RLS for an example) this warning will appear all the time.
@oli-obk is there any way to detect debug or release build from within Clippy?

@oli-obk oli-obk left a comment

Copy link
Copy Markdown
Contributor

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 cfgs have been set by looking at cx.tcx.sess.parse_sess.config. Maybe there are cfgs for release/debug?

Comment thread clippy_lints/src/dbg_macro.rs Outdated
/// ```
declare_clippy_lint! {
pub DBG_MACRO,
style,

Copy link
Copy Markdown
Contributor

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.

Comment thread clippy_lints/src/dbg_macro.rs Resolved
Comment thread clippy_lints/src/dbg_macro.rs Resolved
Comment thread clippy_lints/src/dbg_macro.rs Resolved
Comment thread clippy_lints/src/dbg_macro.rs Resolved
@rhysd

rhysd commented Jan 31, 2019

Copy link
Copy Markdown
Contributor Author

@mati865 @oli-obk

Thank you for the nice point. I found following SO question/answer and debug_assertions might be available to check it's run in release mode.

But I'm not sure actually. What do you think?

@ghost

ghost commented Jan 31, 2019

Copy link
Copy Markdown

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 debugging_remnants lint.

@ghost

ghost commented Jan 31, 2019

Copy link
Copy Markdown

I change my mind about my second point. You definitely wouldn't want to combine them.

@phansch phansch added the S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) label Jan 31, 2019
@rhysd

rhysd commented Feb 1, 2019 •

Copy link
Copy Markdown
Contributor Author

@oli-obk @mikerite @mati865

I changed category to 'restriction' and used suggestion. Though applicability is not machine-applicable. Could you review 7ec5528?

warning: `dbg!` macro is intended as a debugging tool
 --> foo.rs:2:5
  |
2 |     dbg!(42);
  |     ^^^^^^^^
  |
  = note: requested on the command line with `-W clippy::dbg-macro`
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#dbg_macro
help: ensure to avoid having uses of it in version control
  |
2 |     42;
  |     ^^

@rhysd

rhysd commented Feb 1, 2019 •

Copy link
Copy Markdown
Contributor Author

Regarding to category, I'm having two options

  1. 'restriction' category which is disabled by default. Users are able to enable manually it by -W clippy::dbg_macro
  2. 'style' category which is enabled by default. And check it only when debug_assertion config is enabled in later pass. It means check is performed on only --release

I think 2. is potentially confusing since users would not expect that clippy changes its behavior looking release/debug mode.

@oli-obk oli-obk left a comment

Copy link
Copy Markdown
Contributor

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

Comment thread clippy_lints/src/dbg_macro.rs Resolved
Comment thread tests/ui/dbg_macro.rs Resolved
@rhysd

rhysd commented Feb 1, 2019

Copy link
Copy Markdown
Contributor Author

By adding new tests, I found two problems in current implementation.

At first, the token stream inserts a space between token and token:

warning: `dbg!` macro is intended as a debugging tool
  --> foo.rs:22:14
   |
22 |     foo(3) + dbg!(factorial(4));
   |              ^^^^^^^^^^^^^^^^^^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#dbg_macro
help: ensure to avoid having uses of it in version control
   |
22 |     foo(3) + factorial ( 4 );
   |              ^^^^^^^^^^^^^^^

At second, if dbg!() is nested like dbg!(foo(dbg!(bar()))), dbg! is remaining in suggestion:

warning: `dbg!` macro is intended as a debugging tool
  --> foo.rs:21:5
   |
21 |     dbg!(dbg!(dbg!(42)));
   |     ^^^^^^^^^^^^^^^^^^^^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#dbg_macro
help: ensure to avoid having uses of it in version control
   |
21 |     dbg ! ( dbg ! ( 42 ) );
   |     ^^^^^^^^^^^^^^^^^^^^^^

I think I should set "" to suggestion as @oli-obk suggested in review comment. If someone have any idea for workaround of above problems, please let me know..

@rhysd

rhysd commented Feb 1, 2019

Copy link
Copy Markdown
Contributor Author

I also have found empty suggestion message does not work. When I tried to set String::new() at 6th argument of span_lint_and_sugg(), I got warning message as follows:

warning: `dbg!` macro is intended as a debugging tool
  --> foo.rs:23:14
   |
23 |     foo(3) + dbg!(factorial(4));
   |              ^^^^^^^^^^^^^^^^^^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#dbg_macro
help: ensure to avoid having uses of it in version control
   |
23 |     foo(3) + ;
   |             --
@rhysd

rhysd commented Feb 1, 2019

Copy link
Copy Markdown
Contributor Author

@oli-obk I used span_help_and_lint() to show both message and help, but not suggestion at 268ff85. And I added more tests at 54d49af. Could you review them again?

@oli-obk

oli-obk commented Feb 1, 2019

Copy link
Copy Markdown
Contributor

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

Comment thread 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",
);

Copy link
Copy Markdown
Contributor Author

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.

@rhysd

rhysd commented Feb 3, 2019

Copy link
Copy Markdown
Contributor Author

@oli-obk Thank you for your suggestion. I read libsyntax implementation and found that retrieving first and last TokenTrees from TokenStream can make a span object enclosing entire the stream. At 3100fec I revived span_lint_and_sugg with snippet though still using span_help_and_lint when snippet is not available. Could you review the commit?

@oli-obk

oli-obk commented Feb 3, 2019

Copy link
Copy Markdown
Contributor

@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.

@bors

bors commented Feb 3, 2019

Copy link
Copy Markdown
Contributor

📌 Commit 83d620b has been approved by oli-obk

@bors

bors commented Feb 3, 2019

Copy link
Copy Markdown
Contributor

⌛ Testing commit 83d620b with merge 273b7f7...

bors added a commit that referenced this pull request Feb 3, 2019
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.
@bors

bors commented Feb 3, 2019

Copy link
Copy Markdown
Contributor

☀️ Test successful - checks-travis, status-appveyor
Approved by: oli-obk
Pushing 273b7f7 to master...

@bors
bors merged commit 83d620b into rust-lang:master Feb 3, 2019
@rhysd

rhysd commented Feb 3, 2019

Copy link
Copy Markdown
Contributor Author

@oli-obk No problem. It was a good chance to read a source of rustc compiler for me. Thank you for many code reviews!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status)

5 participants