]> git.lizzy.rs Git - rust.git/blob - src/issues.rs
Merge pull request #2129 from topecongiro/changelog
[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 => if c != '(' {
194                 return IssueClassification::Bad(issue);
195             } else {
196                 part = NumberPart::Pound;
197             },
198             NumberPart::Pound => if c == '#' {
199                 part = NumberPart::Number;
200             },
201             NumberPart::Number => if c >= '0' && c <= '9' {
202                 part = NumberPart::CloseParen;
203             } else {
204                 return IssueClassification::Bad(issue);
205             },
206             NumberPart::CloseParen => {}
207         }
208
209         self.state = Seeking::Number {
210             part: part,
211             issue: issue,
212         };
213
214         IssueClassification::None
215     }
216 }
217
218 #[test]
219 fn find_unnumbered_issue() {
220     fn check_fail(text: &str, failing_pos: usize) {
221         let mut seeker = BadIssueSeeker::new(ReportTactic::Unnumbered, ReportTactic::Unnumbered);
222         assert_eq!(
223             Some(failing_pos),
224             text.chars().position(|c| seeker.inspect(c).is_some())
225         );
226     }
227
228     fn check_pass(text: &str) {
229         let mut seeker = BadIssueSeeker::new(ReportTactic::Unnumbered, ReportTactic::Unnumbered);
230         assert_eq!(None, text.chars().position(|c| seeker.inspect(c).is_some()));
231     }
232
233     check_fail("TODO\n", 4);
234     check_pass(" TO FIX DOME\n");
235     check_fail(" \n FIXME\n", 8);
236     check_fail("FIXME(\n", 6);
237     check_fail("FIXME(#\n", 7);
238     check_fail("FIXME(#1\n", 8);
239     check_fail("FIXME(#)1\n", 7);
240     check_pass("FIXME(#1222)\n");
241     check_fail("FIXME(#12\n22)\n", 9);
242     check_pass("FIXME(@maintainer, #1222, hello)\n");
243     check_fail("TODO(#22) FIXME\n", 15);
244 }
245
246 #[test]
247 fn find_issue() {
248     fn is_bad_issue(text: &str, report_todo: ReportTactic, report_fixme: ReportTactic) -> bool {
249         let mut seeker = BadIssueSeeker::new(report_todo, report_fixme);
250         text.chars().any(|c| seeker.inspect(c).is_some())
251     }
252
253     assert!(is_bad_issue(
254         "TODO(@maintainer, #1222, hello)\n",
255         ReportTactic::Always,
256         ReportTactic::Never,
257     ));
258
259     assert!(!is_bad_issue(
260         "TODO: no number\n",
261         ReportTactic::Never,
262         ReportTactic::Always,
263     ));
264
265     assert!(is_bad_issue(
266         "This is a FIXME(#1)\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 }