1 // Objects for seeking through a char stream for occurrences of TODO and FIXME.
2 // Depending on the loaded configuration, may also check that these have an
3 // associated issue number.
7 use crate::config::ReportTactic;
9 const TO_DO_CHARS: &[char] = &['t', 'o', 'd', 'o'];
10 const FIX_ME_CHARS: &[char] = &['f', 'i', 'x', 'm', 'e'];
12 // Enabled implementation detail is here because it is
13 // irrelevant outside the issues module
14 fn is_enabled(report_tactic: ReportTactic) -> bool {
15 report_tactic != ReportTactic::Never
18 #[derive(Clone, Copy)]
20 Issue { todo_idx: usize, fixme_idx: usize },
21 Number { issue: Issue, part: NumberPart },
24 #[derive(Clone, Copy)]
32 #[derive(PartialEq, Eq, Debug, Clone, Copy)]
34 issue_type: IssueType,
35 // Indicates whether we're looking for issues with missing numbers, or
36 // all issues of this type.
40 impl fmt::Display for Issue {
41 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
42 let msg = match self.issue_type {
43 IssueType::Todo => "TODO",
44 IssueType::Fixme => "FIXME",
46 let details = if self.missing_number {
47 " without issue number"
52 write!(fmt, "{}{}", msg, details)
56 #[derive(PartialEq, Eq, Debug, Clone, Copy)]
62 enum IssueClassification {
68 pub(crate) struct BadIssueSeeker {
70 report_todo: ReportTactic,
71 report_fixme: ReportTactic,
75 pub(crate) fn new(report_todo: ReportTactic, report_fixme: ReportTactic) -> BadIssueSeeker {
77 state: Seeking::Issue {
86 pub(crate) fn is_disabled(&self) -> bool {
87 !is_enabled(self.report_todo) && !is_enabled(self.report_fixme)
90 // Check whether or not the current char is conclusive evidence for an
91 // unnumbered TO-DO or FIX-ME.
92 pub(crate) fn inspect(&mut self, c: char) -> Option<Issue> {
98 self.state = self.inspect_issue(c, todo_idx, fixme_idx);
100 Seeking::Number { issue, part } => {
101 let result = self.inspect_number(c, issue, part);
103 if let IssueClassification::None = result {
107 self.state = Seeking::Issue {
112 if let IssueClassification::Bad(issue) = result {
121 fn inspect_issue(&mut self, c: char, mut todo_idx: usize, mut fixme_idx: usize) -> Seeking {
122 if let Some(lower_case_c) = c.to_lowercase().next() {
123 if is_enabled(self.report_todo) && lower_case_c == TO_DO_CHARS[todo_idx] {
125 if todo_idx == TO_DO_CHARS.len() {
126 return Seeking::Number {
128 issue_type: IssueType::Todo,
129 missing_number: matches!(self.report_todo, ReportTactic::Unnumbered),
131 part: NumberPart::OpenParen,
135 } else if is_enabled(self.report_fixme) && lower_case_c == FIX_ME_CHARS[fixme_idx] {
136 // Exploit the fact that the character sets of todo and fixme
137 // are disjoint by adding else.
139 if fixme_idx == FIX_ME_CHARS.len() {
140 return Seeking::Number {
142 issue_type: IssueType::Fixme,
143 missing_number: matches!(self.report_fixme, ReportTactic::Unnumbered),
145 part: NumberPart::OpenParen,
165 mut part: NumberPart,
166 ) -> IssueClassification {
167 if !issue.missing_number || c == '\n' {
168 return IssueClassification::Bad(issue);
170 return if let NumberPart::CloseParen = part {
171 IssueClassification::Good
173 IssueClassification::Bad(issue)
178 NumberPart::OpenParen => {
180 return IssueClassification::Bad(issue);
182 part = NumberPart::Pound;
185 NumberPart::Pound => {
187 part = NumberPart::Number;
190 NumberPart::Number => {
191 if ('0'..='9').contains(&c) {
192 part = NumberPart::CloseParen;
194 return IssueClassification::Bad(issue);
197 NumberPart::CloseParen => {}
200 self.state = Seeking::Number { part, issue };
202 IssueClassification::None
207 fn find_unnumbered_issue() {
208 fn check_fail(text: &str, failing_pos: usize) {
209 let mut seeker = BadIssueSeeker::new(ReportTactic::Unnumbered, ReportTactic::Unnumbered);
212 text.find(|c| seeker.inspect(c).is_some())
216 fn check_pass(text: &str) {
217 let mut seeker = BadIssueSeeker::new(ReportTactic::Unnumbered, ReportTactic::Unnumbered);
218 assert_eq!(None, text.find(|c| seeker.inspect(c).is_some()));
221 check_fail("TODO\n", 4);
222 check_pass(" TO FIX DOME\n");
223 check_fail(" \n FIXME\n", 8);
224 check_fail("FIXME(\n", 6);
225 check_fail("FIXME(#\n", 7);
226 check_fail("FIXME(#1\n", 8);
227 check_fail("FIXME(#)1\n", 7);
228 check_pass("FIXME(#1222)\n");
229 check_fail("FIXME(#12\n22)\n", 9);
230 check_pass("FIXME(@maintainer, #1222, hello)\n");
231 check_fail("TODO(#22) FIXME\n", 15);
236 fn is_bad_issue(text: &str, report_todo: ReportTactic, report_fixme: ReportTactic) -> bool {
237 let mut seeker = BadIssueSeeker::new(report_todo, report_fixme);
238 text.chars().any(|c| seeker.inspect(c).is_some())
241 assert!(is_bad_issue(
242 "TODO(@maintainer, #1222, hello)\n",
243 ReportTactic::Always,
247 assert!(!is_bad_issue(
250 ReportTactic::Always,
253 assert!(!is_bad_issue(
254 "Todo: mixed case\n",
256 ReportTactic::Always,
259 assert!(is_bad_issue(
260 "This is a FIXME(#1)\n",
262 ReportTactic::Always,
265 assert!(is_bad_issue(
266 "This is a FixMe(#1) mixed case\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)