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