From 97cbc50c6d2b8f74c37ac3790013272c843ca6d8 Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Thu, 19 May 2011 17:35:12 +0200 Subject: [PATCH] IDEA-64468 (Inspection "Unnecessary code block" creates false positives.) --- .../ig/psiutils/VariableSearchUtils.java | 35 +++++- .../UnnecessaryBlockStatementInspection.java | 45 ++++++++ .../unnecessary_block_statement/expected.xml | 101 ++++++++++++++++++ ...necessaryBlockStatementInspectionTest.java | 11 ++ 4 files changed, 190 insertions(+), 2 deletions(-) create mode 100644 plugins/InspectionGadgets/test/com/siyeh/igtest/style/unnecessary_block_statement/UnnecessaryBlockStatementInspection.java create mode 100644 plugins/InspectionGadgets/test/com/siyeh/igtest/style/unnecessary_block_statement/expected.xml create mode 100644 plugins/InspectionGadgets/testsrc/com/siyeh/ig/style/UnnecessaryBlockStatementInspectionTest.java diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/psiutils/VariableSearchUtils.java b/plugins/InspectionGadgets/src/com/siyeh/ig/psiutils/VariableSearchUtils.java index 987c86b287d4..38ce7310513b 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/ig/psiutils/VariableSearchUtils.java +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/psiutils/VariableSearchUtils.java @@ -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 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 out) { + while (element != null) { + if (element instanceof PsiCodeBlock) { + out.add((PsiCodeBlock) element); + } else { + findFollowingBlocks(element.getFirstChild(), out); + } + element = element.getNextSibling(); + } + } } \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/style/unnecessary_block_statement/UnnecessaryBlockStatementInspection.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/style/unnecessary_block_statement/UnnecessaryBlockStatementInspection.java new file mode 100644 index 000000000000..b5e2c29d9345 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/style/unnecessary_block_statement/UnnecessaryBlockStatementInspection.java @@ -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; + } + } +} diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/style/unnecessary_block_statement/expected.xml b/plugins/InspectionGadgets/test/com/siyeh/igtest/style/unnecessary_block_statement/expected.xml new file mode 100644 index 000000000000..d5f8b6f82549 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/style/unnecessary_block_statement/expected.xml @@ -0,0 +1,101 @@ + + + + + UnnecessaryBlockStatementInspection.java + 5 + Unnecessary code block + Braces around this statement are unnecessary #loc + + + + UnnecessaryBlockStatementInspection.java + 7 + Unnecessary code block + Braces around this statement are unnecessary #loc + + + + UnnecessaryBlockStatementInspection.java + 8 + Unnecessary code block + Braces around this statement are unnecessary #loc + + + + UnnecessaryBlockStatementInspection.java + 10 + Unnecessary code block + Braces around this statement are unnecessary #loc + + + + UnnecessaryBlockStatementInspection.java + 11 + Unnecessary code block + Braces around this statement are unnecessary #loc + + + + UnnecessaryBlockStatementInspection.java + 13 + Unnecessary code block + Braces around this statement are unnecessary #loc + + + + UnnecessaryBlockStatementInspection.java + 14 + Unnecessary code block + Braces around this statement are unnecessary #loc + + + + UnnecessaryBlockStatementInspection.java + 15 + Unnecessary code block + Braces around this statement are unnecessary #loc + + + + UnnecessaryBlockStatementInspection.java + 17 + Unnecessary code block + Braces around this statement are unnecessary #loc + + + + UnnecessaryBlockStatementInspection.java + 18 + Unnecessary code block + Braces around this statement are unnecessary #loc + + + + UnnecessaryBlockStatementInspection.java + 27 + Unnecessary code block + Braces around this statement are unnecessary #loc + + + + UnnecessaryBlockStatementInspection.java + 30 + Unnecessary code block + Braces around this statement are unnecessary #loc + + + + UnnecessaryBlockStatementInspection.java + 41 + Unnecessary code block + Braces around this statement are unnecessary #loc + + + + UnnecessaryBlockStatementInspection.java + 43 + Unnecessary code block + Braces around this statement are unnecessary #loc + + diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/style/UnnecessaryBlockStatementInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/style/UnnecessaryBlockStatementInspectionTest.java new file mode 100644 index 000000000000..87d3d55f5afe --- /dev/null +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/style/UnnecessaryBlockStatementInspectionTest.java @@ -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()); + } +}