From 20309c56b830c37089975fe8c956fb7c69d7b059 Mon Sep 17 00:00:00 2001 From: Andre Bogus Date: Thu, 28 May 2020 21:37:43 +0200 Subject: [PATCH] check for misplaced headers in missing_safety/error_doc --- clippy_lints/src/doc.rs | 23 +++++++++++++++++++++-- tests/ui/doc_unsafe.rs | 17 +++++++++++++++++ tests/ui/doc_unsafe.stderr | 10 +++++++++- 3 files changed, 47 insertions(+), 3 deletions(-) diff --git a/clippy_lints/src/doc.rs b/clippy_lints/src/doc.rs index 8d1e91f9adbd..c43f4af00464 100644 --- a/clippy_lints/src/doc.rs +++ b/clippy_lints/src/doc.rs @@ -417,8 +417,9 @@ fn check_doc<'a, Events: Iterator, Range o, Err(e) => e - 1, @@ -440,6 +441,24 @@ fn check_doc<'a, Events: Iterator, Range bool { + if in_heading && trimmed_text == &header[2..] { + return true; + } + if let Some(index) = trimmed_text.find(header) { + let bytes = trimmed_text.as_bytes(); + for &b in bytes[0..index].iter().rev() { + if b != b' ' && b != b'\t' { + return b == b'\n'; + } + } + true + } else { + false + } +} + static LEAVE_MAIN_PATTERNS: &[&str] = &["static", "fn main() {}", "extern crate", "async fn main() {"]; fn check_code(cx: &LateContext<'_, '_>, text: &str, span: Span) { diff --git a/tests/ui/doc_unsafe.rs b/tests/ui/doc_unsafe.rs index 484aa72d59a2..cf330a09278f 100644 --- a/tests/ui/doc_unsafe.rs +++ b/tests/ui/doc_unsafe.rs @@ -88,6 +88,21 @@ very_unsafe!(); // we don't lint code from external macros undocd_unsafe!(); +/** This safety section is now recognized. + + # Safety + + No risk, no fun. +*/ +pub unsafe fn strange_doc_comments() { + unimplemented!(); +} + +/// This is not a # Safety section. +pub unsafe fn ceci_n_est_pas_un_titre() { + unimplemented!(); +} + fn main() { unsafe { you_dont_see_me(); @@ -96,5 +111,7 @@ fn main() { apocalypse(&mut universe); private_mod::only_crate_wide_accessible(); drive(); + strange_doc_comments(); + ceci_n_est_pas_un_titre(); } } diff --git a/tests/ui/doc_unsafe.stderr b/tests/ui/doc_unsafe.stderr index c784d41ba172..e507034609ca 100644 --- a/tests/ui/doc_unsafe.stderr +++ b/tests/ui/doc_unsafe.stderr @@ -43,5 +43,13 @@ LL | very_unsafe!(); | = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) -error: aborting due to 5 previous errors +error: unsafe function's docs miss `# Safety` section + --> $DIR/doc_unsafe.rs:102:1 + | +LL | / pub unsafe fn ceci_n_est_pas_un_titre() { +LL | | unimplemented!(); +LL | | } + | |_^ + +error: aborting due to 6 previous errors