Skip to content

Commit b667d02

Browse files
committed
Auto merge of rust-lang#12472 - GuillaumeGomez:fix-10262, r=blyxyas
Don't emit `doc_markdown` lint for missing backticks if it's inside a quote Fixes rust-lang#10262. changelog: Don't emit `doc_markdown` lint for missing backticks if it's inside a quote
2 parents 8a78128 + 03d7ae8 commit b667d02

File tree

5 files changed

+61
-6
lines changed

5 files changed

+61
-6
lines changed

clippy_lints/src/doc/markdown.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,14 @@ 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, code_level: isize) {
11+
pub fn check(
12+
cx: &LateContext<'_>,
13+
valid_idents: &FxHashSet<String>,
14+
text: &str,
15+
span: Span,
16+
code_level: isize,
17+
blockquote_level: isize,
18+
) {
1219
for orig_word in text.split(|c: char| c.is_whitespace() || c == '\'') {
1320
// Trim punctuation as in `some comment (see foo::bar).`
1421
// ^^
@@ -46,11 +53,11 @@ pub fn check(cx: &LateContext<'_>, valid_idents: &FxHashSet<String>, text: &str,
4653
span.parent(),
4754
);
4855

49-
check_word(cx, word, span, code_level);
56+
check_word(cx, word, span, code_level, blockquote_level);
5057
}
5158
}
5259

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

99106
// We assume that mixed-case words are not meant to be put inside backticks. (Issue #2343)
100-
if code_level > 0 || (has_underscore(word) && has_hyphen(word)) {
107+
//
108+
// We also assume that backticks are not necessary if inside a quote. (Issue #10262)
109+
if code_level > 0 || blockquote_level > 0 || (has_underscore(word) && has_hyphen(word)) {
101110
return;
102111
}
103112

clippy_lints/src/doc/mod.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use clippy_utils::{is_entrypoint_fn, method_chain_args};
77
use pulldown_cmark::Event::{
88
Code, End, FootnoteReference, HardBreak, Html, Rule, SoftBreak, Start, TaskListMarker, Text,
99
};
10-
use pulldown_cmark::Tag::{CodeBlock, Heading, Item, Link, Paragraph};
10+
use pulldown_cmark::Tag::{BlockQuote, CodeBlock, Heading, Item, Link, Paragraph};
1111
use pulldown_cmark::{BrokenLink, CodeBlockKind, CowStr, Options};
1212
use rustc_ast::ast::Attribute;
1313
use rustc_data_structures::fx::FxHashSet;
@@ -611,6 +611,7 @@ fn check_doc<'a, Events: Iterator<Item = (pulldown_cmark::Event<'a>, Range<usize
611611
let mut text_to_check: Vec<(CowStr<'_>, Range<usize>, isize)> = Vec::new();
612612
let mut paragraph_range = 0..0;
613613
let mut code_level = 0;
614+
let mut blockquote_level = 0;
614615

615616
for (event, range) in events {
616617
match event {
@@ -619,8 +620,14 @@ fn check_doc<'a, Events: Iterator<Item = (pulldown_cmark::Event<'a>, Range<usize
619620
code_level += 1;
620621
} else if tag.starts_with("</code") {
621622
code_level -= 1;
623+
} else if tag.starts_with("<blockquote") || tag.starts_with("<q") {
624+
blockquote_level += 1;
625+
} else if tag.starts_with("</blockquote") || tag.starts_with("</q") {
626+
blockquote_level -= 1;
622627
}
623628
},
629+
Start(BlockQuote) => blockquote_level += 1,
630+
End(BlockQuote) => blockquote_level -= 1,
624631
Start(CodeBlock(ref kind)) => {
625632
in_code = true;
626633
if let CodeBlockKind::Fenced(lang) = kind {
@@ -672,7 +679,7 @@ fn check_doc<'a, Events: Iterator<Item = (pulldown_cmark::Event<'a>, Range<usize
672679
} else {
673680
for (text, range, assoc_code_level) in text_to_check {
674681
if let Some(span) = fragments.span(cx, range) {
675-
markdown::check(cx, valid_idents, &text, span, assoc_code_level);
682+
markdown::check(cx, valid_idents, &text, span, assoc_code_level, blockquote_level);
676683
}
677684
}
678685
}

tests/ui/doc/issue_10262.fixed

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#![warn(clippy::doc_markdown)]
2+
3+
// Should only warn for the first line!
4+
/// `AviSynth` documentation:
5+
//~^ ERROR: item in documentation is missing backticks
6+
///
7+
/// > AvisynthPluginInit3 may be called more than once with different IScriptEnvironments.
8+
///
9+
/// <blockquote>bla AvisynthPluginInit3 bla</blockquote>
10+
///
11+
/// <q>bla AvisynthPluginInit3 bla</q>
12+
pub struct Foo;

tests/ui/doc/issue_10262.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#![warn(clippy::doc_markdown)]
2+
3+
// Should only warn for the first line!
4+
/// AviSynth documentation:
5+
//~^ ERROR: item in documentation is missing backticks
6+
///
7+
/// > AvisynthPluginInit3 may be called more than once with different IScriptEnvironments.
8+
///
9+
/// <blockquote>bla AvisynthPluginInit3 bla</blockquote>
10+
///
11+
/// <q>bla AvisynthPluginInit3 bla</q>
12+
pub struct Foo;

tests/ui/doc/issue_10262.stderr

Lines changed: 15 additions & 0 deletions
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_10262.rs:4:5
3+
|
4+
LL | /// AviSynth documentation:
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 | /// `AviSynth` documentation:
12+
| ~~~~~~~~~~
13+
14+
error: aborting due to 1 previous error
15+

0 commit comments

Comments
 (0)