View Javadoc

    1   package net.sourceforge.pmd.rules.basic;
    2
    3   import net.sourceforge.pmd.AbstractRule;
    4   import net.sourceforge.pmd.ast.ASTBlock;
    5   import net.sourceforge.pmd.ast.ASTBlockStatement;
    6   import net.sourceforge.pmd.ast.ASTMethodDeclaration;
    7   import net.sourceforge.pmd.ast.ASTReturnStatement;
    8   import net.sourceforge.pmd.ast.ASTStatement;
    9
    10  public class UnnecessaryReturn extends AbstractRule {
    11
    12      public Object visit(ASTMethodDeclaration node, Object data) {
    13
    14          if (node.getResultType().isVoid()) {
    15              super.visit(node, data);
    16          }
    17          return data;
    18      }
    19
    20      public Object visit(ASTReturnStatement node, Object data) {
    21          if (node.jjtGetParent().getClass().equals(ASTStatement.class) && node.jjtGetParent().jjtGetParent().getClass().equals(ASTBlockStatement.class) && node.jjtGetParent().jjtGetParent().jjtGetParent().getClass().equals(ASTBlock.class)
    22                  && node.jjtGetParent().jjtGetParent().jjtGetParent().jjtGetParent().getClass().equals(ASTMethodDeclaration.class)) {
    23              addViolation(data, node);
    24          }
    25          return data;
    26      }
    27
    28  }