Skip to content

check for misplaced headers in missing_safety/error_doc #5659

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
23 changes: 21 additions & 2 deletions clippy_lints/src/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,8 +417,9 @@ fn check_doc<'a, Events: Iterator<Item = (pulldown_cmark::Event<'a>, Range<usize
// text "http://example.com" by pulldown-cmark
continue;
}
headers.safety |= in_heading && text.trim() == "Safety";
headers.errors |= in_heading && text.trim() == "Errors";
let trimmed = text.trim();
headers.safety |= has_header(trimmed, in_heading, "# Safety");
Copy link
Member

@ebroto ebroto May 28, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should check that we are not in_code, otherwise this passes the test:

/// This is not a safety section.
/// ```
///    # Safety
/// ```
pub unsafe fn ceci_n_est_pas_un_titre() {
    unimplemented!();
}

If we add the check (and the missing space in the other test to make it 4) the header is still not recognised.

headers.errors |= has_header(trimmed, in_heading, "# Errors");
let index = match spans.binary_search_by(|c| c.0.cmp(&range.start)) {
Ok(o) => o,
Err(e) => e - 1,
Expand All @@ -440,6 +441,24 @@ fn check_doc<'a, Events: Iterator<Item = (pulldown_cmark::Event<'a>, Range<usize
headers
}

// Normally, the first `if` should suffice, but this also accounts for misplaced headings
fn has_header(trimmed_text: &str, in_heading: bool, header: &'static str) -> 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) {
Expand Down
17 changes: 17 additions & 0 deletions tests/ui/doc_unsafe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,21 @@ very_unsafe!();
// we don't lint code from external macros
undocd_unsafe!();

/** This safety section is now recognized.

# Safety
Copy link
Member

@ebroto ebroto May 28, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problematic example in the issue has an indentation of 4 spaces (here there are only 3). I think this is important because in markdown 4 spaces start a code block, see my other comment.


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();
Expand All @@ -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();
}
}
10 changes: 9 additions & 1 deletion tests/ui/doc_unsafe.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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