Skip to content

Commit e1abd8e

Browse files
committed
missing_safety_doc: guide them out of an HTML block with a header
1 parent 9563a5c commit e1abd8e

9 files changed

+1653
-50
lines changed

clippy_lints/src/doc/doc_suspicious_footnotes.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
2-
use rustc_ast::attr::AttributeExt as _;
32
use rustc_ast::token::CommentKind;
43
use rustc_errors::Applicability;
54
use rustc_hir::{AttrStyle, Attribute};
@@ -8,7 +7,7 @@ use rustc_resolve::rustdoc::DocFragmentKind;
87

98
use std::ops::Range;
109

11-
use super::{DOC_SUSPICIOUS_FOOTNOTES, Fragments};
10+
use super::{DOC_SUSPICIOUS_FOOTNOTES, Fragments, find_doc_attr_by_span};
1211

1312
pub fn check(cx: &LateContext<'_>, doc: &str, range: Range<usize>, fragments: &Fragments<'_>, attrs: &[Attribute]) {
1413
for i in doc[range.clone()]
@@ -44,14 +43,8 @@ pub fn check(cx: &LateContext<'_>, doc: &str, range: Range<usize>, fragments: &F
4443
"looks like a footnote ref, but has no matching footnote",
4544
|diag| {
4645
if this_fragment.kind == DocFragmentKind::SugaredDoc {
47-
let (doc_attr, (_, doc_attr_comment_kind), attr_style) = attrs
48-
.iter()
49-
.filter(|attr| attr.span().overlaps(this_fragment.span))
50-
.rev()
51-
.find_map(|attr| {
52-
Some((attr, attr.doc_str_and_comment_kind()?, attr.doc_resolution_scope()?))
53-
})
54-
.unwrap();
46+
let (doc_attr, doc_attr_comment_kind, attr_style) =
47+
find_doc_attr_by_span(attrs, this_fragment.span).unwrap();
5548
let (to_add, terminator) = match (doc_attr_comment_kind, attr_style) {
5649
(CommentKind::Line, AttrStyle::Outer) => ("\n///\n/// ", ""),
5750
(CommentKind::Line, AttrStyle::Inner) => ("\n//!\n//! ", ""),

clippy_lints/src/doc/lazy_continuation.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,6 @@ use std::ops::Range;
77

88
use super::{DOC_LAZY_CONTINUATION, DOC_OVERINDENTED_LIST_ITEMS, Fragments};
99

10-
fn map_container_to_text(c: &super::Container) -> &'static str {
11-
match c {
12-
super::Container::Blockquote => "> ",
13-
// numbered list can have up to nine digits, plus the dot, plus four spaces on either side
14-
super::Container::List(indent) => &" "[0..*indent],
15-
}
16-
}
17-
1810
pub(super) fn check(
1911
cx: &LateContext<'_>,
2012
doc: &str,
@@ -41,7 +33,7 @@ pub(super) fn check(
4133
let mut doc_start_range = &doc[cooked_range];
4234
let mut suggested = String::new();
4335
for c in containers {
44-
let text = map_container_to_text(c);
36+
let text = c.map_to_text();
4537
if doc_start_range.starts_with(text) {
4638
doc_start_range = &doc_start_range[text.len()..];
4739
span = span.with_lo(

clippy_lints/src/doc/missing_headers.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use super::{DocHeaders, MISSING_ERRORS_DOC, MISSING_PANICS_DOC, MISSING_SAFETY_DOC, UNNECESSARY_SAFETY_DOC};
2-
use clippy_utils::diagnostics::{span_lint, span_lint_and_note};
2+
use clippy_utils::diagnostics::span_lint;
33
use clippy_utils::macros::{is_panic, root_macro_call_first_node};
44
use clippy_utils::ty::{get_type_diagnostic_name, implements_trait_with_env, is_type_diagnostic_item};
55
use clippy_utils::visitors::for_each_expr;
@@ -14,7 +14,7 @@ pub fn check(
1414
cx: &LateContext<'_>,
1515
owner_id: OwnerId,
1616
sig: FnSig<'_>,
17-
headers: DocHeaders,
17+
headers: &DocHeaders,
1818
body_id: Option<BodyId>,
1919
check_private_items: bool,
2020
) {
@@ -33,37 +33,38 @@ pub fn check(
3333
}
3434

3535
let span = cx.tcx.def_span(owner_id);
36-
match (headers.safety, sig.header.safety()) {
37-
(false, Safety::Unsafe) => span_lint(
36+
match (headers.safety.is_missing(), sig.header.safety()) {
37+
(true, Safety::Unsafe) => headers.safety.lint(
3838
cx,
3939
MISSING_SAFETY_DOC,
4040
span,
4141
"unsafe function's docs are missing a `# Safety` section",
4242
),
43-
(true, Safety::Safe) => span_lint(
43+
(false, Safety::Safe) => span_lint(
4444
cx,
4545
UNNECESSARY_SAFETY_DOC,
4646
span,
4747
"safe function's docs have unnecessary `# Safety` section",
4848
),
4949
_ => (),
5050
}
51-
if !headers.panics
51+
if headers.panics.is_missing()
5252
&& let Some(body_id) = body_id
5353
&& let Some(panic_span) = find_panic(cx, body_id)
5454
{
55-
span_lint_and_note(
55+
headers.panics.lint_and_then(
5656
cx,
5757
MISSING_PANICS_DOC,
5858
span,
5959
"docs for function which may panic missing `# Panics` section",
60-
Some(panic_span),
61-
"first possible panic found here",
60+
|diag| {
61+
diag.span_note(panic_span, "first possible panic found here");
62+
},
6263
);
6364
}
64-
if !headers.errors {
65+
if headers.errors.is_missing() {
6566
if is_type_diagnostic_item(cx, return_ty(cx, owner_id), sym::Result) {
66-
span_lint(
67+
headers.errors.lint(
6768
cx,
6869
MISSING_ERRORS_DOC,
6970
span,
@@ -85,7 +86,7 @@ pub fn check(
8586
&& let ty::Coroutine(_, subs) = ret_ty.kind()
8687
&& is_type_diagnostic_item(cx, subs.as_coroutine().return_ty(), sym::Result)
8788
{
88-
span_lint(
89+
headers.errors.lint(
8990
cx,
9091
MISSING_ERRORS_DOC,
9192
span,

0 commit comments

Comments
 (0)