mirror of
https://github.com/rust-lang/rust.git
synced 2025-01-31 17:12:53 +00:00
Auto merge of #5298 - rust-lang:needless_doc_main_code, r=flip1995,Manishearth
needless_doc_main: only check rust code This fixes #5280 by checking the language attribute on code blocks. --- changelog: none
This commit is contained in:
commit
23bd427f92
@ -367,6 +367,8 @@ fn check_attrs<'a>(cx: &LateContext<'_, '_>, valid_idents: &FxHashSet<String>, a
|
|||||||
check_doc(cx, valid_idents, events, &spans)
|
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<Item = (pulldown_cmark::Event<'a>, Range<usize>)>>(
|
fn check_doc<'a, Events: Iterator<Item = (pulldown_cmark::Event<'a>, Range<usize>)>>(
|
||||||
cx: &LateContext<'_, '_>,
|
cx: &LateContext<'_, '_>,
|
||||||
valid_idents: &FxHashSet<String>,
|
valid_idents: &FxHashSet<String>,
|
||||||
@ -374,6 +376,7 @@ fn check_doc<'a, Events: Iterator<Item = (pulldown_cmark::Event<'a>, Range<usize
|
|||||||
spans: &[(usize, Span)],
|
spans: &[(usize, Span)],
|
||||||
) -> DocHeaders {
|
) -> DocHeaders {
|
||||||
// true if a safety header was found
|
// true if a safety header was found
|
||||||
|
use pulldown_cmark::CodeBlockKind;
|
||||||
use pulldown_cmark::Event::{
|
use pulldown_cmark::Event::{
|
||||||
Code, End, FootnoteReference, HardBreak, Html, Rule, SoftBreak, Start, TaskListMarker, Text,
|
Code, End, FootnoteReference, HardBreak, Html, Rule, SoftBreak, Start, TaskListMarker, Text,
|
||||||
};
|
};
|
||||||
@ -386,11 +389,20 @@ fn check_doc<'a, Events: Iterator<Item = (pulldown_cmark::Event<'a>, Range<usize
|
|||||||
let mut in_code = false;
|
let mut in_code = false;
|
||||||
let mut in_link = None;
|
let mut in_link = None;
|
||||||
let mut in_heading = false;
|
let mut in_heading = false;
|
||||||
|
let mut is_rust = false;
|
||||||
for (event, range) in events {
|
for (event, range) in events {
|
||||||
match event {
|
match event {
|
||||||
Start(CodeBlock(_)) => in_code = true,
|
Start(CodeBlock(ref kind)) => {
|
||||||
End(CodeBlock(_)) => in_code = false,
|
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),
|
Start(Link(_, url, _)) => in_link = Some(url),
|
||||||
End(Link(..)) => in_link = None,
|
End(Link(..)) => in_link = None,
|
||||||
Start(Heading(_)) => in_heading = true,
|
Start(Heading(_)) => in_heading = true,
|
||||||
@ -413,7 +425,9 @@ fn check_doc<'a, Events: Iterator<Item = (pulldown_cmark::Event<'a>, Range<usize
|
|||||||
};
|
};
|
||||||
let (begin, span) = spans[index];
|
let (begin, span) = spans[index];
|
||||||
if in_code {
|
if in_code {
|
||||||
|
if is_rust {
|
||||||
check_code(cx, &text, span);
|
check_code(cx, &text, span);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// Adjust for the beginning of the current `Event`
|
// Adjust for the beginning of the current `Event`
|
||||||
let span = span.with_lo(span.lo() + BytePos::from_usize(range.start - begin));
|
let span = span.with_lo(span.lo() + BytePos::from_usize(range.start - begin));
|
||||||
|
@ -8,7 +8,23 @@
|
|||||||
/// unimplemented!();
|
/// unimplemented!();
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
fn bad_doctest() {}
|
///
|
||||||
|
/// This should, too.
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// fn main() {
|
||||||
|
/// unimplemented!();
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// This one too.
|
||||||
|
///
|
||||||
|
/// ```no_run
|
||||||
|
/// fn main() {
|
||||||
|
/// unimplemented!();
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
|
fn bad_doctests() {}
|
||||||
|
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
@ -34,9 +50,25 @@ fn bad_doctest() {}
|
|||||||
/// assert_eq(1u8, test::black_box(1));
|
/// assert_eq(1u8, test::black_box(1));
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
|
///
|
||||||
|
/// We should not lint ignored examples:
|
||||||
|
///
|
||||||
|
/// ```rust,ignore
|
||||||
|
/// fn main() {
|
||||||
|
/// unimplemented!();
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// Or even non-rust examples:
|
||||||
|
///
|
||||||
|
/// ```text
|
||||||
|
/// fn main() {
|
||||||
|
/// is what starts the program
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
fn no_false_positives() {}
|
fn no_false_positives() {}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
bad_doctest();
|
bad_doctests();
|
||||||
no_false_positives();
|
no_false_positives();
|
||||||
}
|
}
|
||||||
|
@ -6,5 +6,17 @@ LL | /// fn main() {
|
|||||||
|
|
|
|
||||||
= note: `-D clippy::needless-doctest-main` implied by `-D warnings`
|
= note: `-D clippy::needless-doctest-main` implied by `-D warnings`
|
||||||
|
|
||||||
error: aborting due to previous error
|
error: needless `fn main` in doctest
|
||||||
|
--> $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
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user