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

Merged
merged 1 commit into from
Nov 21, 2024

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

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 4 times, most recently from d0caf07 to fbf36cc Compare October 29, 2024 21:36
@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 GuillaumeGomez force-pushed the doc_include_without_cfg branch from 18d01ce to 5111470 Compare October 30, 2024 09:44
@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 GuillaumeGomez force-pushed the doc_include_without_cfg branch from afe4556 to 400894d Compare October 30, 2024 12:58
@GuillaumeGomez
Copy link
Member Author

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

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.

I thought I had already approved this! ❤️ 🌈

We'll have to wait until the FCP is sorted out, seems that we're having some problems with hover documentation.

@GuillaumeGomez
Copy link
Member Author

"Hover documentation"? I rewrote the lints page recently so maybe it's something I broke. Do you have an issue or a link so I can know what's wrong to fix it by any chance?

@blyxyas
Copy link
Member

blyxyas commented Oct 30, 2024

There's not a really way to fix it because it's a Rust Analyzer issue (hover documentation as in when you hover your mouse on the IDE)

Here's the FCP: https://rust-lang.zulipchat.com/#narrow/channel/257328-clippy/topic/FCP.20doc_include_without_cfg
And here's the Rust Analyzer issue on hover documentation for cfg_attr: rust-lang/rust-analyzer#18444

@GuillaumeGomez
Copy link
Member Author

Ah I see. We can always make it allow by default if that makes it better. But let's see what clippy and r-a people think about it. 👍

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.

@GuillaumeGomez
Copy link
Member Author

Sure, done!

@GuillaumeGomez
Copy link
Member Author

GuillaumeGomez commented Nov 21, 2024

And CI passed. ~o~

@blyxyas
Copy link
Member

blyxyas commented Nov 21, 2024

Oh, and I forgot to mention: Could you also squash those 6 commits?

@GuillaumeGomez GuillaumeGomez force-pushed the doc_include_without_cfg branch from 5082f34 to 52fe6fd Compare November 21, 2024 21:43
@GuillaumeGomez GuillaumeGomez force-pushed the doc_include_without_cfg branch from 52fe6fd to 404e47a Compare November 21, 2024 21:43
@GuillaumeGomez
Copy link
Member Author

Done as well.

@blyxyas blyxyas enabled auto-merge November 21, 2024 21:46
@blyxyas blyxyas added this pull request to the merge queue Nov 21, 2024
Merged via the queue into rust-lang:master with commit 8298da7 Nov 21, 2024
9 checks passed
@GuillaumeGomez GuillaumeGomez deleted the doc_include_without_cfg branch November 21, 2024 22:02
untitaker added a commit to untitaker/html5gum that referenced this pull request Nov 21, 2024
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