Skip to content

Commit 44dd360

Browse files
JuditKnollhazendaz
andauthored
Fix FNs in CT_CONSTRUCTOR_THROW (#2747)
* Add test * Fix FN in CT_CONSTRUCTOR_THROW * add back accidentally deleted test --------- Co-authored-by: Jeremy Landis <jeremylandis@hotmail.com>
1 parent 10422e8 commit 44dd360

5 files changed

Lines changed: 54 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Currently the versioning policy of this project follows [Semantic Versioning v2.
1111
- 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))
1212
- Applied changes for bcel 6.8.0 with adjustments to constant pool ([#2756](https://github.com/spotbugs/spotbugs/pull/2756))
1313
- More information bcel changes can be found on ([#2757](https://github.com/spotbugs/spotbugs/pull/2757))
14+
- Fix FN in CT_CONSTRUCTOR_THROW when the return value of the called method is not void or primitive type.
1415

1516
### Changed
1617
- Improved Matcher checks for empty strings ([#2755](https://github.com/spotbugs/spotbugs/pull/2755))

spotbugs-tests/src/test/java/edu/umd/cs/findbugs/detect/ConstructorThrowTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,20 @@ void testConstructorThrowCheck20() {
152152
assertCTBugInLine(13);
153153
}
154154

155+
@Test
156+
void testConstructorThrowCheck21() {
157+
performAnalysis("constructorthrow/ConstructorThrowTest21.class");
158+
assertNumOfCTBugs(1);
159+
assertCTBugInLine(9);
160+
}
161+
162+
@Test
163+
void testConstructorThrowCheck22() {
164+
performAnalysis("constructorthrow/ConstructorThrowTest22.class");
165+
assertNumOfCTBugs(1);
166+
assertCTBugInLine(11);
167+
}
168+
155169
@Test
156170
void testGoodConstructorThrowCheck1() {
157171
performAnalysis("constructorthrow/ConstructorThrowNegativeTest1.class");
@@ -255,6 +269,12 @@ void testGoodConstructorThrowCheck17() {
255269
assertNumOfCTBugs(0);
256270
}
257271

272+
@Test
273+
void testGoodConstructorThrowCheck18() {
274+
performAnalysis("constructorthrow/ConstructorThrowNegativeTest18.class");
275+
assertNumOfCTBugs(0);
276+
}
277+
258278
private void assertNumOfCTBugs(int num) {
259279
final BugInstanceMatcher bugTypeMatcher = new BugInstanceMatcherBuilder()
260280
.bugType("CT_CONSTRUCTOR_THROW").build();

spotbugs/src/main/java/edu/umd/cs/findbugs/detect/ConstructorThrow.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ private String getCalledMethodFQN(int seen) {
375375
}
376376
} else {
377377
return String.format("%s.%s : %s", getDottedClassConstantOperand(), getNameConstantOperand(),
378-
getSigConstantOperand());
378+
toDotted(getSigConstantOperand()));
379379
}
380380
return "";
381381
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package constructorthrow;
2+
3+
/**
4+
* Calling a static function with not void or primitive return value from the constructor,
5+
* which throws an unchecked exception.
6+
*/
7+
public class ConstructorThrowTest21 {
8+
public ConstructorThrowTest21() {
9+
ConstructorThrowTest21.test();
10+
}
11+
12+
private static String test() {
13+
throw new IllegalStateException();
14+
}
15+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package constructorthrow;
2+
3+
import java.io.IOException;
4+
5+
/**
6+
* Calling a static function with not void or primitive return value from the constructor,
7+
* which throws a checked exception.
8+
*/
9+
public class ConstructorThrowTest22 {
10+
public ConstructorThrowTest22() throws IOException {
11+
ConstructorThrowTest22.test();
12+
}
13+
14+
private static String test() throws IOException {
15+
throw new IOException();
16+
}
17+
}

0 commit comments

Comments
 (0)