Skip to content

Empty docs #12342

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5158,6 +5158,7 @@ Released 2018-09-13
[`duration_subsec`]: https://rust-lang.github.io/rust-clippy/master/index.html#duration_subsec
[`eager_transmute`]: https://rust-lang.github.io/rust-clippy/master/index.html#eager_transmute
[`else_if_without_else`]: https://rust-lang.github.io/rust-clippy/master/index.html#else_if_without_else
[`empty_docs`]: https://rust-lang.github.io/rust-clippy/master/index.html#empty_docs
[`empty_drop`]: https://rust-lang.github.io/rust-clippy/master/index.html#empty_drop
[`empty_enum`]: https://rust-lang.github.io/rust-clippy/master/index.html#empty_enum
[`empty_enum_variants_with_brackets`]: https://rust-lang.github.io/rust-clippy/master/index.html#empty_enum_variants_with_brackets
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/declared_lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
crate::disallowed_types::DISALLOWED_TYPES_INFO,
crate::doc::DOC_LINK_WITH_QUOTES_INFO,
crate::doc::DOC_MARKDOWN_INFO,
crate::doc::EMPTY_DOCS_INFO,
crate::doc::MISSING_ERRORS_DOC_INFO,
crate::doc::MISSING_PANICS_DOC_INFO,
crate::doc::MISSING_SAFETY_DOC_INFO,
Expand Down
25 changes: 25 additions & 0 deletions clippy_lints/src/doc/empty_docs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use clippy_utils::diagnostics::span_lint_and_help;
use rustc_ast::Attribute;
use rustc_lint::LateContext;

use super::EMPTY_DOCS;

// TODO: Adjust the parameters as necessary
pub(super) fn check(cx: &LateContext<'_>, attrs: &[Attribute]) {
let doc_attrs: Vec<_> = attrs.iter().filter(|attr| attr.doc_str().is_some()).collect();

let span;
if let Some(first) = doc_attrs.first()
&& let Some(last) = doc_attrs.last()
{
span = first.span.with_hi(last.span.hi());
span_lint_and_help(
cx,
EMPTY_DOCS,
span,
"empty doc comment",
None,
"consider removing or filling it",
);
}
}
50 changes: 44 additions & 6 deletions clippy_lints/src/doc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use rustc_span::{sym, Span};
use std::ops::Range;
use url::Url;

mod empty_docs;
mod link_with_quotes;
mod markdown;
mod missing_headers;
Expand Down Expand Up @@ -338,6 +339,29 @@ declare_clippy_lint! {
"suspicious usage of (outer) doc comments"
}

declare_clippy_lint! {
/// ### What it does
/// Detects documentation that is empty.
/// ### Why is this bad?
/// It is unlikely that there is any reason to have empty documentation for an item
/// ### Example
/// ```rust
/// ///
/// fn returns_true() -> bool {
/// true
/// }
/// Use instead:
/// ```rust
/// fn returns_true() -> bool {
/// true
/// }
/// ```
#[clippy::version = "1.78.0"]
pub EMPTY_DOCS,
suspicious,
"docstrings exist but documentation is empty"
}

#[derive(Clone)]
pub struct Documentation {
valid_idents: FxHashSet<String>,
Expand All @@ -364,7 +388,8 @@ impl_lint_pass!(Documentation => [
NEEDLESS_DOCTEST_MAIN,
TEST_ATTR_IN_DOCTEST,
UNNECESSARY_SAFETY_DOC,
SUSPICIOUS_DOC_COMMENTS
SUSPICIOUS_DOC_COMMENTS,
EMPTY_DOCS,
]);

impl<'tcx> LateLintPass<'tcx> for Documentation {
Expand All @@ -378,6 +403,11 @@ impl<'tcx> LateLintPass<'tcx> for Documentation {
let Some(headers) = check_attrs(cx, &self.valid_idents, attrs) else {
return;
};

if headers.empty && !item.span.is_dummy() {
empty_docs::check(cx, attrs);
}

match item.kind {
hir::ItemKind::Fn(ref sig, _, body_id) => {
if !(is_entrypoint_fn(cx, item.owner_id.to_def_id()) || in_external_macro(cx.tcx.sess, item.span)) {
Expand Down Expand Up @@ -477,6 +507,7 @@ struct DocHeaders {
safety: bool,
errors: bool,
panics: bool,
empty: bool,
}

/// Does some pre-processing on raw, desugared `#[doc]` attributes such as parsing them and
Expand All @@ -502,14 +533,21 @@ fn check_attrs(cx: &LateContext<'_>, valid_idents: &FxHashSet<String>, attrs: &[
suspicious_doc_comments::check(cx, attrs);

let (fragments, _) = attrs_to_doc_fragments(attrs.iter().map(|attr| (attr, None)), true);
let mut doc = String::new();
for fragment in &fragments {
add_doc_fragment(&mut doc, fragment);
}
let mut doc = fragments
.iter()
.fold(String::new(), |mut acc, fragment| {
add_doc_fragment(&mut acc, fragment);
acc
})
.trim()
.to_string();
doc.pop();

if doc.is_empty() {
return Some(DocHeaders::default());
return Some(DocHeaders {
empty: true,
..DocHeaders::default()
});
}

let mut cb = fake_broken_link_callback;
Expand Down
61 changes: 61 additions & 0 deletions tests/ui/empty_docs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#![allow(unused)]
#![warn(clippy::empty_docs)]

/// this is a struct
struct Bananas {
/// count
count: usize,
}

///
enum Warn {
///
A,
///
B,
}

enum WarnA {
///
A,
B,
}

enum DontWarn {
/// it's ok
A,
///
B,
}

#[doc = ""]
fn warn_about_this() {}

#[doc = ""]
#[doc = ""]
fn this_doesn_warn() {}

#[doc = "a fine function"]
fn this_is_fine() {}

fn warn_about_this_as_well() {
//!
}

///
fn warn_inner_outer() {
//! what
}

fn this_is_ok() {
//!
//! inside the function
}

fn warn() {
/*! */
}

fn dont_warn() {
/*! dont warn me */
}
45 changes: 45 additions & 0 deletions tests/ui/empty_docs.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
error: empty doc comment
--> tests/ui/empty_docs.rs:10:1
|
LL | ///
| ^^^
|
= help: consider removing or filling it
= note: `-D clippy::empty-docs` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::empty_docs)]`

error: empty doc comment
--> tests/ui/empty_docs.rs:31:1
|
LL | #[doc = ""]
| ^^^^^^^^^^^
|
= help: consider removing or filling it

error: empty doc comment
--> tests/ui/empty_docs.rs:34:1
|
LL | / #[doc = ""]
LL | | #[doc = ""]
| |___________^
|
= help: consider removing or filling it

error: empty doc comment
--> tests/ui/empty_docs.rs:42:5
|
LL | //!
| ^^^
|
= help: consider removing or filling it

error: empty doc comment
--> tests/ui/empty_docs.rs:56:5
|
LL | /*! */
| ^^^^^^
|
= help: consider removing or filling it

error: aborting due to 5 previous errors