2 * Copyright 2003-2007 Dave Griffith, Bas Leijdekkers
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com
.siyeh
.ig
.bugs
;
18 import com
.intellij
.psi
.*;
19 import com
.intellij
.psi
.util
.PsiUtil
;
20 import com
.siyeh
.InspectionGadgetsBundle
;
21 import com
.siyeh
.ig
.BaseInspection
;
22 import com
.siyeh
.ig
.BaseInspectionVisitor
;
23 import com
.siyeh
.ig
.ui
.SingleCheckboxOptionsPanel
;
24 import org
.jetbrains
.annotations
.NotNull
;
28 public class EmptyStatementBodyInspection
extends BaseInspection
{
30 /** @noinspection PublicField*/
31 public boolean m_reportEmptyBlocks
= false;
34 public String
getID(){
35 return "StatementWithEmptyBody";
39 public String
getDisplayName(){
40 return InspectionGadgetsBundle
.message(
41 "statement.with.empty.body.display.name");
45 public String
buildErrorString(Object
... infos
){
46 return InspectionGadgetsBundle
.message(
47 "statement.with.empty.body.problem.descriptor");
50 public boolean isEnabledByDefault(){
54 public JComponent
createOptionsPanel(){
55 return new SingleCheckboxOptionsPanel(
56 InspectionGadgetsBundle
.message(
57 "statement.with.empty.body.include.option"),
58 this, "m_reportEmptyBlocks");
61 public BaseInspectionVisitor
buildVisitor() {
62 return new EmptyStatementVisitor();
65 private class EmptyStatementVisitor
extends BaseInspectionVisitor
{
67 @Override public void visitDoWhileStatement(
68 @NotNull PsiDoWhileStatement statement
) {
69 super.visitDoWhileStatement(statement
);
70 if (PsiUtil
.isInJspFile(statement
.getContainingFile())) {
73 final PsiStatement body
= statement
.getBody();
74 if (body
== null || !isEmpty(body
)) {
77 registerStatementError(statement
);
80 @Override public void visitWhileStatement(@NotNull PsiWhileStatement statement
) {
81 super.visitWhileStatement(statement
);
82 if (PsiUtil
.isInJspFile(statement
.getContainingFile())) {
85 final PsiStatement body
= statement
.getBody();
86 if (body
== null || !isEmpty(body
)) {
89 registerStatementError(statement
);
92 @Override public void visitForStatement(@NotNull PsiForStatement statement
) {
93 super.visitForStatement(statement
);
94 if (PsiUtil
.isInJspFile(statement
.getContainingFile())) {
97 final PsiStatement body
= statement
.getBody();
98 if (body
== null || !isEmpty(body
)) {
101 registerStatementError(statement
);
104 @Override public void visitForeachStatement(
105 @NotNull PsiForeachStatement statement
) {
106 super.visitForeachStatement(statement
);
107 if (PsiUtil
.isInJspFile(statement
.getContainingFile())) {
110 final PsiStatement body
= statement
.getBody();
111 if (body
== null || !isEmpty(body
)) {
114 registerStatementError(statement
);
117 @Override public void visitIfStatement(@NotNull PsiIfStatement statement
) {
118 super.visitIfStatement(statement
);
119 if (PsiUtil
.isInJspFile(statement
.getContainingFile())) {
122 final PsiStatement thenBranch
= statement
.getThenBranch();
123 if (thenBranch
!= null && isEmpty(thenBranch
)) {
124 registerStatementError(statement
);
127 final PsiStatement elseBranch
= statement
.getElseBranch();
128 if (elseBranch
!= null && isEmpty(elseBranch
)) {
129 final PsiElement elseToken
= statement
.getElseElement();
130 if (elseToken
== null) {
133 registerError(elseToken
);
137 private boolean isEmpty(PsiElement body
) {
138 if (body
instanceof PsiEmptyStatement
) {
140 } else if (m_reportEmptyBlocks
&&
141 body
instanceof PsiBlockStatement
) {
142 final PsiBlockStatement block
= (PsiBlockStatement
) body
;
143 final PsiCodeBlock codeBlock
= block
.getCodeBlock();
144 return codeBlockIsEmpty(codeBlock
);
145 } else if (body
instanceof PsiCodeBlock
) {
146 final PsiCodeBlock codeBlock
= (PsiCodeBlock
) body
;
147 return codeBlockIsEmpty(codeBlock
);
152 private boolean codeBlockIsEmpty(PsiCodeBlock codeBlock
) {
153 final PsiStatement
[] statements
= codeBlock
.getStatements();
154 return statements
.length
== 0;