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
This commit is contained in:
bors 2019-12-01 00:13:17 +00:00
commit e47ae20b5b
3 changed files with 44 additions and 1 deletions

View file

@ -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");
}
}

View file

@ -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();
}

View file

@ -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