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.
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.
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.
17 static TO_DO_CHARS: &'static [char] = &['T', 'O', 'D', 'O'];
18 static FIX_ME_CHARS: &'static [char] = &['F', 'I', 'X', 'M', 'E'];
20 #[derive(Clone, Copy)]
21 pub enum ReportTactic {
28 fn is_enabled(&self) -> bool {
30 ReportTactic::Always => true,
31 ReportTactic::Unnumbered => true,
32 ReportTactic::Never => false
37 impl_enum_decodable!(ReportTactic, Always, Unnumbered, Never);
39 #[derive(Clone, Copy)]
51 #[derive(Clone, Copy)]
59 #[derive(PartialEq, Eq, Debug, Clone, Copy)]
61 issue_type: IssueType,
62 // Indicates whether we're looking for issues with missing numbers, or
63 // all issues of this type.
67 impl fmt::Display for Issue {
68 fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
69 let msg = match self.issue_type {
70 IssueType::Todo => "TODO",
71 IssueType::Fixme => "FIXME",
73 let details = if self.missing_number { " without issue number" } else { "" };
75 write!(fmt, "{}{}", msg, details)
79 #[derive(PartialEq, Eq, Debug, Clone, Copy)]
85 enum IssueClassification {
91 pub struct BadIssueSeeker {
93 report_todo: ReportTactic,
94 report_fixme: ReportTactic,
98 pub fn new(report_todo: ReportTactic, report_fixme: ReportTactic) -> BadIssueSeeker {
100 state: Seeking::Issue { todo_idx: 0, fixme_idx: 0 },
101 report_todo: report_todo,
102 report_fixme: report_fixme,
106 // Check whether or not the current char is conclusive evidence for an
107 // unnumbered TO-DO or FIX-ME.
108 pub fn inspect(&mut self, c: char) -> Option<Issue> {
110 Seeking::Issue { todo_idx, fixme_idx } => {
111 self.state = self.inspect_issue(c, todo_idx, fixme_idx);
113 Seeking::Number { issue, part } => {
114 let result = self.inspect_number(c, issue, part);
116 if let IssueClassification::None = result {
120 self.state = Seeking::Issue { todo_idx: 0, fixme_idx: 0 };
122 if let IssueClassification::Bad(issue) = result {
131 fn inspect_issue(&mut self, c: char, mut todo_idx: usize, mut fixme_idx: usize) -> Seeking {
132 // FIXME: Should we also check for lower case characters?
133 if self.report_todo.is_enabled() && c == TO_DO_CHARS[todo_idx] {
135 if todo_idx == TO_DO_CHARS.len() {
136 return Seeking::Number {
138 issue_type: IssueType::Todo,
139 missing_number: if let ReportTactic::Unnumbered = self.report_todo {
145 part: NumberPart::OpenParen
149 } else if self.report_fixme.is_enabled() && c == FIX_ME_CHARS[fixme_idx] {
150 // Exploit the fact that the character sets of todo and fixme
151 // are disjoint by adding else.
153 if fixme_idx == FIX_ME_CHARS.len() {
154 return Seeking::Number {
156 issue_type: IssueType::Fixme,
157 missing_number: if let ReportTactic::Unnumbered = self.report_fixme {
163 part: NumberPart::OpenParen
172 Seeking::Issue { todo_idx: todo_idx, fixme_idx: fixme_idx }
175 fn inspect_number(&mut self,
178 mut part: NumberPart)
179 -> IssueClassification
181 if ! issue.missing_number || c == '\n' {
182 return IssueClassification::Bad(issue);
184 return if let NumberPart::CloseParen = part {
185 IssueClassification::Good
187 IssueClassification::Bad(issue)
192 NumberPart::OpenParen => {
194 return IssueClassification::Bad(issue);
196 part = NumberPart::Pound;
199 NumberPart::Pound => {
201 part = NumberPart::Number;
204 NumberPart::Number => {
205 if c >= '0' && c <= '9' {
206 part = NumberPart::CloseParen;
208 return IssueClassification::Bad(issue);
211 NumberPart::CloseParen => {}
214 self.state = Seeking::Number {
219 IssueClassification::None
224 fn find_unnumbered_issue() {
225 fn check_fail(text: &str, failing_pos: usize) {
226 println!("{:?}", text);
227 let mut seeker = BadIssueSeeker::new(ReportTactic::Unnumbered, ReportTactic::Unnumbered);
228 assert_eq!(Some(failing_pos), text.chars().position(|c| seeker.inspect(c).is_some()));
231 fn check_pass(text: &str) {
232 let mut seeker = BadIssueSeeker::new(ReportTactic::Unnumbered, ReportTactic::Unnumbered);
233 assert_eq!(None, text.chars().position(|c| seeker.inspect(c).is_some()));
236 check_fail("TODO\n", 4);
237 check_pass(" TO FIX DOME\n");
238 check_fail(" \n FIXME\n", 8);
239 check_fail("FIXME(\n", 6);
240 check_fail("FIXME(#\n", 7);
241 check_fail("FIXME(#1\n", 8);
242 check_fail("FIXME(#)1\n", 7);
243 check_pass("FIXME(#1222)\n");
244 check_fail("FIXME(#12\n22)\n", 9);
245 check_pass("FIXME(@maintainer, #1222, hello)\n");
246 check_fail("TODO(#22) FIXME\n", 15);
251 fn is_bad_issue(text: &str, report_todo: ReportTactic, report_fixme: ReportTactic) -> bool {
252 let mut seeker = BadIssueSeeker::new(report_todo, report_fixme);
253 text.chars().any(|c| seeker.inspect(c).is_some())
256 assert!(is_bad_issue("TODO(@maintainer, #1222, hello)\n",
257 ReportTactic::Always,
258 ReportTactic::Never));
260 assert!(! is_bad_issue("TODO: no number\n",
262 ReportTactic::Always));
264 assert!(is_bad_issue("This is a FIXME(#1)\n",
266 ReportTactic::Always));
268 assert!(! is_bad_issue("bad FIXME\n",
269 ReportTactic::Always,
270 ReportTactic::Never));
275 let mut seeker = BadIssueSeeker::new(ReportTactic::Always, ReportTactic::Never);
276 let expected = Some(Issue {
277 issue_type: IssueType::Todo,
278 missing_number: false
282 "TODO(#100): more awesomeness".chars()
283 .map(|c| seeker.inspect(c))
284 .find(Option::is_some)
287 let mut seeker = BadIssueSeeker::new(ReportTactic::Never, ReportTactic::Unnumbered);
288 let expected = Some(Issue {
289 issue_type: IssueType::Fixme,
294 "Test. FIXME: bad, bad, not good".chars()
295 .map(|c| seeker.inspect(c))
296 .find(Option::is_some)