Skip to content
Merged
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
2 changes: 1 addition & 1 deletion clippy_lints/src/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ fn check_doc<'a, Events: Iterator<Item = (pulldown_cmark::Event<'a>, Range<usize
}

fn check_code(cx: &LateContext<'_, '_>, text: &str, span: Span) {
if text.contains("fn main() {") {
if text.contains("fn main() {") && !(text.contains("static") || text.contains("fn main() {}")) {
span_lint(cx, NEEDLESS_DOCTEST_MAIN, span, "needless `fn main` in doctest");
}
}
Expand Down
33 changes: 33 additions & 0 deletions tests/ui/needless_doc_main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/// This is a test for needless `fn main()` in doctests.
///
/// # Examples
///
/// This should lint
/// ```
/// fn main() {
/// unimplemented!();
/// }
/// ```
fn bad_doctest() {}

/// # Examples
///
/// This shouldn't lint, because the `main` is empty:
/// ```
/// fn main(){}
/// ```
///
/// This shouldn't lint either, because there's a `static`:
/// ```
/// static ANSWER: i32 = 42;
///
/// fn main() {
/// assert_eq!(42, ANSWER);
/// }
/// ```
fn no_false_positives() {}

fn main() {
bad_doctest();
no_false_positives();
}
10 changes: 10 additions & 0 deletions tests/ui/needless_doc_main.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
error: needless `fn main` in doctest
--> $DIR/needless_doc_main.rs:7:4
|
LL | /// fn main() {
| ^^^^^^^^^^^^
|
= note: `-D clippy::needless-doctest-main` implied by `-D warnings`

error: aborting due to previous error