Skip to content

Commit f1b5434

Browse files
committed
move suspicious_doc_comments to separate file
1 parent b835952 commit f1b5434

File tree

2 files changed

+51
-43
lines changed

2 files changed

+51
-43
lines changed

clippy_lints/src/doc/mod.rs

Lines changed: 3 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use clippy_utils::attrs::is_doc_hidden;
2-
use clippy_utils::diagnostics::{span_lint, span_lint_and_help, span_lint_and_then};
2+
use clippy_utils::diagnostics::{span_lint, span_lint_and_help};
33
use clippy_utils::macros::{is_panic, root_macro_call_first_node};
44
use clippy_utils::ty::is_type_diagnostic_item;
55
use clippy_utils::visitors::Visitable;
@@ -10,10 +10,7 @@ use pulldown_cmark::Event::{
1010
use pulldown_cmark::Tag::{CodeBlock, Heading, Item, Link, Paragraph};
1111
use pulldown_cmark::{BrokenLink, CodeBlockKind, CowStr, Options};
1212
use rustc_ast::ast::Attribute;
13-
use rustc_ast::token::CommentKind;
14-
use rustc_ast::{AttrKind, AttrStyle};
1513
use rustc_data_structures::fx::FxHashSet;
16-
use rustc_errors::Applicability;
1714
use rustc_hir as hir;
1815
use rustc_hir::intravisit::{self, Visitor};
1916
use rustc_hir::{AnonConst, Expr};
@@ -35,6 +32,7 @@ mod link_with_quotes;
3532
mod markdown;
3633
mod missing_headers;
3734
mod needless_doctest_main;
35+
mod suspicious_doc_comments;
3836

3937
declare_clippy_lint! {
4038
/// ### What it does
@@ -308,7 +306,6 @@ declare_clippy_lint! {
308306
"suspicious usage of (outer) doc comments"
309307
}
310308

311-
#[expect(clippy::module_name_repetitions)]
312309
#[derive(Clone)]
313310
pub struct Doc {
314311
valid_idents: FxHashSet<String>,
@@ -451,7 +448,7 @@ fn check_attrs(cx: &LateContext<'_>, valid_idents: &FxHashSet<String>, attrs: &[
451448
return None;
452449
}
453450

454-
check_almost_inner_doc(cx, attrs);
451+
suspicious_doc_comments::check(cx, attrs);
455452

456453
let (fragments, _) = attrs_to_doc_fragments(attrs.iter().map(|attr| (attr, None)), true);
457454
let mut doc = String::new();
@@ -481,43 +478,6 @@ fn check_attrs(cx: &LateContext<'_>, valid_idents: &FxHashSet<String>, attrs: &[
481478
))
482479
}
483480

484-
/// Looks for `///!` and `/**!` comments, which were probably meant to be `//!` and `/*!`
485-
fn check_almost_inner_doc(cx: &LateContext<'_>, attrs: &[Attribute]) {
486-
let replacements: Vec<_> = attrs
487-
.iter()
488-
.filter_map(|attr| {
489-
if let AttrKind::DocComment(com_kind, sym) = attr.kind
490-
&& let AttrStyle::Outer = attr.style
491-
&& let Some(com) = sym.as_str().strip_prefix('!')
492-
{
493-
let sugg = match com_kind {
494-
CommentKind::Line => format!("//!{com}"),
495-
CommentKind::Block => format!("/*!{com}*/"),
496-
};
497-
Some((attr.span, sugg))
498-
} else {
499-
None
500-
}
501-
})
502-
.collect();
503-
504-
if let Some((&(lo_span, _), &(hi_span, _))) = replacements.first().zip(replacements.last()) {
505-
span_lint_and_then(
506-
cx,
507-
SUSPICIOUS_DOC_COMMENTS,
508-
lo_span.to(hi_span),
509-
"this is an outer doc comment and does not apply to the parent module or crate",
510-
|diag| {
511-
diag.multipart_suggestion(
512-
"use an inner doc comment to document the parent module or crate",
513-
replacements,
514-
Applicability::MaybeIncorrect,
515-
);
516-
},
517-
);
518-
}
519-
}
520-
521481
const RUST_CODE: &[&str] = &["rust", "no_run", "should_panic", "compile_fail"];
522482

523483
/// Checks parsed documentation.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
use clippy_utils::diagnostics::span_lint_and_then;
2+
use rustc_ast::token::CommentKind;
3+
use rustc_ast::{AttrKind, AttrStyle, Attribute};
4+
use rustc_errors::Applicability;
5+
use rustc_lint::LateContext;
6+
use rustc_span::Span;
7+
8+
use super::SUSPICIOUS_DOC_COMMENTS;
9+
10+
pub fn check(cx: &LateContext<'_>, attrs: &[Attribute]) {
11+
let replacements: Vec<_> = collect_doc_replacements(attrs);
12+
13+
if let Some((&(lo_span, _), &(hi_span, _))) = replacements.first().zip(replacements.last()) {
14+
span_lint_and_then(
15+
cx,
16+
SUSPICIOUS_DOC_COMMENTS,
17+
lo_span.to(hi_span),
18+
"this is an outer doc comment and does not apply to the parent module or crate",
19+
|diag| {
20+
diag.multipart_suggestion(
21+
"use an inner doc comment to document the parent module or crate",
22+
replacements,
23+
Applicability::MaybeIncorrect,
24+
);
25+
},
26+
);
27+
}
28+
}
29+
30+
fn collect_doc_replacements(attrs: &[Attribute]) -> Vec<(Span, String)> {
31+
attrs
32+
.iter()
33+
.filter_map(|attr| {
34+
if let AttrKind::DocComment(com_kind, sym) = attr.kind
35+
&& let AttrStyle::Outer = attr.style
36+
&& let Some(com) = sym.as_str().strip_prefix('!')
37+
{
38+
let sugg = match com_kind {
39+
CommentKind::Line => format!("//!{com}"),
40+
CommentKind::Block => format!("/*!{com}*/"),
41+
};
42+
Some((attr.span, sugg))
43+
} else {
44+
None
45+
}
46+
})
47+
.collect()
48+
}

0 commit comments

Comments
 (0)