Skip to content

Commit

Permalink
Add suggestion to missing backticks error
Browse files Browse the repository at this point in the history
  • Loading branch information
Serial-ATA committed Oct 31, 2021
1 parent 00821ca commit 34b2477
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 70 deletions.
25 changes: 15 additions & 10 deletions clippy_lints/src/doc.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use clippy_utils::attrs::is_doc_hidden;
use clippy_utils::diagnostics::{span_lint, span_lint_and_help, span_lint_and_note};
use clippy_utils::source::first_line_of_span;
use clippy_utils::diagnostics::{span_lint, span_lint_and_help, span_lint_and_note, span_lint_and_sugg};
use clippy_utils::source::{first_line_of_span, snippet_with_applicability};
use clippy_utils::ty::{implements_trait, is_type_diagnostic_item};
use clippy_utils::{is_entrypoint_fn, is_expn_of, match_panic_def_id, method_chain_args, return_ty};
use if_chain::if_chain;
Expand All @@ -10,7 +10,7 @@ use rustc_ast::token::CommentKind;
use rustc_data_structures::fx::FxHashSet;
use rustc_data_structures::sync::Lrc;
use rustc_errors::emitter::EmitterWriter;
use rustc_errors::Handler;
use rustc_errors::{Applicability, Handler};
use rustc_hir as hir;
use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
use rustc_hir::{AnonConst, Expr, ExprKind, QPath};
Expand Down Expand Up @@ -48,7 +48,7 @@ declare_clippy_lint! {
/// content are not linted.
///
/// In addition, when writing documentation comments, including `[]` brackets
/// inside a link text would trip the parser. Therfore, documenting link with
/// inside a link text would trip the parser. Therefore, documenting link with
/// `[`SmallVec<[T; INLINE_CAPACITY]>`]` and then [`SmallVec<[T; INLINE_CAPACITY]>`]: SmallVec
/// would fail.
///
Expand Down Expand Up @@ -747,17 +747,22 @@ fn check_word(cx: &LateContext<'_>, word: &str, span: Span) {
}
}

// We assume that mixed-case words are not meant to be put inside bacticks. (Issue #2343)
// We assume that mixed-case words are not meant to be put inside backticks. (Issue #2343)
if has_underscore(word) && has_hyphen(word) {
return;
}

if has_underscore(word) || word.contains("::") || is_camel_case(word) {
span_lint(
let mut applicability = Applicability::MachineApplicable;

span_lint_and_sugg(
cx,
DOC_MARKDOWN,
span,
&format!("you should put `{}` between ticks in the documentation", word),
"item in documentation is missing backticks",
"try",
format!("`{}`", snippet_with_applicability(cx, span, "..", &mut applicability)),
applicability,
);
}
}
Expand Down Expand Up @@ -796,9 +801,9 @@ impl<'a, 'tcx> Visitor<'tcx> for FindPanicUnwrap<'a, 'tcx> {

