-
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_items
Add a new lint `doc_overindented_list_items` 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
11 changed files
with
238 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
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,50 @@ | ||
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_ITEMS; | ||
|
||
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_ITEMS, | ||
span, | ||
"doc list item overindented", | ||
|diag| { | ||
diag.span_suggestion_verbose( | ||
span, | ||
"remove unnecessary spaces", | ||
std::iter::repeat(" ").take(list_indentation).join(""), | ||
Applicability::MaybeIncorrect, | ||
); | ||
}, | ||
); | ||
} | ||
} |
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_items)] | ||
|
||
#[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_items)] | ||
|
||
#[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,70 @@ | ||
error: doc list item overindented | ||
--> tests/ui/doc/doc_overindented_list_items.rs:5:5 | ||
| | ||
LL | /// overindented line | ||
| ^^^^^^^ | ||
| | ||
= note: `-D clippy::doc-overindented-list-items` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(clippy::doc_overindented_list_items)]` | ||
help: remove unnecessary spaces | ||
| | ||
LL | /// overindented line | ||
| ++ | ||
|
||
error: doc list item overindented | ||
--> tests/ui/doc/doc_overindented_list_items.rs:7:5 | ||
| | ||
LL | /// this is overindented line too | ||
| ^^^^^ | ||
| | ||
help: remove unnecessary spaces | ||
| | ||
LL | /// this is overindented line too | ||
| ++ | ||
|
||
error: doc list item overindented | ||
--> tests/ui/doc/doc_overindented_list_items.rs:14:7 | ||
| | ||
LL | /// overindented line | ||
| ^^^^^ | ||
| | ||
help: remove unnecessary spaces | ||
| | ||
LL | /// overindented line | ||
| ++ | ||
|
||
error: doc list item overindented | ||
--> tests/ui/doc/doc_overindented_list_items.rs:16:7 | ||
| | ||
LL | /// this is overindented line too | ||
| ^^^ | ||
| | ||
help: remove unnecessary spaces | ||
| | ||
LL | /// this is overindented line too | ||
| ++ | ||
|
||
error: doc list item overindented | ||
--> tests/ui/doc/doc_overindented_list_items.rs:23:5 | ||
| | ||
LL | /// overindented line | ||
| ^^^^^^^ | ||
| | ||
help: remove unnecessary spaces | ||
| | ||
LL | /// overindented line | ||
| ++ | ||
|
||
error: doc list item overindented | ||
--> tests/ui/doc/doc_overindented_list_items.rs:25:5 | ||
| | ||
LL | /// this is overindented line too | ||
| ^^^^^ | ||
| | ||
help: remove unnecessary spaces | ||
| | ||
LL | /// this is overindented line too | ||
| ++ | ||
|
||
error: aborting due to 6 previous errors | ||
|