Skip to content

Commit b0e9683

Browse files
committed
add empty_docs lint
1 parent 5a52c8a commit b0e9683

File tree

6 files changed

+134
-0
lines changed

6 files changed

+134
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -5158,6 +5158,7 @@ Released 2018-09-13
51585158
[`duration_subsec`]: https://rust-lang.github.io/rust-clippy/master/index.html#duration_subsec
51595159
[`eager_transmute`]: https://rust-lang.github.io/rust-clippy/master/index.html#eager_transmute
51605160
[`else_if_without_else`]: https://rust-lang.github.io/rust-clippy/master/index.html#else_if_without_else
5161+
[`empty_docs`]: https://rust-lang.github.io/rust-clippy/master/index.html#empty_docs
51615162
[`empty_drop`]: https://rust-lang.github.io/rust-clippy/master/index.html#empty_drop
51625163
[`empty_enum`]: https://rust-lang.github.io/rust-clippy/master/index.html#empty_enum
51635164
[`empty_enum_variants_with_brackets`]: https://rust-lang.github.io/rust-clippy/master/index.html#empty_enum_variants_with_brackets

clippy_lints/src/declared_lints.rs

+1
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
152152
crate::drop_forget_ref::MEM_FORGET_INFO,
153153
crate::duplicate_mod::DUPLICATE_MOD_INFO,
154154
crate::else_if_without_else::ELSE_IF_WITHOUT_ELSE_INFO,
155+
crate::empty_docs::EMPTY_DOCS_INFO,
155156
crate::empty_drop::EMPTY_DROP_INFO,
156157
crate::empty_enum::EMPTY_ENUM_INFO,
157158
crate::empty_with_brackets::EMPTY_ENUM_VARIANTS_WITH_BRACKETS_INFO,

clippy_lints/src/empty_docs.rs

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
use clippy_utils::diagnostics::span_lint_and_help;
2+
use rustc_ast::ast::*;
3+
use rustc_lint::{EarlyContext, EarlyLintPass};
4+
use rustc_session::declare_lint_pass;
5+
6+
declare_clippy_lint! {
7+
/// ### What it does
8+
/// Detects documentation that is empty.
9+
/// ### Why is this bad?
10+
/// It is unlikely that there is any reason to have empty documentation for an item
11+
///
12+
/// ### Example
13+
/// ```rust
14+
/// ///
15+
/// fn returns_true() -> bool {
16+
/// true
17+
/// }
18+
/// ```
19+
/// Use instead:
20+
/// ```rust
21+
/// fn returns_true() -> bool {
22+
/// true
23+
/// }
24+
/// ```
25+
#[clippy::version = "1.74.0"]
26+
pub EMPTY_DOCS,
27+
suspicious,
28+
"docstrings exist but documentation is empty"
29+
}
30+
31+
declare_lint_pass!(EmptyDocs => [EMPTY_DOCS]);
32+
33+
fn trim_comment(comment: &str) -> String {
34+
comment
35+
.trim()
36+
.split("\n")
37+
.map(|comment| comment.trim().trim_matches('*').trim_matches('!'))
38+
.filter(|line| !line.is_empty())
39+
.collect::<Vec<&str>>()
40+
.join("")
41+
}
42+
43+
impl EarlyLintPass for EmptyDocs {
44+
fn check_attribute(&mut self, cx: &EarlyContext<'_>, attribute: &Attribute) {
45+
if let AttrKind::DocComment(_line, comment) = attribute.kind {
46+
if trim_comment(comment.as_str()).len() == 0 {
47+
span_lint_and_help(
48+
cx,
49+
EMPTY_DOCS,
50+
attribute.span,
51+
"empty doc comment",
52+
None,
53+
"consider removing or fill it",
54+
);
55+
}
56+
}
57+
}
58+
}

clippy_lints/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ mod double_parens;
119119
mod drop_forget_ref;
120120
mod duplicate_mod;
121121
mod else_if_without_else;
122+
mod empty_docs;
122123
mod empty_drop;
123124
mod empty_enum;
124125
mod empty_with_brackets;
@@ -1116,6 +1117,7 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) {
11161117
});
11171118
store.register_late_pass(move |_| Box::new(incompatible_msrv::IncompatibleMsrv::new(msrv())));
11181119
store.register_late_pass(|_| Box::new(to_string_trait_impl::ToStringTraitImpl));
1120+
store.register_early_pass(|| Box::new(empty_docs::EmptyDocs));
11191121
// add lints here, do not remove this comment, it's used in `new_lint`
11201122
}
11211123

tests/ui/empty_docs.rs

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#![allow(unused)]
2+
#![warn(clippy::empty_docs)]
3+
4+
pub mod outer_module {
5+
6+
//!
7+
//! valid doc comment
8+
//!!
9+
//!! valid doc comment
10+
11+
///
12+
/// valid doc comment
13+
///
14+
/// valid block doc comment
15+
16+
/**
17+
*
18+
*/
19+
20+
/**
21+
* valid block doc comment
22+
*
23+
*/
24+
25+
pub mod inner_module {}
26+
}

tests/ui/empty_docs.stderr

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
error: empty doc comment
2+
--> tests/ui/empty_docs.rs:11:5
3+
|
4+
LL | ///
5+
| ^^^
6+
|
7+
= help: consider removing or fill it
8+
= note: `-D clippy::empty-docs` implied by `-D warnings`
9+
= help: to override `-D warnings` add `#[allow(clippy::empty_docs)]`
10+
11+
error: empty doc comment
12+
--> tests/ui/empty_docs.rs:13:5
13+
|
14+
LL | ///
15+
| ^^^
16+
|
17+
= help: consider removing or fill it
18+
19+
error: empty doc comment
20+
--> tests/ui/empty_docs.rs:16:5
21+
|
22+
LL | / /**
23+
LL | | *
24+
LL | | */
25+
| |_______^
26+
|
27+
= help: consider removing or fill it
28+
29+
error: empty doc comment
30+
--> tests/ui/empty_docs.rs:6:5
31+
|
32+
LL | //!
33+
| ^^^
34+
|
35+
= help: consider removing or fill it
36+
37+
error: empty doc comment
38+
--> tests/ui/empty_docs.rs:8:5
39+
|
40+
LL | //!!
41+
| ^^^^
42+
|
43+
= help: consider removing or fill it
44+
45+
error: aborting due to 5 previous errors
46+

0 commit comments

Comments
 (0)