Skip to content
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

SONARGO-111 Rewrite tests AllBranchesIdenticalCheckTest - EmptyBlockCheckTest #90

Closed
wants to merge 10 commits into from

Conversation

rudy-regazzoni-sonarsource
Copy link
Contributor

@rudy-regazzoni-sonarsource rudy-regazzoni-sonarsource commented Jan 20, 2025

Comment on lines -43 to -49
init.register(IfTree.class, (ctx, ifTree) -> {
if (isIfWithMaxTwoBranches(ctx.parent(), ifTree) && !hasBlockBranch(ifTree)) {
getBooleanLiteral(ifTree.thenBranch(), ifTree.elseBranch())
.ifPresent(booleanLiteral -> ctx.reportIssue(booleanLiteral, MESSAGE));
}
});

Copy link
Contributor Author

Choose a reason for hiding this comment

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

IF cannot be inlined in GO, so removing this part of the code.

@@ -87,7 +88,7 @@ private static boolean hasMinimumSize(FunctionDeclarationTree function) {
if (functionBody == null) {
return false;
}
return functionBody.statementOrExpressions().size() >= MINIMUM_STATEMENTS_COUNT;
return functionBody.statementOrExpressions().stream().filter(TreeUtils.IS_NOT_SEMICOLON).count() >= MINIMUM_STATEMENTS_COUNT;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Semicolon are threated as statements so ignoring them.

Comment on lines 100 to 109
private static boolean isPanicCall(Tree tree) {
return tree instanceof NativeTree exptStmtNativeTree
&& exptStmtNativeTree.nativeKind().toString().equals("[0](ExprStmt)")
&& exptStmtNativeTree.children().size() == 1
&& exptStmtNativeTree.children().get(0) instanceof NativeTree callExprNativeTree
&& callExprNativeTree.nativeKind().toString().equals("X(CallExpr)")
&& !callExprNativeTree.children().isEmpty()
&& callExprNativeTree.children().get(0) instanceof IdentifierTree identifierTree
&& identifierTree.name().equals("panic");
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

There is now throw in Go, but Panic is relatively equivalent in that use case (left the normal execution flow).

Copy link
Contributor

@mstachniuk mstachniuk left a comment

Choose a reason for hiding this comment

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

Good progress on it!
Please look on my suggestion and improve the Go code examples to have move valid syntax.

@@ -44,11 +49,11 @@ public void initialize(InitContext init) {
}

IfTree prevTree = ifTree;
boolean endsWithReturn = endsWithReturnBreakOrThrow(ifTree);
boolean endsWithReturn = endsWithReturnBreakOrPanic(ifTree);
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure if we should change the behaviour of this rule 🤔 Before it raised when panic() was called in last if else statement, what we can see in its/ruling/src/integrationTest/resources/expected/go-S126.json change.
IMO this rule should raise issues even if one of the branch contains throw/return/break/panic().

The rule is not in Sonar_Way so I didn't found any issue on "Next" and the impact is low so it can be as it is now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This change is just about replacing throw by panic, which could be considered the equivalent in Go (at least in this context of execution flow). Before, it simply ignored any panic call.
To me, the new issue raised in its/ruling/src/integrationTest/resources/expected/go-S126.json looks like a TP according to the rule behavior.
I agree that I don't see the why of the logic around throw/return/break/panic(), but it would remove most logic implemented in the check, which would be a significant impact, probably outside of the scope of simply migrating this rule to go 😅

Copy link

Copy link
Contributor

@mstachniuk mstachniuk left a comment

Choose a reason for hiding this comment

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

LGTM

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