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 pub 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
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 {
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 if let Some(lower_case_c) = c.to_lowercase().next() {
131 if self.report_todo.is_enabled() && lower_case_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() && lower_case_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,
181 mut part: NumberPart,
182 ) -> IssueClassification {
183 if !issue.missing_number || c == '\n' {
184 return IssueClassification::Bad(issue);
186 return if let NumberPart::CloseParen = part {
187 IssueClassification::Good
189 IssueClassification::Bad(issue)
194 NumberPart::OpenParen => {
196 return IssueClassification::Bad(issue);
198 part = NumberPart::Pound;
201 NumberPart::Pound => {
203 part = NumberPart::Number;
206 NumberPart::Number => {
207 if c >= '0' && c <= '9' {
208 part = NumberPart::CloseParen;
210 return IssueClassification::Bad(issue);
213 NumberPart::CloseParen => {}
216 self.state = Seeking::Number { part, issue };
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);
228 text.chars().position(|c| seeker.inspect(c).is_some())
232 fn check_pass(text: &str) {
233 let mut seeker = BadIssueSeeker::new(ReportTactic::Unnumbered, ReportTactic::Unnumbered);
234 assert_eq!(None, text.chars().position(|c| seeker.inspect(c).is_some()));
237 check_fail("TODO\n", 4);
238 check_pass(" TO FIX DOME\n");
239 check_fail(" \n FIXME\n", 8);
240 check_fail("FIXME(\n", 6);
241 check_fail("FIXME(#\n", 7);
242 check_fail("FIXME(#1\n", 8);
243 check_fail("FIXME(#)1\n", 7);
244 check_pass("FIXME(#1222)\n");
245 check_fail("FIXME(#12\n22)\n", 9);
246 check_pass("FIXME(@maintainer, #1222, hello)\n");
247 check_fail("TODO(#22) FIXME\n", 15);
252 fn is_bad_issue(text: &str, report_todo: ReportTactic, report_fixme: ReportTactic) -> bool {
253 let mut seeker = BadIssueSeeker::new(report_todo, report_fixme);
254 text.chars().any(|c| seeker.inspect(c).is_some())
257 assert!(is_bad_issue(
258 "TODO(@maintainer, #1222, hello)\n",
259 ReportTactic::Always,
263 assert!(!is_bad_issue(
266 ReportTactic::Always,
269 assert!(!is_bad_issue(
270 "Todo: mixed case\n",
272 ReportTactic::Always,
275 assert!(is_bad_issue(
276 "This is a FIXME(#1)\n",
278 ReportTactic::Always,
281 assert!(is_bad_issue(
282 "This is a FixMe(#1) mixed case\n",
284 ReportTactic::Always,
287 assert!(!is_bad_issue(
289 ReportTactic::Always,
296 let mut seeker = BadIssueSeeker::new(ReportTactic::Always, ReportTactic::Never);
297 let expected = Some(Issue {
298 issue_type: IssueType::Todo,
299 missing_number: false,
304 "TODO(#100): more awesomeness"
306 .map(|c| seeker.inspect(c))
307 .find(Option::is_some)
311 let mut seeker = BadIssueSeeker::new(ReportTactic::Never, ReportTactic::Unnumbered);
312 let expected = Some(Issue {
313 issue_type: IssueType::Fixme,
314 missing_number: true,
319 "Test. FIXME: bad, bad, not good"
321 .map(|c| seeker.inspect(c))
322 .find(Option::is_some)