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-113 Rewrite tests IdenticalBinaryOperandCheckTest - OctalValuesCheckTest #86

Merged
merged 10 commits into from
Jan 20, 2025

Conversation

GabinL21
Copy link
Contributor

@GabinL21 GabinL21 commented Jan 10, 2025

@GabinL21 GabinL21 self-assigned this Jan 10, 2025
Copy link

@GabinL21
Copy link
Contributor Author

Coverage is 100%, no follow-up ticket to rework the checks needed

Comment on lines +50 to +52
SwitchCaseTooBigCheck.class,
SwitchWithoutDefaultCheck.class,
NestedSwitchCheck.class,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Renamed to be more Go-specific

@@ -29,17 +29,16 @@
@Rule(key = "S1764")
public class IdenticalBinaryOperandCheck implements SlangCheck {

public static final String MESSAGE = "Correct one of the identical sub-expressions on both sides this operator";
Copy link
Contributor Author

Choose a reason for hiding this comment

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

To trigger SQS coverage

@@ -57,19 +56,19 @@ private static List<Tree> collectConditions(MatchTree matchTree) {

private static List<Tree> collectConditions(IfTree ifTree, List<Tree> list) {
list.add(skipParentheses(ifTree.condition()));
Tree elseBranch = ifTree.elseBranch();
var elseBranch = ifTree.elseBranch();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

To trigger SQS coverage

@@ -44,16 +44,16 @@ public class IfConditionalAlwaysTrueOrFalseCheck implements SlangCheck {
@Override
public void initialize(InitContext init) {
init.register(IfTree.class, (ctx, ifTree) -> {
Tree condition = ifTree.condition();
var condition = ifTree.condition();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

To trigger SQS coverage

.ifPresent(parentMatch -> reportIssue(ctx, matchTree)));
}

private static void reportIssue(CheckContext ctx, MatchTree matchTree) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

To trigger SQS coverage

@@ -42,7 +42,7 @@ public void initialize(InitContext init) {

private static boolean isException(IntegerLiteralTree literalTree) {
// octal literal < 8 are authorized, as well as octal literals with 3 digits, as they are often used for file permissions
BigInteger value = literalTree.getIntegerValue();
var value = literalTree.getIntegerValue();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

To trigger SQS coverage

@@ -40,7 +40,7 @@ public class MatchCaseTooBigCheck implements SlangCheck {
@Override
public void initialize(InitContext init) {
init.register(MatchCaseTree.class, (ctx, matchCaseTree) -> {
int linesOfCode = matchCaseTree.metaData().linesOfCode().size();
var linesOfCode = matchCaseTree.metaData().linesOfCode().size();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

To trigger SQS coverage


@Override
public void initialize(InitContext init) {
init.register(MatchTree.class, (ctx, tree) -> {
if (tree.cases().stream().noneMatch(matchCase -> matchCase.expression() == null)) {
Token keyword = tree.keyword();
String message = String.format("Add a default clause to this \"%s\" statement.", keyword.text());
var keyword = tree.keyword();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

To trigger SQS coverage

return 1
}

if n := 3; true { // False negative
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Created SONARGO-119

return 1
} else if cond && false { // Noncompliant
return 1
} else if isFoo() && false { // Compliant, isFoo can have a side-effect
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure if it's a correct exception or if we should raise an issue (and create a follow-up ticket)

In the original Slang tests, this case was not documented. It's not a properly defined exception in the check's code either 🤔

Copy link
Contributor

@petertrr petertrr Jan 20, 2025

Choose a reason for hiding this comment

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

For me this would definitely be a problematic code. However, the existing error message Remove this useless if is not very well applicable here. Maybe we have another rule that can flag this?

Edit: however, this rule also checks boolean expressions with variables, so maybe we can just customize error messages to smth like Remove this useless if statement: condition will always have the same value. If call to isFoo is required, extract it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe we have another rule that can flag this?

Indeed, it should be covered by S1125, actually, as it is done in Java and other flagship languages 😄

@GabinL21 GabinL21 requested a review from petertrr January 10, 2025 16:21
Copy link
Contributor

This PR is stale because it has been open 7 days with no activity. If there is no activity in the next 7 days it will be closed automatically

@github-actions github-actions bot added the stale label Jan 18, 2025
Copy link
Contributor

@petertrr petertrr left a comment

Choose a reason for hiding this comment

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

LGTM!

@GabinL21 GabinL21 merged commit 64e8b1e into master Jan 20, 2025
21 checks passed
@GabinL21 GabinL21 deleted the SONARGO-113 branch January 20, 2025 10:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants