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 pub use config::ReportTactic;
19 const TO_DO_CHARS: &'static [char] = &['T', 'O', 'D', 'O'];
20 const FIX_ME_CHARS: &'static [char] = &['F', 'I', 'X', 'M', 'E'];
22 // Enabled implementation detail is here because it is
23 // irrelevant outside the issues module
25 fn is_enabled(&self) -> bool {
26 *self != ReportTactic::Never
30 #[derive(Clone, Copy)]
32 Issue { todo_idx: usize, fixme_idx: usize },
33 Number { issue: Issue, part: NumberPart },
36 #[derive(Clone, Copy)]
44 #[derive(PartialEq, Eq, Debug, Clone, Copy)]
46 issue_type: IssueType,
47 // Indicates whether we're looking for issues with missing numbers, or
48 // all issues of this type.
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",
58 let details = if self.missing_number {
59 " without issue number"
64 write!(fmt, "{}{}", msg, details)
68 #[derive(PartialEq, Eq, Debug, Clone, Copy)]
74 enum IssueClassification {
80 pub struct BadIssueSeeker {
82 report_todo: ReportTactic,
83 report_fixme: ReportTactic,
87 pub fn new(report_todo: ReportTactic, report_fixme: ReportTactic) -> BadIssueSeeker {
89 state: Seeking::Issue {
93 report_todo: report_todo,
94 report_fixme: report_fixme,
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> {
106 self.state = self.inspect_issue(c, todo_idx, fixme_idx);
108 Seeking::Number { issue, part } => {
109 let result = self.inspect_number(c, issue, part);
111 if let IssueClassification::None = result {
115 self.state = Seeking::Issue {
120 if let IssueClassification::Bad(issue) = result {
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] {
133 if todo_idx == TO_DO_CHARS.len() {
134 return Seeking::Number {
136 issue_type: IssueType::Todo,
137 missing_number: if let ReportTactic::Unnumbered = self.report_todo {
143 part: NumberPart::OpenParen,
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.
151 if fixme_idx == FIX_ME_CHARS.len() {
152 return Seeking::Number {
154 issue_type: IssueType::Fixme,
155 missing_number: if let ReportTactic::Unnumbered = self.report_fixme {
161 part: NumberPart::OpenParen,
172 fixme_idx: fixme_idx,
180 mut part: NumberPart,
181 ) -> IssueClassification {
182 if !issue.missing_number || c == '\n' {
183 return IssueClassification::Bad(issue);
185 return if let NumberPart::CloseParen = part {
186 IssueClassification::Good
188 IssueClassification::Bad(issue)
193 NumberPart::OpenParen => if c != '(' {
194 return IssueClassification::Bad(issue);
196 part = NumberPart::Pound;
198 NumberPart::Pound => if c == '#' {
199 part = NumberPart::Number;
201 NumberPart::Number => if c >= '0' && c <= '9' {
202 part = NumberPart::CloseParen;
204 return IssueClassification::Bad(issue);
206 NumberPart::CloseParen => {}
209 self.state = Seeking::Number {
214 IssueClassification::None
219 fn find_unnumbered_issue() {
220 fn check_fail(text: &str, failing_pos: usize) {
221 let mut seeker = BadIssueSeeker::new(ReportTactic::Unnumbered, ReportTactic::Unnumbered);
224 text.chars().position(|c| seeker.inspect(c).is_some())
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()));
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);
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())
253 assert!(is_bad_issue(
254 "TODO(@maintainer, #1222, hello)\n",
255 ReportTactic::Always,
259 assert!(!is_bad_issue(
262 ReportTactic::Always,
265 assert!(is_bad_issue(
266 "This is a FIXME(#1)\n",
268 ReportTactic::Always,
271 assert!(!is_bad_issue(
273 ReportTactic::Always,
280 let mut seeker = BadIssueSeeker::new(ReportTactic::Always, ReportTactic::Never);
281 let expected = Some(Issue {
282 issue_type: IssueType::Todo,
283 missing_number: false,
288 "TODO(#100): more awesomeness"
290 .map(|c| seeker.inspect(c))
291 .find(Option::is_some)
295 let mut seeker = BadIssueSeeker::new(ReportTactic::Never, ReportTactic::Unnumbered);
296 let expected = Some(Issue {
297 issue_type: IssueType::Fixme,
298 missing_number: true,
303 "Test. FIXME: bad, bad, not good"
305 .map(|c| seeker.inspect(c))
306 .find(Option::is_some)