1 use crate::utils::span_lint_and_sugg;
3 use rustc_errors::Applicability;
4 use rustc_lint::{EarlyContext, EarlyLintPass};
5 use rustc_session::{declare_lint_pass, declare_tool_lint};
6 use rustc_span::source_map::{BytePos, Span};
7 use std::convert::TryFrom;
10 /// **What it does:** Checks doc comments for usage of tab characters.
12 /// **Why is this bad?** The rust style-guide promotes spaces instead of tabs for indentation.
13 /// To keep a consistent view on the source, also doc comments should not have tabs.
14 /// Also, explaining ascii-diagrams containing tabs can get displayed incorrectly when the
15 /// display settings of the author and reader differ.
17 /// **Known problems:** None.
22 /// /// Struct to hold two strings:
25 /// pub struct DoubleString {
27 /// /// - First String:
28 /// /// - needs to be inside here
29 /// first_string: String,
31 /// /// - Second String:
32 /// /// - needs to be inside here
33 /// second_string: String,
37 /// Will be converted to:
40 /// /// Struct to hold two strings:
43 /// pub struct DoubleString {
45 /// /// - First String:
46 /// /// - needs to be inside here
47 /// first_string: String,
49 /// /// - Second String:
50 /// /// - needs to be inside here
51 /// second_string: String,
54 pub TABS_IN_DOC_COMMENTS,
56 "using tabs in doc comments is not recommended"
59 declare_lint_pass!(TabsInDocComments => [TABS_IN_DOC_COMMENTS]);
61 impl TabsInDocComments {
62 fn warn_if_tabs_in_doc(cx: &EarlyContext<'_>, attr: &ast::Attribute) {
63 if let ast::AttrKind::DocComment(_, comment) = attr.kind {
64 let comment = comment.as_str();
66 for (lo, hi) in get_chunks_of_tabs(&comment) {
67 // +3 skips the opening delimiter
68 let new_span = Span::new(
69 attr.span.lo() + BytePos(3 + lo),
70 attr.span.lo() + BytePos(3 + hi),
77 "using tabs in doc comments is not recommended",
78 "consider using four spaces per tab",
79 " ".repeat((hi - lo) as usize),
80 Applicability::MaybeIncorrect,
87 impl EarlyLintPass for TabsInDocComments {
88 fn check_attribute(&mut self, cx: &EarlyContext<'_>, attribute: &ast::Attribute) {
89 Self::warn_if_tabs_in_doc(cx, &attribute);
94 /// scans the string for groups of tabs and returns the start(inclusive) and end positions
95 /// (exclusive) of all groups
96 /// e.g. "sd\tasd\t\taa" will be converted to [(2, 3), (6, 8)] as
99 fn get_chunks_of_tabs(the_str: &str) -> Vec<(u32, u32)> {
100 let line_length_way_to_long = "doc comment longer than 2^32 chars";
101 let mut spans: Vec<(u32, u32)> = vec![];
102 let mut current_start: u32 = 0;
104 // tracker to decide if the last group of tabs is not closed by a non-tab character
105 let mut is_active = false;
107 let chars_array: Vec<_> = the_str.chars().collect();
109 if chars_array == vec!['\t'] {
113 for (index, arr) in chars_array.windows(2).enumerate() {
114 let index = u32::try_from(index).expect(line_length_way_to_long);
117 // either string starts with double tab, then we have to set it active,
118 // otherwise is_active is true anyway
122 // as ['\t', '\t'] is excluded, this has to be a start of a tab group,
123 // set indices accordingly
125 current_start = index + 1;
128 // this now has to be an end of the group, hence we have to push a new tuple
130 spans.push((current_start, index + 1));
136 // only possible when tabs are at the end, insert last group
140 u32::try_from(the_str.chars().count()).expect(line_length_way_to_long),
148 mod tests_for_get_chunks_of_tabs {
149 use super::get_chunks_of_tabs;
152 fn test_empty_string() {
153 let res = get_chunks_of_tabs("");
155 assert_eq!(res, vec![]);
160 let res = get_chunks_of_tabs("sd\t\t\taa");
162 assert_eq!(res, vec![(2, 5)]);
167 let res = get_chunks_of_tabs("\t\t");
169 assert_eq!(res, vec![(0, 2)]);
173 fn test_only_one_t() {
174 let res = get_chunks_of_tabs("\t");
176 assert_eq!(res, vec![(0, 1)]);
181 let res = get_chunks_of_tabs("sd\tasd\t\taa");
183 assert_eq!(res, vec![(2, 3), (6, 8)]);
188 let res = get_chunks_of_tabs("\t\taa");
190 assert_eq!(res, vec![(0, 2)]);
195 let res = get_chunks_of_tabs("aa\t\t");
197 assert_eq!(res, vec![(2, 4)]);
201 fn test_start_single() {
202 let res = get_chunks_of_tabs("\taa");
204 assert_eq!(res, vec![(0, 1)]);
208 fn test_end_single() {
209 let res = get_chunks_of_tabs("aa\t");
211 assert_eq!(res, vec![(2, 3)]);
216 let res = get_chunks_of_tabs("dsfs");
218 assert_eq!(res, vec![]);