TodoComment

Since Checkstyle 3.0

Description

Checks for TODO: comments. Actually it is a generic pattern matcher on Java comments. To check for other patterns in Java comments, set the format property.

Notes

Using TODO: comments is a great way to keep track of tasks that need to be done. Having them reported by Checkstyle makes it very hard to forget about them.

Properties

name description type default value since
format Specify pattern to match comments against. Pattern TODO: 3.0

Examples

To configure the check:


<module name="Checker">
  <module name="TreeWalker">
    <module name="TodoComment"/>
  </module>
</module>

Example:


public class Example1 {
  int i;
  int x;
  public void test() {
    // violation below 'matches to-do format'
    i++;   // TODO: do differently in future
    i++;   // todo: do differently in future
    i=i/x; // FIXME: handle x = 0 case
    i=i/x; // FIX :  handle x = 0 case
  }
}

To configure the check for comments that contain TODO and FIXME, case-insensitive:


<module name="Checker">
  <module name="TreeWalker">
    <module name="TodoComment">
      <property name="format" value="(?i)(TODO)|(FIXME)"/>
    </module>
  </module>
</module>

Example:


public class Example2 {
  int i;
  int x;
  public void test() {
    // violation below 'matches to-do format'
    i++;   // TODO: do differently in future
    // violation below 'matches to-do format'
    i++;   // todo: do differently in future
    // violation below 'matches to-do format'
    i=i/x; // FIXME: handle x = 0 case
    i=i/x; // FIX :  handle x = 0 case
  }
}

To configure the check to detect box comments (Google Java Style Guide §4.8.6.1):


<module name="Checker">
  <module name="TreeWalker">
    <module name="TodoComment">
      <property name="id" value="BoxComments"/>
      <property name="format" value="^\s*([*=#])\1{8,}\s*$"/>
      <message key="todo.match"
               value="Comment uses box-like repetitive character pattern."/>
    </module>
  </module>
</module>

Example:


/** // violation 'Comment uses box-like repetitive character pattern'
 =========================================
 Box comment with equals
 =========================================
 */

public class Example3 {

  // violation below 'Comment uses box-like repetitive character pattern'
  // =========
  void method1() {
    int y = 2;
  }

  // violation below 'Comment uses box-like repetitive character pattern'
  // *********
  void method2() {
    int z = 3;
  }

  // violation below 'Comment uses box-like repetitive character pattern'
  // #########
  void method3() {
    int a = 4;
  }

  // normal comment
  void method4() {
    int b = 5;
  }

  // ###### (only 6 chars - below 9-char threshold)
  void method5() {
    int c = 6;
  }
}

Example of Usage

Violation Messages

All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.

Fully Qualified Name

com.puppycrawl.tools.checkstyle.checks.TodoCommentCheck

Use this fully qualified class name in configuration when an exact class reference is required.

Parent Module

TreeWalker