IG: ignore blocks containing error elements (IDEA-177570)

This commit is contained in:
Bas Leijdekkers
2017-08-21 21:10:43 +02:00
parent babad27fc8
commit d0c265416c
3 changed files with 35 additions and 1 deletions
@@ -21,6 +21,7 @@ import com.intellij.openapi.util.Pair;
import com.intellij.psi.*;
import com.intellij.psi.util.FileTypeUtils;
import com.intellij.psi.util.PsiUtil;
import com.intellij.psi.util.PsiUtilCore;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.BaseInspectionVisitor;
@@ -97,7 +98,11 @@ public class SingleStatementInBlockInspection extends BaseInspection {
@Override
protected boolean isApplicable(PsiStatement body) {
if (body instanceof PsiBlockStatement) {
final PsiStatement[] statements = ((PsiBlockStatement)body).getCodeBlock().getStatements();
final PsiCodeBlock codeBlock = ((PsiBlockStatement)body).getCodeBlock();
if (PsiUtilCore.hasErrorElementChild(codeBlock)) {
return false;
}
final PsiStatement[] statements = codeBlock.getStatements();
if (statements.length == 1 && !(statements[0] instanceof PsiDeclarationStatement) && !isDanglingElseProblem(statements[0], body)) {
final PsiFile file = body.getContainingFile();
//this inspection doesn't work in JSP files, as it can't tell about tags
@@ -0,0 +1,28 @@
/*
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
class X {
public int toIntegerValue() {
int result = 0;
fo<caret>r (CssPrefix prefix : myEnabledPrefixes) {
float (getValue) {
result |= prefix.myIntMask;
}
}
return result;
}
void anotherMethod() {}
}
@@ -33,6 +33,7 @@ public class SingleStatementInBlockFixTest extends IGQuickFixesTestCase {
public void testWhile() { doTest("while"); }
public void testForEach() { doTest("for"); }
public void testForIndex() { doTest("for"); }
public void testForMalformed() { assertQuickfixNotAvailable(getMessage("for"));}
public void testDoWhile() { doTest("do"); }
public void testIfWithLoop() { doTest("if"); }
public void testElseWithLoop() { doTest("else"); }