Skip to content

Commit 23bd427

Browse files
committed
Auto merge of #5298 - rust-lang:needless_doc_main_code, r=flip1995,Manishearth
needless_doc_main: only check rust code This fixes #5280 by checking the language attribute on code blocks. --- changelog: none
2 parents 16b925b + d5c8b78 commit 23bd427

File tree

3 files changed

+65
-7
lines changed

3 files changed

+65
-7
lines changed

clippy_lints/src/doc.rs

+18-4
Original file line numberDiff line numberDiff line change
@@ -367,13 +367,16 @@ fn check_attrs<'a>(cx: &LateContext<'_, '_>, valid_idents: &FxHashSet<String>, a
367367
check_doc(cx, valid_idents, events, &spans)
368368
}
369369

370+
const RUST_CODE: &[&str] = &["rust", "no_run", "should_panic", "compile_fail", "edition2018"];
371+
370372
fn check_doc<'a, Events: Iterator<Item = (pulldown_cmark::Event<'a>, Range<usize>)>>(
371373
cx: &LateContext<'_, '_>,
372374
valid_idents: &FxHashSet<String>,
373375
events: Events,
374376
spans: &[(usize, Span)],
375377
) -> DocHeaders {
376378
// true if a safety header was found
379+
use pulldown_cmark::CodeBlockKind;
377380
use pulldown_cmark::Event::{
378381
Code, End, FootnoteReference, HardBreak, Html, Rule, SoftBreak, Start, TaskListMarker, Text,
379382
};
@@ -386,11 +389,20 @@ fn check_doc<'a, Events: Iterator<Item = (pulldown_cmark::Event<'a>, Range<usize
386389
let mut in_code = false;
387390
let mut in_link = None;
388391
let mut in_heading = false;
389-
392+
let mut is_rust = false;
390393
for (event, range) in events {
391394
match event {
392-
Start(CodeBlock(_)) => in_code = true,
393-
End(CodeBlock(_)) => in_code = false,
395+
Start(CodeBlock(ref kind)) => {
396+
in_code = true;
397+
if let CodeBlockKind::Fenced(lang) = kind {
398+
is_rust =
399+
lang.is_empty() || !lang.contains("ignore") && lang.split(',').any(|i| RUST_CODE.contains(&i));
400+
}
401+
},
402+
End(CodeBlock(_)) => {
403+
in_code = false;
404+
is_rust = false;
405+
},
394406
Start(Link(_, url, _)) => in_link = Some(url),
395407
End(Link(..)) => in_link = None,
396408
Start(Heading(_)) => in_heading = true,
@@ -413,7 +425,9 @@ fn check_doc<'a, Events: Iterator<Item = (pulldown_cmark::Event<'a>, Range<usize
413425
};
414426
let (begin, span) = spans[index];
415427
if in_code {
416-
check_code(cx, &text, span);
428+
if is_rust {
429+
check_code(cx, &text, span);
430+
}
417431
} else {
418432
// Adjust for the beginning of the current `Event`
419433
let span = span.with_lo(span.lo() + BytePos::from_usize(range.start - begin));

tests/ui/needless_doc_main.rs

+34-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,23 @@
88
/// unimplemented!();
99
/// }
1010
/// ```
11-
fn bad_doctest() {}
11+
///
12+
/// This should, too.
13+
///
14+
/// ```rust
15+
/// fn main() {
16+
/// unimplemented!();
17+
/// }
18+
/// ```
19+
///
20+
/// This one too.
21+
///
22+
/// ```no_run
23+
/// fn main() {
24+
/// unimplemented!();
25+
/// }
26+
/// ```
27+
fn bad_doctests() {}
1228

1329
/// # Examples
1430
///
@@ -34,9 +50,25 @@ fn bad_doctest() {}
3450
/// assert_eq(1u8, test::black_box(1));
3551
/// }
3652
/// ```
53+
///
54+
/// We should not lint ignored examples:
55+
///
56+
/// ```rust,ignore
57+
/// fn main() {
58+
/// unimplemented!();
59+
/// }
60+
/// ```
61+
///
62+
/// Or even non-rust examples:
63+
///
64+
/// ```text
65+
/// fn main() {
66+
/// is what starts the program
67+
/// }
68+
/// ```
3769
fn no_false_positives() {}
3870

3971
fn main() {
40-
bad_doctest();
72+
bad_doctests();
4173
no_false_positives();
4274
}

tests/ui/needless_doc_main.stderr

+13-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,17 @@ LL | /// fn main() {
66
|
77
= note: `-D clippy::needless-doctest-main` implied by `-D warnings`
88

9-
error: aborting due to previous error
9+
error: needless `fn main` in doctest
10+
--> $DIR/needless_doc_main.rs:15:4
11+
|
12+
LL | /// fn main() {
13+
| ^^^^^^^^^^^^
14+
15+
error: needless `fn main` in doctest
16+
--> $DIR/needless_doc_main.rs:23:4
17+
|
18+
LL | /// fn main() {
19+
| ^^^^^^^^^^^^
20+
21+
error: aborting due to 3 previous errors
1022

0 commit comments

Comments
 (0)