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