Skip to content

Move unused_doc_comment lint to a later pass to after proc macro expansion #67838

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

Closed
yaahc opened this issue Jan 3, 2020 · 3 comments · Fixed by #69084
Closed

Move unused_doc_comment lint to a later pass to after proc macro expansion #67838

yaahc opened this issue Jan 3, 2020 · 3 comments · Fixed by #69084
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-enhancement Category: An issue proposing an enhancement or a PR with one. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.

Comments

@yaahc
Copy link
Member

yaahc commented Jan 3, 2020

I'm currently writing a crate where I'd like to use doc comments as attributes for a proc macro that generates context directives for errors and log messages via tracing.

https://github.com/yaahc/spandoc/blob/master/src/lib.rs

I had originally assumed that if my proc macro stripped out the doc attribute after converting it to a span then the compiler would not lint on the doc comments that actually got used as code, but when I tested this it still lints which implies to me that the unused doc comment lint happens in an earlier pass than proc-macro expansion does.

I'd like to be able to avoid using #[allow(unused_doc_comments)] on either the whole function or file because I want the lints to help me find doc comments that I add, expecting them to turn into spans, but which my proc macro does not handle and therefore leaves in as doc comments.

@yaahc
Copy link
Member Author

yaahc commented Jan 3, 2020

@QuietMisdreavus has told me to cc @GuillaumeGomez

@yaahc yaahc changed the title Move unused_doc_comment pass to after proc macro expansion Move unused_doc_comment lint to a later pass to after proc macro expansion Jan 3, 2020
@GuillaumeGomez GuillaumeGomez added the T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. label Jan 6, 2020
@GuillaumeGomez
Copy link
Member

Could you provide a code sample alongside please?

@yaahc
Copy link
Member Author

yaahc commented Jan 6, 2020

@GuillaumeGomez here is a minimal example of spandoc in action

https://github.com/yaahc/spandoc-example/blob/master/src/lib.rs#L1-L5

When I comment out the spandoc attribute and expand it you'll see that it generates a doc attribute and outputs the lint as expected.

❯ cargo expand | grep doc
    Checking spandoc-example v0.1.0 (/home/jlusby/git/rust/spandoc-example)
warning: unused doc comment
 --> src/lib.rs:3:5
  |
3 |     /// Span in example
  |     ^^^^^^^^^^^^^^^^^^^
4 |     42
  |     -- rustdoc does not generate documentation for expressions
  |
  = note: `#[warn(unused_doc_comments)]` on by default
    Finished check [unoptimized + debuginfo] target(s) in 0.07s

    # [ doc = " Span in example" ] 42

and when I uncomment the spandoc attribute so that it transforms the doc attribute into a span you'll see that it does not have a doc attribute in the final output but it still produces the lint.

❯ cargo expand | grep doc
    Checking spandoc-example v0.1.0 (/home/jlusby/git/rust/spandoc-example)
warning: unused doc comment
 --> src/lib.rs:3:5
  |
3 |     /// Span in example
  |     ^^^^^^^^^^^^^^^^^^^
4 |     42
  |     -- rustdoc does not generate documentation for expressions
  |
  = note: `#[warn(unused_doc_comments)]` on by default
    Finished check [unoptimized + debuginfo] target(s) in 0.11s

                        "spandoc_example",
                        Some("spandoc_example"),

@JohnTitor JohnTitor added A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-enhancement Category: An issue proposing an enhancement or a PR with one. labels Jan 12, 2020
bors added a commit that referenced this issue Feb 23, 2020
Split non macro portion of unused_doc_comment from macro part into two passes/lints

## Motivation

This change is motivated by the needs of the [spandoc library](https://github.com/yaahc/spandoc). The specific use case is that my macro is removing doc comments when an attribute is applied to a fn with doc comments, but I would like the lint to still appear when I forget to add the `#[spandoc]` attribute to a fn, so I don't want to have to silence the lint globally.

## Approach

This change splits the `unused _doc_comment` lint into two lints, `unused_macro_doc_comment` and `unused_doc_comment`. The non macro portion is moved into an `early_lint_pass` rather than a pre_expansion_pass. This allows proc macros to silence `unused_doc_comment` warnings by either adding an attribute to silence it or by removing the doc comment before the early_pass runs.

The `unused_macro_doc_comment` lint however will still be impossible for proc-macros to silence, but the only alternative that I can see is to remove this lint entirely, which I don't think is acceptable / is a decision I'm not comfortable making personally, so instead I opted to split the macro portion of the check into a separate lint so that it can be silenced globally with an attribute if necessary without needing to globally silence the `unused_doc_comment` lint as well, which is still desireable.

fixes #67838
@bors bors closed this as completed in b1f395d Feb 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-enhancement Category: An issue proposing an enhancement or a PR with one. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants