Class SelfAssignment

java.lang.Object
com.google.errorprone.bugpatterns.BugChecker
com.google.errorprone.bugpatterns.SelfAssignment
All Implemented Interfaces:
BugChecker.AssignmentTreeMatcher, BugChecker.VariableTreeMatcher, Suppressible, Serializable

public class SelfAssignment extends BugChecker implements BugChecker.AssignmentTreeMatcher, BugChecker.VariableTreeMatcher
TODO(eaftan): Consider cases where the parent is not a statement or there is no parent?
Author:
eaftan@google.com (Eddie Aftandilian), scottjohnson@google.com (Scott Johnson)
See Also:
  • Constructor Details

    • SelfAssignment

      public SelfAssignment()
  • Method Details

    • matchAssignment

      public Description matchAssignment(com.sun.source.tree.AssignmentTree tree, VisitorState state)
      Specified by:
      matchAssignment in interface BugChecker.AssignmentTreeMatcher
    • matchVariable

      public Description matchVariable(com.sun.source.tree.VariableTree tree, VisitorState state)
      Specified by:
      matchVariable in interface BugChecker.VariableTreeMatcher
    • describeForVarDecl

      public Description describeForVarDecl(com.sun.source.tree.VariableTree tree, VisitorState state)
    • describeForAssignment

      public Description describeForAssignment(com.sun.source.tree.AssignmentTree assignmentTree, VisitorState state)
      We expect that the lhs is a field and the rhs is an identifier, specifically a parameter to the method. We base our suggested fixes on this expectation.

      Case 1: If lhs is a field and rhs is an identifier, find a method parameter of the same type and similar name and suggest it as the rhs. (Guess that they have misspelled the identifier.)

      Case 2: If lhs is a field and rhs is not an identifier, find a method parameter of the same type and similar name and suggest it as the rhs.

      Case 3: If lhs is not a field and rhs is an identifier, find a class field of the same type and similar name and suggest it as the lhs.

      Case 4: Otherwise suggest deleting the assignment.