Skip to content

Commit da3e0af

Browse files
author
Lukas Markeffsky
committed
Add rustdoc::unescaped_backtick lint
1 parent c2ff8ad commit da3e0af

File tree

7 files changed

+1634
-1
lines changed

7 files changed

+1634
-1
lines changed

src/doc/rustdoc/src/lints.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,3 +374,38 @@ warning: this URL is not a hyperlink
374374
375375
warning: 2 warnings emitted
376376
```
377+
378+
## `unescaped_backticks`
379+
380+
This lint **warns by default**. It detects backticks (\`) that are not escaped.
381+
This usually means broken inline code. For example:
382+
383+
```rust
384+
#![warn(rustdoc::unescaped_backticks)] // note: unnecessary - warns by default.
385+
386+
/// Addition is commutative, which means that `add(a, b) is the same as `add(b, a)`.
387+
pub fn add(a: i32, b: i32) -> i32 { a + b }
388+
```
389+
390+
Which will give:
391+
392+
```text
393+
warning: unescaped backtick
394+
--> src/lib.rs:1:83
395+
|
396+
1 | /// Addition is commutative, which means that `add(a, b) is the same as `add(b, a)`.
397+
| ^
398+
|
399+
= help: the opening or closing backtick of an inline code may be missing
400+
= note: `#[warn(rustdoc::unescaped_backticks)]` on by default
401+
help: a previous inline code might be longer than expected
402+
|
403+
1 | /// Addition is commutative, which means that `add(a, b)` is the same as `add(b, a)`.
404+
| +
405+
help: if you meant to use a literal backtick, escape it
406+
|
407+
1 | /// Addition is commutative, which means that `add(a, b) is the same as `add(b, a)\`.
408+
| +
409+
410+
warning: 1 warning emitted
411+
```

src/librustdoc/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@
88
#![feature(box_patterns)]
99
#![feature(control_flow_enum)]
1010
#![feature(drain_filter)]
11+
#![feature(iter_intersperse)]
12+
#![feature(is_some_and)]
1113
#![feature(is_terminal)]
1214
#![feature(let_chains)]
1315
#![feature(test)]
1416
#![feature(never_type)]
1517
#![feature(once_cell)]
18+
#![feature(round_char_boundary)]
1619
#![feature(type_ascription)]
17-
#![feature(iter_intersperse)]
1820
#![feature(type_alias_impl_trait)]
1921
#![recursion_limit = "256"]
2022
#![warn(rustc::internal)]

src/librustdoc/lint.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,17 @@ declare_rustdoc_lint! {
174174
"codeblock could not be parsed as valid Rust or is empty"
175175
}
176176

177+
declare_rustdoc_lint! {
178+
/// The `unescaped_backticks` lint detects unescaped backticks (\`), which usually
179+
/// mean broken inline code. This is a `rustdoc` only lint, see the documentation
180+
/// in the [rustdoc book].
181+
///
182+
/// [rustdoc book]: ../../../rustdoc/lints.html#unescaped_backticks
183+
UNESCAPED_BACKTICKS,
184+
Warn,
185+
"detects unescaped backticks in doc comments"
186+
}
187+
177188
pub(crate) static RUSTDOC_LINTS: Lazy<Vec<&'static Lint>> = Lazy::new(|| {
178189
vec![
179190
BROKEN_INTRA_DOC_LINKS,
@@ -185,6 +196,7 @@ pub(crate) static RUSTDOC_LINTS: Lazy<Vec<&'static Lint>> = Lazy::new(|| {
185196
INVALID_HTML_TAGS,
186197
BARE_URLS,
187198
MISSING_CRATE_LEVEL_DOCS,
199+
UNESCAPED_BACKTICKS,
188200
]
189201
});
190202

src/librustdoc/passes/lint.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
mod bare_urls;
55
mod check_code_block_syntax;
66
mod html_tags;
7+
mod unescaped_backticks;
78

89
use super::Pass;
910
use crate::clean::*;
@@ -27,6 +28,7 @@ impl<'a, 'tcx> DocVisitor for Linter<'a, 'tcx> {
2728
bare_urls::visit_item(self.cx, item);
2829
check_code_block_syntax::visit_item(self.cx, item);
2930
html_tags::visit_item(self.cx, item);
31+
unescaped_backticks::visit_item(self.cx, item);
3032

3133
self.visit_item_recur(item)
3234
}

0 commit comments

Comments
 (0)