From afe45565a179c977801e38808a8bac35b7e88fa5 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Wed, 30 Oct 2024 13:56:58 +0100 Subject: [PATCH] Only emit `doc_include_without_cfg` when `include_str!` is used --- clippy_lints/src/doc/include_in_doc_without_cfg.rs | 7 ++++++- tests/ui/doc/doc_include_without_cfg.fixed | 8 ++++++++ tests/ui/doc/doc_include_without_cfg.rs | 8 ++++++++ tests/ui/doc/doc_include_without_cfg.stderr | 4 ++-- 4 files changed, 24 insertions(+), 3 deletions(-) diff --git a/clippy_lints/src/doc/include_in_doc_without_cfg.rs b/clippy_lints/src/doc/include_in_doc_without_cfg.rs index 0f490eb09e3c..49978d4a6555 100644 --- a/clippy_lints/src/doc/include_in_doc_without_cfg.rs +++ b/clippy_lints/src/doc/include_in_doc_without_cfg.rs @@ -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, diff --git a/tests/ui/doc/doc_include_without_cfg.fixed b/tests/ui/doc/doc_include_without_cfg.fixed index 74378808b077..98173879b58e 100644 --- a/tests/ui/doc/doc_include_without_cfg.fixed +++ b/tests/ui/doc/doc_include_without_cfg.fixed @@ -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 @@ -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])*) => { @@ -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"] diff --git a/tests/ui/doc/doc_include_without_cfg.rs b/tests/ui/doc/doc_include_without_cfg.rs index 8a042a7f7a2e..e3de9843829e 100644 --- a/tests/ui/doc/doc_include_without_cfg.rs +++ b/tests/ui/doc/doc_include_without_cfg.rs @@ -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 @@ -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])*) => { @@ -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"] diff --git a/tests/ui/doc/doc_include_without_cfg.stderr b/tests/ui/doc/doc_include_without_cfg.stderr index b53e4a0737f7..b6366b7a47f1 100644 --- a/tests/ui/doc/doc_include_without_cfg.stderr +++ b/tests/ui/doc/doc_include_without_cfg.stderr @@ -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"))]` @@ -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"))]`