mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-64468 (Inspection "Unnecessary code block" creates false positives.)
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2003-2010 Dave Griffith, Bas Leijdekkers
|
||||
* Copyright 2003-2011 Dave Griffith, Bas Leijdekkers
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -19,6 +19,9 @@ import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.*;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class VariableSearchUtils {
|
||||
|
||||
private VariableSearchUtils() {}
|
||||
@@ -38,6 +41,9 @@ public class VariableSearchUtils {
|
||||
|
||||
public static boolean containsConflictingDeclarations(
|
||||
PsiCodeBlock block, PsiCodeBlock parentBlock) {
|
||||
final List<PsiCodeBlock> followingBlocks = new ArrayList();
|
||||
findFollowingBlocks(block.getParent().getNextSibling(),
|
||||
followingBlocks);
|
||||
final PsiStatement[] statements = block.getStatements();
|
||||
final Project project = block.getProject();
|
||||
final JavaPsiFacade facade = JavaPsiFacade.getInstance(project);
|
||||
@@ -56,14 +62,39 @@ public class VariableSearchUtils {
|
||||
}
|
||||
final PsiLocalVariable localVariable =
|
||||
(PsiLocalVariable) variable;
|
||||
final String variableName = localVariable.getName();
|
||||
final PsiVariable target =
|
||||
resolveHelper.resolveAccessibleReferencedVariable(
|
||||
localVariable.getName(), parentBlock);
|
||||
variableName, parentBlock);
|
||||
if (target != null) {
|
||||
return true;
|
||||
}
|
||||
for (PsiCodeBlock codeBlock : followingBlocks) {
|
||||
final PsiVariable target1 =
|
||||
resolveHelper.resolveAccessibleReferencedVariable(
|
||||
variableName, codeBlock);
|
||||
if (target1 != null) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Depth first traversal to find all PsiCodeBlock children. Does not find
|
||||
* children of found blocks.
|
||||
*/
|
||||
private static void findFollowingBlocks(PsiElement element,
|
||||
List<PsiCodeBlock> out) {
|
||||
while (element != null) {
|
||||
if (element instanceof PsiCodeBlock) {
|
||||
out.add((PsiCodeBlock) element);
|
||||
} else {
|
||||
findFollowingBlocks(element.getFirstChild(), out);
|
||||
}
|
||||
element = element.getNextSibling();
|
||||
}
|
||||
}
|
||||
}
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
package com.siyeh.igtest.style.unnecessary_block_statement;
|
||||
|
||||
public class UnnecessaryBlockStatementInspection {
|
||||
public static void main(String[] args) {
|
||||
{
|
||||
System.out.println("3");
|
||||
}
|
||||
{
|
||||
int a;
|
||||
}
|
||||
{
|
||||
int b;
|
||||
}
|
||||
{
|
||||
{
|
||||
int a;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void oldSwitcharoo() {
|
||||
switch (5) {
|
||||
case 1: {
|
||||
int x = 0;
|
||||
break;
|
||||
}
|
||||
case 2: {
|
||||
int x = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ifThenElse() {
|
||||
{
|
||||
int i = 0;
|
||||
}
|
||||
if (true) {
|
||||
int i = 0;
|
||||
}
|
||||
{
|
||||
int i = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
+101
@@ -0,0 +1,101 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<problems>
|
||||
|
||||
<problem>
|
||||
<file>UnnecessaryBlockStatementInspection.java</file>
|
||||
<line>5</line>
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Unnecessary code block</problem_class>
|
||||
<description>Braces around this statement are unnecessary #loc</description>
|
||||
</problem>
|
||||
|
||||
<problem>
|
||||
<file>UnnecessaryBlockStatementInspection.java</file>
|
||||
<line>7</line>
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Unnecessary code block</problem_class>
|
||||
<description>Braces around this statement are unnecessary #loc</description>
|
||||
</problem>
|
||||
|
||||
<problem>
|
||||
<file>UnnecessaryBlockStatementInspection.java</file>
|
||||
<line>8</line>
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Unnecessary code block</problem_class>
|
||||
<description>Braces around this statement are unnecessary #loc</description>
|
||||
</problem>
|
||||
|
||||
<problem>
|
||||
<file>UnnecessaryBlockStatementInspection.java</file>
|
||||
<line>10</line>
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Unnecessary code block</problem_class>
|
||||
<description>Braces around this statement are unnecessary #loc</description>
|
||||
</problem>
|
||||
|
||||
<problem>
|
||||
<file>UnnecessaryBlockStatementInspection.java</file>
|
||||
<line>11</line>
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Unnecessary code block</problem_class>
|
||||
<description>Braces around this statement are unnecessary #loc</description>
|
||||
</problem>
|
||||
|
||||
<problem>
|
||||
<file>UnnecessaryBlockStatementInspection.java</file>
|
||||
<line>13</line>
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Unnecessary code block</problem_class>
|
||||
<description>Braces around this statement are unnecessary #loc</description>
|
||||
</problem>
|
||||
|
||||
<problem>
|
||||
<file>UnnecessaryBlockStatementInspection.java</file>
|
||||
<line>14</line>
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Unnecessary code block</problem_class>
|
||||
<description>Braces around this statement are unnecessary #loc</description>
|
||||
</problem>
|
||||
|
||||
<problem>
|
||||
<file>UnnecessaryBlockStatementInspection.java</file>
|
||||
<line>15</line>
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Unnecessary code block</problem_class>
|
||||
<description>Braces around this statement are unnecessary #loc</description>
|
||||
</problem>
|
||||
|
||||
<problem>
|
||||
<file>UnnecessaryBlockStatementInspection.java</file>
|
||||
<line>17</line>
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Unnecessary code block</problem_class>
|
||||
<description>Braces around this statement are unnecessary #loc</description>
|
||||
</problem>
|
||||
|
||||
<problem>
|
||||
<file>UnnecessaryBlockStatementInspection.java</file>
|
||||
<line>18</line>
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Unnecessary code block</problem_class>
|
||||
<description>Braces around this statement are unnecessary #loc</description>
|
||||
</problem>
|
||||
|
||||
<problem>
|
||||
<file>UnnecessaryBlockStatementInspection.java</file>
|
||||
<line>27</line>
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Unnecessary code block</problem_class>
|
||||
<description>Braces around this statement are unnecessary #loc</description>
|
||||
</problem>
|
||||
|
||||
<problem>
|
||||
<file>UnnecessaryBlockStatementInspection.java</file>
|
||||
<line>30</line>
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Unnecessary code block</problem_class>
|
||||
<description>Braces around this statement are unnecessary #loc</description>
|
||||
</problem>
|
||||
|
||||
<problem>
|
||||
<file>UnnecessaryBlockStatementInspection.java</file>
|
||||
<line>41</line>
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Unnecessary code block</problem_class>
|
||||
<description>Braces around this statement are unnecessary #loc</description>
|
||||
</problem>
|
||||
|
||||
<problem>
|
||||
<file>UnnecessaryBlockStatementInspection.java</file>
|
||||
<line>43</line>
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Unnecessary code block</problem_class>
|
||||
<description>Braces around this statement are unnecessary #loc</description>
|
||||
</problem>
|
||||
</problems>
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package com.siyeh.ig.style;
|
||||
|
||||
import com.siyeh.ig.IGInspectionTestCase;
|
||||
|
||||
public class UnnecessaryBlockStatementInspectionTest extends IGInspectionTestCase {
|
||||
|
||||
public void test() throws Exception {
|
||||
doTest("com/siyeh/igtest/style/unnecessary_block_statement",
|
||||
new UnnecessaryBlockStatementInspection());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user