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.
16 pub use config::ReportTactic;
18 const TO_DO_CHARS: &'static [char] = &['T', 'O', 'D', 'O'];
19 const FIX_ME_CHARS: &'static [char] = &['F', 'I', 'X', 'M', 'E'];
21 // Enabled implementation detail is here because it is
22 // irrelevant outside the issues module
24 fn is_enabled(&self) -> bool {
25 *self != ReportTactic::Never
29 #[derive(Clone, Copy)]
31 Issue { todo_idx: usize, fixme_idx: usize },
32 Number { issue: Issue, part: NumberPart },
35 #[derive(Clone, Copy)]
43 #[derive(PartialEq, Eq, Debug, Clone, Copy)]
45 issue_type: IssueType,
46 // Indicates whether we're looking for issues with missing numbers, or
47 // all issues of this type.
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",
57 let details = if self.missing_number {
58 " without issue number"
63 write!(fmt, "{}{}", msg, details)
67 #[derive(PartialEq, Eq, Debug, Clone, Copy)]
73 enum IssueClassification {
79 pub struct BadIssueSeeker {
81 report_todo: ReportTactic,
82 report_fixme: ReportTactic,
86 pub fn new(report_todo: ReportTactic, report_fixme: ReportTactic) -> BadIssueSeeker {
88 state: Seeking::Issue {
92 report_todo: report_todo,
93 report_fixme: report_fixme,
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> {
105 self.state = self.inspect_issue(c, todo_idx, fixme_idx);
107 Seeking::Number { issue, part } => {
108 let result = self.inspect_number(c, issue, part);
110 if let IssueClassification::None = result {
114 self.state = Seeking::Issue {
119 if let IssueClassification::Bad(issue) = result {
128 fn inspect_issue(&mut self, c: char, mut todo_idx: usize, mut fixme_idx: usize) -> Seeking {
129 // FIXME: Should we also check for lower case characters?
130 if self.report_todo.is_enabled() && c == TO_DO_CHARS[todo_idx] {
132 if todo_idx == TO_DO_CHARS.len() {
133 return Seeking::Number {
135 issue_type: IssueType::Todo,
136 missing_number: if let ReportTactic::Unnumbered = self.report_todo {
142 part: NumberPart::OpenParen,
146 } else if self.report_fixme.is_enabled() && c == FIX_ME_CHARS[fixme_idx] {
147 // Exploit the fact that the character sets of todo and fixme
148 // are disjoint by adding else.
150 if fixme_idx == FIX_ME_CHARS.len() {
151 return Seeking::Number {
153 issue_type: IssueType::Fixme,
154 missing_number: if let ReportTactic::Unnumbered = self.report_fixme {
160 part: NumberPart::OpenParen,
171 fixme_idx: fixme_idx,
175 fn inspect_number(&mut self,
178 mut part: NumberPart)
179 -> IssueClassification {
180 if !issue.missing_number || c == '\n' {
181 return IssueClassification::Bad(issue);
183 return if let NumberPart::CloseParen = part {
184 IssueClassification::Good
186 IssueClassification::Bad(issue)
191 NumberPart::OpenParen => {
193 return IssueClassification::Bad(issue);
195 part = NumberPart::Pound;
198 NumberPart::Pound => {
200 part = NumberPart::Number;
203 NumberPart::Number => {
204 if c >= '0' && c <= '9' {
205 part = NumberPart::CloseParen;
207 return IssueClassification::Bad(issue);
210 NumberPart::CloseParen => {}
213 self.state = Seeking::Number {
218 IssueClassification::None
223 fn find_unnumbered_issue() {
224 fn check_fail(text: &str, failing_pos: usize) {
225 let mut seeker = BadIssueSeeker::new(ReportTactic::Unnumbered, ReportTactic::Unnumbered);
226 assert_eq!(Some(failing_pos),
227 text.chars().position(|c| seeker.inspect(c).is_some()));
230 fn check_pass(text: &str) {
231 let mut seeker = BadIssueSeeker::new(ReportTactic::Unnumbered, ReportTactic::Unnumbered);
232 assert_eq!(None, text.chars().position(|c| seeker.inspect(c).is_some()));
235 check_fail("TODO\n", 4);
236 check_pass(" TO FIX DOME\n");
237 check_fail(" \n FIXME\n", 8);
238 check_fail("FIXME(\n", 6);
239 check_fail("FIXME(#\n", 7);
240 check_fail("FIXME(#1\n", 8);
241 check_fail("FIXME(#)1\n", 7);
242 check_pass("FIXME(#1222)\n");
243 check_fail("FIXME(#12\n22)\n", 9);
244 check_pass("FIXME(@maintainer, #1222, hello)\n");
245 check_fail("TODO(#22) FIXME\n", 15);
250 fn is_bad_issue(text: &str, report_todo: ReportTactic, report_fixme: ReportTactic) -> bool {
251 let mut seeker = BadIssueSeeker::new(report_todo, report_fixme);
252 text.chars().any(|c| seeker.inspect(c).is_some())
255 assert!(is_bad_issue("TODO(@maintainer, #1222, hello)\n",
256 ReportTactic::Always,
257 ReportTactic::Never));
259 assert!(!is_bad_issue("TODO: no number\n",
261 ReportTactic::Always));
263 assert!(is_bad_issue("This is a FIXME(#1)\n",
265 ReportTactic::Always));
267 assert!(!is_bad_issue("bad FIXME\n", ReportTactic::Always, ReportTactic::Never));
272 let mut seeker = BadIssueSeeker::new(ReportTactic::Always, ReportTactic::Never);
273 let expected = Some(Issue {
274 issue_type: IssueType::Todo,
275 missing_number: false,
279 "TODO(#100): more awesomeness"
281 .map(|c| seeker.inspect(c))
282 .find(Option::is_some)
285 let mut seeker = BadIssueSeeker::new(ReportTactic::Never, ReportTactic::Unnumbered);
286 let expected = Some(Issue {
287 issue_type: IssueType::Fixme,
288 missing_number: true,
292 "Test. FIXME: bad, bad, not good"
294 .map(|c| seeker.inspect(c))
295 .find(Option::is_some)