Skip to content

Conversation

@rombirli
Copy link

@rombirli rombirli commented Dec 3, 2025

Waiting on this PR to add asciidoc and metadata

- Missing rule asciidoc/metadata yet
@hashicorp-vault-sonar-prod
Copy link

hashicorp-vault-sonar-prod bot commented Dec 3, 2025

SONARJAVA-5754

if (
tree instanceof UnaryExpressionTree unaryExpr &&
unaryExpr.expression() instanceof IdentifierTree identifier &&
// above condition should always be true, just used to pattern match safely
Copy link
Contributor

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.

Copy link
Author

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

class IncDecOnFPCheckTest {

@Test
void detected() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We ideally want to add a test .withoutSemantic() as well. It tests our checks in the scenario where the code is not analyzed as thouroughly as we ideally would likt.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added it, runs exactly the same test (same java check file), and no test fail, I'm not sure to understand how

// above condition should always be true, just used to pattern match safely
(identifier.symbolType().isPrimitive(Primitives.FLOAT) || identifier.symbolType().isPrimitive(Primitives.DOUBLE))
) {
reportIssue(unaryExpr, "Increment and decrement operators (++/--) should not be used with floating point variables");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think it is better to have one common error message or one for ++ and another for --? I have no particular opinion, but if you prefer just one, then I'd like to know why.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's an interesting question, I would say it yes we can distinguish between both in the error message

Cons :
  • The code is a bit more complex.
Pros :
  • Message is more natural : makes it easier to read for the customer
  • Simplicity of implementation : it only requires a string formatting and don't require duplicating the message for both case

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants