diff options
| author | Guillaume Gomez <guillaume1.gomez@gmail.com> | 2024-03-05 12:06:51 +0100 |
|---|---|---|
| committer | Guillaume Gomez <guillaume1.gomez@gmail.com> | 2024-03-05 12:06:51 +0100 |
| commit | ee375e48bef4f3b745f59164bedc7262415e5c1e (patch) | |
| tree | b694941ca7ba1a90e778613d4a6a7f06cf8e97c6 | |
| parent | 193456e0a592b3b7a17413a73e2c941e7a0a27e2 (diff) | |
| download | rust-ee375e48bef4f3b745f59164bedc7262415e5c1e.tar.gz rust-ee375e48bef4f3b745f59164bedc7262415e5c1e.zip | |
Don't emit "missing backticks" lint if the element is wrapped in `<code>` HTML tags
| -rw-r--r-- | clippy_lints/src/doc/markdown.rs | 8 | ||||
| -rw-r--r-- | clippy_lints/src/doc/mod.rs | 18 |
2 files changed, 17 insertions, 9 deletions
diff --git a/clippy_lints/src/doc/markdown.rs b/clippy_lints/src/doc/markdown.rs index a58219c2946..3c8836f45d2 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<String>, text: &str, span: Span) { +pub fn check(cx: &LateContext<'_>, valid_idents: &FxHashSet<String>, 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<String>, 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). @@ -90,7 +90,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<Item = (pulldown_cmark::Event<'a>, Range<usize let mut ignore = false; let mut edition = None; let mut ticks_unbalanced = false; - let mut text_to_check: Vec<(CowStr<'_>, Range<usize>)> = Vec::new(); + let mut text_to_check: Vec<(CowStr<'_>, Range<usize>, 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("<code") { + code_level += 1; + } else if tag.starts_with("</code") { + code_level -= 1; + } + }, Start(CodeBlock(ref kind)) => { in_code = true; if let CodeBlockKind::Fenced(lang) = kind { @@ -652,16 +661,15 @@ fn check_doc<'a, Events: Iterator<Item = (pulldown_cmark::Event<'a>, Range<usize "a backtick may be missing a pair", ); } else { - for (text, range) in text_to_check { + for (text, range, assoc_code_level) in text_to_check { if let Some(span) = fragments.span(cx, range) { - markdown::check(cx, valid_idents, &text, span); + markdown::check(cx, valid_idents, &text, span, assoc_code_level); } } } text_to_check = Vec::new(); }, Start(_tag) | End(_tag) => (), // 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<Item = (pulldown_cmark::Event<'a>, Range<usize // Don't check the text associated with external URLs continue; } - text_to_check.push((text, range)); + text_to_check.push((text, range, code_level)); } }, } |
