SONARJAVA-6305 Fix the expected exceptions filter to work without semantic information#5626
SONARJAVA-6305 Fix the expected exceptions filter to work without semantic information#5626aurelien-coet-sonarsource wants to merge 8 commits into
Conversation
Agentic Analysis: Early ResultsAgentic Analysis and Context Augmentation are available on your project. Here are some issues that could have been prevented. Follow the links to learn how to put them into action. 5 issue(s) found across 1 file(s):
Analyzed by SonarQube Agentic Analysis in 5.5 s |
Code Review ✅ Approved 2 resolved / 2 findingsRefactored the expected exceptions filter to function without semantic information and renamed the conversion check to broaden its scope. Addressed an unchecked argument index access and a duplicate test method name. ✅ 2 resolved✅ Quality: Duplicate method name in test file
✅ Edge Case: Unchecked argument index access may throw IndexOutOfBoundsException
OptionsAuto-apply is off → Gitar will not commit updates to this branch. Comment with these commands to change:
Was this helpful? React with 👍 / 👎 | Gitar |
|
NoemieBenard
left a comment
There was a problem hiding this comment.
Overall LGTM and should work for the FPs that we have, just some comments
| } | ||
| } | ||
|
|
||
| // When semantic information is missing, the filter should conservatively activate. |
There was a problem hiding this comment.
Is it really about semantic here ?
| @Test(expected = DateTimeException) | ||
| void wrongExpectedType() { | ||
| Instant.from(date); // WithIssue | ||
| } | ||
|
|
||
| @Test(expect = DateTimeException.class) | ||
| void wrongAnnotationAttribute() { | ||
| Instant.from(date); // WithIssue | ||
| } | ||
|
|
||
| @Test | ||
| void wrongAssertMethodSignature() { | ||
| assertThrows(Instant.from(date)); // WithIssue | ||
| catchThrowableOfType(Instant.from(date)); // WithIssue |
There was a problem hiding this comment.
I wonder if we are not being too strict with these tests, we could prevent the filter from activating on patterns that we don't recognize even though they are correct. (but maybe we can keep it as it is for now, WDYT?)
| if (expression.is(Tree.Kind.MEMBER_SELECT)) { | ||
| MemberSelectExpressionTree memberSelect = (MemberSelectExpressionTree) expression; | ||
| if ("class".equals(memberSelect.identifier().name())) { | ||
| return Optional.of(memberSelect.expression().symbolType()); | ||
| } | ||
| } |
There was a problem hiding this comment.
This looks nicer IMO:
| if (expression instanceof MemberSelectExpressionTree memberSelect | |
| && "class".equals(memberSelect.identifier().name())) { | |
| return Optional.of(memberSelect.expression().symbolType()); | |
| } |
| import org.sonar.plugins.java.api.tree.UnionTypeTree; | ||
|
|
||
| import static org.sonar.java.checks.helpers.MethodTreeUtils.consecutiveMethodInvocation; | ||
| import static org.sonar.java.checks.helpers.UnitTestUtils.isTryCatchFail; |
There was a problem hiding this comment.
The unused import should be removed




Summary by Gitar
InstantConversionsChecktoDateTimeConversionsCheckto reflect its broadened scope.LocalDate,LocalTime, andZonedDateTimetypes.ExpectedExceptionFilterto allow these conversions to work correctly without full semantic information.DateTimeConversionsCheckSampleandDateTimeConversionsCheckTestto verify the new detection logic.ExpectedExceptionFilter.javato validate filter behavior with various exception types and signatures.This will update automatically on new commits.