Skip to content

Commit

Permalink
Fix FNs in CT_CONSTRUCTOR_THROW (#2747)
Browse files Browse the repository at this point in the history
* Add test

* Fix FN in CT_CONSTRUCTOR_THROW

* add back accidentally deleted test

---------

Co-authored-by: Jeremy Landis <jeremylandis@hotmail.com>
  • Loading branch information
JuditKnoll and hazendaz committed Dec 9, 2023
1 parent 10422e8 commit 44dd360
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Currently the versioning policy of this project follows [Semantic Versioning v2.
- Fix FP in CT_CONSTRUCTOR_THROW when the finalizer does not run, since the exception is thrown before java.lang.Object's constructor exits for checked exceptions ([#2710](https://github.com/spotbugs/spotbugs/issues/2710))
- Applied changes for bcel 6.8.0 with adjustments to constant pool ([#2756](https://github.com/spotbugs/spotbugs/pull/2756))
- More information bcel changes can be found on ([#2757](https://github.com/spotbugs/spotbugs/pull/2757))
- Fix FN in CT_CONSTRUCTOR_THROW when the return value of the called method is not void or primitive type.

### Changed
- Improved Matcher checks for empty strings ([#2755](https://github.com/spotbugs/spotbugs/pull/2755))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,20 @@ void testConstructorThrowCheck20() {
assertCTBugInLine(13);
}

@Test
void testConstructorThrowCheck21() {
performAnalysis("constructorthrow/ConstructorThrowTest21.class");
assertNumOfCTBugs(1);
assertCTBugInLine(9);
}

@Test
void testConstructorThrowCheck22() {
performAnalysis("constructorthrow/ConstructorThrowTest22.class");
assertNumOfCTBugs(1);
assertCTBugInLine(11);
}

@Test
void testGoodConstructorThrowCheck1() {
performAnalysis("constructorthrow/ConstructorThrowNegativeTest1.class");
Expand Down Expand Up @@ -255,6 +269,12 @@ void testGoodConstructorThrowCheck17() {
assertNumOfCTBugs(0);
}

@Test
void testGoodConstructorThrowCheck18() {
performAnalysis("constructorthrow/ConstructorThrowNegativeTest18.class");
assertNumOfCTBugs(0);
}

private void assertNumOfCTBugs(int num) {
final BugInstanceMatcher bugTypeMatcher = new BugInstanceMatcherBuilder()
.bugType("CT_CONSTRUCTOR_THROW").build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ private String getCalledMethodFQN(int seen) {
}
} else {
return String.format("%s.%s : %s", getDottedClassConstantOperand(), getNameConstantOperand(),
getSigConstantOperand());
toDotted(getSigConstantOperand()));
}
return "";
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package constructorthrow;

/**
* Calling a static function with not void or primitive return value from the constructor,
* which throws an unchecked exception.
*/
public class ConstructorThrowTest21 {
public ConstructorThrowTest21() {
ConstructorThrowTest21.test();
}

private static String test() {
throw new IllegalStateException();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package constructorthrow;

import java.io.IOException;

/**
* Calling a static function with not void or primitive return value from the constructor,
* which throws a checked exception.
*/
public class ConstructorThrowTest22 {
public ConstructorThrowTest22() throws IOException {
ConstructorThrowTest22.test();
}

private static String test() throws IOException {
throw new IOException();
}
}

0 comments on commit 44dd360

Please sign in to comment.