// check for `unwrap`
if let Some(arglists) = method_chain_args(expr, &["unwrap"]) {
let reciever_ty = self.typeck_results.expr_ty(&arglists[0][0]).peel_refs();
if is_type_diagnostic_item(self.cx, reciever_ty, sym::Option)
|| is_type_diagnostic_item(self.cx, reciever_ty, sym::Result)
let receiver_ty = self.typeck_results.expr_ty(&arglists[0][0]).peel_refs();
if is_type_diagnostic_item(self.cx, receiver_ty, sym::Option)
|| is_type_diagnostic_item(self.cx, receiver_ty, sym::Result)
{
self.panic_span = Some(expr.span);
}
Expand Down
108 changes: 54 additions & 54 deletions tests/ui/doc/doc.stderr
Original file line number Diff line number Diff line change
@@ -1,160 +1,160 @@
error: you should put `foo_bar` between ticks in the documentation
error: item in documentation is missing backticks
--> $DIR/doc.rs:8:9
|
LL | /// The foo_bar function does _nothing_. See also foo::bar. (note the dot there)
| ^^^^^^^
| ^^^^^^^ help: try: ``foo_bar``
|
= note: `-D clippy::doc-markdown` implied by `-D warnings`

error: you should put `foo::bar` between ticks in the documentation
error: item in documentation is missing backticks
--> $DIR/doc.rs:8:51
|
LL | /// The foo_bar function does _nothing_. See also foo::bar. (note the dot there)
| ^^^^^^^^
| ^^^^^^^^ help: try: ``foo::bar``

error: you should put `Foo::some_fun` between ticks in the documentation
error: item in documentation is missing backticks
--> $DIR/doc.rs:9:83
|
LL | /// Markdown is _weird_. I mean _really weird_. This /_ is ok. So is `_`. But not Foo::some_fun
| ^^^^^^^^^^^^^
| ^^^^^^^^^^^^^ help: try: ``Foo::some_fun``

error: you should put `a::global:path` between ticks in the documentation
error: item in documentation is missing backticks
--> $DIR/doc.rs:11:15
|
LL | /// Here be ::a::global:path.
| ^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^ help: try: ``a::global:path``

error: you should put `NotInCodeBlock` between ticks in the documentation
error: item in documentation is missing backticks
--> $DIR/doc.rs:12:22
|
LL | /// That's not code ~NotInCodeBlock~.
| ^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^ help: try: ``NotInCodeBlock``

error: you should put `be_sure_we_got_to_the_end_of_it` between ticks in the documentation
error: item in documentation is missing backticks
--> $DIR/doc.rs:13:5
|
LL | /// be_sure_we_got_to_the_end_of_it
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: ``be_sure_we_got_to_the_end_of_it``

error: you should put `be_sure_we_got_to_the_end_of_it` between ticks in the documentation
error: item in documentation is missing backticks
--> $DIR/doc.rs:27:5
|
LL | /// be_sure_we_got_to_the_end_of_it
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: ``be_sure_we_got_to_the_end_of_it``

error: you should put `be_sure_we_got_to_the_end_of_it` between ticks in the documentation
error: item in documentation is missing backticks
--> $DIR/doc.rs:34:5
|
LL | /// be_sure_we_got_to_the_end_of_it
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: ``be_sure_we_got_to_the_end_of_it``

error: you should put `be_sure_we_got_to_the_end_of_it` between ticks in the documentation
error: item in documentation is missing backticks
--> $DIR/doc.rs:48:5
|
LL | /// be_sure_we_got_to_the_end_of_it
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: ``be_sure_we_got_to_the_end_of_it``

error: you should put `be_sure_we_got_to_the_end_of_it` between ticks in the documentation
error: item in documentation is missing backticks
--> $DIR/doc.rs:71:5
|
LL | /// be_sure_we_got_to_the_end_of_it
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: ``be_sure_we_got_to_the_end_of_it``

error: you should put `link_with_underscores` between ticks in the documentation
error: item in documentation is missing backticks
--> $DIR/doc.rs:75:22
|
LL | /// This test has [a link_with_underscores][chunked-example] inside it. See #823.
| ^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^ help: try: ``link_with_underscores``

error: you should put `inline_link2` between ticks in the documentation
error: item in documentation is missing backticks
--> $DIR/doc.rs:78:21
|
LL | /// It can also be [inline_link2].
| ^^^^^^^^^^^^
| ^^^^^^^^^^^^ help: try: ``inline_link2``

error: you should put `be_sure_we_got_to_the_end_of_it` between ticks in the documentation
error: item in documentation is missing backticks
--> $DIR/doc.rs:88:5
|
LL | /// be_sure_we_got_to_the_end_of_it
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: ``be_sure_we_got_to_the_end_of_it``

error: you should put `CamelCaseThing` between ticks in the documentation
error: item in documentation is missing backticks
--> $DIR/doc.rs:96:8
|
LL | /// ## CamelCaseThing
| ^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^ help: try: ``CamelCaseThing``

error: you should put `CamelCaseThing` between ticks in the documentation
error: item in documentation is missing backticks
--> $DIR/doc.rs:99:7
|
LL | /// # CamelCaseThing
| ^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^ help: try: ``CamelCaseThing``

error: you should put `CamelCaseThing` between ticks in the documentation
error: item in documentation is missing backticks
--> $DIR/doc.rs:101:22
|
LL | /// Not a title #897 CamelCaseThing
| ^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^ help: try: ``CamelCaseThing``

error: you should put `be_sure_we_got_to_the_end_of_it` between ticks in the documentation
error: item in documentation is missing backticks
--> $DIR/doc.rs:102:5
|
LL | /// be_sure_we_got_to_the_end_of_it
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: ``be_sure_we_got_to_the_end_of_it``

error: you should put `be_sure_we_got_to_the_end_of_it` between ticks in the documentation
error: item in documentation is missing backticks
--> $DIR/doc.rs:109:5
|
LL | /// be_sure_we_got_to_the_end_of_it
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: ``be_sure_we_got_to_the_end_of_it``

error: you should put `be_sure_we_got_to_the_end_of_it` between ticks in the documentation
error: item in documentation is missing backticks
--> $DIR/doc.rs:122:5
|
LL | /// be_sure_we_got_to_the_end_of_it
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: ``be_sure_we_got_to_the_end_of_it``

error: you should put `FooBar` between ticks in the documentation
error: item in documentation is missing backticks
--> $DIR/doc.rs:133:43
|
LL | /** E.g., serialization of an empty list: FooBar
| ^^^^^^
| ^^^^^^ help: try: ``FooBar``

error: you should put `BarQuz` between ticks in the documentation
error: item in documentation is missing backticks
--> $DIR/doc.rs:138:5
|
LL | And BarQuz too.
| ^^^^^^
| ^^^^^^ help: try: ``BarQuz``

error: you should put `be_sure_we_got_to_the_end_of_it` between ticks in the documentation
error: item in documentation is missing backticks
--> $DIR/doc.rs:139:1
|
LL | be_sure_we_got_to_the_end_of_it
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: ``be_sure_we_got_to_the_end_of_it``

error: you should put `FooBar` between ticks in the documentation
error: item in documentation is missing backticks
--> $DIR/doc.rs:144:43
|
LL | /** E.g., serialization of an empty list: FooBar
| ^^^^^^
| ^^^^^^ help: try: ``FooBar``

error: you should put `BarQuz` between ticks in the documentation
error: item in documentation is missing backticks
--> $DIR/doc.rs:149:5
|
LL | And BarQuz too.
| ^^^^^^
| ^^^^^^ help: try: ``BarQuz``

error: you should put `be_sure_we_got_to_the_end_of_it` between ticks in the documentation
error: item in documentation is missing backticks
--> $DIR/doc.rs:150:1
|
LL | be_sure_we_got_to_the_end_of_it
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: ``be_sure_we_got_to_the_end_of_it``

error: you should put `be_sure_we_got_to_the_end_of_it` between ticks in the documentation
error: item in documentation is missing backticks
--> $DIR/doc.rs:161:5
|
LL | /// be_sure_we_got_to_the_end_of_it
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: ``be_sure_we_got_to_the_end_of_it``

error: you should put bare URLs between `<`/`>` or make a proper Markdown link
--> $DIR/doc.rs:188:13
Expand All @@ -180,11 +180,11 @@ error: you should put bare URLs between `<`/`>` or make a proper Markdown link
LL | /// Not ok: http://www.unicode.org/reports/tr9/#Reordering_Resolved_Levels
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: you should put `mycrate::Collection` between ticks in the documentation
error: item in documentation is missing backticks
--> $DIR/doc.rs:194:22
|
LL | /// An iterator over mycrate::Collection's values.
| ^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^ help: try: ``mycrate::Collection``

error: aborting due to 31 previous errors

9 changes: 9 additions & 0 deletions tests/ui/doc/missing_ticks.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// run-rustfix
#![allow(dead_code)]
#![warn(clippy::doc_markdown)]

/// `SomeItem`
fn main() {}

/// `SomeItem`
fn main1() {}
9 changes: 9 additions & 0 deletions tests/ui/doc/missing_ticks.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// run-rustfix
#![allow(dead_code)]
#![warn(clippy::doc_markdown)]

/// SomeItem
fn main() {}

/// `SomeItem`
fn main1() {}
10 changes: 10 additions & 0 deletions tests/ui/doc/missing_ticks.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
error: item in documentation is missing backticks
--> $DIR/missing_ticks.rs:5:5
|
LL | /// SomeItem
| ^^^^^^^^ help: try: ``SomeItem``
|
= note: `-D clippy::doc-markdown` implied by `-D warnings`

error: aborting due to previous error

12 changes: 6 additions & 6 deletions tests/ui/doc/unbalanced_ticks.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ LL | /// This paragraph has `unbalanced_tick marks and should stop_linting.
|
= help: a backtick may be missing a pair

error: you should put `should_be` between ticks in the documentation
error: item in documentation is missing backticks
--> $DIR/unbalanced_ticks.rs:15:32
|
LL | /// This paragraph is fine and should_be linted normally.
| ^^^^^^^^^
| ^^^^^^^^^ help: try: ``should_be``

error: backticks are unbalanced
--> $DIR/unbalanced_ticks.rs:17:1
Expand All @@ -32,11 +32,11 @@ LL | /// Double unbalanced backtick from ``here to here` should lint.
|
= help: a backtick may be missing a pair

error: you should put `not_fine` between ticks in the documentation
error: item in documentation is missing backticks
--> $DIR/unbalanced_ticks.rs:30:8
|
LL | /// ## not_fine
| ^^^^^^^^
| ^^^^^^^^ help: try: ``not_fine``

error: backticks are unbalanced
--> $DIR/unbalanced_ticks.rs:32:1
Expand All @@ -54,11 +54,11 @@ LL | /// - This `item has unbalanced tick marks
|
= help: a backtick may be missing a pair

error: you should put `backticks_here` between ticks in the documentation
error: item in documentation is missing backticks
--> $DIR/unbalanced_ticks.rs:35:23
|
LL | /// - This item needs backticks_here
| ^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^ help: try: ``backticks_here``

error: aborting due to 8 previous errors

0 comments on commit 34b2477

Please sign in to comment.