2021-03-25 18:29:11 +00:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_sugg;
|
2020-03-01 03:23:33 +00:00
|
|
|
use rustc_ast::ast;
|
2019-11-15 15:18:08 +00:00
|
|
|
use rustc_errors::Applicability;
|
2020-01-12 06:08:41 +00:00
|
|
|
use rustc_lint::{EarlyContext, EarlyLintPass};
|
2020-01-11 11:37:08 +00:00
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2020-01-04 10:00:00 +00:00
|
|
|
use rustc_span::source_map::{BytePos, Span};
|
2019-11-15 15:18:08 +00:00
|
|
|
use std::convert::TryFrom;
|
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks doc comments for usage of tab characters.
|
2019-11-15 15:18:08 +00:00
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// The rust style-guide promotes spaces instead of tabs for indentation.
|
2019-11-15 15:18:08 +00:00
|
|
|
/// To keep a consistent view on the source, also doc comments should not have tabs.
|
|
|
|
/// Also, explaining ascii-diagrams containing tabs can get displayed incorrectly when the
|
|
|
|
/// display settings of the author and reader differ.
|
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Example
|
2019-11-15 15:18:08 +00:00
|
|
|
/// ```rust
|
|
|
|
/// ///
|
|
|
|
/// /// Struct to hold two strings:
|
|
|
|
/// /// - first one
|
|
|
|
/// /// - second one
|
|
|
|
/// pub struct DoubleString {
|
|
|
|
/// ///
|
|
|
|
/// /// - First String:
|
|
|
|
/// /// - needs to be inside here
|
|
|
|
/// first_string: String,
|
|
|
|
/// ///
|
|
|
|
/// /// - Second String:
|
|
|
|
/// /// - needs to be inside here
|
|
|
|
/// second_string: String,
|
|
|
|
///}
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Will be converted to:
|
2020-01-06 06:30:43 +00:00
|
|
|
/// ```rust
|
2019-11-15 15:18:08 +00:00
|
|
|
/// ///
|
|
|
|
/// /// Struct to hold two strings:
|
|
|
|
/// /// - first one
|
|
|
|
/// /// - second one
|
|
|
|
/// pub struct DoubleString {
|
|
|
|
/// ///
|
|
|
|
/// /// - First String:
|
|
|
|
/// /// - needs to be inside here
|
|
|
|
/// first_string: String,
|
|
|
|
/// ///
|
|
|
|
/// /// - Second String:
|
|
|
|
/// /// - needs to be inside here
|
|
|
|
/// second_string: String,
|
|
|
|
///}
|
|
|
|
/// ```
|
Added `clippy::version` attribute to all normal lints
So, some context for this, well, more a story. I'm not used to scripting, I've never really scripted anything, even if it's a valuable skill. I just never really needed it. Now, `@flip1995` correctly suggested using a script for this in `rust-clippy#7813`...
And I decided to write a script using nushell because why not? This was a mistake... I spend way more time on this than I would like to admit. It has definitely been more than 4 hours. It shouldn't take that long, but me being new to scripting and nushell just wasn't a good mixture... Anyway, here is the script that creates another script which adds the versions. Fun...
Just execute this on the `gh-pages` branch and the resulting `replacer.sh` in `clippy_lints` and it should all work.
```nu
mv v0.0.212 rust-1.00.0;
mv beta rust-1.57.0;
mv master rust-1.58.0;
let paths = (open ./rust-1.58.0/lints.json | select id id_span | flatten | select id path);
let versions = (
ls | where name =~ "rust-" | select name | format {name}/lints.json |
each { open $it | select id | insert version $it | str substring "5,11" version} |
group-by id | rotate counter-clockwise id version |
update version {get version | first 1} | flatten | select id version);
$paths | each { |row|
let version = ($versions | where id == ($row.id) | format {version})
let idu = ($row.id | str upcase)
$"sed -i '0,/($idu),/{s/pub ($idu),/#[clippy::version = "($version)"]\n pub ($idu),/}' ($row.path)"
} | str collect ";" | str find-replace --all '1.00.0' 'pre 1.29.0' | save "replacer.sh";
```
And this still has some problems, but at this point I just want to be done -.-
2021-10-21 19:06:26 +00:00
|
|
|
#[clippy::version = "1.41.0"]
|
2019-11-15 15:18:08 +00:00
|
|
|
pub TABS_IN_DOC_COMMENTS,
|
|
|
|
style,
|
|
|
|
"using tabs in doc comments is not recommended"
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint_pass!(TabsInDocComments => [TABS_IN_DOC_COMMENTS]);
|
|
|
|
|
|
|
|
impl TabsInDocComments {
|
|
|
|
fn warn_if_tabs_in_doc(cx: &EarlyContext<'_>, attr: &ast::Attribute) {
|
2020-07-22 14:59:17 +00:00
|
|
|
if let ast::AttrKind::DocComment(_, comment) = attr.kind {
|
2019-11-15 15:18:08 +00:00
|
|
|
let comment = comment.as_str();
|
|
|
|
|
|
|
|
for (lo, hi) in get_chunks_of_tabs(&comment) {
|
2020-08-04 21:26:23 +00:00
|
|
|
// +3 skips the opening delimiter
|
2019-11-15 15:18:08 +00:00
|
|
|
let new_span = Span::new(
|
2020-07-22 14:59:17 +00:00
|
|
|
attr.span.lo() + BytePos(3 + lo),
|
|
|
|
attr.span.lo() + BytePos(3 + hi),
|
2019-11-15 15:18:08 +00:00
|
|
|
attr.span.ctxt(),
|
2021-04-18 12:27:04 +00:00
|
|
|
attr.span.parent(),
|
2019-11-15 15:18:08 +00:00
|
|
|
);
|
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
TABS_IN_DOC_COMMENTS,
|
|
|
|
new_span,
|
|
|
|
"using tabs in doc comments is not recommended",
|
|
|
|
"consider using four spaces per tab",
|
|
|
|
" ".repeat((hi - lo) as usize),
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl EarlyLintPass for TabsInDocComments {
|
|
|
|
fn check_attribute(&mut self, cx: &EarlyContext<'_>, attribute: &ast::Attribute) {
|
2021-04-08 15:50:13 +00:00
|
|
|
Self::warn_if_tabs_in_doc(cx, attribute);
|
2019-11-15 15:18:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
///
|
|
|
|
/// scans the string for groups of tabs and returns the start(inclusive) and end positions
|
|
|
|
/// (exclusive) of all groups
|
|
|
|
/// e.g. "sd\tasd\t\taa" will be converted to [(2, 3), (6, 8)] as
|
|
|
|
/// 012 3456 7 89
|
|
|
|
/// ^-^ ^---^
|
|
|
|
fn get_chunks_of_tabs(the_str: &str) -> Vec<(u32, u32)> {
|
|
|
|
let line_length_way_to_long = "doc comment longer than 2^32 chars";
|
|
|
|
let mut spans: Vec<(u32, u32)> = vec![];
|
|
|
|
let mut current_start: u32 = 0;
|
|
|
|
|
|
|
|
// tracker to decide if the last group of tabs is not closed by a non-tab character
|
|
|
|
let mut is_active = false;
|
|
|
|
|
2021-04-22 09:31:13 +00:00
|
|
|
// Note that we specifically need the char _byte_ indices here, not the positional indexes
|
|
|
|
// within the char array to deal with multi-byte characters properly. `char_indices` does
|
|
|
|
// exactly that. It provides an iterator over tuples of the form `(byte position, char)`.
|
|
|
|
let char_indices: Vec<_> = the_str.char_indices().collect();
|
2019-11-15 15:18:08 +00:00
|
|
|
|
2021-04-22 09:31:13 +00:00
|
|
|
if let [(_, '\t')] = char_indices.as_slice() {
|
2019-11-15 15:18:08 +00:00
|
|
|
return vec![(0, 1)];
|
|
|
|
}
|
|
|
|
|
2021-04-22 09:31:13 +00:00
|
|
|
for entry in char_indices.windows(2) {
|
|
|
|
match entry {
|
|
|
|
[(_, '\t'), (_, '\t')] => {
|
2019-11-15 15:18:08 +00:00
|
|
|
// either string starts with double tab, then we have to set it active,
|
|
|
|
// otherwise is_active is true anyway
|
|
|
|
is_active = true;
|
|
|
|
},
|
2021-04-22 09:31:13 +00:00
|
|
|
[(_, _), (index_b, '\t')] => {
|
2019-11-15 15:18:08 +00:00
|
|
|
// as ['\t', '\t'] is excluded, this has to be a start of a tab group,
|
|
|
|
// set indices accordingly
|
|
|
|
is_active = true;
|
2021-04-22 09:31:13 +00:00
|
|
|
current_start = u32::try_from(*index_b).unwrap();
|
2019-11-15 15:18:08 +00:00
|
|
|
},
|
2021-04-22 09:31:13 +00:00
|
|
|
[(_, '\t'), (index_b, _)] => {
|
2019-11-15 15:18:08 +00:00
|
|
|
// this now has to be an end of the group, hence we have to push a new tuple
|
|
|
|
is_active = false;
|
2021-04-22 09:31:13 +00:00
|
|
|
spans.push((current_start, u32::try_from(*index_b).unwrap()));
|
2019-11-15 15:18:08 +00:00
|
|
|
},
|
|
|
|
_ => {},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// only possible when tabs are at the end, insert last group
|
|
|
|
if is_active {
|
|
|
|
spans.push((
|
|
|
|
current_start,
|
2021-04-22 09:31:13 +00:00
|
|
|
u32::try_from(char_indices.last().unwrap().0 + 1).expect(line_length_way_to_long),
|
2019-11-15 15:18:08 +00:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
spans
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests_for_get_chunks_of_tabs {
|
|
|
|
use super::get_chunks_of_tabs;
|
|
|
|
|
2021-04-22 09:31:13 +00:00
|
|
|
#[test]
|
|
|
|
fn test_unicode_han_string() {
|
|
|
|
let res = get_chunks_of_tabs(" \u{4f4d}\t");
|
|
|
|
|
|
|
|
assert_eq!(res, vec![(4, 5)]);
|
|
|
|
}
|
|
|
|
|
2019-11-15 15:18:08 +00:00
|
|
|
#[test]
|
|
|
|
fn test_empty_string() {
|
|
|
|
let res = get_chunks_of_tabs("");
|
|
|
|
|
|
|
|
assert_eq!(res, vec![]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_simple() {
|
|
|
|
let res = get_chunks_of_tabs("sd\t\t\taa");
|
|
|
|
|
|
|
|
assert_eq!(res, vec![(2, 5)]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_only_t() {
|
|
|
|
let res = get_chunks_of_tabs("\t\t");
|
|
|
|
|
|
|
|
assert_eq!(res, vec![(0, 2)]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_only_one_t() {
|
|
|
|
let res = get_chunks_of_tabs("\t");
|
|
|
|
|
|
|
|
assert_eq!(res, vec![(0, 1)]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_double() {
|
|
|
|
let res = get_chunks_of_tabs("sd\tasd\t\taa");
|
|
|
|
|
|
|
|
assert_eq!(res, vec![(2, 3), (6, 8)]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_start() {
|
|
|
|
let res = get_chunks_of_tabs("\t\taa");
|
|
|
|
|
|
|
|
assert_eq!(res, vec![(0, 2)]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_end() {
|
|
|
|
let res = get_chunks_of_tabs("aa\t\t");
|
|
|
|
|
|
|
|
assert_eq!(res, vec![(2, 4)]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_start_single() {
|
|
|
|
let res = get_chunks_of_tabs("\taa");
|
|
|
|
|
|
|
|
assert_eq!(res, vec![(0, 1)]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_end_single() {
|
|
|
|
let res = get_chunks_of_tabs("aa\t");
|
|
|
|
|
|
|
|
assert_eq!(res, vec![(2, 3)]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_no_tabs() {
|
|
|
|
let res = get_chunks_of_tabs("dsfs");
|
|
|
|
|
|
|
|
assert_eq!(res, vec![]);
|
|
|
|
}
|
|
|
|
}
|