IDEA-154177 ('for statement does not loop' inspection false positive)

This commit is contained in:
Bas Leijdekkers
2016-04-06 11:26:09 +02:00
parent 05d0431c43
commit 5784af5e91
4 changed files with 128 additions and 65 deletions
@@ -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) {
@@ -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;
// }
}
}
@@ -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);
<warning descr="'for' statement does not loop">for</warning>(; ;)
{
break;
}
<warning descr="'while' statement does not loop">while</warning>(true)
{
break;
}
<warning descr="'do' statement does not loop">do</warning>
{
break;
}
while(true);
<warning descr="'while' statement does not loop">while</warning>(true)
{
throw new Exception();
}
<warning descr="'for' statement does not loop">for</warning>(<error descr="Unreachable statement">;</error> ;)
{
return;
}
}
enum Modification {
NONE, SET, REMOVE;
}
boolean foo(List<Modification> list){
for (Modification modification : list) {
switch (modification) {
case SET:
case REMOVE:
return true;
}
}
<warning descr="'for' statement does not loop">for</warning> (Modification modification : list) {
switch (modification) {
case SET:
case REMOVE:
return true;
case NONE:
return false;
}
}
return false;
}
}
@@ -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();
}
}