Skip to content

Commit

Permalink
Do not emit include_in_doc_without_cfg inside macros
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Nov 21, 2024
1 parent 2f92eda commit 52fe6fd
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 3 deletions.
10 changes: 8 additions & 2 deletions clippy_lints/src/doc/include_in_doc_without_cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ use super::DOC_INCLUDE_WITHOUT_CFG;

pub fn check(cx: &LateContext<'_>, attrs: &[Attribute]) {
for attr in attrs {
if let AttrKind::Normal(ref normal) = attr.kind
if !attr.span.from_expansion()
&& let AttrKind::Normal(ref normal) = attr.kind
&& normal.item.path == sym::doc
&& let AttrArgs::Eq(_, AttrArgsEq::Hir(ref meta)) = normal.item.args
&& !attr.span.contains(meta.span)
Expand All @@ -20,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,
Expand Down
21 changes: 21 additions & 0 deletions tests/ui/doc/doc_include_without_cfg.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,29 @@
#![doc = "some doc"]
//! more doc
macro_rules! man_link {
($a:literal, $b:literal) => {
concat!($a, $b)
};
}

// Should not lint!
macro_rules! tst {
($(#[$attr:meta])*) => {
$(#[$attr])*
fn blue() {
println!("Hello, world!");
}
}
}

tst! {
/// This is a test with no included file
}

#[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"]
Expand Down
21 changes: 21 additions & 0 deletions tests/ui/doc/doc_include_without_cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,29 @@
#![doc = "some doc"]
//! more doc
macro_rules! man_link {
($a:literal, $b:literal) => {
concat!($a, $b)
};
}

// Should not lint!
macro_rules! tst {
($(#[$attr:meta])*) => {
$(#[$attr])*
fn blue() {
println!("Hello, world!");
}
}
}

tst! {
/// This is a test with no included file
}

#[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"]
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/doc/doc_include_without_cfg.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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:11: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"))]`
Expand Down

0 comments on commit 52fe6fd

Please sign in to comment.