Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
22 changes: 18 additions & 4 deletions clippy_lints/src/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,13 +367,16 @@ fn check_attrs<'a>(cx: &LateContext<'_, '_>, valid_idents: &FxHashSet<String>, a
check_doc(cx, valid_idents, events, &spans)
}

const RUST_CODE: &[&str] = &["rust", "no_run", "should_panic", "compile_fail", "edition2018"];

fn check_doc<'a, Events: Iterator<Item = (pulldown_cmark::Event<'a>, Range<usize>)>>(
cx: &LateContext<'_, '_>,
valid_idents: &FxHashSet<String>,
events: Events,
spans: &[(usize, Span)],
) -> DocHeaders {
// true if a safety header was found
use pulldown_cmark::CodeBlockKind;
use pulldown_cmark::Event::{
Code, End, FootnoteReference, HardBreak, Html, Rule, SoftBreak, Start, TaskListMarker, Text,
};
Expand All @@ -386,11 +389,20 @@ fn check_doc<'a, Events: Iterator<Item = (pulldown_cmark::Event<'a>, Range<usize
let mut in_code = false;
let mut in_link = None;
let mut in_heading = false;

let mut is_rust = false;
for (event, range) in events {
match event {
Start(CodeBlock(_)) => in_code = true,
End(CodeBlock(_)) => in_code = false,
Start(CodeBlock(ref kind)) => {
in_code = true;
if let CodeBlockKind::Fenced(lang) = kind {
is_rust =
lang.is_empty() || !lang.contains("ignore") && lang.split(',').any(|i| RUST_CODE.contains(&i));
}
},
End(CodeBlock(_)) => {
in_code = false;
is_rust = false;
},
Start(Link(_, url, _)) => in_link = Some(url),
End(Link(..)) => in_link = None,
Start(Heading(_)) => in_heading = true,
Expand All @@ -413,7 +425,9 @@ fn check_doc<'a, Events: Iterator<Item = (pulldown_cmark::Event<'a>, Range<usize
};
let (begin, span) = spans[index];
if in_code {
check_code(cx, &text, span);
if is_rust {
check_code(cx, &text, span);
}
} else {
// Adjust for the beginning of the current `Event`
let span = span.with_lo(span.lo() + BytePos::from_usize(range.start - begin));
Expand Down
36 changes: 34 additions & 2 deletions tests/ui/needless_doc_main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,23 @@
/// unimplemented!();
/// }
/// ```
fn bad_doctest() {}
///
/// This should, too.
///
/// ```rust
/// fn main() {
/// unimplemented!();
/// }
/// ```
///
/// This one too.
///
/// ```no_run
/// fn main() {
/// unimplemented!();
/// }
/// ```
fn bad_doctests() {}

/// # Examples
///
Expand All @@ -34,9 +50,25 @@ fn bad_doctest() {}
/// assert_eq(1u8, test::black_box(1));
/// }
/// ```
///
/// We should not lint non-rust examples:
///
/// ```rust,ignore
/// fn main() {
/// unimplemented!();
/// }
/// ```
///
/// Or even non-rust examples:
///
/// ```text
/// fn main() {
/// is what starts the program
/// }
/// ```
fn no_false_positives() {}

fn main() {
bad_doctest();
bad_doctests();
no_false_positives();
}
14 changes: 13 additions & 1 deletion tests/ui/needless_doc_main.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,17 @@ LL | /// fn main() {
|
= note: `-D clippy::needless-doctest-main` implied by `-D warnings`

error: aborting due to previous error
error: needless `fn main` in doctest
--> $DIR/needless_doc_main.rs:15:4
|
LL | /// fn main() {
| ^^^^^^^^^^^^

error: needless `fn main` in doctest
--> $DIR/needless_doc_main.rs:23:4
|
LL | /// fn main() {
| ^^^^^^^^^^^^

error: aborting due to 3 previous errors