Add new lint doc_include_without_cfg (#13625)

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`
This commit is contained in:
Alejandra González 2024-11-21 21:48:57 +00:00 committed by GitHub
commit 8298da72e7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 174 additions and 1 deletions

View file

@ -0,0 +1,40 @@
#![warn(clippy::doc_include_without_cfg)]
// Should not lint.
#![doc(html_playground_url = "https://playground.example.com/")]
#![cfg_attr(doc, doc = include_str!("../approx_const.rs"))] //~ doc_include_without_cfg
// Should not lint.
#![cfg_attr(feature = "whatever", doc = include_str!("../approx_const.rs"))]
#![cfg_attr(doc, doc = include_str!("../approx_const.rs"))]
#![doc = "some doc"]
//! more doc
macro_rules! man_link {
($a:literal, $b:literal) => {
concat!($a, $b)
};
}
// Should not lint!
macro_rules! tst {
($(#[$attr:meta])*) => {
$(#[$attr])*
fn blue() {
println!("Hello, world!");
}
}
}
tst! {
/// This is a test with no included file
}
#[cfg_attr(doc, doc = include_str!("../approx_const.rs"))] //~ doc_include_without_cfg
// Should not lint.
#[doc = man_link!("bla", "blob")]
#[cfg_attr(feature = "whatever", doc = include_str!("../approx_const.rs"))]
#[cfg_attr(doc, doc = include_str!("../approx_const.rs"))]
#[doc = "some doc"]
/// more doc
fn main() {
// test code goes here
}

View file

@ -0,0 +1,40 @@
#![warn(clippy::doc_include_without_cfg)]
// Should not lint.
#![doc(html_playground_url = "https://playground.example.com/")]
#![doc = include_str!("../approx_const.rs")] //~ doc_include_without_cfg
// Should not lint.
#![cfg_attr(feature = "whatever", doc = include_str!("../approx_const.rs"))]
#![cfg_attr(doc, doc = include_str!("../approx_const.rs"))]
#![doc = "some doc"]
//! more doc
macro_rules! man_link {
($a:literal, $b:literal) => {
concat!($a, $b)
};
}
// Should not lint!
macro_rules! tst {
($(#[$attr:meta])*) => {
$(#[$attr])*
fn blue() {
println!("Hello, world!");
}
}
}
tst! {
/// This is a test with no included file
}
#[doc = include_str!("../approx_const.rs")] //~ doc_include_without_cfg
// Should not lint.
#[doc = man_link!("bla", "blob")]
#[cfg_attr(feature = "whatever", doc = include_str!("../approx_const.rs"))]
#[cfg_attr(doc, doc = include_str!("../approx_const.rs"))]
#[doc = "some doc"]
/// more doc
fn main() {
// test code goes here
}

View file

@ -0,0 +1,17 @@
error: included a file in documentation unconditionally
--> tests/ui/doc/doc_include_without_cfg.rs:4:1
|
LL | #![doc = include_str!("../approx_const.rs")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `cfg_attr(doc, doc = "...")`: `#![cfg_attr(doc, doc = include_str!("../approx_const.rs"))]`
|
= note: `-D clippy::doc-include-without-cfg` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::doc_include_without_cfg)]`
error: included a file in documentation unconditionally
--> tests/ui/doc/doc_include_without_cfg.rs:31:1
|
LL | #[doc = include_str!("../approx_const.rs")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `cfg_attr(doc, doc = "...")`: `#[cfg_attr(doc, doc = include_str!("../approx_const.rs"))]`
error: aborting due to 2 previous errors

View file

@ -1,4 +1,5 @@
#![warn(clippy::missing_docs_in_private_items)]
#![allow(clippy::doc_include_without_cfg)]
#![doc = include_str!("../../README.md")]
fn main() {}