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 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> {
103 if self.is_disabled() {
112 self.state = self.inspect_issue(c, todo_idx, fixme_idx);
114 Seeking::Number { issue, part } => {
115 let result = self.inspect_number(c, issue, part);
117 if let IssueClassification::None = result {
121 self.state = Seeking::Issue {
126 if let IssueClassification::Bad(issue) = result {
135 fn inspect_issue(&mut self, c: char, mut todo_idx: usize, mut fixme_idx: usize) -> Seeking {
136 if let Some(lower_case_c) = c.to_lowercase().next() {
137 if is_enabled(self.report_todo) && lower_case_c == TO_DO_CHARS[todo_idx] {
139 if todo_idx == TO_DO_CHARS.len() {
140 return Seeking::Number {
142 issue_type: IssueType::Todo,
143 missing_number: if let ReportTactic::Unnumbered = self.report_todo {
149 part: NumberPart::OpenParen,
153 } else if is_enabled(self.report_fixme) && lower_case_c == FIX_ME_CHARS[fixme_idx] {
154 // Exploit the fact that the character sets of todo and fixme
155 // are disjoint by adding else.
157 if fixme_idx == FIX_ME_CHARS.len() {
158 return Seeking::Number {
160 issue_type: IssueType::Fixme,
161 missing_number: if let ReportTactic::Unnumbered = self.report_fixme {
167 part: NumberPart::OpenParen,
187 mut part: NumberPart,
188 ) -> IssueClassification {
189 if !issue.missing_number || c == '\n' {
190 return IssueClassification::Bad(issue);
192 return if let NumberPart::CloseParen = part {
193 IssueClassification::Good
195 IssueClassification::Bad(issue)
200 NumberPart::OpenParen => {
202 return IssueClassification::Bad(issue);
204 part = NumberPart::Pound;
207 NumberPart::Pound => {
209 part = NumberPart::Number;
212 NumberPart::Number => {
213 if c >= '0' && c <= '9' {
214 part = NumberPart::CloseParen;
216 return IssueClassification::Bad(issue);
219 NumberPart::CloseParen => {}
222 self.state = Seeking::Number { part, issue };
224 IssueClassification::None
229 fn find_unnumbered_issue() {
230 fn check_fail(text: &str, failing_pos: usize) {
231 let mut seeker = BadIssueSeeker::new(ReportTactic::Unnumbered, ReportTactic::Unnumbered);
234 text.chars().position(|c| seeker.inspect(c).is_some())
238 fn check_pass(text: &str) {
239 let mut seeker = BadIssueSeeker::new(ReportTactic::Unnumbered, ReportTactic::Unnumbered);
240 assert_eq!(None, text.chars().position(|c| seeker.inspect(c).is_some()));
243 check_fail("TODO\n", 4);
244 check_pass(" TO FIX DOME\n");
245 check_fail(" \n FIXME\n", 8);
246 check_fail("FIXME(\n", 6);
247 check_fail("FIXME(#\n", 7);
248 check_fail("FIXME(#1\n", 8);
249 check_fail("FIXME(#)1\n", 7);
250 check_pass("FIXME(#1222)\n");
251 check_fail("FIXME(#12\n22)\n", 9);
252 check_pass("FIXME(@maintainer, #1222, hello)\n");
253 check_fail("TODO(#22) FIXME\n", 15);
258 fn is_bad_issue(text: &str, report_todo: ReportTactic, report_fixme: ReportTactic) -> bool {
259 let mut seeker = BadIssueSeeker::new(report_todo, report_fixme);
260 text.chars().any(|c| seeker.inspect(c).is_some())
263 assert!(is_bad_issue(
264 "TODO(@maintainer, #1222, hello)\n",
265 ReportTactic::Always,
269 assert!(!is_bad_issue(
272 ReportTactic::Always,
275 assert!(!is_bad_issue(
276 "Todo: mixed case\n",
278 ReportTactic::Always,
281 assert!(is_bad_issue(
282 "This is a FIXME(#1)\n",
284 ReportTactic::Always,
287 assert!(is_bad_issue(
288 "This is a FixMe(#1) mixed case\n",
290 ReportTactic::Always,
293 assert!(!is_bad_issue(
295 ReportTactic::Always,
302 let mut seeker = BadIssueSeeker::new(ReportTactic::Always, ReportTactic::Never);
303 let expected = Some(Issue {
304 issue_type: IssueType::Todo,
305 missing_number: false,
310 "TODO(#100): more awesomeness"
312 .map(|c| seeker.inspect(c))
313 .find(Option::is_some)
317 let mut seeker = BadIssueSeeker::new(ReportTactic::Never, ReportTactic::Unnumbered);
318 let expected = Some(Issue {
319 issue_type: IssueType::Fixme,
320 missing_number: true,
325 "Test. FIXME: bad, bad, not good"
327 .map(|c| seeker.inspect(c))
328 .find(Option::is_some)