Skip to content

Commit 21efd39

Browse files
committed
Auto merge of #12423 - GuillaumeGomez:code-wrapped-element, r=Alexendoo
Don't emit "missing backticks" lint if the element is wrapped in `<code>` HTML tags Fixes #9473. changelog: Don't emit "missing backticks" lint if the element is wrapped in `<code>` HTML tags
2 parents eb4a441 + bd9b9ff commit 21efd39

File tree

5 files changed

+50
-9
lines changed

5 files changed

+50
-9
lines changed

clippy_lints/src/doc/markdown.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use url::Url;
88

99
use crate::doc::DOC_MARKDOWN;
1010

11-
pub fn check(cx: &LateContext<'_>, valid_idents: &FxHashSet<String>, text: &str, span: Span) {
11+
pub fn check(cx: &LateContext<'_>, valid_idents: &FxHashSet<String>, text: &str, span: Span, code_level: isize) {
1212
for orig_word in text.split(|c: char| c.is_whitespace() || c == '\'') {
1313
// Trim punctuation as in `some comment (see foo::bar).`
1414
// ^^
@@ -46,11 +46,11 @@ pub fn check(cx: &LateContext<'_>, valid_idents: &FxHashSet<String>, text: &str,
4646
span.parent(),
4747
);
4848

49-
check_word(cx, word, span);
49+
check_word(cx, word, span, code_level);
5050
}
5151
}
5252

53-
fn check_word(cx: &LateContext<'_>, word: &str, span: Span) {
53+
fn check_word(cx: &LateContext<'_>, word: &str, span: Span, code_level: isize) {
5454
/// Checks if a string is upper-camel-case, i.e., starts with an uppercase and
5555
/// contains at least two uppercase letters (`Clippy` is ok) and one lower-case
5656
/// letter (`NASA` is ok).
@@ -97,7 +97,7 @@ fn check_word(cx: &LateContext<'_>, word: &str, span: Span) {
9797
}
9898

9999
// We assume that mixed-case words are not meant to be put inside backticks. (Issue #2343)
100-
if has_underscore(word) && has_hyphen(word) {
100+
if code_level > 0 || (has_underscore(word) && has_hyphen(word)) {
101101
return;
102102
}
103103

clippy_lints/src/doc/mod.rs

+13-5
Original file line numberDiff line numberDiff line change
@@ -599,10 +599,19 @@ fn check_doc<'a, Events: Iterator<Item = (pulldown_cmark::Event<'a>, Range<usize
599599
let mut ignore = false;
600600
let mut edition = None;
601601
let mut ticks_unbalanced = false;
602-
let mut text_to_check: Vec<(CowStr<'_>, Range<usize>)> = Vec::new();
602+
let mut text_to_check: Vec<(CowStr<'_>, Range<usize>, isize)> = Vec::new();
603603
let mut paragraph_range = 0..0;
604+
let mut code_level = 0;
605+
604606
for (event, range) in events {
605607
match event {
608+
Html(tag) => {
609+
if tag.starts_with("<code") {
610+
code_level += 1;
611+
} else if tag.starts_with("</code") {
612+
code_level -= 1;
613+
}
614+
},
606615
Start(CodeBlock(ref kind)) => {
607616
in_code = true;
608617
if let CodeBlockKind::Fenced(lang) = kind {
@@ -652,16 +661,15 @@ fn check_doc<'a, Events: Iterator<Item = (pulldown_cmark::Event<'a>, Range<usize
652661
"a backtick may be missing a pair",
653662
);
654663
} else {
655-
for (text, range) in text_to_check {
664+
for (text, range, assoc_code_level) in text_to_check {
656665
if let Some(span) = fragments.span(cx, range) {
657-
markdown::check(cx, valid_idents, &text, span);
666+
markdown::check(cx, valid_idents, &text, span, assoc_code_level);
658667
}
659668
}
660669
}
661670
text_to_check = Vec::new();
662671
},
663672
Start(_tag) | End(_tag) => (), // We don't care about other tags
664-
Html(_html) => (), // HTML is weird, just ignore it
665673
SoftBreak | HardBreak | TaskListMarker(_) | Code(_) | Rule => (),
666674
FootnoteReference(text) | Text(text) => {
667675
paragraph_range.end = range.end;
@@ -694,7 +702,7 @@ fn check_doc<'a, Events: Iterator<Item = (pulldown_cmark::Event<'a>, Range<usize
694702
// Don't check the text associated with external URLs
695703
continue;
696704
}
697-
text_to_check.push((text, range));
705+
text_to_check.push((text, range, code_level));
698706
}
699707
},
700708
}

tests/ui/doc/issue_9473.fixed

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#![warn(clippy::doc_markdown)]
2+
3+
// Should not warn!
4+
/// Blah blah blah <code>[FooBar]&lt;[FooBar]&gt;</code>.
5+
pub struct Foo(u32);
6+
7+
// Should warn.
8+
/// Blah blah blah <code>[FooBar]&lt;[FooBar]&gt;</code>[`FooBar`].
9+
pub struct FooBar(u32);

tests/ui/doc/issue_9473.rs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#![warn(clippy::doc_markdown)]
2+
3+
// Should not warn!
4+
/// Blah blah blah <code>[FooBar]&lt;[FooBar]&gt;</code>.
5+
pub struct Foo(u32);
6+
7+
// Should warn.
8+
/// Blah blah blah <code>[FooBar]&lt;[FooBar]&gt;</code>[FooBar].
9+
pub struct FooBar(u32);

tests/ui/doc/issue_9473.stderr

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error: item in documentation is missing backticks
2+
--> tests/ui/doc/issue_9473.rs:8:58
3+
|
4+
LL | /// Blah blah blah <code>[FooBar]&lt;[FooBar]&gt;</code>[FooBar].
5+
| ^^^^^^
6+
|
7+
= note: `-D clippy::doc-markdown` implied by `-D warnings`
8+
= help: to override `-D warnings` add `#[allow(clippy::doc_markdown)]`
9+
help: try
10+
|
11+
LL | /// Blah blah blah <code>[FooBar]&lt;[FooBar]&gt;</code>[`FooBar`].
12+
| ~~~~~~~~
13+
14+
error: aborting due to 1 previous error
15+

0 commit comments

Comments
 (0)