Skip to content

Commit afe4556

Browse files
Only emit doc_include_without_cfg when include_str! is used
1 parent 5111470 commit afe4556

File tree

4 files changed

+24
-3
lines changed

4 files changed

+24
-3
lines changed

clippy_lints/src/doc/include_in_doc_without_cfg.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,13 @@ pub fn check(cx: &LateContext<'_>, attrs: &[Attribute]) {
2121
// several lines.
2222
&& let Some(start) = snippet.find('[')
2323
&& let Some(end) = snippet.rfind(']')
24+
&& let snippet = &snippet[start + 1..end]
25+
// We check that the expansion actually comes from `include_str!` and not just from
26+
// another macro.
27+
&& let Some(sub_snippet) = snippet.trim().strip_prefix("doc")
28+
&& let Some(sub_snippet) = sub_snippet.trim().strip_prefix("=")
29+
&& sub_snippet.trim().starts_with("include_str!")
2430
{
25-
let snippet = &snippet[start + 1..end];
2631
span_lint_and_sugg(
2732
cx,
2833
DOC_INCLUDE_WITHOUT_CFG,

tests/ui/doc/doc_include_without_cfg.fixed

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![warn(clippy::doc_include_without_cfg)]
2+
23
// Should not lint.
34
#![doc(html_playground_url = "https://playground.example.com/")]
45
#![cfg_attr(doc, doc = include_str!("../approx_const.rs"))] //~ doc_include_without_cfg
@@ -8,6 +9,12 @@
89
#![doc = "some doc"]
910
//! more doc
1011

12+
macro_rules! man_link {
13+
($a:literal, $b:literal) => {
14+
concat!($a, $b)
15+
}
16+
}
17+
1118
// Should not lint!
1219
macro_rules! tst {
1320
($(#[$attr:meta])*) => {
@@ -24,6 +31,7 @@ tst! {
2431

2532
#[cfg_attr(doc, doc = include_str!("../approx_const.rs"))] //~ doc_include_without_cfg
2633
// Should not lint.
34+
#[doc = man_link!("bla", "blob")]
2735
#[cfg_attr(feature = "whatever", doc = include_str!("../approx_const.rs"))]
2836
#[cfg_attr(doc, doc = include_str!("../approx_const.rs"))]
2937
#[doc = "some doc"]

tests/ui/doc/doc_include_without_cfg.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![warn(clippy::doc_include_without_cfg)]
2+
23
// Should not lint.
34
#![doc(html_playground_url = "https://playground.example.com/")]
45
#![doc = include_str!("../approx_const.rs")] //~ doc_include_without_cfg
@@ -8,6 +9,12 @@
89
#![doc = "some doc"]
910
//! more doc
1011
12+
macro_rules! man_link {
13+
($a:literal, $b:literal) => {
14+
concat!($a, $b)
15+
}
16+
}
17+
1118
// Should not lint!
1219
macro_rules! tst {
1320
($(#[$attr:meta])*) => {
@@ -24,6 +31,7 @@ tst! {
2431

2532
#[doc = include_str!("../approx_const.rs")] //~ doc_include_without_cfg
2633
// Should not lint.
34+
#[doc = man_link!("bla", "blob")]
2735
#[cfg_attr(feature = "whatever", doc = include_str!("../approx_const.rs"))]
2836
#[cfg_attr(doc, doc = include_str!("../approx_const.rs"))]
2937
#[doc = "some doc"]

tests/ui/doc/doc_include_without_cfg.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: included a file in documentation unconditionally
2-
--> tests/ui/doc/doc_include_without_cfg.rs:4:1
2+
--> tests/ui/doc/doc_include_without_cfg.rs:5:1
33
|
44
LL | #![doc = include_str!("../approx_const.rs")]
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 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")]
88
= help: to override `-D warnings` add `#[allow(clippy::doc_include_without_cfg)]`
99

1010
error: included a file in documentation unconditionally
11-
--> tests/ui/doc/doc_include_without_cfg.rs:25:1
11+
--> tests/ui/doc/doc_include_without_cfg.rs:32:1
1212
|
1313
LL | #[doc = include_str!("../approx_const.rs")]
1414
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `cfg_attr(doc, doc = "...")`: `#[cfg_attr(doc, doc = include_str!("../approx_const.rs"))]`

0 commit comments

Comments
 (0)