]> git.lizzy.rs Git - rust.git/blob - src/issues.rs
Remove BlockIndentStyle::Inherit
[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 occurences 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 pub use config::ReportTactic;
17
18 const TO_DO_CHARS: &'static [char] = &['T', 'O', 'D', 'O'];
19 const FIX_ME_CHARS: &'static [char] = &['F', 'I', 'X', 'M', 'E'];
20
21 // Enabled implementation detail is here because it is
22 // irrelevant outside the issues module
23 impl ReportTactic {
24     fn is_enabled(&self) -> bool {
25         *self != ReportTactic::Never
26     }
27 }
28
29 #[derive(Clone, Copy)]
30 enum Seeking {
31     Issue { todo_idx: usize, fixme_idx: usize },
32     Number { issue: Issue, part: NumberPart },
33 }
34
35 #[derive(Clone, Copy)]
36 enum NumberPart {
37     OpenParen,
38     Pound,
39     Number,
40     CloseParen,
41 }
42
43 #[derive(PartialEq, Eq, Debug, Clone, Copy)]
44 pub struct Issue {
45     issue_type: IssueType,
46     // Indicates whether we're looking for issues with missing numbers, or
47     // all issues of this type.
48     missing_number: bool,
49 }
50
51 impl fmt::Display for Issue {
52     fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
53         let msg = match self.issue_type {
54             IssueType::Todo => "TODO",
55             IssueType::Fixme => "FIXME",
56         };
57         let details = if self.missing_number {
58             " without issue number"
59         } else {
60             ""
61         };
62
63         write!(fmt, "{}{}", msg, details)
64     }
65 }
66
67 #[derive(PartialEq, Eq, Debug, Clone, Copy)]
68 enum IssueType {
69     Todo,
70     Fixme,
71 }
72
73 enum IssueClassification {
74     Good,
75     Bad(Issue),
76     None,
77 }
78
79 pub struct BadIssueSeeker {
80     state: Seeking,
81     report_todo: ReportTactic,
82     report_fixme: ReportTactic,
83 }
84
85 impl BadIssueSeeker {
86     pub fn new(report_todo: ReportTactic, report_fixme: ReportTactic) -> BadIssueSeeker {
87         BadIssueSeeker {
88             state: Seeking::Issue {
89                 todo_idx: 0,
90                 fixme_idx: 0,
91             },
92             report_todo: report_todo,
93             report_fixme: report_fixme,
94         }
95     }
96
97     // Check whether or not the current char is conclusive evidence for an
98     // unnumbered TO-DO or FIX-ME.
99     pub fn inspect(&mut self, c: char) -> Option<Issue> {
100         match self.state {
101             Seeking::Issue { todo_idx, fixme_idx } => {
102                 self.state = self.inspect_issue(c, todo_idx, fixme_idx);
103             }
104             Seeking::Number { issue, part } => {
105                 let result = self.inspect_number(c, issue, part);
106
107                 if let IssueClassification::None = result {
108                     return None;
109                 }
110
111                 self.state = Seeking::Issue {
112                     todo_idx: 0,
113                     fixme_idx: 0,
114                 };
115
116                 if let IssueClassification::Bad(issue) = result {
117                     return Some(issue);
118                 }
119             }
120         }
121
122         None
123     }
124
125     fn inspect_issue(&mut self, c: char, mut todo_idx: usize, mut fixme_idx: usize) -> Seeking {
126         // FIXME: Should we also check for lower case characters?
127         if self.report_todo.is_enabled() && c == TO_DO_CHARS[todo_idx] {
128             todo_idx += 1;
129             if todo_idx == TO_DO_CHARS.len() {
130                 return Seeking::Number {
131                            issue: Issue {
132                                issue_type: IssueType::Todo,
133                                missing_number: if let ReportTactic::Unnumbered = self.report_todo {
134                                    true
135                                } else {
136                                    false
137                                },
138                            },
139                            part: NumberPart::OpenParen,
140                        };
141             }
142             fixme_idx = 0;
143         } else if self.report_fixme.is_enabled() && c == FIX_ME_CHARS[fixme_idx] {
144             // Exploit the fact that the character sets of todo and fixme
145             // are disjoint by adding else.
146             fixme_idx += 1;
147             if fixme_idx == FIX_ME_CHARS.len() {
148                 return Seeking::Number {
149                            issue: Issue {
150                                issue_type: IssueType::Fixme,
151                                missing_number: if let ReportTactic::Unnumbered =
152                     self.report_fixme {
153                                    true
154                                } else {
155                                    false
156                                },
157                            },
158                            part: NumberPart::OpenParen,
159                        };
160             }
161             todo_idx = 0;
162         } else {
163             todo_idx = 0;
164             fixme_idx = 0;
165         }
166
167         Seeking::Issue {
168             todo_idx: todo_idx,
169             fixme_idx: fixme_idx,
170         }
171     }
172
173     fn inspect_number(&mut self,
174                       c: char,
175                       issue: Issue,
176                       mut part: NumberPart)
177                       -> IssueClassification {
178         if !issue.missing_number || c == '\n' {
179             return IssueClassification::Bad(issue);
180         } else if c == ')' {
181             return if let NumberPart::CloseParen = part {
182                        IssueClassification::Good
183                    } else {
184                        IssueClassification::Bad(issue)
185                    };
186         }
187
188         match part {
189             NumberPart::OpenParen => {
190                 if c != '(' {
191                     return IssueClassification::Bad(issue);
192                 } else {
193                     part = NumberPart::Pound;
194                 }
195             }
196             NumberPart::Pound => {
197                 if c == '#' {
198                     part = NumberPart::Number;
199                 }
200             }
201             NumberPart::Number => {
202                 if c >= '0' && c <= '9' {
203                     part = NumberPart::CloseParen;
204                 } else {
205                     return IssueClassification::Bad(issue);
206                 }
207             }
208             NumberPart::CloseParen => {}
209         }
210
211         self.state = Seeking::Number {
212             part: part,
213             issue: issue,
214         };
215
216         IssueClassification::None
217     }
218 }
219
220 #[test]
221 fn find_unnumbered_issue() {
222     fn check_fail(text: &str, failing_pos: usize) {
223         let mut seeker = BadIssueSeeker::new(ReportTactic::Unnumbered, ReportTactic::Unnumbered);
224         assert_eq!(Some(failing_pos),
225                    text.chars().position(|c| seeker.inspect(c).is_some()));
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("TODO(@maintainer, #1222, hello)\n",
254                          ReportTactic::Always,
255                          ReportTactic::Never));
256
257     assert!(!is_bad_issue("TODO: no number\n",
258                           ReportTactic::Never,
259                           ReportTactic::Always));
260
261     assert!(is_bad_issue("This is a FIXME(#1)\n",
262                          ReportTactic::Never,
263                          ReportTactic::Always));
264
265     assert!(!is_bad_issue("bad FIXME\n", ReportTactic::Always, ReportTactic::Never));
266 }
267
268 #[test]
269 fn issue_type() {
270     let mut seeker = BadIssueSeeker::new(ReportTactic::Always, ReportTactic::Never);
271     let expected = Some(Issue {
272                             issue_type: IssueType::Todo,
273                             missing_number: false,
274                         });
275
276     assert_eq!(expected,
277                "TODO(#100): more awesomeness"
278                    .chars()
279                    .map(|c| seeker.inspect(c))
280                    .find(Option::is_some)
281                    .unwrap());
282
283     let mut seeker = BadIssueSeeker::new(ReportTactic::Never, ReportTactic::Unnumbered);
284     let expected = Some(Issue {
285                             issue_type: IssueType::Fixme,
286                             missing_number: true,
287                         });
288
289     assert_eq!(expected,
290                "Test. FIXME: bad, bad, not good"
291                    .chars()
292                    .map(|c| seeker.inspect(c))
293                    .find(Option::is_some)
294                    .unwrap());
295 }