-
Notifications
You must be signed in to change notification settings - Fork 713
SONARJAVA-5754 : Increment and decrement operators (++/--) should not be used with floating point variables #5371
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
java-checks/src/main/java/org/sonar/java/checks/IncDecOnFPCheck.java
Outdated
Show resolved
Hide resolved
java-checks/src/main/java/org/sonar/java/checks/IncDecOnFPCheck.java
Outdated
Show resolved
Hide resolved
java-checks/src/main/java/org/sonar/java/checks/IncDecOnFPCheck.java
Outdated
Show resolved
Hide resolved
| if ( | ||
| tree instanceof UnaryExpressionTree unaryExpr && | ||
| unaryExpr.expression() instanceof IdentifierTree identifier && | ||
| // above condition should always be true, just used to pattern match safely |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Conditions that are always be true have an annoying habit of triggering low code coverage ;) Let me know if this happens and I'll show you the workaround.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes it seems it has an impact on branch coverage, I'm interested in knowing more about this workaround
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You wrap tree in an optional and convert if-then into fuctional programming. It looks weird and is unreadable, but the coverage does not look inside :)
Optional.of(tree)
.filter(UnaryExpressionTree.class::isInstance)
.map(UnaryExpressionTree.class::cast)
.map(UnaryExpressionTree::expression)
.filter(IdentifierTree.class::isInstance)
.map(IdentifierTree.class::cast)
.filter(identifier -> isFloatingPoint(identifier.symbolType()))
.ifPresent(identifier -> reportIssue(...));
java-checks/src/test/java/org/sonar/java/checks/IncDecOnFPCheckTest.java
Outdated
Show resolved
Hide resolved
java-checks/src/main/java/org/sonar/java/checks/IncDecOnFPCheck.java
Outdated
Show resolved
Hide resolved
tomasz-tylenda-sonarsource
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll take another look when the rspec is merged.
| if ( | ||
| tree instanceof UnaryExpressionTree unaryExpr && | ||
| unaryExpr.expression() instanceof IdentifierTree identifier && | ||
| // above condition should always be true, just used to pattern match safely |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You wrap tree in an optional and convert if-then into fuctional programming. It looks weird and is unreadable, but the coverage does not look inside :)
Optional.of(tree)
.filter(UnaryExpressionTree.class::isInstance)
.map(UnaryExpressionTree.class::cast)
.map(UnaryExpressionTree::expression)
.filter(IdentifierTree.class::isInstance)
.map(IdentifierTree.class::cast)
.filter(identifier -> isFloatingPoint(identifier.symbolType()))
.ifPresent(identifier -> reportIssue(...));
java-checks/src/test/java/org/sonar/java/checks/IncDecOnFPCheckTest.java
Outdated
Show resolved
Hide resolved
java-checks/src/test/java/org/sonar/java/checks/IncDecOnFloatingPointCheckTest.java
Show resolved
Hide resolved
0975e01 to
e67b2df
Compare
|




Waiting on this PR to add asciidoc and metadata