Skip to content

Commit 1839b79

Browse files
committed
Auto merge of #11956 - intgr:doc_markdown-include-function-parenthesis, r=Alexendoo
[`doc_markdown`] Recognize words followed by empty parentheses `()` for quoting *Please write a short comment explaining your change (or "none" for internal only changes)* changelog: [`doc_markdown`] Recognize words followed by empty parentheses for quoting, e.g. `func()`. --- Developers often write function/method names with trailing `()`, but `doc_markdown` lint did not consider that. Old clippy suggestion was not very good: ```patch -/// There is no try (do() or do_not()). +/// There is no try (do() or `do_not`()). ``` New behavior recognizes function names such as `do()` even they contain no `_`/`::`; and backticks are suggested outside of the `()`: ```patch -/// There is no try (do() or do_not()). +/// There is no try (`do()` or `do_not()`). ```
2 parents c19508b + 1f2ca81 commit 1839b79

File tree

5 files changed

+43
-5
lines changed

5 files changed

+43
-5
lines changed

clippy_lints/src/doc/markdown.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,21 @@ use url::Url;
99
use crate::doc::DOC_MARKDOWN;
1010

1111
pub fn check(cx: &LateContext<'_>, valid_idents: &FxHashSet<String>, text: &str, span: Span) {
12-
for word in text.split(|c: char| c.is_whitespace() || c == '\'') {
12+
for orig_word in text.split(|c: char| c.is_whitespace() || c == '\'') {
1313
// Trim punctuation as in `some comment (see foo::bar).`
1414
// ^^
1515
// Or even as in `_foo bar_` which is emphasized. Also preserve `::` as a prefix/suffix.
16-
let mut word = word.trim_matches(|c: char| !c.is_alphanumeric() && c != ':');
16+
let trim_pattern = |c: char| !c.is_alphanumeric() && c != ':';
17+
let mut word = orig_word.trim_end_matches(trim_pattern);
18+
19+
// If word is immediately followed by `()`, claw it back.
20+
if let Some(tmp_word) = orig_word.get(..word.len() + 2)
21+
&& tmp_word.ends_with("()")
22+
{
23+
word = tmp_word;
24+
}
25+
26+
word = word.trim_start_matches(trim_pattern);
1727

1828
// Remove leading or trailing single `:` which may be part of a sentence.
1929
if word.starts_with(':') && !word.starts_with("::") {
@@ -84,7 +94,7 @@ fn check_word(cx: &LateContext<'_>, word: &str, span: Span) {
8494
return;
8595
}
8696

87-
if has_underscore(word) || word.contains("::") || is_camel_case(word) {
97+
if has_underscore(word) || word.contains("::") || is_camel_case(word) || word.ends_with("()") {
8898
let mut applicability = Applicability::MachineApplicable;
8999

90100
span_lint_and_then(

clippy_lints/src/missing_asserts_for_indexing.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ enum LengthComparison {
9494

9595
/// Extracts parts out of a length comparison expression.
9696
///
97-
/// E.g. for `v.len() > 5` this returns `Some((LengthComparison::IntLessThanLength, 5, `v.len()`))`
97+
/// E.g. for `v.len() > 5` this returns `Some((LengthComparison::IntLessThanLength, 5, v.len()))`
9898
fn len_comparison<'hir>(
9999
bin_op: BinOp,
100100
left: &'hir Expr<'hir>,

tests/ui/doc/doc-fixable.fixed

+3
Original file line numberDiff line numberDiff line change
@@ -227,3 +227,6 @@ where [(); N.checked_next_power_of_two().unwrap()]: {
227227

228228
/// this checks if the lowerCamelCase issue is fixed
229229
fn issue_11568() {}
230+
231+
/// There is no try (`do()` or `do_not()`).
232+
fn parenthesized_word() {}

tests/ui/doc/doc-fixable.rs

+3
Original file line numberDiff line numberDiff line change
@@ -227,3 +227,6 @@ where [(); N.checked_next_power_of_two().unwrap()]: {
227227

228228
/// this checks if the lowerCamelCase issue is fixed
229229
fn issue_11568() {}
230+
231+
/// There is no try (do() or do_not()).
232+
fn parenthesized_word() {}

tests/ui/doc/doc-fixable.stderr

+23-1
Original file line numberDiff line numberDiff line change
@@ -319,5 +319,27 @@ help: try
319319
LL | /// Foo \[bar\] \[baz\] \[qux\]. `DocMarkdownLint`
320320
| ~~~~~~~~~~~~~~~~~
321321

322-
error: aborting due to 29 previous errors
322+
error: item in documentation is missing backticks
323+
--> $DIR/doc-fixable.rs:231:22
324+
|
325+
LL | /// There is no try (do() or do_not()).
326+
| ^^^^
327+
|
328+
help: try
329+
|
330+
LL | /// There is no try (`do()` or do_not()).
331+
| ~~~~~~
332+
333+
error: item in documentation is missing backticks
334+
--> $DIR/doc-fixable.rs:231:30
335+
|
336+
LL | /// There is no try (do() or do_not()).
337+
| ^^^^^^^^
338+
|
339+
help: try
340+
|
341+
LL | /// There is no try (do() or `do_not()`).
342+
| ~~~~~~~~~~
343+
344+
error: aborting due to 31 previous errors
323345

0 commit comments

Comments
 (0)