-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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`
- Loading branch information
Showing
9 changed files
with
174 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use clippy_utils::source::snippet_opt; | ||
use rustc_ast::{AttrArgs, AttrArgsEq, AttrKind, AttrStyle, Attribute}; | ||
use rustc_errors::Applicability; | ||
use rustc_lint::LateContext; | ||
use rustc_span::sym; | ||
|
||
use super::DOC_INCLUDE_WITHOUT_CFG; | ||
|
||
pub fn check(cx: &LateContext<'_>, attrs: &[Attribute]) { | ||
for attr in attrs { | ||
if !attr.span.from_expansion() | ||
&& let AttrKind::Normal(ref normal) = attr.kind | ||
&& normal.item.path == sym::doc | ||
&& let AttrArgs::Eq(_, AttrArgsEq::Hir(ref meta)) = normal.item.args | ||
&& !attr.span.contains(meta.span) | ||
// Since the `include_str` is already expanded at this point, we can only take the | ||
// whole attribute snippet and then modify for our suggestion. | ||
&& let Some(snippet) = snippet_opt(cx, attr.span) | ||
// We cannot remove this because a `#[doc = include_str!("...")]` attribute can occupy | ||
// several lines. | ||
&& let Some(start) = snippet.find('[') | ||
&& let Some(end) = snippet.rfind(']') | ||
&& let snippet = &snippet[start + 1..end] | ||
// We check that the expansion actually comes from `include_str!` and not just from | ||
// another macro. | ||
&& let Some(sub_snippet) = snippet.trim().strip_prefix("doc") | ||
&& let Some(sub_snippet) = sub_snippet.trim().strip_prefix("=") | ||
&& sub_snippet.trim().starts_with("include_str!") | ||
{ | ||
span_lint_and_sugg( | ||
cx, | ||
DOC_INCLUDE_WITHOUT_CFG, | ||
attr.span, | ||
"included a file in documentation unconditionally", | ||
"use `cfg_attr(doc, doc = \"...\")`", | ||
format!( | ||
"#{}[cfg_attr(doc, {snippet})]", | ||
if attr.style == AttrStyle::Inner { "!" } else { "" } | ||
), | ||
Applicability::MachineApplicable, | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
#![warn(clippy::missing_docs_in_private_items)] | ||
#![allow(clippy::doc_include_without_cfg)] | ||
#![doc = include_str!("../../README.md")] | ||
|
||
fn main() {} |