View Javadoc

1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.lang.java.rule.unnecessary;
5   
6   import net.sourceforge.pmd.lang.java.ast.ASTBlock;
7   import net.sourceforge.pmd.lang.java.ast.ASTBlockStatement;
8   import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration;
9   import net.sourceforge.pmd.lang.java.ast.ASTReturnStatement;
10  import net.sourceforge.pmd.lang.java.ast.ASTStatement;
11  import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
12  
13  public class UnnecessaryReturnRule extends AbstractJavaRule {
14  
15      public Object visit(ASTMethodDeclaration node, Object data) {
16  
17          if (node.getResultType().isVoid()) {
18              super.visit(node, data);
19          }
20          return data;
21      }
22  
23      public Object visit(ASTReturnStatement node, Object data) {
24          if (node.jjtGetParent() instanceof ASTStatement && node.getNthParent(2) instanceof ASTBlockStatement && node.getNthParent(3) instanceof ASTBlock
25                  && node.getNthParent(4) instanceof ASTMethodDeclaration) {
26              addViolation(data, node);
27          }
28          return data;
29      }
30  
31  }