Skip to content

Commit

Permalink
fix: updated RV_01_TO_INT to include float and long (#1851)
Browse files Browse the repository at this point in the history
* fix: updated RV_01_TO_INT to include float and long

* chore: resolving spotbugs:spotlessJavaCheck

* chore: resolving spotbugs:spotlessJavaCheck for Issue1518Test.java

* chore: updated CHANGELOG.md
  • Loading branch information
heeki committed Dec 11, 2021
1 parent 1f8f6d0 commit 22fba92
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ This is the changelog for SpotBugs. This follows [Keep a Changelog v1.0.0](http:
Currently the versioning policy of this project follows [Semantic Versioning v2.0.0](http://semver.org/spec/v2.0.0.html).

## Unreleased - 2022-??-??
### Fixed
- Updated RV_01_TO_INT to handle float and long checks ([#1518](https://github.com/spotbugs/spotbugs/issues/1518))

## 4.5.1 - 2021-12-08
### Fixed
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package edu.umd.cs.findbugs.detect;

import edu.umd.cs.findbugs.AbstractIntegrationTest;
import edu.umd.cs.findbugs.test.matcher.BugInstanceMatcher;
import edu.umd.cs.findbugs.test.matcher.BugInstanceMatcherBuilder;
import org.junit.Test;

import static edu.umd.cs.findbugs.test.CountMatcher.containsExactly;
import static org.hamcrest.MatcherAssert.assertThat;

/**
* @see <a href="https://github.com/spotbugs/spotbugs/issues/1518">GitHub issue #1518</a>
*/
public class Issue1518Test extends AbstractIntegrationTest {
@Test
public void test() {
performAnalysis("ghIssues/Issue1518.class");
BugInstanceMatcher bugTypeMatcher = new BugInstanceMatcherBuilder()
.bugType("RV_01_TO_INT")
.build();
assertThat(getBugCollection(), containsExactly(3, bugTypeMatcher));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1174,14 +1174,18 @@ else if (special == OpcodeStack.Item.HASHCODE_INT) {
// + " " + getMethodName());
switch (randomNextIntState) {
case 0:
if (seen == Const.INVOKEVIRTUAL && CLASS_NAME_RANDOM.equals(getClassConstantOperand())
&& "nextDouble".equals(getNameConstantOperand()) || seen == Const.INVOKESTATIC
&& ClassName.isMathClass(getClassConstantOperand()) && "random".equals(getNameConstantOperand())) {
if (seen == Const.INVOKEVIRTUAL && CLASS_NAME_RANDOM.equals(getClassConstantOperand()) && "nextDouble".equals(
getNameConstantOperand()) ||
seen == Const.INVOKEVIRTUAL && CLASS_NAME_RANDOM.equals(getClassConstantOperand()) && "nextFloat".equals(
getNameConstantOperand()) ||
seen == Const.INVOKEVIRTUAL && CLASS_NAME_RANDOM.equals(getClassConstantOperand()) && "nextLong".equals(
getNameConstantOperand()) ||
seen == Const.INVOKESTATIC && ClassName.isMathClass(getClassConstantOperand()) && "random".equals(getNameConstantOperand())) {
randomNextIntState = 1;
}
break;
case 1:
if (seen == Const.D2I) {
if (seen == Const.D2I || seen == Const.F2I || seen == Const.L2I) {
accumulator.accumulateBug(new BugInstance(this, "RV_01_TO_INT", HIGH_PRIORITY).addClassAndMethod(this), this);
randomNextIntState = 0;
} else if (seen == Const.DMUL) {
Expand Down
22 changes: 22 additions & 0 deletions spotbugsTestCases/src/java/ghIssues/Issue1518.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package ghIssues;

import java.util.Random;

public class Issue1518 {
private final Random RANDOM = new Random();

public void testDouble() {
int testDouble = (int)RANDOM.nextDouble();
System.out.println(testDouble);
}

public void testFloat() {
int testFloat = (int)RANDOM.nextFloat();
System.out.println(testFloat);
}

public void testLong() {
int testLong = (int)RANDOM.nextLong();
System.out.println(testLong);
}
}

0 comments on commit 22fba92

Please sign in to comment.