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:
commit
8298da72e7
9 changed files with 174 additions and 1 deletions
40
tests/ui/doc/doc_include_without_cfg.fixed
Normal file
40
tests/ui/doc/doc_include_without_cfg.fixed
Normal 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
|
||||
}
|
||||
40
tests/ui/doc/doc_include_without_cfg.rs
Normal file
40
tests/ui/doc/doc_include_without_cfg.rs
Normal 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
|
||||
}
|
||||
17
tests/ui/doc/doc_include_without_cfg.stderr
Normal file
17
tests/ui/doc/doc_include_without_cfg.stderr
Normal 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
|
||||
|
||||
|
|
@ -1,4 +1,5 @@
|
|||
#![warn(clippy::missing_docs_in_private_items)]
|
||||
#![allow(clippy::doc_include_without_cfg)]
|
||||
#![doc = include_str!("../../README.md")]
|
||||
|
||||
fn main() {}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue