Review comments & Add testing

This commit is contained in:
blyxyas 2025-05-21 23:42:38 +02:00
parent 565cf5a89e
commit acff5d36cc
4 changed files with 138 additions and 32 deletions

View File

@ -7,7 +7,7 @@ use rustc_span::{BytePos, Pos, Span};
use url::Url;
use crate::doc::{DOC_MARKDOWN, Fragments};
use std::ops::{ControlFlow, Range};
use std::ops::Range;
pub fn check(
cx: &LateContext<'_>,
@ -70,7 +70,7 @@ pub fn check(
let fragment_offset = word.as_ptr() as usize - text.as_ptr() as usize;
// Adjust for the current word
if check_word(
check_word(
cx,
word,
fragments,
@ -78,11 +78,7 @@ pub fn check(
fragment_offset,
code_level,
blockquote_level,
)
.is_break()
{
return;
}
);
}
}
@ -94,10 +90,10 @@ fn check_word(
fragment_offset: usize,
code_level: isize,
blockquote_level: isize,
) -> ControlFlow<()> {
) {
/// 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).[
/// letter (`NASA` is ok).
/// Plurals are also excluded (`IDs` is ok).
fn is_camel_case(s: &str) -> bool {
if s.starts_with(|c: char| c.is_ascii_digit() | c.is_ascii_lowercase()) {
@ -135,9 +131,8 @@ fn check_word(
&& !url.cannot_be_a_base()
{
let Some(fragment_span) = fragments.span(cx, range.clone()) else {
return ControlFlow::Break(());
return;
};
let span = Span::new(
fragment_span.lo() + BytePos::from_usize(fragment_offset),
fragment_span.lo() + BytePos::from_usize(fragment_offset + word.len()),
@ -154,19 +149,19 @@ fn check_word(
format!("<{word}>"),
Applicability::MachineApplicable,
);
return ControlFlow::Continue(());
return;
}
// We assume that mixed-case words are not meant to be put inside backticks. (Issue #2343)
//
// We also assume that backticks are not necessary if inside a quote. (Issue #10262)
if code_level > 0 || blockquote_level > 0 || (has_underscore(word) && has_hyphen(word)) {
return ControlFlow::Break(());
return;
}
if has_underscore(word) || word.contains("::") || is_camel_case(word) || word.ends_with("()") {
let Some(fragment_span) = fragments.span(cx, range.clone()) else {
return ControlFlow::Break(());
return;
};
let span = Span::new(
@ -188,5 +183,4 @@ fn check_word(
},
);
}
ControlFlow::Continue(())
}

View File

@ -1,11 +1,37 @@
// This test checks that words starting with capital letters and ending with "ified" don't
// trigger the lint.
#![deny(clippy::doc_markdown)]
#![allow(clippy::doc_lazy_continuation)]
mod issue13097 {
// This test checks that words starting with capital letters and ending with "ified" don't
// trigger the lint.
pub enum OutputFormat {
/// `HumaNified`
//~^ ERROR: item in documentation is missing backticks
Plain,
// Should not warn!
/// JSONified console output
Json,
}
}
#[rustfmt::skip]
pub enum OutputFormat {
/// `HumaNified`
//~^ ERROR: item in documentation is missing backticks
/**
* `HumaNified`
//~^ ERROR: item in documentation is missing backticks
* Before \u{08888} `HumaNified` \{u08888} After
//~^ ERROR: item in documentation is missing backticks
* meow meow \[`meow_meow`\] meow meow?
//~^ ERROR: item in documentation is missing backticks
* \u{08888} `meow_meow` \[meow meow] meow?
//~^ ERROR: item in documentation is missing backticks
* Above
* \u{08888}
* \[hi\](<https://example.com>) `HumaNified` \[example](<https://example.com>)
//~^ ERROR: item in documentation is missing backticks
* \u{08888}
* Below
*/
Plain,
// Should not warn!
/// JSONified console output

View File

@ -1,11 +1,37 @@
// This test checks that words starting with capital letters and ending with "ified" don't
// trigger the lint.
#![deny(clippy::doc_markdown)]
#![allow(clippy::doc_lazy_continuation)]
mod issue13097 {
// This test checks that words starting with capital letters and ending with "ified" don't
// trigger the lint.
pub enum OutputFormat {
/// HumaNified
//~^ ERROR: item in documentation is missing backticks
Plain,
// Should not warn!
/// JSONified console output
Json,
}
}
#[rustfmt::skip]
pub enum OutputFormat {
/// HumaNified
//~^ ERROR: item in documentation is missing backticks
/**
* HumaNified
//~^ ERROR: item in documentation is missing backticks
* Before \u{08888} HumaNified \{u08888} After
//~^ ERROR: item in documentation is missing backticks
* meow meow \[meow_meow\] meow meow?
//~^ ERROR: item in documentation is missing backticks
* \u{08888} meow_meow \[meow meow] meow?
//~^ ERROR: item in documentation is missing backticks
* Above
* \u{08888}
* \[hi\](<https://example.com>) HumaNified \[example](<https://example.com>)
//~^ ERROR: item in documentation is missing backticks
* \u{08888}
* Below
*/
Plain,
// Should not warn!
/// JSONified console output

View File

@ -1,19 +1,79 @@
error: item in documentation is missing backticks
--> tests/ui/doc/doc_markdown-issue_13097.rs:7:9
--> tests/ui/doc/doc_markdown-issue_13097.rs:8:13
|
LL | /// HumaNified
| ^^^^^^^^^^
LL | /// HumaNified
| ^^^^^^^^^^
|
note: the lint level is defined here
--> tests/ui/doc/doc_markdown-issue_13097.rs:4:9
--> tests/ui/doc/doc_markdown-issue_13097.rs:1:9
|
LL | #![deny(clippy::doc_markdown)]
| ^^^^^^^^^^^^^^^^^^^^
help: try
|
LL - /// HumaNified
LL + /// `HumaNified`
LL - /// HumaNified
LL + /// `HumaNified`
|
error: aborting due to 1 previous error
error: item in documentation is missing backticks
--> tests/ui/doc/doc_markdown-issue_13097.rs:20:8
|
LL | * HumaNified
| ^^^^^^^^^^
|
help: try
|
LL - * HumaNified
LL + * `HumaNified`
|
error: item in documentation is missing backticks
--> tests/ui/doc/doc_markdown-issue_13097.rs:22:25
|
LL | * Before \u{08888} HumaNified \{u08888} After
| ^^^^^^^^^^
|
help: try
|
LL - * Before \u{08888} HumaNified \{u08888} After
LL + * Before \u{08888} `HumaNified` \{u08888} After
|
error: item in documentation is missing backticks
--> tests/ui/doc/doc_markdown-issue_13097.rs:24:20
|
LL | * meow meow \[meow_meow\] meow meow?
| ^^^^^^^^^
|
help: try
|
LL - * meow meow \[meow_meow\] meow meow?
LL + * meow meow \[`meow_meow`\] meow meow?
|
error: item in documentation is missing backticks
--> tests/ui/doc/doc_markdown-issue_13097.rs:26:18
|
LL | * \u{08888} meow_meow \[meow meow] meow?
| ^^^^^^^^^
|
help: try
|
LL - * \u{08888} meow_meow \[meow meow] meow?
LL + * \u{08888} `meow_meow` \[meow meow] meow?
|
error: item in documentation is missing backticks
--> tests/ui/doc/doc_markdown-issue_13097.rs:30:38
|
LL | * \[hi\](<https://example.com>) HumaNified \[example](<https://example.com>)
| ^^^^^^^^^^
|
help: try
|
LL - * \[hi\](<https://example.com>) HumaNified \[example](<https://example.com>)
LL + * \[hi\](<https://example.com>) `HumaNified` \[example](<https://example.com>)
|
error: aborting due to 6 previous errors