Skip to content

Commit

Permalink
Only emit doc_include_without_cfg when include_str! is used
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Oct 30, 2024
1 parent 5111470 commit afe4556
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
7 changes: 6 additions & 1 deletion clippy_lints/src/doc/include_in_doc_without_cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,13 @@ pub fn check(cx: &LateContext<'_>, attrs: &[Attribute]) {
// 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!")
{
let snippet = &snippet[start + 1..end];
span_lint_and_sugg(
cx,
DOC_INCLUDE_WITHOUT_CFG,
Expand Down
8 changes: 8 additions & 0 deletions tests/ui/doc/doc_include_without_cfg.fixed
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![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
Expand All @@ -8,6 +9,12 @@
#![doc = "some doc"]
//! more doc

macro_rules! man_link {
($a:literal, $b:literal) => {
concat!($a, $b)
}
}

// Should not lint!
macro_rules! tst {
($(#[$attr:meta])*) => {
Expand All @@ -24,6 +31,7 @@ tst! {

#[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"]
Expand Down
8 changes: 8 additions & 0 deletions tests/ui/doc/doc_include_without_cfg.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![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
Expand All @@ -8,6 +9,12 @@
#![doc = "some doc"]
//! more doc

macro_rules! man_link {
($a:literal, $b:literal) => {
concat!($a, $b)
}
}

// Should not lint!
macro_rules! tst {
($(#[$attr:meta])*) => {
Expand All @@ -24,6 +31,7 @@ tst! {

#[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"]
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/doc/doc_include_without_cfg.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: included a file in documentation unconditionally
--> tests/ui/doc/doc_include_without_cfg.rs:4:1
--> tests/ui/doc/doc_include_without_cfg.rs:5:1
|
LL | #![doc = include_str!("../approx_const.rs")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `cfg_attr(doc, doc = "...")`: `#![cfg_attr(doc, doc = include_str!("../approx_const.rs"))]`
Expand All @@ -8,7 +8,7 @@ LL | #![doc = include_str!("../approx_const.rs")]
= 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:25:1
--> tests/ui/doc/doc_include_without_cfg.rs:32:1
|
LL | #[doc = include_str!("../approx_const.rs")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `cfg_attr(doc, doc = "...")`: `#[cfg_attr(doc, doc = include_str!("../approx_const.rs"))]`
Expand Down

0 comments on commit afe4556

Please sign in to comment.