-
Notifications
You must be signed in to change notification settings - Fork 10
Self comparison and assignment #45
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
Merged
keizer619
merged 13 commits into
ballerina-platform:main
from
SasinduDilshara:self_comparison_and_assignment
Mar 16, 2025
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
9705314
Add static rule for self comparison operators
SasinduDilshara 34dbcdb
Add static rule for trivial operations
SasinduDilshara 0f212e1
Add self assignment static analysis rule
SasinduDilshara f57ab0f
Add tests for indexed expression nodes
SasinduDilshara 2cb6eec
Merge branch 'main' of https://github.com/ballerina-platform/static-c…
SasinduDilshara b541605
Fix tests in self comparison static rule
SasinduDilshara d6bade8
Update tests for trivial operations related rules
SasinduDilshara 0a3fec4
Update self assignment rule descriptions
SasinduDilshara 1f98e73
Update the rule ids
SasinduDilshara 3a2bf18
Merge branch 'main' of https://github.com/ballerina-platform/static-c…
SasinduDilshara ff0badc
Update log rules
SasinduDilshara 69701bb
Merge branch 'main' of https://github.com/ballerina-platform/static-c…
SasinduDilshara e8663fb
Fix checkstyle failures
SasinduDilshara File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
scan-command/src/main/java/io/ballerina/scan/utils/ScanCodeAnalyzerUtils.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| /* | ||
| * Copyright (c) 2025, WSO2 LLC. (https://www.wso2.com). | ||
| * | ||
| * WSO2 LLC. licenses this file to you under the Apache License, | ||
| * Version 2.0 (the "License"); you may not use this file except | ||
| * in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| package io.ballerina.scan.utils; | ||
|
|
||
| import io.ballerina.compiler.syntax.tree.BasicLiteralNode; | ||
| import io.ballerina.compiler.syntax.tree.BuiltinSimpleNameReferenceNode; | ||
| import io.ballerina.compiler.syntax.tree.ExpressionNode; | ||
| import io.ballerina.compiler.syntax.tree.FieldAccessExpressionNode; | ||
| import io.ballerina.compiler.syntax.tree.IndexedExpressionNode; | ||
| import io.ballerina.compiler.syntax.tree.Node; | ||
| import io.ballerina.compiler.syntax.tree.QualifiedNameReferenceNode; | ||
| import io.ballerina.compiler.syntax.tree.SeparatedNodeList; | ||
| import io.ballerina.compiler.syntax.tree.SimpleNameReferenceNode; | ||
| import io.ballerina.compiler.syntax.tree.SyntaxKind; | ||
| import io.ballerina.compiler.syntax.tree.UnaryExpressionNode; | ||
| import io.ballerina.projects.Document; | ||
| import io.ballerina.scan.Rule; | ||
| import io.ballerina.scan.ScannerContext; | ||
| import io.ballerina.tools.diagnostics.Location; | ||
|
|
||
| /** | ||
| * {@code ScanCodeAnalyzerUtils} contains the util functions for the static code analysis. | ||
| * | ||
| * @since 0.1.0 | ||
| * */ | ||
| public class ScanCodeAnalyzerUtils { | ||
|
SasinduDilshara marked this conversation as resolved.
|
||
| public static void reportIssue(ScannerContext scannerContext, Document document, Node node, Rule rule) { | ||
| scannerContext.getReporter().reportIssue(document, node.location(), rule); | ||
| } | ||
|
|
||
| public static void reportIssue(ScannerContext scannerContext, Document document, Location location, Rule rule) { | ||
| scannerContext.getReporter().reportIssue(document, location, rule); | ||
| } | ||
|
|
||
| public static boolean isSameSimpleExpression(Node n1, Node n2) { | ||
| if (n1 instanceof SimpleNameReferenceNode lhsExp && n2 instanceof SimpleNameReferenceNode rhsExpr) { | ||
| return lhsExp.name().text().equals(rhsExpr.name().text()); | ||
| } | ||
|
|
||
| if (n1 instanceof QualifiedNameReferenceNode lhsExp && n2 instanceof QualifiedNameReferenceNode rhsExpr) { | ||
| return lhsExp.modulePrefix() != null && rhsExpr.modulePrefix() != null | ||
| && lhsExp.modulePrefix().text().equals(rhsExpr.modulePrefix().text()) | ||
| && lhsExp.identifier().text().equals(rhsExpr.identifier().text()); | ||
| } | ||
|
|
||
| if (n1 instanceof FieldAccessExpressionNode lhsExp && n2 instanceof FieldAccessExpressionNode rhsExpr) { | ||
| // only the simple field access expressions will be considered. | ||
| return isSameSimpleExpression(lhsExp.fieldName(), rhsExpr.fieldName()) | ||
| && isSameSimpleExpression(lhsExp.expression(), rhsExpr.expression()); | ||
| } | ||
|
|
||
| if (n1 instanceof IndexedExpressionNode lhsExp && n2 instanceof IndexedExpressionNode rhsExpr) { | ||
| // only the simple field access expressions will be considered. | ||
| return isSameSimpleExpression(lhsExp.containerExpression(), rhsExpr.containerExpression()) | ||
| && isSameKeyExpression(lhsExp.keyExpression(), rhsExpr.keyExpression()); | ||
| } | ||
|
|
||
| if (n1 instanceof BuiltinSimpleNameReferenceNode lhsExp | ||
| && n2 instanceof BuiltinSimpleNameReferenceNode rhsExpr) { | ||
| return lhsExp.name().text().equals(rhsExpr.name().text()); | ||
| } | ||
|
|
||
| if (n1 instanceof BasicLiteralNode lhsExp && n2 instanceof BasicLiteralNode rhsExpr) { | ||
| return lhsExp.literalToken().text().equals(rhsExpr.literalToken().text()); | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| private static boolean isSameKeyExpression(SeparatedNodeList<ExpressionNode> lhsKeyExpNodes, | ||
| SeparatedNodeList<ExpressionNode> rhsKeyExpNodes) { | ||
| if (lhsKeyExpNodes.size() != rhsKeyExpNodes.size()) { | ||
| return false; | ||
| } | ||
|
|
||
| for (int i = 0; i < lhsKeyExpNodes.size(); i++) { | ||
| if (!isSameSimpleExpression(lhsKeyExpNodes.get(i), rhsKeyExpNodes.get(i))) { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| public static boolean isDefinedQualifiedNameReference(Node node, String module, String identifier) { | ||
| if (node instanceof QualifiedNameReferenceNode ref) { | ||
| return ref.modulePrefix() != null && ref.identifier() != null | ||
| && ref.modulePrefix().text().equals(module) && ref.identifier().text().equals(identifier); | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| public static boolean isEqualToProvidedLiteralIdentifier(Node node, String identifierName) { | ||
| if (identifierName.equals(Constants.Token.MINUS_ONE)) { | ||
| if (node instanceof UnaryExpressionNode exp) { | ||
| return exp.unaryOperator().kind().equals(SyntaxKind.MINUS_TOKEN) | ||
| && isEqualToProvidedLiteralIdentifier(exp.expression(), Constants.Token.ONE); | ||
| } | ||
| return false; | ||
| } | ||
| if (node instanceof BasicLiteralNode ref) { | ||
| return ref.literalToken().text().equals(identifierName); | ||
| } | ||
| return false; | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.