diff --git a/clippy_lints/src/doc/markdown.rs b/clippy_lints/src/doc/markdown.rs index 3bedd425141..d2f14756591 100644 --- a/clippy_lints/src/doc/markdown.rs +++ b/clippy_lints/src/doc/markdown.rs @@ -8,7 +8,7 @@ use url::Url; use crate::doc::DOC_MARKDOWN; -pub fn check(cx: &LateContext<'_>, valid_idents: &FxHashSet, text: &str, span: Span) { +pub fn check(cx: &LateContext<'_>, valid_idents: &FxHashSet, text: &str, span: Span, code_level: isize) { for orig_word in text.split(|c: char| c.is_whitespace() || c == '\'') { // Trim punctuation as in `some comment (see foo::bar).` // ^^ @@ -46,11 +46,11 @@ pub fn check(cx: &LateContext<'_>, valid_idents: &FxHashSet, text: &str, span.parent(), ); - check_word(cx, word, span); + check_word(cx, word, span, code_level); } } -fn check_word(cx: &LateContext<'_>, word: &str, span: Span) { +fn check_word(cx: &LateContext<'_>, word: &str, span: Span, code_level: isize) { /// Checks if a string is upper-camel-case, i.e., starts with an uppercase and /// contains at least two uppercase letters (`Clippy` is ok) and one lower-case /// letter (`NASA` is ok). @@ -97,7 +97,7 @@ fn check_word(cx: &LateContext<'_>, word: &str, span: Span) { } // We assume that mixed-case words are not meant to be put inside backticks. (Issue #2343) - if has_underscore(word) && has_hyphen(word) { + if code_level > 0 || (has_underscore(word) && has_hyphen(word)) { return; } diff --git a/clippy_lints/src/doc/mod.rs b/clippy_lints/src/doc/mod.rs index 9af34c3a7bf..003d26b7b89 100644 --- a/clippy_lints/src/doc/mod.rs +++ b/clippy_lints/src/doc/mod.rs @@ -599,10 +599,19 @@ fn check_doc<'a, Events: Iterator, Range, Range)> = Vec::new(); + let mut text_to_check: Vec<(CowStr<'_>, Range, isize)> = Vec::new(); let mut paragraph_range = 0..0; + let mut code_level = 0; + for (event, range) in events { match event { + Html(tag) => { + if tag.starts_with(" { in_code = true; if let CodeBlockKind::Fenced(lang) = kind { @@ -652,16 +661,15 @@ fn check_doc<'a, Events: Iterator, Range (), // We don't care about other tags - Html(_html) => (), // HTML is weird, just ignore it SoftBreak | HardBreak | TaskListMarker(_) | Code(_) | Rule => (), FootnoteReference(text) | Text(text) => { paragraph_range.end = range.end; @@ -694,7 +702,7 @@ fn check_doc<'a, Events: Iterator, Range[FooBar]<[FooBar]>. +pub struct Foo(u32); + +// Should warn. +/// Blah blah blah [FooBar]<[FooBar]>[`FooBar`]. +pub struct FooBar(u32); diff --git a/tests/ui/doc/issue_9473.rs b/tests/ui/doc/issue_9473.rs new file mode 100644 index 00000000000..52527f7106d --- /dev/null +++ b/tests/ui/doc/issue_9473.rs @@ -0,0 +1,9 @@ +#![warn(clippy::doc_markdown)] + +// Should not warn! +/// Blah blah blah [FooBar]<[FooBar]>. +pub struct Foo(u32); + +// Should warn. +/// Blah blah blah [FooBar]<[FooBar]>[FooBar]. +pub struct FooBar(u32); diff --git a/tests/ui/doc/issue_9473.stderr b/tests/ui/doc/issue_9473.stderr new file mode 100644 index 00000000000..35aa2884cc1 --- /dev/null +++ b/tests/ui/doc/issue_9473.stderr @@ -0,0 +1,15 @@ +error: item in documentation is missing backticks + --> tests/ui/doc/issue_9473.rs:8:58 + | +LL | /// Blah blah blah [FooBar]<[FooBar]>[FooBar]. + | ^^^^^^ + | + = note: `-D clippy::doc-markdown` implied by `-D warnings` + = help: to override `-D warnings` add `#[allow(clippy::doc_markdown)]` +help: try + | +LL | /// Blah blah blah [FooBar]<[FooBar]>[`FooBar`]. + | ~~~~~~~~ + +error: aborting due to 1 previous error +