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..d4ae810d7385 100644 --- a/tests/ui/doc/doc_include_without_cfg.fixed +++ b/tests/ui/doc/doc_include_without_cfg.fixed @@ -8,6 +8,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 +30,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..c82f6bf20356 100644 --- a/tests/ui/doc/doc_include_without_cfg.rs +++ b/tests/ui/doc/doc_include_without_cfg.rs @@ -8,6 +8,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 +30,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..17ea53c7c318 100644 --- a/tests/ui/doc/doc_include_without_cfg.stderr +++ b/tests/ui/doc/doc_include_without_cfg.stderr @@ -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:31:1 | LL | #[doc = include_str!("../approx_const.rs")] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `cfg_attr(doc, doc = "...")`: `#[cfg_attr(doc, doc = include_str!("../approx_const.rs"))]`