Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 84219f4

Browse files
committed
working naive with outside check_attrs
1 parent 3093b29 commit 84219f4

File tree

4 files changed

+58
-47
lines changed

4 files changed

+58
-47
lines changed

clippy_lints/src/doc/empty_docs.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
use clippy_utils::diagnostics::span_lint_and_help;
2+
use rustc_ast::Attribute;
3+
use rustc_lint::LateContext;
4+
5+
use super::EMPTY_DOCS;
6+
7+
// TODO: Adjust the parameters as necessary
8+
pub(super) fn check(cx: &LateContext<'_>, attrs: &[Attribute]) {
9+
let doc_attrs: Vec<_> = attrs.iter().filter(|attr| attr.doc_str().is_some()).collect();
10+
11+
let span;
12+
if let Some(first) = doc_attrs.first()
13+
&& let Some(last) = doc_attrs.last()
14+
{
15+
span = first.span.with_hi(last.span.hi());
16+
span_lint_and_help(
17+
cx,
18+
EMPTY_DOCS,
19+
span,
20+
"empty doc comment",
21+
None,
22+
"consider removing or filling it",
23+
);
24+
}
25+
}

clippy_lints/src/doc/mod.rs

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ use rustc_resolve::rustdoc::{
2323
};
2424
use rustc_session::impl_lint_pass;
2525
use rustc_span::edition::Edition;
26-
use rustc_span::{sym, Span, DUMMY_SP};
26+
use rustc_span::{sym, Span};
2727
use std::ops::Range;
2828
use url::Url;
2929

30+
mod empty_docs;
3031
mod link_with_quotes;
3132
mod markdown;
3233
mod missing_headers;
@@ -403,17 +404,8 @@ impl<'tcx> LateLintPass<'tcx> for Documentation {
403404
return;
404405
};
405406

406-
if let Some(span) = get_empty_doc_combined_span(attrs, item.span)
407-
&& headers.empty
408-
{
409-
span_lint_and_help(
410-
cx,
411-
EMPTY_DOCS,
412-
span,
413-
"empty doc comment",
414-
None,
415-
"consider removing or filling it",
416-
);
407+
if headers.empty && !item.span.is_dummy() {
408+
empty_docs::check(cx, attrs);
417409
}
418410

419411
match item.kind {
@@ -759,20 +751,3 @@ impl<'a, 'tcx> Visitor<'tcx> for FindPanicUnwrap<'a, 'tcx> {
759751
self.cx.tcx.hir()
760752
}
761753
}
762-
763-
fn get_empty_doc_combined_span(attrs: &[Attribute], item_span: Span) -> Option<Span> {
764-
let mut attrs_span = DUMMY_SP;
765-
if attrs.len() > 0 {
766-
attrs_span = attrs
767-
.iter()
768-
.map(|attr| attr.span)
769-
.fold(attrs[0].span, |acc, next| acc.to(next));
770-
}
771-
772-
match (!item_span.is_dummy(), !attrs_span.is_dummy()) {
773-
(true, true) => Some(item_span.shrink_to_lo().to(attrs_span)),
774-
(true, false) => Some(item_span),
775-
(false, true) => Some(attrs_span),
776-
(false, false) => None,
777-
}
778-
}

tests/ui/empty_docs.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,47 @@ enum Warn {
1515
B,
1616
}
1717

18-
enum WarnForB {
18+
enum WarnA {
19+
///
20+
A,
21+
B,
22+
}
23+
24+
enum DontWarn {
1925
/// it's ok
2026
A,
2127
///
2228
B,
2329
}
2430

25-
#[doc = ""]
2631
#[doc = ""]
2732
fn warn_about_this() {}
2833

34+
#[doc = ""]
35+
#[doc = ""]
36+
fn this_doesn_warn() {}
37+
2938
#[doc = "a fine function"]
3039
fn this_is_fine() {}
3140

3241
fn warn_about_this_as_well() {
3342
//!
3443
}
3544

45+
///
46+
fn warn_inner_outer() {
47+
//! what
48+
}
49+
3650
fn this_is_ok() {
3751
//!
3852
//! inside the function
3953
}
4054

4155
fn warn() {
42-
/*! inside the function */
56+
/*! */
57+
}
58+
59+
fn dont_warn() {
60+
/*! dont warn me */
4361
}

tests/ui/empty_docs.stderr

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,26 @@
11
error: empty doc comment
22
--> tests/ui/empty_docs.rs:10:1
33
|
4-
LL | / ///
5-
LL | | enum Warn {
6-
| |_
4+
LL | ///
5+
| ^^^
76
|
87
= help: consider removing or filling it
98
= note: `-D clippy::empty-docs` implied by `-D warnings`
109
= help: to override `-D warnings` add `#[allow(clippy::empty_docs)]`
1110

1211
error: empty doc comment
13-
--> tests/ui/empty_docs.rs:18:1
12+
--> tests/ui/empty_docs.rs:31:1
1413
|
15-
LL | / enum WarnForB {
16-
LL | | /// it's ok
17-
LL | | A,
18-
LL | | ///
19-
LL | | B,
20-
LL | | }
21-
| |_^
14+
LL | #[doc = ""]
15+
| ^^^^^^^^^^^
2216
|
2317
= help: consider removing or filling it
2418

2519
error: empty doc comment
26-
--> tests/ui/empty_docs.rs:32:1
20+
--> tests/ui/empty_docs.rs:42:5
2721
|
28-
LL | / fn warn_about_this_as_well() {
29-
LL | | //!
30-
| |_______^
22+
LL | //!
23+
| ^^^
3124
|
3225
= help: consider removing or filling it
3326

0 commit comments

Comments
 (0)