Skip to content

Commit 52fe6fd

Browse files
Do not emit include_in_doc_without_cfg inside macros
1 parent 2f92eda commit 52fe6fd

File tree

4 files changed

+51
-3
lines changed

4 files changed

+51
-3
lines changed

clippy_lints/src/doc/include_in_doc_without_cfg.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ use super::DOC_INCLUDE_WITHOUT_CFG;
99

1010
pub fn check(cx: &LateContext<'_>, attrs: &[Attribute]) {
1111
for attr in attrs {
12-
if let AttrKind::Normal(ref normal) = attr.kind
12+
if !attr.span.from_expansion()
13+
&& let AttrKind::Normal(ref normal) = attr.kind
1314
&& normal.item.path == sym::doc
1415
&& let AttrArgs::Eq(_, AttrArgsEq::Hir(ref meta)) = normal.item.args
1516
&& !attr.span.contains(meta.span)
@@ -20,8 +21,13 @@ pub fn check(cx: &LateContext<'_>, attrs: &[Attribute]) {
2021
// several lines.
2122
&& let Some(start) = snippet.find('[')
2223
&& 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!")
2330
{
24-
let snippet = &snippet[start + 1..end];
2531
span_lint_and_sugg(
2632
cx,
2733
DOC_INCLUDE_WITHOUT_CFG,

tests/ui/doc/doc_include_without_cfg.fixed

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,29 @@
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+
17+
// Should not lint!
18+
macro_rules! tst {
19+
($(#[$attr:meta])*) => {
20+
$(#[$attr])*
21+
fn blue() {
22+
println!("Hello, world!");
23+
}
24+
}
25+
}
26+
27+
tst! {
28+
/// This is a test with no included file
29+
}
30+
1131
#[cfg_attr(doc, doc = include_str!("../approx_const.rs"))] //~ doc_include_without_cfg
1232
// Should not lint.
33+
#[doc = man_link!("bla", "blob")]
1334
#[cfg_attr(feature = "whatever", doc = include_str!("../approx_const.rs"))]
1435
#[cfg_attr(doc, doc = include_str!("../approx_const.rs"))]
1536
#[doc = "some doc"]

tests/ui/doc/doc_include_without_cfg.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,29 @@
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+
17+
// Should not lint!
18+
macro_rules! tst {
19+
($(#[$attr:meta])*) => {
20+
$(#[$attr])*
21+
fn blue() {
22+
println!("Hello, world!");
23+
}
24+
}
25+
}
26+
27+
tst! {
28+
/// This is a test with no included file
29+
}
30+
1131
#[doc = include_str!("../approx_const.rs")] //~ doc_include_without_cfg
1232
// Should not lint.
33+
#[doc = man_link!("bla", "blob")]
1334
#[cfg_attr(feature = "whatever", doc = include_str!("../approx_const.rs"))]
1435
#[cfg_attr(doc, doc = include_str!("../approx_const.rs"))]
1536
#[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:11: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)