Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
141 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
spotbugs-tests/src/test/java/edu/umd/cs/findbugs/detect/DontUseFloatsAsLoopCountersTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package edu.umd.cs.findbugs.detect; | ||
|
||
import org.junit.Test; | ||
|
||
import edu.umd.cs.findbugs.AbstractIntegrationTest; | ||
import edu.umd.cs.findbugs.test.matcher.BugInstanceMatcher; | ||
import edu.umd.cs.findbugs.test.matcher.BugInstanceMatcherBuilder; | ||
|
||
import static edu.umd.cs.findbugs.test.CountMatcher.containsExactly; | ||
import static org.hamcrest.Matchers.hasItem; | ||
import static org.junit.Assert.assertThat; | ||
|
||
public class DontUseFloatsAsLoopCountersTest extends AbstractIntegrationTest { | ||
@Test | ||
public void testChecks() { | ||
performAnalysis("DontUseFloatsAsLoopCounters.class"); | ||
assertNumOfEOSBugs(3); | ||
assertBug("main", 5); | ||
assertBug("main", 9); | ||
assertBug("main", 12); | ||
} | ||
|
||
private void assertBug(String method, int line) { | ||
final BugInstanceMatcher bugInstanceMatcher = new BugInstanceMatcherBuilder() | ||
.bugType("FL_FLOATS_AS_LOOP_COUNTERS") | ||
.inClass("DontUseFloatsAsLoopCounters") | ||
.inMethod(method) | ||
.atLine(line) | ||
.build(); | ||
assertThat(getBugCollection(), hasItem(bugInstanceMatcher)); | ||
} | ||
|
||
private void assertNumOfEOSBugs(int num) { | ||
final BugInstanceMatcher bugTypeMatcher = new BugInstanceMatcherBuilder() | ||
.bugType("FL_FLOATS_AS_LOOP_COUNTERS").build(); | ||
assertThat(getBugCollection(), containsExactly(num, bugTypeMatcher)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
spotbugs/src/main/java/edu/umd/cs/findbugs/detect/DontUseFloatsAsLoopCounters.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package edu.umd.cs.findbugs.detect; | ||
|
||
import org.apache.bcel.Const; | ||
|
||
import edu.umd.cs.findbugs.BugInstance; | ||
import edu.umd.cs.findbugs.BugReporter; | ||
import edu.umd.cs.findbugs.StatelessDetector; | ||
import edu.umd.cs.findbugs.bcel.OpcodeStackDetector; | ||
import java.util.stream.Stream; | ||
|
||
public class DontUseFloatsAsLoopCounters extends OpcodeStackDetector implements StatelessDetector { | ||
|
||
private final BugReporter bugReporter; | ||
|
||
public DontUseFloatsAsLoopCounters(BugReporter bugReporter) { | ||
this.bugReporter = bugReporter; | ||
} | ||
|
||
@Override | ||
public void sawOpcode(int seen) { | ||
if ((seen == Const.GOTO || seen == Const.GOTO_W) && | ||
Stream.of(Const.FLOAD, Const.FLOAD_0, Const.FLOAD_1, Const.FLOAD_2, Const.FLOAD_3, Const.DLOAD, Const.DLOAD_0, Const.DLOAD_1, | ||
Const.DLOAD_2, Const.DLOAD_3).anyMatch(x -> x == getCodeByte(getBranchTarget())) && (getBranchTarget() < getPC())) { | ||
bugReporter.reportBug(new BugInstance(this, "FL_FLOATS_AS_LOOP_COUNTERS", NORMAL_PRIORITY) | ||
.addClassAndMethod(this).addSourceLine(this, getBranchTarget())); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
spotbugsTestCases/src/java/DontUseFloatsAsLoopCounters.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
class DontUseFloatsAsLoopCounters { | ||
public static void main(String[] args) { | ||
//noncompliant | ||
float x = 0.1f; | ||
while (x<10){ | ||
System.out.println(x); | ||
x++; | ||
} | ||
for (float y = 0.2f; y <= 1.0f; y += 0.1f) { | ||
System.out.println(y); | ||
} | ||
for (double d = 0.2d; d <= 1.0d; d += 0.1d) { | ||
System.out.println(d); | ||
} | ||
//compliant | ||
for (int count = 1; count <= 10; count += 1) { | ||
float q = count/10.0f; | ||
System.out.println(q); | ||
System.out.println(count); | ||
} | ||
int c = 0; | ||
while (c<5){ | ||
c++; | ||
} | ||
boolean b = true; | ||
while (b){ | ||
b = false; | ||
} | ||
int p = 1; | ||
while (p<9){ | ||
p*=2; | ||
} | ||
} | ||
} |