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 Oct 30, 2024
1 parent fbf36cc commit 5111470
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
3 changes: 2 additions & 1 deletion 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 Down
14 changes: 14 additions & 0 deletions tests/ui/doc/doc_include_without_cfg.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,20 @@
#![doc = "some doc"]
//! more doc

// 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.
#[cfg_attr(feature = "whatever", doc = include_str!("../approx_const.rs"))]
Expand Down
14 changes: 14 additions & 0 deletions tests/ui/doc/doc_include_without_cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,20 @@
#![doc = "some doc"]
//! more doc

// 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.
#[cfg_attr(feature = "whatever", doc = include_str!("../approx_const.rs"))]
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:25: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 5111470

Please sign in to comment.