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