Skip to content

Commit 400894d

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

File tree

4 files changed

+21
-2
lines changed

4 files changed

+21
-2
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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88
#![doc = "some doc"]
99
//! more doc
1010

11+
macro_rules! man_link {
12+
($a:literal, $b:literal) => {
13+
concat!($a, $b)
14+
};
15+
}
16+
1117
// Should not lint!
1218
macro_rules! tst {
1319
($(#[$attr:meta])*) => {
@@ -24,6 +30,7 @@ tst! {
2430

2531
#[cfg_attr(doc, doc = include_str!("../approx_const.rs"))] //~ doc_include_without_cfg
2632
// Should not lint.
33+
#[doc = man_link!("bla", "blob")]
2734
#[cfg_attr(feature = "whatever", doc = include_str!("../approx_const.rs"))]
2835
#[cfg_attr(doc, doc = include_str!("../approx_const.rs"))]
2936
#[doc = "some doc"]

tests/ui/doc/doc_include_without_cfg.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88
#![doc = "some doc"]
99
//! more doc
1010
11+
macro_rules! man_link {
12+
($a:literal, $b:literal) => {
13+
concat!($a, $b)
14+
};
15+
}
16+
1117
// Should not lint!
1218
macro_rules! tst {
1319
($(#[$attr:meta])*) => {
@@ -24,6 +30,7 @@ tst! {
2430

2531
#[doc = include_str!("../approx_const.rs")] //~ doc_include_without_cfg
2632
// Should not lint.
33+
#[doc = man_link!("bla", "blob")]
2734
#[cfg_attr(feature = "whatever", doc = include_str!("../approx_const.rs"))]
2835
#[cfg_attr(doc, doc = include_str!("../approx_const.rs"))]
2936
#[doc = "some doc"]

tests/ui/doc/doc_include_without_cfg.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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:31: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)