]> git.lizzy.rs Git - rust.git/blob - src/issues.rs
Format source codes
[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: &'static [char] = &['T', 'O', 'D', 'O'];
20 const FIX_ME_CHARS: &'static [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         // FIXME: Should we also check for lower case characters?
131         if self.report_todo.is_enabled() && 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() && 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         Seeking::Issue {
171             todo_idx: todo_idx,
172             fixme_idx: fixme_idx,
173         }
174     }
175
176     fn inspect_number(
177         &mut self,
178         c: char,
179         issue: Issue,
180         mut part: NumberPart,
181     ) -> IssueClassification {
182         if !issue.missing_number || c == '\n' {
183             return IssueClassification::Bad(issue);
184         } else if c == ')' {
185             return if let NumberPart::CloseParen = part {
186                 IssueClassification::Good
187             } else {
188                 IssueClassification::Bad(issue)
189             };
190         }
191
192         match part {
193             NumberPart::OpenParen => {
194                 if c != '(' {
195                     return IssueClassification::Bad(issue);
196                 } else {
197                     part = NumberPart::Pound;
198                 }
199             }
200             NumberPart::Pound => {
201                 if c == '#' {
202                     part = NumberPart::Number;
203                 }
204             }
205             NumberPart::Number => {
206                 if c >= '0' && c <= '9' {
207                     part = NumberPart::CloseParen;
208                 } else {
209                     return IssueClassification::Bad(issue);
210                 }
211             }
212             NumberPart::CloseParen => {}
213         }
214
215         self.state = Seeking::Number {
216             part: part,
217             issue: issue,
218         };
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.chars().position(|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.chars().position(|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         "This is a FIXME(#1)\n",
273         ReportTactic::Never,
274         ReportTactic::Always,
275     ));
276
277     assert!(!is_bad_issue(
278         "bad FIXME\n",
279         ReportTactic::Always,
280         ReportTactic::Never,
281     ));
282 }
283
284 #[test]
285 fn issue_type() {
286     let mut seeker = BadIssueSeeker::new(ReportTactic::Always, ReportTactic::Never);
287     let expected = Some(Issue {
288         issue_type: IssueType::Todo,
289         missing_number: false,
290     });
291
292     assert_eq!(
293         expected,
294         "TODO(#100): more awesomeness"
295             .chars()
296             .map(|c| seeker.inspect(c))
297             .find(Option::is_some)
298             .unwrap()
299     );
300
301     let mut seeker = BadIssueSeeker::new(ReportTactic::Never, ReportTactic::Unnumbered);
302     let expected = Some(Issue {
303         issue_type: IssueType::Fixme,
304         missing_number: true,
305     });
306
307     assert_eq!(
308         expected,
309         "Test. FIXME: bad, bad, not good"
310             .chars()
311             .map(|c| seeker.inspect(c))
312             .find(Option::is_some)
313             .unwrap()
314     );
315 }