Skip to content

Commit

Permalink
SONARJAVA-2944: reduce scope of the rule to avoid FP
Browse files Browse the repository at this point in the history
  • Loading branch information
quentin-jaquier-sonarsource authored and Wohops committed May 14, 2020
1 parent b77f2b8 commit 594d6f6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void unreachable(boolean cond) {
// ...
} catch (IOException e) { // Compliant
// ...
} catch (CustomException e) { // Noncompliant [[sc=14;ec=29;secondary=67]]
} catch (CustomException e) { // FN, line 67 hide this catch, but we don't support correctly the presence of another type of Exception
// ...
}

Expand All @@ -77,7 +77,7 @@ void unreachable(boolean cond) {
throwIOException();
} catch (CustomDerivedException | IOException e) {
// ...
} catch (CustomException e) { // Noncompliant
} catch (CustomException e) { // FN, we don't support correctly the presence of another type of Exception
// ...
}

Expand Down Expand Up @@ -215,6 +215,23 @@ public void close() throws FileNotFoundException {
// ...
}

try {
throwCustomDerivedException();
throwOtherCustomDerivedException();
} catch (CustomDerivedException e) {
// ...
} catch (CustomException e) { // Compliant
// ...
}

try {
throwBoth();
} catch (CustomDerivedException e) {
// ...
} catch (CustomException e) { // Compliant
// ...
}

}

void throwCustomException() throws CustomException {
Expand All @@ -225,6 +242,13 @@ void throwCustomDerivedException() throws CustomDerivedException {
throw new CustomDerivedException();
}

void throwBoth() throws CustomDerivedException,OtherCustomDerivedException {
}

void throwOtherCustomDerivedException() throws OtherCustomDerivedException {
throw new OtherCustomDerivedException();
}

void throwCustomDerivedDerivedException() throws CustomDerivedDerivedException {
throw new CustomDerivedDerivedException();
}
Expand All @@ -247,6 +271,9 @@ public static class CustomException extends Exception {
public static class CustomDerivedException extends CustomException {
}

public static class OtherCustomDerivedException extends CustomException {
}

public static class CustomDerivedDerivedException extends CustomDerivedException {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,12 @@ private static boolean isChecked(Type type) {
}

private static boolean isHiding(Type derivedType, List<Type> thrownTypes) {
return thrownTypes.stream().allMatch(thrownType ->
// Only throwing a subtype of the first caught exception, hiding the base one
thrownType.isSubtypeOf(derivedType) ||
// Or throwing an unrelated exception
(!thrownType.isUnknown() && !derivedType.isUnknown() && !derivedType.isSubtypeOf(thrownType))
);
// All thrown exceptions are caught by the subtype exception, hiding the base one
// This logic could be improved to remove FN, but it's not trivial and source of FP.
return thrownTypes.stream()
.allMatch(thrownType ->
thrownType.isSubtypeOf(derivedType)
);
}

private static class ThrownExceptionCollector extends BaseTreeVisitor {
Expand Down

0 comments on commit 594d6f6

Please sign in to comment.