Skip to content

Commit e47ae20

Browse files
committed
Auto merge of #4866 - rust-lang:needful-doctest-main, r=flip1995
Less needless_doctest_main false positives This checks if a) the `fn main() {}` function is empty or if the doctest contains a `static`. In both cases don't lint. While this fixes #4858 at the cost of some false negatives, but this seems a better solution than disabling the lint outright. In the long run, using `syn` should solve the issue in the right way. changelog: none
2 parents b561b6c + ef7587d commit e47ae20

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

clippy_lints/src/doc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ fn check_doc<'a, Events: Iterator<Item = (pulldown_cmark::Event<'a>, Range<usize
343343
}
344344

345345
fn check_code(cx: &LateContext<'_, '_>, text: &str, span: Span) {
346-
if text.contains("fn main() {") {
346+
if text.contains("fn main() {") && !(text.contains("static") || text.contains("fn main() {}")) {
347347
span_lint(cx, NEEDLESS_DOCTEST_MAIN, span, "needless `fn main` in doctest");
348348
}
349349
}

tests/ui/needless_doc_main.rs

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/// This is a test for needless `fn main()` in doctests.
2+
///
3+
/// # Examples
4+
///
5+
/// This should lint
6+
/// ```
7+
/// fn main() {
8+
/// unimplemented!();
9+
/// }
10+
/// ```
11+
fn bad_doctest() {}
12+
13+
/// # Examples
14+
///
15+
/// This shouldn't lint, because the `main` is empty:
16+
/// ```
17+
/// fn main(){}
18+
/// ```
19+
///
20+
/// This shouldn't lint either, because there's a `static`:
21+
/// ```
22+
/// static ANSWER: i32 = 42;
23+
///
24+
/// fn main() {
25+
/// assert_eq!(42, ANSWER);
26+
/// }
27+
/// ```
28+
fn no_false_positives() {}
29+
30+
fn main() {
31+
bad_doctest();
32+
no_false_positives();
33+
}

tests/ui/needless_doc_main.stderr

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: needless `fn main` in doctest
2+
--> $DIR/needless_doc_main.rs:7:4
3+
|
4+
LL | /// fn main() {
5+
| ^^^^^^^^^^^^
6+
|
7+
= note: `-D clippy::needless-doctest-main` implied by `-D warnings`
8+
9+
error: aborting due to previous error
10+

0 commit comments

Comments
 (0)