]> git.lizzy.rs Git - rust.git/blob - src/tools/rustfmt/src/issues.rs
Rollup merge of #85223 - simbleau:master, r=steveklabnik
[rust.git] / src / tools / rustfmt / src / issues.rs
1 // Objects for seeking through a char stream for occurrences of TODO and FIXME.
2 // Depending on the loaded configuration, may also check that these have an
3 // associated issue number.
4
5 use std::fmt;
6
7 use crate::config::ReportTactic;
8
9 const TO_DO_CHARS: &[char] = &['t', 'o', 'd', 'o'];
10 const FIX_ME_CHARS: &[char] = &['f', 'i', 'x', 'm', 'e'];
11
12 // Enabled implementation detail is here because it is
13 // irrelevant outside the issues module
14 fn is_enabled(report_tactic: ReportTactic) -> bool {
15     report_tactic != ReportTactic::Never
16 }
17
18 #[derive(Clone, Copy)]
19 enum Seeking {
20     Issue { todo_idx: usize, fixme_idx: usize },
21     Number { issue: Issue, part: NumberPart },
22 }
23
24 #[derive(Clone, Copy)]
25 enum NumberPart {
26     OpenParen,
27     Pound,
28     Number,
29     CloseParen,
30 }
31
32 #[derive(PartialEq, Eq, Debug, Clone, Copy)]
33 pub struct Issue {
34     issue_type: IssueType,
35     // Indicates whether we're looking for issues with missing numbers, or
36     // all issues of this type.
37     missing_number: bool,
38 }
39
40 impl fmt::Display for Issue {
41     fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
42         let msg = match self.issue_type {
43             IssueType::Todo => "TODO",
44             IssueType::Fixme => "FIXME",
45         };
46         let details = if self.missing_number {
47             " without issue number"
48         } else {
49             ""
50         };
51
52         write!(fmt, "{}{}", msg, details)
53     }
54 }
55
56 #[derive(PartialEq, Eq, Debug, Clone, Copy)]
57 enum IssueType {
58     Todo,
59     Fixme,
60 }
61
62 enum IssueClassification {
63     Good,
64     Bad(Issue),
65     None,
66 }
67
68 pub(crate) struct BadIssueSeeker {
69     state: Seeking,
70     report_todo: ReportTactic,
71     report_fixme: ReportTactic,
72 }
73
74 impl BadIssueSeeker {
75     pub(crate) fn new(report_todo: ReportTactic, report_fixme: ReportTactic) -> BadIssueSeeker {
76         BadIssueSeeker {
77             state: Seeking::Issue {
78                 todo_idx: 0,
79                 fixme_idx: 0,
80             },
81             report_todo,
82             report_fixme,
83         }
84     }
85
86     pub(crate) fn is_disabled(&self) -> bool {
87         !is_enabled(self.report_todo) && !is_enabled(self.report_fixme)
88     }
89
90     // Check whether or not the current char is conclusive evidence for an
91     // unnumbered TO-DO or FIX-ME.
92     pub(crate) fn inspect(&mut self, c: char) -> Option<Issue> {
93         match self.state {
94             Seeking::Issue {
95                 todo_idx,
96                 fixme_idx,
97             } => {
98                 self.state = self.inspect_issue(c, todo_idx, fixme_idx);
99             }
100             Seeking::Number { issue, part } => {
101                 let result = self.inspect_number(c, issue, part);
102
103                 if let IssueClassification::None = result {
104                     return None;
105                 }
106
107                 self.state = Seeking::Issue {
108                     todo_idx: 0,
109                     fixme_idx: 0,
110                 };
111
112                 if let IssueClassification::Bad(issue) = result {
113                     return Some(issue);
114                 }
115             }
116         }
117
118         None
119     }
120
121     fn inspect_issue(&mut self, c: char, mut todo_idx: usize, mut fixme_idx: usize) -> Seeking {
122         if let Some(lower_case_c) = c.to_lowercase().next() {
123             if is_enabled(self.report_todo) && lower_case_c == TO_DO_CHARS[todo_idx] {
124                 todo_idx += 1;
125                 if todo_idx == TO_DO_CHARS.len() {
126                     return Seeking::Number {
127                         issue: Issue {
128                             issue_type: IssueType::Todo,
129                             missing_number: matches!(self.report_todo, ReportTactic::Unnumbered),
130                         },
131                         part: NumberPart::OpenParen,
132                     };
133                 }
134                 fixme_idx = 0;
135             } else if is_enabled(self.report_fixme) && lower_case_c == FIX_ME_CHARS[fixme_idx] {
136                 // Exploit the fact that the character sets of todo and fixme
137                 // are disjoint by adding else.
138                 fixme_idx += 1;
139                 if fixme_idx == FIX_ME_CHARS.len() {
140                     return Seeking::Number {
141                         issue: Issue {
142                             issue_type: IssueType::Fixme,
143                             missing_number: matches!(self.report_fixme, ReportTactic::Unnumbered),
144                         },
145                         part: NumberPart::OpenParen,
146                     };
147                 }
148                 todo_idx = 0;
149             } else {
150                 todo_idx = 0;
151                 fixme_idx = 0;
152             }
153         }
154
155         Seeking::Issue {
156             todo_idx,
157             fixme_idx,
158         }
159     }
160
161     fn inspect_number(
162         &mut self,
163         c: char,
164         issue: Issue,
165         mut part: NumberPart,
166     ) -> IssueClassification {
167         if !issue.missing_number || c == '\n' {
168             return IssueClassification::Bad(issue);
169         } else if c == ')' {
170             return if let NumberPart::CloseParen = part {
171                 IssueClassification::Good
172             } else {
173                 IssueClassification::Bad(issue)
174             };
175         }
176
177         match part {
178             NumberPart::OpenParen => {
179                 if c != '(' {
180                     return IssueClassification::Bad(issue);
181                 } else {
182                     part = NumberPart::Pound;
183                 }
184             }
185             NumberPart::Pound => {
186                 if c == '#' {
187                     part = NumberPart::Number;
188                 }
189             }
190             NumberPart::Number => {
191                 if ('0'..='9').contains(&c) {
192                     part = NumberPart::CloseParen;
193                 } else {
194                     return IssueClassification::Bad(issue);
195                 }
196             }
197             NumberPart::CloseParen => {}
198         }
199
200         self.state = Seeking::Number { part, issue };
201
202         IssueClassification::None
203     }
204 }
205
206 #[test]
207 fn find_unnumbered_issue() {
208     fn check_fail(text: &str, failing_pos: usize) {
209         let mut seeker = BadIssueSeeker::new(ReportTactic::Unnumbered, ReportTactic::Unnumbered);
210         assert_eq!(
211             Some(failing_pos),
212             text.find(|c| seeker.inspect(c).is_some())
213         );
214     }
215
216     fn check_pass(text: &str) {
217         let mut seeker = BadIssueSeeker::new(ReportTactic::Unnumbered, ReportTactic::Unnumbered);
218         assert_eq!(None, text.find(|c| seeker.inspect(c).is_some()));
219     }
220
221     check_fail("TODO\n", 4);
222     check_pass(" TO FIX DOME\n");
223     check_fail(" \n FIXME\n", 8);
224     check_fail("FIXME(\n", 6);
225     check_fail("FIXME(#\n", 7);
226     check_fail("FIXME(#1\n", 8);
227     check_fail("FIXME(#)1\n", 7);
228     check_pass("FIXME(#1222)\n");
229     check_fail("FIXME(#12\n22)\n", 9);
230     check_pass("FIXME(@maintainer, #1222, hello)\n");
231     check_fail("TODO(#22) FIXME\n", 15);
232 }
233
234 #[test]
235 fn find_issue() {
236     fn is_bad_issue(text: &str, report_todo: ReportTactic, report_fixme: ReportTactic) -> bool {
237         let mut seeker = BadIssueSeeker::new(report_todo, report_fixme);
238         text.chars().any(|c| seeker.inspect(c).is_some())
239     }
240
241     assert!(is_bad_issue(
242         "TODO(@maintainer, #1222, hello)\n",
243         ReportTactic::Always,
244         ReportTactic::Never,
245     ));
246
247     assert!(!is_bad_issue(
248         "TODO: no number\n",
249         ReportTactic::Never,
250         ReportTactic::Always,
251     ));
252
253     assert!(!is_bad_issue(
254         "Todo: mixed case\n",
255         ReportTactic::Never,
256         ReportTactic::Always,
257     ));
258
259     assert!(is_bad_issue(
260         "This is a FIXME(#1)\n",
261         ReportTactic::Never,
262         ReportTactic::Always,
263     ));
264
265     assert!(is_bad_issue(
266         "This is a FixMe(#1) mixed case\n",
267         ReportTactic::Never,
268         ReportTactic::Always,
269     ));
270
271     assert!(!is_bad_issue(
272         "bad FIXME\n",
273         ReportTactic::Always,
274         ReportTactic::Never,
275     ));
276 }
277
278 #[test]
279 fn issue_type() {
280     let mut seeker = BadIssueSeeker::new(ReportTactic::Always, ReportTactic::Never);
281     let expected = Some(Issue {
282         issue_type: IssueType::Todo,
283         missing_number: false,
284     });
285
286     assert_eq!(
287         expected,
288         "TODO(#100): more awesomeness"
289             .chars()
290             .map(|c| seeker.inspect(c))
291             .find(Option::is_some)
292             .unwrap()
293     );
294
295     let mut seeker = BadIssueSeeker::new(ReportTactic::Never, ReportTactic::Unnumbered);
296     let expected = Some(Issue {
297         issue_type: IssueType::Fixme,
298         missing_number: true,
299     });
300
301     assert_eq!(
302         expected,
303         "Test. FIXME: bad, bad, not good"
304             .chars()
305             .map(|c| seeker.inspect(c))
306             .find(Option::is_some)
307             .unwrap()
308     );
309 }