Skip to content
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

Add new lint doc_include_without_cfg #13625

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

GuillaumeGomez
Copy link
Member

It's becoming more and more common to see people including markdown files in their code using doc = include_str!("..."), which is great. However, often there is no condition on this include, which is not great because it slows down compilation and might trigger recompilation if these files are updated.

This lint aims at fixing this situation.

changelog: Add new lint doc_include_without_cfg

@rustbot
Copy link
Collaborator

rustbot commented Oct 29, 2024

r? @blyxyas

rustbot has assigned @blyxyas.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Oct 29, 2024
Copy link
Member

@blyxyas blyxyas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very very good PR, just a nit.
I'll open the FCP now, because I'm sure this review cycle will go fast

&& normal.item.path == sym::doc
&& let AttrArgs::Eq(_, AttrArgsEq::Hir(ref meta)) = normal.item.args
&& !attr.span.contains(meta.span)
&& let Some(snippet) = snippet_opt(cx, attr.span)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've looked for way to remove this snippet, but as the include_macro expands before we check the #[doc] attribute, I don't think it's possible.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding a comment will still be a good improvement. :)

&& !attr.span.contains(meta.span)
&& let Some(snippet) = snippet_opt(cx, attr.span)
&& let Some(start) = snippet.find('[')
&& let Some(end) = snippet.rfind(']')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a "we cannot remove this because a #[doc = include] can occupy several lines" comment would be useful here, I also checked removing these, but it doesn't seem possible =^w^=

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a test for one of the other ways that the #[doc] attribute can behave?

#![doc(html_playground_url = "https://playground.example.com/")]

And such

@GuillaumeGomez GuillaumeGomez force-pushed the doc_include_without_cfg branch 3 times, most recently from 43466bb to a1a88d9 Compare October 29, 2024 21:25
@GuillaumeGomez
Copy link
Member Author

Added code comments and added extra tests.

@samueltardieu
Copy link
Contributor

samueltardieu commented Oct 30, 2024

@GuillaumeGomez Using macros may trigger false positives (found by looking at the lintcheck diff):

macro_rules! tst {
    ($(#[$attr:meta])*) => {
        $(#[$attr])*
        fn main() {
            println!("Hello, world!");
        }
    }
}

tst!{
    /// This is a test with no included file
}

will trigger with:

warning: included a file in documentation unconditionally
  --> /tmp/t.rs:3:11
   |
3  |           $(#[$attr])*
   |             ^^^^^^^^ help: use `cfg_attr(doc, doc = "...")`: `#[cfg_attr(doc, $attr)]`
...
10 | / tst!{
11 | |     /// This is a test with no included file
12 | | }
   | |_- in this macro invocation

@GuillaumeGomez
Copy link
Member Author

Great catch! If the attribute is generated from a macro, the lint now ignores it.

@GuillaumeGomez
Copy link
Member Author

Fixed formatting...

@samueltardieu
Copy link
Contributor

Great. The new hits in clap and bumpalo look legitimate, however it looks like hits in socket2 might be false positives.

@GuillaumeGomez
Copy link
Member Author

They were false positives. So we're forced to check that the snippet is indeed include_str!. Fun times. ^^'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants