-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new lint
doc_overindented_list_item
Add a new lint `doc_overindented_list_item` to detect and fix list items in docs that are overindented. For example, ```rs /// - first line /// second line fn foo() {} ``` this would be fixed to: ```rs /// - first line /// second line fn foo() {} ``` This lint improves readabiliy and consistency in doc.
- Loading branch information
Showing
10 changed files
with
244 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use clippy_utils::diagnostics::span_lint_and_then; | ||
use itertools::Itertools; | ||
use rustc_errors::Applicability; | ||
use rustc_lint::LateContext; | ||
use rustc_span::Span; | ||
use std::ops::Range; | ||
|
||
use super::DOC_OVERINDENTED_LIST_ITEM; | ||
|
||
pub(super) fn check(cx: &LateContext<'_>, doc: &str, range: Range<usize>, span: Span, containers: &[super::Container]) { | ||
if doc[range.clone()].contains('\t') { | ||
// We don't do tab stops correctly. | ||
return; | ||
} | ||
|
||
let is_blockquote = containers.iter().any(|c| matches!(c, super::Container::Blockquote)); | ||
if is_blockquote { | ||
// If this doc is a blockquote, we don't go further. | ||
return; | ||
} | ||
|
||
let leading_spaces = doc[range].chars().filter(|c| *c == ' ').count(); | ||
let list_indentation = containers | ||
.iter() | ||
.map(|c| { | ||
if let super::Container::List(indent) = c { | ||
*indent | ||
} else { | ||
0 | ||
} | ||
}) | ||
.sum(); | ||
|
||
if leading_spaces > list_indentation { | ||
span_lint_and_then( | ||
cx, | ||
DOC_OVERINDENTED_LIST_ITEM, | ||
span, | ||
"doc list item overindented", | ||
|diag| { | ||
diag.span_suggestion_verbose( | ||
span, | ||
"indent this line", | ||
std::iter::repeat(" ").take(list_indentation).join(""), | ||
Applicability::MaybeIncorrect, | ||
); | ||
diag.help("if this is supposed to be its own paragraph, add a blank line"); | ||
}, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#![warn(clippy::doc_overindented_list_item)] | ||
|
||
#[rustfmt::skip] | ||
/// - first list item | ||
/// overindented line | ||
//~^ ERROR: doc list item overindented | ||
/// this is overindented line too | ||
//~^ ERROR: doc list item overindented | ||
/// - second list item | ||
fn foo() {} | ||
|
||
#[rustfmt::skip] | ||
/// - first list item | ||
/// overindented line | ||
//~^ ERROR: doc list item overindented | ||
/// this is overindented line too | ||
//~^ ERROR: doc list item overindented | ||
/// - second list item | ||
fn bar() {} | ||
|
||
#[rustfmt::skip] | ||
/// * first list item | ||
/// overindented line | ||
//~^ ERROR: doc list item overindented | ||
/// this is overindented line too | ||
//~^ ERROR: doc list item overindented | ||
/// * second list item | ||
fn baz() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#![warn(clippy::doc_overindented_list_item)] | ||
|
||
#[rustfmt::skip] | ||
/// - first list item | ||
/// overindented line | ||
//~^ ERROR: doc list item overindented | ||
/// this is overindented line too | ||
//~^ ERROR: doc list item overindented | ||
/// - second list item | ||
fn foo() {} | ||
|
||
#[rustfmt::skip] | ||
/// - first list item | ||
/// overindented line | ||
//~^ ERROR: doc list item overindented | ||
/// this is overindented line too | ||
//~^ ERROR: doc list item overindented | ||
/// - second list item | ||
fn bar() {} | ||
|
||
#[rustfmt::skip] | ||
/// * first list item | ||
/// overindented line | ||
//~^ ERROR: doc list item overindented | ||
/// this is overindented line too | ||
//~^ ERROR: doc list item overindented | ||
/// * second list item | ||
fn baz() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
error: doc list item overindented | ||
--> tests/ui/doc/doc_overindented_list_item.rs:5:5 | ||
| | ||
LL | /// overindented line | ||
| ^^^^^^^ | ||
| | ||
= help: if this is supposed to be its own paragraph, add a blank line | ||
= note: `-D clippy::doc-overindented-list-item` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(clippy::doc_overindented_list_item)]` | ||
help: indent this line | ||
| | ||
LL | /// overindented line | ||
| ++ | ||
|
||
error: doc list item overindented | ||
--> tests/ui/doc/doc_overindented_list_item.rs:7:5 | ||
| | ||
LL | /// this is overindented line too | ||
| ^^^^^ | ||
| | ||
= help: if this is supposed to be its own paragraph, add a blank line | ||
help: indent this line | ||
| | ||
LL | /// this is overindented line too | ||
| ++ | ||
|
||
error: doc list item overindented | ||
--> tests/ui/doc/doc_overindented_list_item.rs:14:7 | ||
| | ||
LL | /// overindented line | ||
| ^^^^^ | ||
| | ||
= help: if this is supposed to be its own paragraph, add a blank line | ||
help: indent this line | ||
| | ||
LL | /// overindented line | ||
| ++ | ||
|
||
error: doc list item overindented | ||
--> tests/ui/doc/doc_overindented_list_item.rs:16:7 | ||
| | ||
LL | /// this is overindented line too | ||
| ^^^ | ||
| | ||
= help: if this is supposed to be its own paragraph, add a blank line | ||
help: indent this line | ||
| | ||
LL | /// this is overindented line too | ||
| ++ | ||
|
||
error: doc list item overindented | ||
--> tests/ui/doc/doc_overindented_list_item.rs:23:5 | ||
| | ||
LL | /// overindented line | ||
| ^^^^^^^ | ||
| | ||
= help: if this is supposed to be its own paragraph, add a blank line | ||
help: indent this line | ||
| | ||
LL | /// overindented line | ||
| ++ | ||
|
||
error: doc list item overindented | ||
--> tests/ui/doc/doc_overindented_list_item.rs:25:5 | ||
| | ||
LL | /// this is overindented line too | ||
| ^^^^^ | ||
| | ||
= help: if this is supposed to be its own paragraph, add a blank line | ||
help: indent this line | ||
| | ||
LL | /// this is overindented line too | ||
| ++ | ||
|
||
error: aborting due to 6 previous errors | ||
|