RedundantThis

Since Checkstyle 14.4.0

Description

Checks that references to instance variables and methods of the current object avoid unnecessary use of this, unless it is required to resolve ambiguity with a shadowed field.

By default, a redundant this is tolerated when the same statement, or the statement immediately before or after it, contains a this that is required to resolve a shadowed field. This keeps groups of assignments consistent, for example constructor bodies where only some parameters shadow fields. Set allowAdjacentToRequiredThis to false to report every redundant this.

Properties

name description type default value since
allowAdjacentToRequiredThis Allow redundant "this" when the same statement, or the statement immediately before or after it, contains a required "this". boolean true 14.4.0
checkMethods Check whether to check redundant "this" with method call. boolean false 14.4.0

Examples

To configure the check:


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

Example1:


public class Example1 {
  private int age;

  public void setAge(int value) {
    this.age = value;
    // violation above, 'Redundant "this", field 'age' can be accessed directly.'
  }

  public void process() {
    this.show();

  }
  public void show() {}

  private int x;
  private int y;
  private int z;

  public Example1(int x, int y) {
    this.x = x;
    this.y = y;
    this.z = 3;

  }
}

Example of violations.


<module name="Checker">
  <module name="TreeWalker">
    <module name="RedundantThis">
      <property name="checkMethods" value="true"/>
    </module>
  </module>
</module>

Example2:


public class Example2 {
  private int age;

  public void setAge(int value) {
    this.age = value;
    // violation above, 'Redundant "this", field 'age' can be accessed directly.'
  }

  public void process() {
    this.show();
    // violation above, 'Redundant "this", method 'show' can be accessed directly.'
  }
  public void show() {}

  private int x;
  private int y;
  private int z;

  public Example2(int x, int y) {
    this.x = x;
    this.y = y;
    this.z = 3;

  }
}

To configure the check to report every redundant "this", without allowing adjacent statements:


<module name="Checker">
  <module name="TreeWalker">
    <module name="RedundantThis">
      <property name="allowAdjacentToRequiredThis" value="false"/>
    </module>
  </module>
</module>

Example3:


public class Example3 {
  private int age;

  public void setAge(int value) {
    this.age = value;
    // violation above, 'Redundant "this", field 'age' can be accessed directly.'
  }

  public void process() {
    this.show();

  }
  public void show() {}

  private int x;
  private int y;
  private int z;

  public Example3(int x, int y) {
    this.x = x;
    this.y = y;
    this.z = 3;
    // violation above, 'Redundant "this", field 'z' can be accessed directly.'
  }
}

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.coding.RedundantThisCheck

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

Parent Module

TreeWalker