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 occurrences of TODO and FIXME.
12 // Depending on the loaded configuration, may also check that these have an
13 // associated issue number.
17 use config::ReportTactic;
19 const TO_DO_CHARS: &[char] = &['t', 'o', 'd', 'o'];
20 const FIX_ME_CHARS: &[char] = &['f', 'i', 'x', 'm', 'e'];
22 // Enabled implementation detail is here because it is
23 // irrelevant outside the issues module
24 fn is_enabled(report_tactic: ReportTactic) -> bool {
25 report_tactic != ReportTactic::Never
28 #[derive(Clone, Copy)]
30 Issue { todo_idx: usize, fixme_idx: usize },
31 Number { issue: Issue, part: NumberPart },
34 #[derive(Clone, Copy)]
42 #[derive(PartialEq, Eq, Debug, Clone, Copy)]
44 issue_type: IssueType,
45 // Indicates whether we're looking for issues with missing numbers, or
46 // all issues of this type.
50 impl fmt::Display for Issue {
51 fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
52 let msg = match self.issue_type {
53 IssueType::Todo => "TODO",
54 IssueType::Fixme => "FIXME",
56 let details = if self.missing_number {
57 " without issue number"
62 write!(fmt, "{}{}", msg, details)
66 #[derive(PartialEq, Eq, Debug, Clone, Copy)]
72 enum IssueClassification {
78 pub struct BadIssueSeeker {
80 report_todo: ReportTactic,
81 report_fixme: ReportTactic,
85 pub fn new(report_todo: ReportTactic, report_fixme: ReportTactic) -> BadIssueSeeker {
87 state: Seeking::Issue {
96 pub fn is_disabled(&self) -> bool {
97 !is_enabled(self.report_todo) && !is_enabled(self.report_fixme)
100 // Check whether or not the current char is conclusive evidence for an
101 // unnumbered TO-DO or FIX-ME.
102 pub fn inspect(&mut self, c: char) -> Option<Issue> {
108 self.state = self.inspect_issue(c, todo_idx, fixme_idx);
110 Seeking::Number { issue, part } => {
111 let result = self.inspect_number(c, issue, part);
113 if let IssueClassification::None = result {
117 self.state = Seeking::Issue {
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 if let Some(lower_case_c) = c.to_lowercase().next() {
133 if is_enabled(self.report_todo) && lower_case_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 is_enabled(self.report_fixme) && lower_case_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,
183 mut part: NumberPart,
184 ) -> IssueClassification {
185 if !issue.missing_number || c == '\n' {
186 return IssueClassification::Bad(issue);
188 return if let NumberPart::CloseParen = part {
189 IssueClassification::Good
191 IssueClassification::Bad(issue)
196 NumberPart::OpenParen => {
198 return IssueClassification::Bad(issue);
200 part = NumberPart::Pound;
203 NumberPart::Pound => {
205 part = NumberPart::Number;
208 NumberPart::Number => {
209 if c >= '0' && c <= '9' {
210 part = NumberPart::CloseParen;
212 return IssueClassification::Bad(issue);
215 NumberPart::CloseParen => {}
218 self.state = Seeking::Number { part, issue };
220 IssueClassification::None
225 fn find_unnumbered_issue() {
226 fn check_fail(text: &str, failing_pos: usize) {
227 let mut seeker = BadIssueSeeker::new(ReportTactic::Unnumbered, ReportTactic::Unnumbered);
230 text.find(|c| seeker.inspect(c).is_some())
234 fn check_pass(text: &str) {
235 let mut seeker = BadIssueSeeker::new(ReportTactic::Unnumbered, ReportTactic::Unnumbered);
236 assert_eq!(None, text.find(|c| seeker.inspect(c).is_some()));
239 check_fail("TODO\n", 4);
240 check_pass(" TO FIX DOME\n");
241 check_fail(" \n FIXME\n", 8);
242 check_fail("FIXME(\n", 6);
243 check_fail("FIXME(#\n", 7);
244 check_fail("FIXME(#1\n", 8);
245 check_fail("FIXME(#)1\n", 7);
246 check_pass("FIXME(#1222)\n");
247 check_fail("FIXME(#12\n22)\n", 9);
248 check_pass("FIXME(@maintainer, #1222, hello)\n");
249 check_fail("TODO(#22) FIXME\n", 15);
254 fn is_bad_issue(text: &str, report_todo: ReportTactic, report_fixme: ReportTactic) -> bool {
255 let mut seeker = BadIssueSeeker::new(report_todo, report_fixme);
256 text.chars().any(|c| seeker.inspect(c).is_some())
259 assert!(is_bad_issue(
260 "TODO(@maintainer, #1222, hello)\n",
261 ReportTactic::Always,
265 assert!(!is_bad_issue(
268 ReportTactic::Always,
271 assert!(!is_bad_issue(
272 "Todo: mixed case\n",
274 ReportTactic::Always,
277 assert!(is_bad_issue(
278 "This is a FIXME(#1)\n",
280 ReportTactic::Always,
283 assert!(is_bad_issue(
284 "This is a FixMe(#1) mixed case\n",
286 ReportTactic::Always,
289 assert!(!is_bad_issue(
291 ReportTactic::Always,
298 let mut seeker = BadIssueSeeker::new(ReportTactic::Always, ReportTactic::Never);
299 let expected = Some(Issue {
300 issue_type: IssueType::Todo,
301 missing_number: false,
306 "TODO(#100): more awesomeness"
308 .map(|c| seeker.inspect(c))
309 .find(Option::is_some)
313 let mut seeker = BadIssueSeeker::new(ReportTactic::Never, ReportTactic::Unnumbered);
314 let expected = Some(Issue {
315 issue_type: IssueType::Fixme,
316 missing_number: true,
321 "Test. FIXME: bad, bad, not good"
323 .map(|c| seeker.inspect(c))
324 .find(Option::is_some)