-
Notifications
You must be signed in to change notification settings - Fork 1
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
Conversation
init.register(IfTree.class, (ctx, ifTree) -> { | ||
if (isIfWithMaxTwoBranches(ctx.parent(), ifTree) && !hasBlockBranch(ifTree)) { | ||
getBooleanLiteral(ifTree.thenBranch(), ifTree.elseBranch()) | ||
.ifPresent(booleanLiteral -> ctx.reportIssue(booleanLiteral, MESSAGE)); | ||
} | ||
}); | ||
|
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.
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; |
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.
Semicolon are threated as statements so ignoring them.
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"); | ||
} |
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.
There is now throw
in Go, but Panic is relatively equivalent in that use case (left the normal execution flow).
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.
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); |
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'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.
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.
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 😅
sonar-go-checks/src/main/java/org/sonar/go/checks/ElseIfWithoutElseCheck.java
Show resolved
Hide resolved
sonar-go-checks/src/test/resources/checks/BadFunctionNameCheck/bad_function_name.go
Outdated
Show resolved
Hide resolved
sonar-go-checks/src/test/resources/checks/BadFunctionNameCheck/bad_function_name.go
Show resolved
Hide resolved
sonar-go-checks/src/test/resources/checks/BooleanInversionCheck/boolean_inversion.go
Outdated
Show resolved
Hide resolved
...resources/checks/DuplicatedFunctionImplementationCheck/duplicated_function_implementation.go
Outdated
Show resolved
Hide resolved
...resources/checks/DuplicatedFunctionImplementationCheck/duplicated_function_implementation.go
Outdated
Show resolved
Hide resolved
...resources/checks/DuplicatedFunctionImplementationCheck/duplicated_function_implementation.go
Outdated
Show resolved
Hide resolved
...resources/checks/DuplicatedFunctionImplementationCheck/duplicated_function_implementation.go
Outdated
Show resolved
Hide resolved
...resources/checks/DuplicatedFunctionImplementationCheck/duplicated_function_implementation.go
Outdated
Show resolved
Hide resolved
|
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.
LGTM
SONARGO-111