From 2fa3da486924b9c8807308fa00b7be6ae1adc167 Mon Sep 17 00:00:00 2001 From: Andre Bogus Date: Tue, 10 Mar 2020 20:19:37 +0100 Subject: [PATCH 1/2] needless_doc_main: only check rust code --- clippy_lints/src/doc.rs | 22 +++++++++++++++---- tests/ui/needless_doc_main.rs | 36 +++++++++++++++++++++++++++++-- tests/ui/needless_doc_main.stderr | 14 +++++++++++- 3 files changed, 65 insertions(+), 7 deletions(-) diff --git a/clippy_lints/src/doc.rs b/clippy_lints/src/doc.rs index 35578ae68..754d38f40 100644 --- a/clippy_lints/src/doc.rs +++ b/clippy_lints/src/doc.rs @@ -367,6 +367,8 @@ fn check_attrs<'a>(cx: &LateContext<'_, '_>, valid_idents: &FxHashSet, a check_doc(cx, valid_idents, events, &spans) } +const RUST_CODE: &[&str] = &["rust", "no_run", "should_panic", "compile_fail", "edition2018"]; + fn check_doc<'a, Events: Iterator, Range)>>( cx: &LateContext<'_, '_>, valid_idents: &FxHashSet, @@ -374,6 +376,7 @@ fn check_doc<'a, Events: Iterator, Range DocHeaders { // true if a safety header was found + use pulldown_cmark::CodeBlockKind; use pulldown_cmark::Event::{ Code, End, FootnoteReference, HardBreak, Html, Rule, SoftBreak, Start, TaskListMarker, Text, }; @@ -386,11 +389,20 @@ fn check_doc<'a, Events: Iterator, Range in_code = true, - End(CodeBlock(_)) => in_code = false, + Start(CodeBlock(ref kind)) => { + in_code = true; + if let CodeBlockKind::Fenced(lang) = kind { + is_rust = + lang.is_empty() || !lang.contains("ignore") && lang.split(',').any(|i| RUST_CODE.contains(&i)); + } + }, + End(CodeBlock(_)) => { + in_code = false; + is_rust = false; + }, Start(Link(_, url, _)) => in_link = Some(url), End(Link(..)) => in_link = None, Start(Heading(_)) => in_heading = true, @@ -413,7 +425,9 @@ fn check_doc<'a, Events: Iterator, Range $DIR/needless_doc_main.rs:15:4 + | +LL | /// fn main() { + | ^^^^^^^^^^^^ + +error: needless `fn main` in doctest + --> $DIR/needless_doc_main.rs:23:4 + | +LL | /// fn main() { + | ^^^^^^^^^^^^ + +error: aborting due to 3 previous errors From d5c8b783b8ceb0f739e307670a4f3a92ba8fdfcc Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Tue, 10 Mar 2020 14:30:07 -0700 Subject: [PATCH 2/2] Update tests/ui/needless_doc_main.rs Co-Authored-By: Philipp Krones --- tests/ui/needless_doc_main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ui/needless_doc_main.rs b/tests/ui/needless_doc_main.rs index 418d11eb3..682d7b3c4 100644 --- a/tests/ui/needless_doc_main.rs +++ b/tests/ui/needless_doc_main.rs @@ -51,7 +51,7 @@ fn bad_doctests() {} /// } /// ``` /// -/// We should not lint non-rust examples: +/// We should not lint ignored examples: /// /// ```rust,ignore /// fn main() {