Skip to content

Commit a68ef55

Browse files
committed
Auto merge of #4947 - rust-lang:doc-main-extern-crate, r=flip1995
Avoid needless_doctest_main on 'extern crate' This fixes #4927. r? @flip1995 changelog: none
2 parents 1837cbc + 129d0cd commit a68ef55

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

clippy_lints/src/doc.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,8 +389,10 @@ fn check_doc<'a, Events: Iterator<Item = (pulldown_cmark::Event<'a>, Range<usize
389389
headers
390390
}
391391

392+
static LEAVE_MAIN_PATTERNS: &[&str] = &["static", "fn main() {}", "extern crate"];
393+
392394
fn check_code(cx: &LateContext<'_, '_>, text: &str, span: Span) {
393-
if text.contains("fn main() {") && !(text.contains("static") || text.contains("fn main() {}")) {
395+
if text.contains("fn main() {") && !LEAVE_MAIN_PATTERNS.iter().any(|p| text.contains(p)) {
394396
span_lint(cx, NEEDLESS_DOCTEST_MAIN, span, "needless `fn main` in doctest");
395397
}
396398
}

tests/ui/needless_doc_main.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ fn bad_doctest() {}
2525
/// assert_eq!(42, ANSWER);
2626
/// }
2727
/// ```
28+
///
29+
/// Neither should this lint because of `extern crate`:
30+
/// ```
31+
/// #![feature(test)]
32+
/// extern crate test;
33+
/// fn main() {
34+
/// assert_eq(1u8, test::black_box(1));
35+
/// }
36+
/// ```
2837
fn no_false_positives() {}
2938

3039
fn main() {

0 commit comments

Comments
 (0)