From 5784af5e918fa8c16637588383aa66e8716958ff Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Wed, 6 Apr 2016 11:24:24 +0200 Subject: [PATCH] IDEA-154177 ('for statement does not loop' inspection false positive) --- .../siyeh/ig/psiutils/ControlFlowUtils.java | 4 +- .../LoopStatementsThatDontLoopInspection.java | 63 ------------- .../LoopStatementsThatDontLoop.java | 90 +++++++++++++++++++ ...pStatementsThatDontLoopInspectionTest.java | 36 ++++++++ 4 files changed, 128 insertions(+), 65 deletions(-) delete mode 100644 plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/LoopStatementsThatDontLoopInspection.java create mode 100644 plugins/InspectionGadgets/test/com/siyeh/igtest/controlflow/loop_statements_that_dont_loop/LoopStatementsThatDontLoop.java create mode 100644 plugins/InspectionGadgets/testsrc/com/siyeh/ig/controlflow/LoopStatementsThatDontLoopInspectionTest.java diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/ControlFlowUtils.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/ControlFlowUtils.java index a09e2143f4ab..23ad190409c8 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/ControlFlowUtils.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/ControlFlowUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2003-2015 Dave Griffith, Bas Leijdekkers + * Copyright 2003-2016 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. @@ -153,6 +153,7 @@ public class ControlFlowUtils { boolean hasDefaultCase = false; for (PsiStatement statement : statements) { if (statement instanceof PsiSwitchLabelStatement) { + numCases++; final PsiSwitchLabelStatement switchLabelStatement = (PsiSwitchLabelStatement)statement; if (switchLabelStatement.isDefaultCase()) { hasDefaultCase = true; @@ -164,7 +165,6 @@ public class ControlFlowUtils { return true; } } - numCases++; } final boolean isEnum = isEnumSwitch(switchStatement); if (!hasDefaultCase && !isEnum) { diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/LoopStatementsThatDontLoopInspection.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/LoopStatementsThatDontLoopInspection.java deleted file mode 100644 index d0ed0405c972..000000000000 --- a/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/LoopStatementsThatDontLoopInspection.java +++ /dev/null @@ -1,63 +0,0 @@ -package com.siyeh.igtest.bugs; - -public class LoopStatementsThatDontLoopInspection -{ - private final int m_foo = 3; - - public static void main(String[] args) throws Exception - { - new LoopStatementsThatDontLoopInspection().foo(); - } - - public LoopStatementsThatDontLoopInspection() - { - } - - public static boolean isContainingHash( final String s ) - { - final char[] c = s.toCharArray(); - - for( int i = 0; i < c.length; i++ ) - { - switch( c[i] ) - { - case '#': - return false; - } - } - - return true; - } - - private void foo() throws Exception - { - System.out.println("m_foo =" + m_foo); - for(; ;) - { - break; - } - - while(true) - { - break; - } - - do - { - break; - } - while(true); - - while(true) - { - throw new Exception(); - } - - // for(; ;) - //{ - // return; - // } - - } - -} diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/controlflow/loop_statements_that_dont_loop/LoopStatementsThatDontLoop.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/controlflow/loop_statements_that_dont_loop/LoopStatementsThatDontLoop.java new file mode 100644 index 000000000000..a2c4ed98d3b0 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/controlflow/loop_statements_that_dont_loop/LoopStatementsThatDontLoop.java @@ -0,0 +1,90 @@ +package com.siyeh.igtest.bugs; + +import java.util.List; + +public class LoopStatementsThatDontLoop +{ + private final int m_foo = 3; + + public static void main(String[] args) throws Exception + { + new LoopStatementsThatDontLoop().foo(); + } + + public LoopStatementsThatDontLoop() + { + } + + public static boolean isContainingHash( final String s ) + { + final char[] c = s.toCharArray(); + + for( int i = 0; i < c.length; i++ ) + { + switch( c[i] ) + { + case '#': + return false; + } + } + + return true; + } + + private void foo() throws Exception + { + System.out.println("m_foo =" + m_foo); + for(; ;) + { + break; + } + + while(true) + { + break; + } + + do + { + break; + } + while(true); + + while(true) + { + throw new Exception(); + } + + for(; ;) + { + return; + } + + } + + enum Modification { + NONE, SET, REMOVE; + } + + boolean foo(List list){ + for (Modification modification : list) { + switch (modification) { + case SET: + case REMOVE: + return true; + } + } + for (Modification modification : list) { + switch (modification) { + case SET: + case REMOVE: + return true; + case NONE: + return false; + } + + } + return false; + } + +} diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/controlflow/LoopStatementsThatDontLoopInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/controlflow/LoopStatementsThatDontLoopInspectionTest.java new file mode 100644 index 000000000000..d876b60d352c --- /dev/null +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/controlflow/LoopStatementsThatDontLoopInspectionTest.java @@ -0,0 +1,36 @@ +/* + * Copyright 2000-2016 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. + */ +package com.siyeh.ig.controlflow; + +import com.intellij.codeInspection.InspectionProfileEntry; +import com.siyeh.ig.LightInspectionTestCase; +import org.jetbrains.annotations.Nullable; + +/** + * @author Bas Leijdekkers + */ +public class LoopStatementsThatDontLoopInspectionTest extends LightInspectionTestCase { + + public void testLoopStatementsThatDontLoop() { + doTest(); + } + + @Nullable + @Override + protected InspectionProfileEntry getInspection() { + return new LoopStatementsThatDontLoopInspection(); + } +}