-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
tabs_in_doc_comments.rs
219 lines (186 loc) · 6.02 KB
/
tabs_in_doc_comments.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
use crate::utils::span_lint_and_sugg;
use rustc_ast::ast;
use rustc_errors::Applicability;
use rustc_lint::{EarlyContext, EarlyLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::source_map::{BytePos, Span};
use std::convert::TryFrom;
declare_clippy_lint! {
/// **What it does:** Checks doc comments for usage of tab characters.
///
/// **Why is this bad?** The rust style-guide promotes spaces instead of tabs for indentation.
/// 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.
///
/// **Known problems:** None.
///
/// **Example:**
/// ```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:
/// ```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,
///}
/// ```
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) {
if let ast::AttrKind::DocComment(comment) = attr.kind {
let comment = comment.as_str();
for (lo, hi) in get_chunks_of_tabs(&comment) {
let new_span = Span::new(
attr.span.lo() + BytePos(lo),
attr.span.lo() + BytePos(hi),
attr.span.ctxt(),
);
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) {
Self::warn_if_tabs_in_doc(cx, &attribute);
}
}
///
/// 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;
let chars_array: Vec<_> = the_str.chars().collect();
if chars_array == vec!['\t'] {
return vec![(0, 1)];
}
for (index, arr) in chars_array.windows(2).enumerate() {
let index = u32::try_from(index).expect(line_length_way_to_long);
match arr {
['\t', '\t'] => {
// either string starts with double tab, then we have to set it active,
// otherwise is_active is true anyway
is_active = true;
},
[_, '\t'] => {
// as ['\t', '\t'] is excluded, this has to be a start of a tab group,
// set indices accordingly
is_active = true;
current_start = index + 1;
},
['\t', _] => {
// this now has to be an end of the group, hence we have to push a new tuple
is_active = false;
spans.push((current_start, index + 1));
},
_ => {},
}
}
// only possible when tabs are at the end, insert last group
if is_active {
spans.push((
current_start,
u32::try_from(the_str.chars().count()).expect(line_length_way_to_long),
));
}
spans
}
#[cfg(test)]
mod tests_for_get_chunks_of_tabs {
use super::get_chunks_of_tabs;
#[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![]);
}
}