From eb08cccde0c74219637856b4ca6d445d96a5e870 Mon Sep 17 00:00:00 2001 From: Anupama Pathirage Date: Mon, 23 Sep 2024 18:16:37 -0500 Subject: [PATCH 01/18] Create CODEOWNERS file --- .github/CODEOWNERS | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 .github/CODEOWNERS diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS new file mode 100644 index 0000000..c72bea2 --- /dev/null +++ b/.github/CODEOWNERS @@ -0,0 +1,4 @@ +# See: https://help.github.com/articles/about-codeowners/ + +# These owners will be the default owners for everything in the repo. +* @MaryamZi From ddba9f1f6508fec3eadc97ecca682960e8100ff6 Mon Sep 17 00:00:00 2001 From: Anupama Pathirage Date: Tue, 1 Oct 2024 14:55:12 -0500 Subject: [PATCH 02/18] Update CODEOWNERS --- .github/CODEOWNERS | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index c72bea2..d9d3955 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -1,4 +1,4 @@ # See: https://help.github.com/articles/about-codeowners/ # These owners will be the default owners for everything in the repo. -* @MaryamZi +* @azinneera From 3f0b23333a5b2a2dd100f554ebf93bf46e35d0ac Mon Sep 17 00:00:00 2001 From: Sasindu Alahakoon Date: Wed, 2 Oct 2024 23:03:39 +0530 Subject: [PATCH 03/18] Add static rule implementation for unused func params --- .../io/ballerina/scan/internal/CoreRule.java | 5 +- .../scan/internal/StaticCodeAnalyzer.java | 40 ++++- .../io/ballerina/scan/utils/Constants.java | 8 + .../scan/utils/ScanCodeAnalyzerUtils.java | 12 ++ .../scan/internal/StaticCodeAnalyzerTest.java | 146 ++++++++++++++- .../core-rules/unused_func_parameters.bal | 166 ++++++++++++++++++ 6 files changed, 366 insertions(+), 11 deletions(-) create mode 100644 scan-command/src/main/java/io/ballerina/scan/utils/ScanCodeAnalyzerUtils.java create mode 100644 scan-command/src/test/resources/test-resources/core-rules/unused_func_parameters.bal diff --git a/scan-command/src/main/java/io/ballerina/scan/internal/CoreRule.java b/scan-command/src/main/java/io/ballerina/scan/internal/CoreRule.java index 211049e..2043871 100644 --- a/scan-command/src/main/java/io/ballerina/scan/internal/CoreRule.java +++ b/scan-command/src/main/java/io/ballerina/scan/internal/CoreRule.java @@ -20,6 +20,7 @@ import io.ballerina.scan.Rule; import io.ballerina.scan.RuleKind; +import io.ballerina.scan.utils.Constants; import java.util.ArrayList; import java.util.List; @@ -30,7 +31,9 @@ * @since 0.1.0 * */ enum CoreRule { - AVOID_CHECKPANIC(RuleFactory.createRule(1, "Avoid checkpanic", RuleKind.CODE_SMELL)); + AVOID_CHECKPANIC(RuleFactory.createRule(1, Constants.RuleDescription.AVOID_CHECKPANIC, RuleKind.CODE_SMELL)), + UNUSED_FUNCTION_PARAMETERS(RuleFactory.createRule(4, + Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL)); private final Rule rule; diff --git a/scan-command/src/main/java/io/ballerina/scan/internal/StaticCodeAnalyzer.java b/scan-command/src/main/java/io/ballerina/scan/internal/StaticCodeAnalyzer.java index d7cf2c0..32f9d4a 100644 --- a/scan-command/src/main/java/io/ballerina/scan/internal/StaticCodeAnalyzer.java +++ b/scan-command/src/main/java/io/ballerina/scan/internal/StaticCodeAnalyzer.java @@ -18,7 +18,10 @@ package io.ballerina.scan.internal; +import io.ballerina.compiler.api.SemanticModel; import io.ballerina.compiler.syntax.tree.CheckExpressionNode; +import io.ballerina.compiler.syntax.tree.FunctionSignatureNode; +import io.ballerina.compiler.syntax.tree.MethodCallExpressionNode; import io.ballerina.compiler.syntax.tree.ModulePartNode; import io.ballerina.compiler.syntax.tree.NodeVisitor; import io.ballerina.compiler.syntax.tree.SyntaxKind; @@ -26,6 +29,8 @@ import io.ballerina.projects.Document; import io.ballerina.scan.ScannerContext; +import static io.ballerina.scan.utils.ScanCodeAnalyzerUtils.reportIssue; + /** * {@code StaticCodeAnalyzer} contains the logic to perform core static code analysis on Ballerina documents. * @@ -35,11 +40,13 @@ class StaticCodeAnalyzer extends NodeVisitor { private final Document document; private final SyntaxTree syntaxTree; private final ScannerContext scannerContext; + private final SemanticModel semanticModel; StaticCodeAnalyzer(Document document, ScannerContextImpl scannerContext) { this.document = document; this.syntaxTree = document.syntaxTree(); this.scannerContext = scannerContext; + semanticModel = document.module().getCompilation().getSemanticModel(); } void analyze() { @@ -54,8 +61,39 @@ void analyze() { @Override public void visit(CheckExpressionNode checkExpressionNode) { if (checkExpressionNode.checkKeyword().kind().equals(SyntaxKind.CHECKPANIC_KEYWORD)) { - scannerContext.getReporter().reportIssue(document, checkExpressionNode.location(), + reportIssue(scannerContext, document, checkExpressionNode, CoreRule.AVOID_CHECKPANIC.rule()); } } + + @Override + public void visit(ModulePartNode modulePartNode) { + modulePartNode.members().forEach(member -> member.accept(this)); + } + + @Override + public void visit(FunctionSignatureNode functionSignatureNode) { + functionSignatureNode.parameters().forEach(parameter -> { + semanticModel.symbol(parameter).ifPresent(symbol -> { + if (semanticModel.references(symbol).size() == 1) { + reportIssue(scannerContext, document, parameter, + CoreRule.UNUSED_FUNCTION_PARAMETERS.rule()); + } + }); + parameter.accept(this); + }); + } + + @Override + public void visit(MethodCallExpressionNode methodCallExpressionNode) { + methodCallExpressionNode.arguments().forEach(argument -> { + semanticModel.symbol(argument).ifPresent(symbol -> { + if (semanticModel.references(symbol).size() == 1) { + reportIssue(scannerContext, document, argument, + CoreRule.UNUSED_FUNCTION_PARAMETERS.rule()); + } + }); + argument.accept(this); + }); + } } diff --git a/scan-command/src/main/java/io/ballerina/scan/utils/Constants.java b/scan-command/src/main/java/io/ballerina/scan/utils/Constants.java index 372de85..cfc200e 100644 --- a/scan-command/src/main/java/io/ballerina/scan/utils/Constants.java +++ b/scan-command/src/main/java/io/ballerina/scan/utils/Constants.java @@ -62,6 +62,14 @@ public class Constants { static final String RULE_DESCRIPTION_COLUMN = "Rule Description"; static final String[] RULE_PRIORITY_LIST = {"ballerina", "ballerina/", "ballerinax/", "wso2/"}; + public static class RuleDescription { + public static final String AVOID_CHECKPANIC = "Avoid checkpanic"; + public static final String UNUSED_FUNCTION_PARAMETERS = "Unused function parameters"; + + private RuleDescription() { + } + } + private Constants() { } } diff --git a/scan-command/src/main/java/io/ballerina/scan/utils/ScanCodeAnalyzerUtils.java b/scan-command/src/main/java/io/ballerina/scan/utils/ScanCodeAnalyzerUtils.java new file mode 100644 index 0000000..5b294a0 --- /dev/null +++ b/scan-command/src/main/java/io/ballerina/scan/utils/ScanCodeAnalyzerUtils.java @@ -0,0 +1,12 @@ +package io.ballerina.scan.utils; + +import io.ballerina.compiler.syntax.tree.Node; +import io.ballerina.projects.Document; +import io.ballerina.scan.Rule; +import io.ballerina.scan.ScannerContext; + +public class ScanCodeAnalyzerUtils { + public static void reportIssue(ScannerContext scannerContext, Document document, Node node, Rule rule) { + scannerContext.getReporter().reportIssue(document, node.location(), rule); + } +} \ No newline at end of file diff --git a/scan-command/src/test/java/io/ballerina/scan/internal/StaticCodeAnalyzerTest.java b/scan-command/src/test/java/io/ballerina/scan/internal/StaticCodeAnalyzerTest.java index 242b11e..8718ec6 100644 --- a/scan-command/src/test/java/io/ballerina/scan/internal/StaticCodeAnalyzerTest.java +++ b/scan-command/src/test/java/io/ballerina/scan/internal/StaticCodeAnalyzerTest.java @@ -27,6 +27,8 @@ import io.ballerina.scan.Rule; import io.ballerina.scan.RuleKind; import io.ballerina.scan.Source; +import io.ballerina.scan.utils.Constants; +import io.ballerina.tools.text.LinePosition; import io.ballerina.tools.text.LineRange; import org.testng.Assert; import org.testng.annotations.Test; @@ -57,18 +59,144 @@ void testCheckpanicAnalyzer() { staticCodeAnalyzer.analyze(); List issues = scannerContext.getReporter().getIssues(); Assert.assertEquals(issues.size(), 1); - Issue issue = issues.get(0); + assertIssue(issues.get(0), documentName, 20, 17, 20, 39, "ballerina:1", 1, + Constants.RuleDescription.AVOID_CHECKPANIC, RuleKind.CODE_SMELL); + } + + @Test(description = "test checkpanic analyzer") + void testUnusedFunctionParameterAnalyzer() { + String documentName = "unused_func_parameters.bal"; + Document document = loadDocument(documentName); + ScannerContextImpl scannerContext = new ScannerContextImpl(List.of(CoreRule.UNUSED_FUNCTION_PARAMETERS.rule())); + StaticCodeAnalyzer staticCodeAnalyzer = new StaticCodeAnalyzer(document, scannerContext); + staticCodeAnalyzer.analyze(); + List issues = scannerContext.getReporter().getIssues(); + Assert.assertEquals(issues.size(), 55); + assertIssue(issues.get(0), documentName, 8, 29, 8, 34, "ballerina:4", 4, + Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(1), documentName, 12, 29, 12, 34, "ballerina:4", 4, + Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(2), documentName, 14, 29, 14, 34, "ballerina:4", 4, + Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(3), documentName, 14, 36, 14, 41, "ballerina:4", 4, + Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(4), documentName, 18, 29, 18, 34, "ballerina:4", 4, + Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(5), documentName, 18, 36, 18, 41, "ballerina:4", 4, + Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(6), documentName, 24, 29, 24, 38, "ballerina:4", 4, + Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(7), documentName, 28, 29, 28, 38, "ballerina:4", 4, + Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(8), documentName, 32, 23, 32, 28, "ballerina:4", 4, + Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(9), documentName, 32, 30, 32, 38, "ballerina:4", 4, + Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(10), documentName, 34, 23, 34, 28, "ballerina:4", 4, + Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(11), documentName, 34, 30, 34, 38, "ballerina:4", 4, + Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(12), documentName, 45, 33, 45, 38, "ballerina:4", 4, + Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(13), documentName, 49, 33, 49, 38, "ballerina:4", 4, + Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(14), documentName, 51, 33, 51, 38, "ballerina:4", 4, + Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(15), documentName, 51, 40, 51, 45, "ballerina:4", 4, + Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(16), documentName, 55, 33, 55, 38, "ballerina:4", 4, + Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(17), documentName, 55, 40, 55, 45, "ballerina:4", 4, + Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(18), documentName, 61, 33, 61, 42, "ballerina:4", 4, + Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(19), documentName, 65, 33, 65, 42, "ballerina:4", 4, + Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(20), documentName, 69, 27, 69, 32, "ballerina:4", 4, + Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(21), documentName, 69, 34, 69, 42, "ballerina:4", 4, + Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(22), documentName, 71, 27, 71, 32, "ballerina:4", 4, + Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(23), documentName, 71, 34, 71, 42, "ballerina:4", 4, + Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(24), documentName, 83, 46, 83, 51, "ballerina:4", 4, + Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(25), documentName, 87, 47, 87, 52, "ballerina:4", 4, + Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(26), documentName, 89, 47, 89, 52, "ballerina:4", 4, + Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(27), documentName, 89, 54, 89, 59, "ballerina:4", 4, + Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(28), documentName, 93, 47, 93, 52, "ballerina:4", 4, + Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(29), documentName, 93, 54, 93, 59, "ballerina:4", 4, + Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(30), documentName, 99, 47, 99, 56, "ballerina:4", 4, + Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(31), documentName, 103, 47, 103, 56, "ballerina:4", 4, + Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(32), documentName, 107, 41, 107, 46, "ballerina:4", 4, + Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(33), documentName, 107, 48, 107, 56, "ballerina:4", 4, + Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(34), documentName, 109, 41, 109, 46, "ballerina:4", 4, + Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(35), documentName, 109, 48, 109, 56, "ballerina:4", 4, + Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(36), documentName, 121, 33, 121, 38, "ballerina:4", 4, + Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(37), documentName, 125, 33, 125, 38, "ballerina:4", 4, + Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(38), documentName, 127, 33, 127, 38, "ballerina:4", 4, + Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(39), documentName, 127, 40, 127, 45, "ballerina:4", 4, + Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(40), documentName, 131, 33, 131, 38, "ballerina:4", 4, + Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(41), documentName, 131, 40, 131, 45, "ballerina:4", 4, + Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(42), documentName, 137, 33, 137, 42, "ballerina:4", 4, + Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(43), documentName, 141, 33, 141, 42, "ballerina:4", 4, + Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(44), documentName, 145, 27, 145, 32, "ballerina:4", 4, + Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(45), documentName, 145, 34, 145, 42, "ballerina:4", 4, + Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(46), documentName, 147, 27, 147, 32, "ballerina:4", 4, + Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(47), documentName, 147, 34, 147, 42, "ballerina:4", 4, + Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(48), documentName, 152, 28, 152, 33, "ballerina:4", 4, + Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(49), documentName, 154, 18, 154, 31, "ballerina:4", 4, + Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(50), documentName, 158, 19, 158, 24, "ballerina:4", 4, + Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(51), documentName, 162, 18, 162, 23, "ballerina:4", 4, + Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(52), documentName, 162, 32, 162, 37, "ballerina:4", 4, + Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(53), documentName, 163, 22, 163, 28, "ballerina:4", 4, + Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(54), documentName, 163, 30, 163, 36, "ballerina:4", 4, + Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + } + + void assertIssue(Issue issue, String documentName, int startLine, int startOffset, int endLine, int endOffset, + String ruleId, int numericId, String description, RuleKind ruleKind) { Assert.assertEquals(issue.source(), Source.BUILT_IN); LineRange location = issue.location().lineRange(); Assert.assertEquals(location.fileName(), documentName); - Assert.assertEquals(location.startLine().line(), 20); - Assert.assertEquals(location.startLine().offset(), 17); - Assert.assertEquals(location.endLine().line(), 20); - Assert.assertEquals(location.endLine().offset(), 39); + Assert.assertEquals(location.startLine().line(), startLine); + Assert.assertEquals(location.startLine().offset(), startOffset); + Assert.assertEquals(location.endLine().line(), endLine); + Assert.assertEquals(location.endLine().offset(), endOffset); Rule rule = issue.rule(); - Assert.assertEquals(rule.id(), "ballerina:1"); - Assert.assertEquals(rule.numericId(), 1); - Assert.assertEquals(rule.description(), "Avoid checkpanic"); - Assert.assertEquals(rule.kind(), RuleKind.CODE_SMELL); + Assert.assertEquals(rule.id(), ruleId); + Assert.assertEquals(rule.numericId(), numericId); + Assert.assertEquals(rule.description(), description); + Assert.assertEquals(rule.kind(), ruleKind); } } diff --git a/scan-command/src/test/resources/test-resources/core-rules/unused_func_parameters.bal b/scan-command/src/test/resources/test-resources/core-rules/unused_func_parameters.bal new file mode 100644 index 0000000..0df893a --- /dev/null +++ b/scan-command/src/test/resources/test-resources/core-rules/unused_func_parameters.bal @@ -0,0 +1,166 @@ +import ballerina/http; + +function test(int a, int b) returns int { + return a + b; +} + +function test2(int a, int b) returns int => a + b; + +function test3(int a, int b, int c) returns int { // warning + return a + b; +} + +function test4(int a, int b, int c) returns int => a + b; // warning + +function test5(int a, int b, int c, int d) returns int { // warning * 2 + return test(a, b); +} + +function test6(int a, int b, int c, int d) returns int => test(a, b); // warning * 2 + +function test7(int a, int b, int c = 3) returns int { + return a + b + c; +} + +function test8(int a, int b, int c = 3) returns int { //warning + return a + b; +} + +function test9(int a, int b, int c = 3) returns int => a + b; // warning + +function test10(int a, int b, int c = 3) returns int => c + test(a, b); + +function test11(int a, int b, int... c) returns string => a.toString(); // warning * 2 + +function test12(int a, int b, int... c) returns string { // warning * 2 + return a.toString(); +} + +class A { + function test(int a, int b) returns int { + return a + b; + } + + function test2(int a, int b) returns int => a + b; + + function test3(int a, int b, int c) returns int { // warning + return a + b; + } + + function test4(int a, int b, int c) returns int => a + b; // warning + + function test5(int a, int b, int c, int d) returns int { // warning * 2 + return test(a, b); + } + + function test6(int a, int b, int c, int d) returns int => test(a, b); // warning * 2 + + function test7(int a, int b, int c = 3) returns int { + return a + b + c; + } + + function test8(int a, int b, int c = 3) returns int { // warning + return a + b; + } + + function test9(int a, int b, int c = 3) returns int => a + b; // warning + + function test10(int a, int b, int c = 3) returns int => c + test(a, b); + + function test11(int a, int b, int... c) returns string => a.toString(); // warning * 2 + + function test12(int a, int b, int... c) returns string { // warning * 2 + return a.toString(); + } +} + +service /a on new http:Listener(8080) { + resource function get test(int a, int b) returns int { + return a + b; + } + + resource function get test2(int a, int b) returns int => a + b; + + resource function get test3(int a, int b, int c) returns int { // warning + return a + b; + } + + resource function post test4(int a, int b, int c) returns int => a + b; // warning + + resource function post test5(int a, int b, int c, int d) returns int { // warning * 2 + return test(a, b); + } + + resource function post test6(int a, int b, int c, int d) returns int => test(a, b); // warning * 2 + + resource function post test7(int a, int b, int c = 3) returns int { + return a + b + c; + } + + resource function post test8(int a, int b, int c = 3) returns int { // warning + return a + b; + } + + resource function post test9(int a, int b, int c = 3) returns int => a + b; // warning + + resource function post test10(int a, int b, int c = 3) returns int => c + test(a, b); + + resource function post test11(int a, int b, int... c) returns string => a.toString(); // warning * 2 + + resource function post test12(int a, int b, int... c) returns string { // warning * 2 + return a.toString(); + } +} + +object {} a = object { + function test(int a, int b) returns int { + return a + b; + } + + function test2(int a, int b) returns int => a + b; + + function test3(int a, int b, int c) returns int { // warning + return a + b; + } + + function test4(int a, int b, int c) returns int => a + b; // warning + + function test5(int a, int b, int c, int d) returns int { // warning * 2 + return test(a, b); + } + + function test6(int a, int b, int c, int d) returns int => test(a, b); // warning * 2 + + function test7(int a, int b, int c = 3) returns int { + return a + b + c; + } + + function test8(int a, int b, int c = 3) returns int { // warning + return a + b; + } + + function test9(int a, int b, int c = 3) returns int => a + b; // warning + + function test10(int a, int b, int c = 3) returns int => c + test(a, b); + + function test11(int a, int b, int... c) returns string => a.toString(); // warning * 2 + + function test12(int a, int b, int... c) returns string { // warning * 2 + return a.toString(); + } +}; + +public function main(int a, int b, int c) { // warning + _ = test(a, c); + [1,2].forEach(element => ()); // warning + [1,2].forEach(element => doNothing(element)); +} + +function doNothing(int a) { // warning + return; +} + +public function t(int a, int b, int c) { // warning + var fn = function(int a2, int b2) returns int => b; + int _ = fn(1,2); +} \ No newline at end of file From fc3c5b60c41378d4907cef1a94e237e92df8f7e9 Mon Sep 17 00:00:00 2001 From: Sasindu Alahakoon Date: Tue, 8 Oct 2024 14:06:59 +0530 Subject: [PATCH 04/18] Update scan cmd tests with unused function args test --- .../io/ballerina/scan/internal/CoreRule.java | 2 +- .../scan/utils/ScanCodeAnalyzerUtils.java | 2 +- .../ballerina/scan/internal/CoreRuleTest.java | 14 ++- .../scan/internal/StaticCodeAnalyzerTest.java | 113 +++++++++--------- .../unix/list-rules-output.txt | 5 +- .../unix/print-rules-to-console.txt | 5 +- 6 files changed, 76 insertions(+), 65 deletions(-) diff --git a/scan-command/src/main/java/io/ballerina/scan/internal/CoreRule.java b/scan-command/src/main/java/io/ballerina/scan/internal/CoreRule.java index 2043871..b428519 100644 --- a/scan-command/src/main/java/io/ballerina/scan/internal/CoreRule.java +++ b/scan-command/src/main/java/io/ballerina/scan/internal/CoreRule.java @@ -32,7 +32,7 @@ * */ enum CoreRule { AVOID_CHECKPANIC(RuleFactory.createRule(1, Constants.RuleDescription.AVOID_CHECKPANIC, RuleKind.CODE_SMELL)), - UNUSED_FUNCTION_PARAMETERS(RuleFactory.createRule(4, + UNUSED_FUNCTION_PARAMETERS(RuleFactory.createRule(2, Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL)); private final Rule rule; diff --git a/scan-command/src/main/java/io/ballerina/scan/utils/ScanCodeAnalyzerUtils.java b/scan-command/src/main/java/io/ballerina/scan/utils/ScanCodeAnalyzerUtils.java index 5b294a0..3d00163 100644 --- a/scan-command/src/main/java/io/ballerina/scan/utils/ScanCodeAnalyzerUtils.java +++ b/scan-command/src/main/java/io/ballerina/scan/utils/ScanCodeAnalyzerUtils.java @@ -9,4 +9,4 @@ public class ScanCodeAnalyzerUtils { public static void reportIssue(ScannerContext scannerContext, Document document, Node node, Rule rule) { scannerContext.getReporter().reportIssue(document, node.location(), rule); } -} \ No newline at end of file +} diff --git a/scan-command/src/test/java/io/ballerina/scan/internal/CoreRuleTest.java b/scan-command/src/test/java/io/ballerina/scan/internal/CoreRuleTest.java index 212b359..377bfc8 100644 --- a/scan-command/src/test/java/io/ballerina/scan/internal/CoreRuleTest.java +++ b/scan-command/src/test/java/io/ballerina/scan/internal/CoreRuleTest.java @@ -20,6 +20,7 @@ import io.ballerina.scan.Rule; import io.ballerina.scan.RuleKind; +import io.ballerina.scan.utils.Constants; import org.testng.Assert; import org.testng.annotations.Test; @@ -31,7 +32,7 @@ public class CoreRuleTest { @Test(description = "test all rules") void testAllRules() { - Assert.assertEquals(CoreRule.rules().size(), 1); + Assert.assertEquals(CoreRule.rules().size(), 2); } @Test(description = "test checkpanic rule") @@ -39,7 +40,16 @@ void testCheckpanicRule() { Rule rule = CoreRule.AVOID_CHECKPANIC.rule(); Assert.assertEquals(rule.id(), "ballerina:1"); Assert.assertEquals(rule.numericId(), 1); - Assert.assertEquals(rule.description(), "Avoid checkpanic"); + Assert.assertEquals(rule.description(), Constants.RuleDescription.AVOID_CHECKPANIC); + Assert.assertEquals(rule.kind(), RuleKind.CODE_SMELL); + } + + @Test(description = "test unused function parameters test") + void testUnusedFunctionParameterRule() { + Rule rule = CoreRule.UNUSED_FUNCTION_PARAMETERS.rule(); + Assert.assertEquals(rule.id(), "ballerina:2"); + Assert.assertEquals(rule.numericId(), 2); + Assert.assertEquals(rule.description(), Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS); Assert.assertEquals(rule.kind(), RuleKind.CODE_SMELL); } } diff --git a/scan-command/src/test/java/io/ballerina/scan/internal/StaticCodeAnalyzerTest.java b/scan-command/src/test/java/io/ballerina/scan/internal/StaticCodeAnalyzerTest.java index 8718ec6..2fcec8e 100644 --- a/scan-command/src/test/java/io/ballerina/scan/internal/StaticCodeAnalyzerTest.java +++ b/scan-command/src/test/java/io/ballerina/scan/internal/StaticCodeAnalyzerTest.java @@ -28,7 +28,6 @@ import io.ballerina.scan.RuleKind; import io.ballerina.scan.Source; import io.ballerina.scan.utils.Constants; -import io.ballerina.tools.text.LinePosition; import io.ballerina.tools.text.LineRange; import org.testng.Assert; import org.testng.annotations.Test; @@ -63,7 +62,7 @@ void testCheckpanicAnalyzer() { Constants.RuleDescription.AVOID_CHECKPANIC, RuleKind.CODE_SMELL); } - @Test(description = "test checkpanic analyzer") + @Test(description = "test unused function parameters analyzer") void testUnusedFunctionParameterAnalyzer() { String documentName = "unused_func_parameters.bal"; Document document = loadDocument(documentName); @@ -72,115 +71,115 @@ void testUnusedFunctionParameterAnalyzer() { staticCodeAnalyzer.analyze(); List issues = scannerContext.getReporter().getIssues(); Assert.assertEquals(issues.size(), 55); - assertIssue(issues.get(0), documentName, 8, 29, 8, 34, "ballerina:4", 4, + assertIssue(issues.get(0), documentName, 8, 29, 8, 34, "ballerina:2", 2, Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(1), documentName, 12, 29, 12, 34, "ballerina:4", 4, + assertIssue(issues.get(1), documentName, 12, 29, 12, 34, "ballerina:2", 2, Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(2), documentName, 14, 29, 14, 34, "ballerina:4", 4, + assertIssue(issues.get(2), documentName, 14, 29, 14, 34, "ballerina:2", 2, Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(3), documentName, 14, 36, 14, 41, "ballerina:4", 4, + assertIssue(issues.get(3), documentName, 14, 36, 14, 41, "ballerina:2", 2, Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(4), documentName, 18, 29, 18, 34, "ballerina:4", 4, + assertIssue(issues.get(4), documentName, 18, 29, 18, 34, "ballerina:2", 2, Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(5), documentName, 18, 36, 18, 41, "ballerina:4", 4, + assertIssue(issues.get(5), documentName, 18, 36, 18, 41, "ballerina:2", 2, Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(6), documentName, 24, 29, 24, 38, "ballerina:4", 4, + assertIssue(issues.get(6), documentName, 24, 29, 24, 38, "ballerina:2", 2, Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(7), documentName, 28, 29, 28, 38, "ballerina:4", 4, + assertIssue(issues.get(7), documentName, 28, 29, 28, 38, "ballerina:2", 2, Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(8), documentName, 32, 23, 32, 28, "ballerina:4", 4, + assertIssue(issues.get(8), documentName, 32, 23, 32, 28, "ballerina:2", 2, Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(9), documentName, 32, 30, 32, 38, "ballerina:4", 4, + assertIssue(issues.get(9), documentName, 32, 30, 32, 38, "ballerina:2", 2, Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(10), documentName, 34, 23, 34, 28, "ballerina:4", 4, + assertIssue(issues.get(10), documentName, 34, 23, 34, 28, "ballerina:2", 2, Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(11), documentName, 34, 30, 34, 38, "ballerina:4", 4, + assertIssue(issues.get(11), documentName, 34, 30, 34, 38, "ballerina:2", 2, Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(12), documentName, 45, 33, 45, 38, "ballerina:4", 4, + assertIssue(issues.get(12), documentName, 45, 33, 45, 38, "ballerina:2", 2, Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(13), documentName, 49, 33, 49, 38, "ballerina:4", 4, + assertIssue(issues.get(13), documentName, 49, 33, 49, 38, "ballerina:2", 2, Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(14), documentName, 51, 33, 51, 38, "ballerina:4", 4, + assertIssue(issues.get(14), documentName, 51, 33, 51, 38, "ballerina:2", 2, Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(15), documentName, 51, 40, 51, 45, "ballerina:4", 4, + assertIssue(issues.get(15), documentName, 51, 40, 51, 45, "ballerina:2", 2, Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(16), documentName, 55, 33, 55, 38, "ballerina:4", 4, + assertIssue(issues.get(16), documentName, 55, 33, 55, 38, "ballerina:2", 2, Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(17), documentName, 55, 40, 55, 45, "ballerina:4", 4, + assertIssue(issues.get(17), documentName, 55, 40, 55, 45, "ballerina:2", 2, Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(18), documentName, 61, 33, 61, 42, "ballerina:4", 4, + assertIssue(issues.get(18), documentName, 61, 33, 61, 42, "ballerina:2", 2, Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(19), documentName, 65, 33, 65, 42, "ballerina:4", 4, + assertIssue(issues.get(19), documentName, 65, 33, 65, 42, "ballerina:2", 2, Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(20), documentName, 69, 27, 69, 32, "ballerina:4", 4, + assertIssue(issues.get(20), documentName, 69, 27, 69, 32, "ballerina:2", 2, Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(21), documentName, 69, 34, 69, 42, "ballerina:4", 4, + assertIssue(issues.get(21), documentName, 69, 34, 69, 42, "ballerina:2", 2, Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(22), documentName, 71, 27, 71, 32, "ballerina:4", 4, + assertIssue(issues.get(22), documentName, 71, 27, 71, 32, "ballerina:2", 2, Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(23), documentName, 71, 34, 71, 42, "ballerina:4", 4, + assertIssue(issues.get(23), documentName, 71, 34, 71, 42, "ballerina:2", 2, Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(24), documentName, 83, 46, 83, 51, "ballerina:4", 4, + assertIssue(issues.get(24), documentName, 83, 46, 83, 51, "ballerina:2", 2, Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(25), documentName, 87, 47, 87, 52, "ballerina:4", 4, + assertIssue(issues.get(25), documentName, 87, 47, 87, 52, "ballerina:2", 2, Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(26), documentName, 89, 47, 89, 52, "ballerina:4", 4, + assertIssue(issues.get(26), documentName, 89, 47, 89, 52, "ballerina:2", 2, Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(27), documentName, 89, 54, 89, 59, "ballerina:4", 4, + assertIssue(issues.get(27), documentName, 89, 54, 89, 59, "ballerina:2", 2, Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(28), documentName, 93, 47, 93, 52, "ballerina:4", 4, + assertIssue(issues.get(28), documentName, 93, 47, 93, 52, "ballerina:2", 2, Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(29), documentName, 93, 54, 93, 59, "ballerina:4", 4, + assertIssue(issues.get(29), documentName, 93, 54, 93, 59, "ballerina:2", 2, Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(30), documentName, 99, 47, 99, 56, "ballerina:4", 4, + assertIssue(issues.get(30), documentName, 99, 47, 99, 56, "ballerina:2", 2, Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(31), documentName, 103, 47, 103, 56, "ballerina:4", 4, + assertIssue(issues.get(31), documentName, 103, 47, 103, 56, "ballerina:2", 2, Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(32), documentName, 107, 41, 107, 46, "ballerina:4", 4, + assertIssue(issues.get(32), documentName, 107, 41, 107, 46, "ballerina:2", 2, Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(33), documentName, 107, 48, 107, 56, "ballerina:4", 4, + assertIssue(issues.get(33), documentName, 107, 48, 107, 56, "ballerina:2", 2, Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(34), documentName, 109, 41, 109, 46, "ballerina:4", 4, + assertIssue(issues.get(34), documentName, 109, 41, 109, 46, "ballerina:2", 2, Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(35), documentName, 109, 48, 109, 56, "ballerina:4", 4, + assertIssue(issues.get(35), documentName, 109, 48, 109, 56, "ballerina:2", 2, Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(36), documentName, 121, 33, 121, 38, "ballerina:4", 4, + assertIssue(issues.get(36), documentName, 121, 33, 121, 38, "ballerina:2", 2, Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(37), documentName, 125, 33, 125, 38, "ballerina:4", 4, + assertIssue(issues.get(37), documentName, 125, 33, 125, 38, "ballerina:2", 2, Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(38), documentName, 127, 33, 127, 38, "ballerina:4", 4, + assertIssue(issues.get(38), documentName, 127, 33, 127, 38, "ballerina:2", 2, Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(39), documentName, 127, 40, 127, 45, "ballerina:4", 4, + assertIssue(issues.get(39), documentName, 127, 40, 127, 45, "ballerina:2", 2, Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(40), documentName, 131, 33, 131, 38, "ballerina:4", 4, + assertIssue(issues.get(40), documentName, 131, 33, 131, 38, "ballerina:2", 2, Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(41), documentName, 131, 40, 131, 45, "ballerina:4", 4, + assertIssue(issues.get(41), documentName, 131, 40, 131, 45, "ballerina:2", 2, Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(42), documentName, 137, 33, 137, 42, "ballerina:4", 4, + assertIssue(issues.get(42), documentName, 137, 33, 137, 42, "ballerina:2", 2, Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(43), documentName, 141, 33, 141, 42, "ballerina:4", 4, + assertIssue(issues.get(43), documentName, 141, 33, 141, 42, "ballerina:2", 2, Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(44), documentName, 145, 27, 145, 32, "ballerina:4", 4, + assertIssue(issues.get(44), documentName, 145, 27, 145, 32, "ballerina:2", 2, Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(45), documentName, 145, 34, 145, 42, "ballerina:4", 4, + assertIssue(issues.get(45), documentName, 145, 34, 145, 42, "ballerina:2", 2, Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(46), documentName, 147, 27, 147, 32, "ballerina:4", 4, + assertIssue(issues.get(46), documentName, 147, 27, 147, 32, "ballerina:2", 2, Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(47), documentName, 147, 34, 147, 42, "ballerina:4", 4, + assertIssue(issues.get(47), documentName, 147, 34, 147, 42, "ballerina:2", 2, Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(48), documentName, 152, 28, 152, 33, "ballerina:4", 4, + assertIssue(issues.get(48), documentName, 152, 28, 152, 33, "ballerina:2", 2, Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(49), documentName, 154, 18, 154, 31, "ballerina:4", 4, + assertIssue(issues.get(49), documentName, 154, 18, 154, 31, "ballerina:2", 2, Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(50), documentName, 158, 19, 158, 24, "ballerina:4", 4, + assertIssue(issues.get(50), documentName, 158, 19, 158, 24, "ballerina:2", 2, Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(51), documentName, 162, 18, 162, 23, "ballerina:4", 4, + assertIssue(issues.get(51), documentName, 162, 18, 162, 23, "ballerina:2", 2, Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(52), documentName, 162, 32, 162, 37, "ballerina:4", 4, + assertIssue(issues.get(52), documentName, 162, 32, 162, 37, "ballerina:2", 2, Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(53), documentName, 163, 22, 163, 28, "ballerina:4", 4, + assertIssue(issues.get(53), documentName, 163, 22, 163, 28, "ballerina:2", 2, Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(54), documentName, 163, 30, 163, 36, "ballerina:4", 4, + assertIssue(issues.get(54), documentName, 163, 30, 163, 36, "ballerina:2", 2, Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); } diff --git a/scan-command/src/test/resources/command-outputs/unix/list-rules-output.txt b/scan-command/src/test/resources/command-outputs/unix/list-rules-output.txt index 991c7a8..c9d415b 100644 --- a/scan-command/src/test/resources/command-outputs/unix/list-rules-output.txt +++ b/scan-command/src/test/resources/command-outputs/unix/list-rules-output.txt @@ -1,7 +1,8 @@ Loading scan tool configurations from src/test/resources/test-resources/bal-project-with-config-file/Scan.toml - RuleID | Rule Kind | Rule Description - ------------------------------------------------------------------------------------ + RuleID | Rule Kind | Rule Description + ---------------------------------------------------------------------------------------------- ballerina:1 | CODE_SMELL | Avoid checkpanic + ballerina:2 | CODE_SMELL | Unused function parameters ballerina/example_module_static_code_analyzer:1 | CODE_SMELL | rule 1 ballerina/example_module_static_code_analyzer:2 | BUG | rule 2 ballerina/example_module_static_code_analyzer:3 | VULNERABILITY | rule 3 diff --git a/scan-command/src/test/resources/command-outputs/unix/print-rules-to-console.txt b/scan-command/src/test/resources/command-outputs/unix/print-rules-to-console.txt index 51bcff0..629d17e 100644 --- a/scan-command/src/test/resources/command-outputs/unix/print-rules-to-console.txt +++ b/scan-command/src/test/resources/command-outputs/unix/print-rules-to-console.txt @@ -1,7 +1,8 @@ Loading scan tool configurations from src/test/resources/test-resources/bal-project-with-config-file/Scan.toml - RuleID | Rule Kind | Rule Description - ------------------------------------------------------------------------------------ + RuleID | Rule Kind | Rule Description + ---------------------------------------------------------------------------------------------- ballerina:1 | CODE_SMELL | Avoid checkpanic + ballerina:2 | CODE_SMELL | Unused function parameters ballerina/example_module_static_code_analyzer:1 | CODE_SMELL | rule 1 ballerina/example_module_static_code_analyzer:2 | BUG | rule 2 ballerina/example_module_static_code_analyzer:3 | VULNERABILITY | rule 3 From a92d47c409f44acb2998be76a41f68376a9c3b17 Mon Sep 17 00:00:00 2001 From: Sasindu Alahakoon Date: Tue, 8 Oct 2024 14:12:02 +0530 Subject: [PATCH 05/18] refactor unused function parameter test file --- .../test-resources/core-rules/unused_func_parameters.bal | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scan-command/src/test/resources/test-resources/core-rules/unused_func_parameters.bal b/scan-command/src/test/resources/test-resources/core-rules/unused_func_parameters.bal index 0df893a..cb88c6f 100644 --- a/scan-command/src/test/resources/test-resources/core-rules/unused_func_parameters.bal +++ b/scan-command/src/test/resources/test-resources/core-rules/unused_func_parameters.bal @@ -163,4 +163,4 @@ function doNothing(int a) { // warning public function t(int a, int b, int c) { // warning var fn = function(int a2, int b2) returns int => b; int _ = fn(1,2); -} \ No newline at end of file +} From ba32fc275590cdf02219f9cebb45b5f5f31b0145 Mon Sep 17 00:00:00 2001 From: Sasindu Alahakoon Date: Tue, 8 Oct 2024 14:15:31 +0530 Subject: [PATCH 06/18] Add warning comments test files in unused function parameter rule --- .../test-resources/core-rules/unused_func_parameters.bal | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scan-command/src/test/resources/test-resources/core-rules/unused_func_parameters.bal b/scan-command/src/test/resources/test-resources/core-rules/unused_func_parameters.bal index cb88c6f..91b24e1 100644 --- a/scan-command/src/test/resources/test-resources/core-rules/unused_func_parameters.bal +++ b/scan-command/src/test/resources/test-resources/core-rules/unused_func_parameters.bal @@ -160,7 +160,7 @@ function doNothing(int a) { // warning return; } -public function t(int a, int b, int c) { // warning - var fn = function(int a2, int b2) returns int => b; +public function t(int a, int b, int c) { // warning * 2 + var fn = function(int a2, int b2) returns int => b; // warning * 2 int _ = fn(1,2); } From bba45c4fdba128154fa90917322c0b6ed8ef0e44 Mon Sep 17 00:00:00 2001 From: Sasindu Alahakoon Date: Tue, 8 Oct 2024 14:48:55 +0530 Subject: [PATCH 07/18] Update assertion text files for windows tests --- .../resources/command-outputs/windows/list-rules-output.txt | 5 +++-- .../command-outputs/windows/print-rules-to-console.txt | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/scan-command/src/test/resources/command-outputs/windows/list-rules-output.txt b/scan-command/src/test/resources/command-outputs/windows/list-rules-output.txt index e31d510..8c1508c 100644 --- a/scan-command/src/test/resources/command-outputs/windows/list-rules-output.txt +++ b/scan-command/src/test/resources/command-outputs/windows/list-rules-output.txt @@ -1,7 +1,8 @@ Loading scan tool configurations from src\test\resources\test-resources\bal-project-with-config-file\Scan.toml - RuleID | Rule Kind | Rule Description - ------------------------------------------------------------------------------------ + RuleID | Rule Kind | Rule Description + ---------------------------------------------------------------------------------------------- ballerina:1 | CODE_SMELL | Avoid checkpanic + ballerina:2 | CODE_SMELL | Unused function parameters ballerina/example_module_static_code_analyzer:1 | CODE_SMELL | rule 1 ballerina/example_module_static_code_analyzer:2 | BUG | rule 2 ballerina/example_module_static_code_analyzer:3 | VULNERABILITY | rule 3 diff --git a/scan-command/src/test/resources/command-outputs/windows/print-rules-to-console.txt b/scan-command/src/test/resources/command-outputs/windows/print-rules-to-console.txt index 9b4032f..4667823 100644 --- a/scan-command/src/test/resources/command-outputs/windows/print-rules-to-console.txt +++ b/scan-command/src/test/resources/command-outputs/windows/print-rules-to-console.txt @@ -1,7 +1,8 @@ Loading scan tool configurations from src\test\resources\test-resources\bal-project-with-config-file\Scan.toml - RuleID | Rule Kind | Rule Description - ------------------------------------------------------------------------------------ + RuleID | Rule Kind | Rule Description + ---------------------------------------------------------------------------------------------- ballerina:1 | CODE_SMELL | Avoid checkpanic + ballerina:2 | CODE_SMELL | Unused function parameters ballerina/example_module_static_code_analyzer:1 | CODE_SMELL | rule 1 ballerina/example_module_static_code_analyzer:2 | BUG | rule 2 ballerina/example_module_static_code_analyzer:3 | VULNERABILITY | rule 3 From 88ed2cf523305751aa6d59e7f114792cdc4cf14d Mon Sep 17 00:00:00 2001 From: Sasindu Alahakoon Date: Wed, 9 Oct 2024 14:05:23 +0530 Subject: [PATCH 08/18] Add analyzer for anonymous function parameters --- .../io/ballerina/scan/internal/CoreRule.java | 6 +- .../scan/internal/ProjectAnalyzer.java | 3 +- .../scan/internal/StaticCodeAnalyzer.java | 84 +++++--- .../io/ballerina/scan/utils/Constants.java | 8 - .../ballerina/scan/utils/RuleDescription.java | 9 + .../scan/utils/ScanCodeAnalyzerUtils.java | 12 -- .../scan/internal/CheckPanicAnalyzerTest.java | 28 +++ .../ballerina/scan/internal/CoreRuleTest.java | 6 +- .../scan/internal/StaticCodeAnalyzerTest.java | 138 +------------ .../UnusedFunctionParamAnalyzerTest.java | 184 ++++++++++++++++++ .../unused_anonymous_func_parameters.bal | 56 ++++++ .../core-rules/unused_func_parameters.bal | 54 +++++ scan-command/src/test/resources/testng.xml | 3 +- 13 files changed, 401 insertions(+), 190 deletions(-) create mode 100644 scan-command/src/main/java/io/ballerina/scan/utils/RuleDescription.java delete mode 100644 scan-command/src/main/java/io/ballerina/scan/utils/ScanCodeAnalyzerUtils.java create mode 100644 scan-command/src/test/java/io/ballerina/scan/internal/CheckPanicAnalyzerTest.java create mode 100644 scan-command/src/test/java/io/ballerina/scan/internal/UnusedFunctionParamAnalyzerTest.java create mode 100644 scan-command/src/test/resources/test-resources/core-rules/unused_anonymous_func_parameters.bal diff --git a/scan-command/src/main/java/io/ballerina/scan/internal/CoreRule.java b/scan-command/src/main/java/io/ballerina/scan/internal/CoreRule.java index b428519..222d820 100644 --- a/scan-command/src/main/java/io/ballerina/scan/internal/CoreRule.java +++ b/scan-command/src/main/java/io/ballerina/scan/internal/CoreRule.java @@ -20,7 +20,7 @@ import io.ballerina.scan.Rule; import io.ballerina.scan.RuleKind; -import io.ballerina.scan.utils.Constants; +import io.ballerina.scan.utils.RuleDescription; import java.util.ArrayList; import java.util.List; @@ -31,9 +31,9 @@ * @since 0.1.0 * */ enum CoreRule { - AVOID_CHECKPANIC(RuleFactory.createRule(1, Constants.RuleDescription.AVOID_CHECKPANIC, RuleKind.CODE_SMELL)), + AVOID_CHECKPANIC(RuleFactory.createRule(1, RuleDescription.AVOID_CHECKPANIC, RuleKind.CODE_SMELL)), UNUSED_FUNCTION_PARAMETERS(RuleFactory.createRule(2, - Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL)); + RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL)); private final Rule rule; diff --git a/scan-command/src/main/java/io/ballerina/scan/internal/ProjectAnalyzer.java b/scan-command/src/main/java/io/ballerina/scan/internal/ProjectAnalyzer.java index 2b60df4..86489cd 100644 --- a/scan-command/src/main/java/io/ballerina/scan/internal/ProjectAnalyzer.java +++ b/scan-command/src/main/java/io/ballerina/scan/internal/ProjectAnalyzer.java @@ -113,7 +113,8 @@ List analyze(List inbuiltRules) { private Consumer analyzeDocument(Module module, ScannerContextImpl scannerContext) { return documentId -> { Document document = module.document(documentId); - StaticCodeAnalyzer analyzer = new StaticCodeAnalyzer(document, scannerContext); + StaticCodeAnalyzer analyzer = new StaticCodeAnalyzer(document, scannerContext, + module.getCompilation().getSemanticModel()); analyzer.analyze(); }; } diff --git a/scan-command/src/main/java/io/ballerina/scan/internal/StaticCodeAnalyzer.java b/scan-command/src/main/java/io/ballerina/scan/internal/StaticCodeAnalyzer.java index 32f9d4a..ceaa775 100644 --- a/scan-command/src/main/java/io/ballerina/scan/internal/StaticCodeAnalyzer.java +++ b/scan-command/src/main/java/io/ballerina/scan/internal/StaticCodeAnalyzer.java @@ -19,17 +19,23 @@ package io.ballerina.scan.internal; import io.ballerina.compiler.api.SemanticModel; +import io.ballerina.compiler.api.symbols.Symbol; import io.ballerina.compiler.syntax.tree.CheckExpressionNode; import io.ballerina.compiler.syntax.tree.FunctionSignatureNode; -import io.ballerina.compiler.syntax.tree.MethodCallExpressionNode; +import io.ballerina.compiler.syntax.tree.ImplicitAnonymousFunctionExpressionNode; +import io.ballerina.compiler.syntax.tree.ImplicitAnonymousFunctionParameters; +import io.ballerina.compiler.syntax.tree.IncludedRecordParameterNode; import io.ballerina.compiler.syntax.tree.ModulePartNode; +import io.ballerina.compiler.syntax.tree.Node; import io.ballerina.compiler.syntax.tree.NodeVisitor; +import io.ballerina.compiler.syntax.tree.SimpleNameReferenceNode; import io.ballerina.compiler.syntax.tree.SyntaxKind; import io.ballerina.compiler.syntax.tree.SyntaxTree; import io.ballerina.projects.Document; +import io.ballerina.scan.Rule; import io.ballerina.scan.ScannerContext; -import static io.ballerina.scan.utils.ScanCodeAnalyzerUtils.reportIssue; +import java.util.Optional; /** * {@code StaticCodeAnalyzer} contains the logic to perform core static code analysis on Ballerina documents. @@ -42,11 +48,11 @@ class StaticCodeAnalyzer extends NodeVisitor { private final ScannerContext scannerContext; private final SemanticModel semanticModel; - StaticCodeAnalyzer(Document document, ScannerContextImpl scannerContext) { + StaticCodeAnalyzer(Document document, ScannerContextImpl scannerContext, SemanticModel semanticModel) { this.document = document; this.syntaxTree = document.syntaxTree(); this.scannerContext = scannerContext; - semanticModel = document.module().getCompilation().getSemanticModel(); + this.semanticModel = semanticModel; } void analyze() { @@ -61,39 +67,65 @@ void analyze() { @Override public void visit(CheckExpressionNode checkExpressionNode) { if (checkExpressionNode.checkKeyword().kind().equals(SyntaxKind.CHECKPANIC_KEYWORD)) { - reportIssue(scannerContext, document, checkExpressionNode, - CoreRule.AVOID_CHECKPANIC.rule()); + reportIssue(scannerContext, document, checkExpressionNode, CoreRule.AVOID_CHECKPANIC.rule()); } } - @Override - public void visit(ModulePartNode modulePartNode) { - modulePartNode.members().forEach(member -> member.accept(this)); - } - @Override public void visit(FunctionSignatureNode functionSignatureNode) { functionSignatureNode.parameters().forEach(parameter -> { - semanticModel.symbol(parameter).ifPresent(symbol -> { - if (semanticModel.references(symbol).size() == 1) { - reportIssue(scannerContext, document, parameter, - CoreRule.UNUSED_FUNCTION_PARAMETERS.rule()); - } - }); - parameter.accept(this); + if (parameter instanceof IncludedRecordParameterNode includedRecordParameterNode) { + includedRecordParameterNode.paramName().ifPresent(name -> { + if (isUnusedNode(name, semanticModel)) { + reportIssue(scannerContext, document, name, CoreRule.UNUSED_FUNCTION_PARAMETERS.rule()); + } + }); + return; + } + + if (isUnusedNode(parameter, semanticModel)) { + reportIssue(scannerContext, document, parameter, CoreRule.UNUSED_FUNCTION_PARAMETERS.rule()); + } + this.visitSyntaxNode(parameter); }); + functionSignatureNode.returnTypeDesc().ifPresent(returnTypeDesc -> returnTypeDesc.accept(this)); } @Override - public void visit(MethodCallExpressionNode methodCallExpressionNode) { - methodCallExpressionNode.arguments().forEach(argument -> { - semanticModel.symbol(argument).ifPresent(symbol -> { - if (semanticModel.references(symbol).size() == 1) { - reportIssue(scannerContext, document, argument, - CoreRule.UNUSED_FUNCTION_PARAMETERS.rule()); + public void visit(ImplicitAnonymousFunctionExpressionNode implicitAnonymousFunctionExpressionNode) { + checkUnusedParametersInImplicitFunctionExpression(implicitAnonymousFunctionExpressionNode.params()); + this.visitSyntaxNode(implicitAnonymousFunctionExpressionNode.expression()); + } + + private void checkUnusedParametersInImplicitFunctionExpression(Node params) { + if (params instanceof ImplicitAnonymousFunctionParameters parameters) { + parameters.parameters().forEach(parameter -> { + if (isUnusedNode(parameter, semanticModel)) { + reportIssue(scannerContext, document, parameter, CoreRule.UNUSED_FUNCTION_PARAMETERS.rule()); } }); - argument.accept(this); - }); + } + + if (params instanceof SimpleNameReferenceNode) { + if (isUnusedNode(params, semanticModel)) { + reportIssue(scannerContext, document, params, CoreRule.UNUSED_FUNCTION_PARAMETERS.rule()); + } + } + } + + private void reportIssue(ScannerContext scannerContext, Document document, Node node, Rule rule) { + scannerContext.getReporter().reportIssue(document, node.location(), rule); + } + + private boolean isUnusedNode(Node node, SemanticModel semanticModel) { + Optional symbol = semanticModel.symbol(node); + if (symbol.isEmpty()) { + return false; + } + + if (semanticModel.references(symbol.get()).size() == 1) { + return true; + } + return false; } } diff --git a/scan-command/src/main/java/io/ballerina/scan/utils/Constants.java b/scan-command/src/main/java/io/ballerina/scan/utils/Constants.java index cfc200e..372de85 100644 --- a/scan-command/src/main/java/io/ballerina/scan/utils/Constants.java +++ b/scan-command/src/main/java/io/ballerina/scan/utils/Constants.java @@ -62,14 +62,6 @@ public class Constants { static final String RULE_DESCRIPTION_COLUMN = "Rule Description"; static final String[] RULE_PRIORITY_LIST = {"ballerina", "ballerina/", "ballerinax/", "wso2/"}; - public static class RuleDescription { - public static final String AVOID_CHECKPANIC = "Avoid checkpanic"; - public static final String UNUSED_FUNCTION_PARAMETERS = "Unused function parameters"; - - private RuleDescription() { - } - } - private Constants() { } } diff --git a/scan-command/src/main/java/io/ballerina/scan/utils/RuleDescription.java b/scan-command/src/main/java/io/ballerina/scan/utils/RuleDescription.java new file mode 100644 index 0000000..0563d30 --- /dev/null +++ b/scan-command/src/main/java/io/ballerina/scan/utils/RuleDescription.java @@ -0,0 +1,9 @@ +package io.ballerina.scan.utils; + +public class RuleDescription { + public static final String AVOID_CHECKPANIC = "Avoid checkpanic"; + public static final String UNUSED_FUNCTION_PARAMETERS = "Unused function parameters"; + + private RuleDescription() { + } +} diff --git a/scan-command/src/main/java/io/ballerina/scan/utils/ScanCodeAnalyzerUtils.java b/scan-command/src/main/java/io/ballerina/scan/utils/ScanCodeAnalyzerUtils.java deleted file mode 100644 index 3d00163..0000000 --- a/scan-command/src/main/java/io/ballerina/scan/utils/ScanCodeAnalyzerUtils.java +++ /dev/null @@ -1,12 +0,0 @@ -package io.ballerina.scan.utils; - -import io.ballerina.compiler.syntax.tree.Node; -import io.ballerina.projects.Document; -import io.ballerina.scan.Rule; -import io.ballerina.scan.ScannerContext; - -public class ScanCodeAnalyzerUtils { - public static void reportIssue(ScannerContext scannerContext, Document document, Node node, Rule rule) { - scannerContext.getReporter().reportIssue(document, node.location(), rule); - } -} diff --git a/scan-command/src/test/java/io/ballerina/scan/internal/CheckPanicAnalyzerTest.java b/scan-command/src/test/java/io/ballerina/scan/internal/CheckPanicAnalyzerTest.java new file mode 100644 index 0000000..7184148 --- /dev/null +++ b/scan-command/src/test/java/io/ballerina/scan/internal/CheckPanicAnalyzerTest.java @@ -0,0 +1,28 @@ +package io.ballerina.scan.internal; + +import io.ballerina.projects.Document; +import io.ballerina.scan.Issue; +import io.ballerina.scan.RuleKind; +import io.ballerina.scan.utils.RuleDescription; +import org.testng.Assert; +import org.testng.annotations.Test; + +import java.util.List; + +public class CheckPanicAnalyzerTest extends StaticCodeAnalyzerTest { + + @Test(description = "test checkpanic analyzer") + void testCheckpanicAnalyzer() { + String documentName = "rule_checkpanic.bal"; + Document document = loadDocument(documentName); + ScannerContextImpl scannerContext = new ScannerContextImpl(List.of(CoreRule.AVOID_CHECKPANIC.rule())); + StaticCodeAnalyzer staticCodeAnalyzer = new StaticCodeAnalyzer(document, + scannerContext, document.module().getCompilation().getSemanticModel()); + staticCodeAnalyzer.analyze(); + List issues = scannerContext.getReporter().getIssues(); + Assert.assertEquals(issues.size(), 1); + + assertIssue(issues.get(0), documentName, 20, 17, 20, 39, "ballerina:1", 1, + RuleDescription.AVOID_CHECKPANIC, RuleKind.CODE_SMELL); + } +} diff --git a/scan-command/src/test/java/io/ballerina/scan/internal/CoreRuleTest.java b/scan-command/src/test/java/io/ballerina/scan/internal/CoreRuleTest.java index 377bfc8..4c91df6 100644 --- a/scan-command/src/test/java/io/ballerina/scan/internal/CoreRuleTest.java +++ b/scan-command/src/test/java/io/ballerina/scan/internal/CoreRuleTest.java @@ -20,7 +20,7 @@ import io.ballerina.scan.Rule; import io.ballerina.scan.RuleKind; -import io.ballerina.scan.utils.Constants; +import io.ballerina.scan.utils.RuleDescription; import org.testng.Assert; import org.testng.annotations.Test; @@ -40,7 +40,7 @@ void testCheckpanicRule() { Rule rule = CoreRule.AVOID_CHECKPANIC.rule(); Assert.assertEquals(rule.id(), "ballerina:1"); Assert.assertEquals(rule.numericId(), 1); - Assert.assertEquals(rule.description(), Constants.RuleDescription.AVOID_CHECKPANIC); + Assert.assertEquals(rule.description(), RuleDescription.AVOID_CHECKPANIC); Assert.assertEquals(rule.kind(), RuleKind.CODE_SMELL); } @@ -49,7 +49,7 @@ void testUnusedFunctionParameterRule() { Rule rule = CoreRule.UNUSED_FUNCTION_PARAMETERS.rule(); Assert.assertEquals(rule.id(), "ballerina:2"); Assert.assertEquals(rule.numericId(), 2); - Assert.assertEquals(rule.description(), Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS); + Assert.assertEquals(rule.description(), RuleDescription.UNUSED_FUNCTION_PARAMETERS); Assert.assertEquals(rule.kind(), RuleKind.CODE_SMELL); } } diff --git a/scan-command/src/test/java/io/ballerina/scan/internal/StaticCodeAnalyzerTest.java b/scan-command/src/test/java/io/ballerina/scan/internal/StaticCodeAnalyzerTest.java index 2fcec8e..88f4394 100644 --- a/scan-command/src/test/java/io/ballerina/scan/internal/StaticCodeAnalyzerTest.java +++ b/scan-command/src/test/java/io/ballerina/scan/internal/StaticCodeAnalyzerTest.java @@ -27,7 +27,7 @@ import io.ballerina.scan.Rule; import io.ballerina.scan.RuleKind; import io.ballerina.scan.Source; -import io.ballerina.scan.utils.Constants; +import io.ballerina.scan.utils.RuleDescription; import io.ballerina.tools.text.LineRange; import org.testng.Assert; import org.testng.annotations.Test; @@ -43,146 +43,12 @@ public class StaticCodeAnalyzerTest extends BaseTest { private final Path coreRuleBalFiles = testResources.resolve("test-resources").resolve("core-rules"); - private Document loadDocument(String documentName) { + Document loadDocument(String documentName) { Project project = SingleFileProject.load(coreRuleBalFiles.resolve(documentName)); Module defaultModule = project.currentPackage().getDefaultModule(); return defaultModule.document(defaultModule.documentIds().iterator().next()); } - @Test(description = "test checkpanic analyzer") - void testCheckpanicAnalyzer() { - String documentName = "rule_checkpanic.bal"; - Document document = loadDocument(documentName); - ScannerContextImpl scannerContext = new ScannerContextImpl(List.of(CoreRule.AVOID_CHECKPANIC.rule())); - StaticCodeAnalyzer staticCodeAnalyzer = new StaticCodeAnalyzer(document, scannerContext); - staticCodeAnalyzer.analyze(); - List issues = scannerContext.getReporter().getIssues(); - Assert.assertEquals(issues.size(), 1); - assertIssue(issues.get(0), documentName, 20, 17, 20, 39, "ballerina:1", 1, - Constants.RuleDescription.AVOID_CHECKPANIC, RuleKind.CODE_SMELL); - } - - @Test(description = "test unused function parameters analyzer") - void testUnusedFunctionParameterAnalyzer() { - String documentName = "unused_func_parameters.bal"; - Document document = loadDocument(documentName); - ScannerContextImpl scannerContext = new ScannerContextImpl(List.of(CoreRule.UNUSED_FUNCTION_PARAMETERS.rule())); - StaticCodeAnalyzer staticCodeAnalyzer = new StaticCodeAnalyzer(document, scannerContext); - staticCodeAnalyzer.analyze(); - List issues = scannerContext.getReporter().getIssues(); - Assert.assertEquals(issues.size(), 55); - assertIssue(issues.get(0), documentName, 8, 29, 8, 34, "ballerina:2", 2, - Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(1), documentName, 12, 29, 12, 34, "ballerina:2", 2, - Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(2), documentName, 14, 29, 14, 34, "ballerina:2", 2, - Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(3), documentName, 14, 36, 14, 41, "ballerina:2", 2, - Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(4), documentName, 18, 29, 18, 34, "ballerina:2", 2, - Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(5), documentName, 18, 36, 18, 41, "ballerina:2", 2, - Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(6), documentName, 24, 29, 24, 38, "ballerina:2", 2, - Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(7), documentName, 28, 29, 28, 38, "ballerina:2", 2, - Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(8), documentName, 32, 23, 32, 28, "ballerina:2", 2, - Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(9), documentName, 32, 30, 32, 38, "ballerina:2", 2, - Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(10), documentName, 34, 23, 34, 28, "ballerina:2", 2, - Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(11), documentName, 34, 30, 34, 38, "ballerina:2", 2, - Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(12), documentName, 45, 33, 45, 38, "ballerina:2", 2, - Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(13), documentName, 49, 33, 49, 38, "ballerina:2", 2, - Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(14), documentName, 51, 33, 51, 38, "ballerina:2", 2, - Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(15), documentName, 51, 40, 51, 45, "ballerina:2", 2, - Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(16), documentName, 55, 33, 55, 38, "ballerina:2", 2, - Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(17), documentName, 55, 40, 55, 45, "ballerina:2", 2, - Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(18), documentName, 61, 33, 61, 42, "ballerina:2", 2, - Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(19), documentName, 65, 33, 65, 42, "ballerina:2", 2, - Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(20), documentName, 69, 27, 69, 32, "ballerina:2", 2, - Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(21), documentName, 69, 34, 69, 42, "ballerina:2", 2, - Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(22), documentName, 71, 27, 71, 32, "ballerina:2", 2, - Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(23), documentName, 71, 34, 71, 42, "ballerina:2", 2, - Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(24), documentName, 83, 46, 83, 51, "ballerina:2", 2, - Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(25), documentName, 87, 47, 87, 52, "ballerina:2", 2, - Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(26), documentName, 89, 47, 89, 52, "ballerina:2", 2, - Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(27), documentName, 89, 54, 89, 59, "ballerina:2", 2, - Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(28), documentName, 93, 47, 93, 52, "ballerina:2", 2, - Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(29), documentName, 93, 54, 93, 59, "ballerina:2", 2, - Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(30), documentName, 99, 47, 99, 56, "ballerina:2", 2, - Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(31), documentName, 103, 47, 103, 56, "ballerina:2", 2, - Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(32), documentName, 107, 41, 107, 46, "ballerina:2", 2, - Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(33), documentName, 107, 48, 107, 56, "ballerina:2", 2, - Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(34), documentName, 109, 41, 109, 46, "ballerina:2", 2, - Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(35), documentName, 109, 48, 109, 56, "ballerina:2", 2, - Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(36), documentName, 121, 33, 121, 38, "ballerina:2", 2, - Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(37), documentName, 125, 33, 125, 38, "ballerina:2", 2, - Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(38), documentName, 127, 33, 127, 38, "ballerina:2", 2, - Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(39), documentName, 127, 40, 127, 45, "ballerina:2", 2, - Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(40), documentName, 131, 33, 131, 38, "ballerina:2", 2, - Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(41), documentName, 131, 40, 131, 45, "ballerina:2", 2, - Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(42), documentName, 137, 33, 137, 42, "ballerina:2", 2, - Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(43), documentName, 141, 33, 141, 42, "ballerina:2", 2, - Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(44), documentName, 145, 27, 145, 32, "ballerina:2", 2, - Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(45), documentName, 145, 34, 145, 42, "ballerina:2", 2, - Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(46), documentName, 147, 27, 147, 32, "ballerina:2", 2, - Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(47), documentName, 147, 34, 147, 42, "ballerina:2", 2, - Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(48), documentName, 152, 28, 152, 33, "ballerina:2", 2, - Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(49), documentName, 154, 18, 154, 31, "ballerina:2", 2, - Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(50), documentName, 158, 19, 158, 24, "ballerina:2", 2, - Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(51), documentName, 162, 18, 162, 23, "ballerina:2", 2, - Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(52), documentName, 162, 32, 162, 37, "ballerina:2", 2, - Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(53), documentName, 163, 22, 163, 28, "ballerina:2", 2, - Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(54), documentName, 163, 30, 163, 36, "ballerina:2", 2, - Constants.RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - } - void assertIssue(Issue issue, String documentName, int startLine, int startOffset, int endLine, int endOffset, String ruleId, int numericId, String description, RuleKind ruleKind) { Assert.assertEquals(issue.source(), Source.BUILT_IN); diff --git a/scan-command/src/test/java/io/ballerina/scan/internal/UnusedFunctionParamAnalyzerTest.java b/scan-command/src/test/java/io/ballerina/scan/internal/UnusedFunctionParamAnalyzerTest.java new file mode 100644 index 0000000..891ee9f --- /dev/null +++ b/scan-command/src/test/java/io/ballerina/scan/internal/UnusedFunctionParamAnalyzerTest.java @@ -0,0 +1,184 @@ +package io.ballerina.scan.internal; + +import io.ballerina.projects.Document; +import io.ballerina.scan.Issue; +import io.ballerina.scan.RuleKind; +import io.ballerina.scan.utils.RuleDescription; +import org.testng.Assert; +import org.testng.annotations.Test; + +import java.util.List; + +public class UnusedFunctionParamAnalyzerTest extends StaticCodeAnalyzerTest { + + @Test(description = "test unused function parameters analyzer") + void testUnusedFunctionParameterAnalyzer() { + String documentName = "unused_func_parameters.bal"; + Document document = loadDocument(documentName); + ScannerContextImpl scannerContext = new ScannerContextImpl(List.of(CoreRule.UNUSED_FUNCTION_PARAMETERS.rule())); + StaticCodeAnalyzer staticCodeAnalyzer = new StaticCodeAnalyzer(document, + scannerContext, document.module().getCompilation().getSemanticModel()); + staticCodeAnalyzer.analyze(); + List issues = scannerContext.getReporter().getIssues(); + Assert.assertEquals(issues.size(), 59); + + assertIssue(issues.get(0), documentName, 24, 29, 24, 34, "ballerina:2", 2, + RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(1), documentName, 28, 29, 28, 34, "ballerina:2", 2, + RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(2), documentName, 30, 29, 30, 34, "ballerina:2", 2, + RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(3), documentName, 30, 36, 30, 41, "ballerina:2", 2, + RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(4), documentName, 34, 29, 34, 34, "ballerina:2", 2, + RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(5), documentName, 34, 36, 34, 41, "ballerina:2", 2, + RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(6), documentName, 40, 29, 40, 38, "ballerina:2", 2, + RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(7), documentName, 44, 29, 44, 38, "ballerina:2", 2, + RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(8), documentName, 48, 23, 48, 28, "ballerina:2", 2, + RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(9), documentName, 48, 30, 48, 38, "ballerina:2", 2, + RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(10), documentName, 50, 23, 50, 28, "ballerina:2", 2, + RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(11), documentName, 50, 30, 50, 38, "ballerina:2", 2, + RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(12), documentName, 61, 33, 61, 38, "ballerina:2", 2, + RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(13), documentName, 65, 33, 65, 38, "ballerina:2", 2, + RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(14), documentName, 67, 33, 67, 38, "ballerina:2", 2, + RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(15), documentName, 67, 40, 67, 45, "ballerina:2", 2, + RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(16), documentName, 71, 33, 71, 38, "ballerina:2", 2, + RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(17), documentName, 71, 40, 71, 45, "ballerina:2", 2, + RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(18), documentName, 77, 33, 77, 42, "ballerina:2", 2, + RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(19), documentName, 81, 33, 81, 42, "ballerina:2", 2, + RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(20), documentName, 85, 27, 85, 32, "ballerina:2", 2, + RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(21), documentName, 85, 34, 85, 42, "ballerina:2", 2, + RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(22), documentName, 87, 27, 87, 32, "ballerina:2", 2, + RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(23), documentName, 87, 34, 87, 42, "ballerina:2", 2, + RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(24), documentName, 91, 48, 91, 61, "ballerina:2", 2, + RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(25), documentName, 108, 46, 108, 51, "ballerina:2", 2, + RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(26), documentName, 112, 47, 112, 52, "ballerina:2", 2, + RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(27), documentName, 114, 47, 114, 52, "ballerina:2", 2, + RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(28), documentName, 114, 54, 114, 59, "ballerina:2", 2, + RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(29), documentName, 118, 47, 118, 52, "ballerina:2", 2, + RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(30), documentName, 118, 54, 118, 59, "ballerina:2", 2, + RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(31), documentName, 124, 47, 124, 56, "ballerina:2", 2, + RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(32), documentName, 128, 47, 128, 56, "ballerina:2", 2, + RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(33), documentName, 132, 41, 132, 46, "ballerina:2", 2, + RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(34), documentName, 132, 48, 132, 56, "ballerina:2", 2, + RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(35), documentName, 134, 41, 134, 46, "ballerina:2", 2, + RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(36), documentName, 134, 48, 134, 56, "ballerina:2", 2, + RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(37), documentName, 138, 48, 138, 61, "ballerina:2", 2, + RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(38), documentName, 155, 33, 155, 38, "ballerina:2", 2, + RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(39), documentName, 159, 33, 159, 38, "ballerina:2", 2, + RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(40), documentName, 161, 33, 161, 38, "ballerina:2", 2, + RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(41), documentName, 161, 40, 161, 45, "ballerina:2", 2, + RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(42), documentName, 165, 33, 165, 38, "ballerina:2", 2, + RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(43), documentName, 165, 40, 165, 45, "ballerina:2", 2, + RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(44), documentName, 171, 33, 171, 42, "ballerina:2", 2, + RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(45), documentName, 175, 33, 175, 42, "ballerina:2", 2, + RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(46), documentName, 179, 27, 179, 32, "ballerina:2", 2, + RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(47), documentName, 179, 34, 179, 42, "ballerina:2", 2, + RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(48), documentName, 181, 27, 181, 32, "ballerina:2", 2, + RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(49), documentName, 181, 34, 181, 42, "ballerina:2", 2, + RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(50), documentName, 185, 48, 185, 61, "ballerina:2", 2, + RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(51), documentName, 195, 28, 195, 33, "ballerina:2", 2, + RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(52), documentName, 197, 18, 197, 25, "ballerina:2", 2, + RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(53), documentName, 201, 19, 201, 24, "ballerina:2", 2, + RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(54), documentName, 205, 18, 205, 23, "ballerina:2", 2, + RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(55), documentName, 205, 32, 205, 37, "ballerina:2", 2, + RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(56), documentName, 206, 22, 206, 28, "ballerina:2", 2, + RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(57), documentName, 206, 30, 206, 36, "ballerina:2", 2, + RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(58), documentName, 212, 44, 212, 57, "ballerina:2", 2, + RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + } + + @Test(description = "test unused anonymous function parameters analyzer") + void testUnusedAnonymousFunctionParameterAnalyzer() { + String documentName = "unused_anonymous_func_parameters.bal"; + Document document = loadDocument(documentName); + ScannerContextImpl scannerContext = new ScannerContextImpl(List.of(CoreRule.UNUSED_FUNCTION_PARAMETERS.rule())); + StaticCodeAnalyzer staticCodeAnalyzer = new StaticCodeAnalyzer(document, + scannerContext, document.module().getCompilation() + .getSemanticModel()); + staticCodeAnalyzer.analyze(); + List issues = scannerContext.getReporter().getIssues(); + Assert.assertEquals(issues.size(), 13); + + assertIssue(issues.get(0), documentName, 16, 34, 16, 39, "ballerina:2", 2, + RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(1), documentName, 16, 41, 16, 46, "ballerina:2", 2, + RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(2), documentName, 17, 17, 17, 24, "ballerina:2", 2, + RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(3), documentName, 25, 26, 25, 31, "ballerina:2", 2, + RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(4), documentName, 27, 50, 27, 57, "ballerina:2", 2, + RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(5), documentName, 29, 48, 29, 49, "ballerina:2", 2, + RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(6), documentName, 31, 61, 31, 66, "ballerina:2", 2, + RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(7), documentName, 33, 26, 33, 31, "ballerina:2", 2, + RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(8), documentName, 33, 57, 33, 64, "ballerina:2", 2, + RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(9), documentName, 39, 26, 39, 31, "ballerina:2", 2, + RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(10), documentName, 44, 19, 44, 24, "ballerina:2", 2, + RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(11), documentName, 50, 12, 50, 13, "ballerina:2", 2, + RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(12), documentName, 51, 17, 51, 22, "ballerina:2", 2, + RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + } +} diff --git a/scan-command/src/test/resources/test-resources/core-rules/unused_anonymous_func_parameters.bal b/scan-command/src/test/resources/test-resources/core-rules/unused_anonymous_func_parameters.bal new file mode 100644 index 0000000..678e744 --- /dev/null +++ b/scan-command/src/test/resources/test-resources/core-rules/unused_anonymous_func_parameters.bal @@ -0,0 +1,56 @@ +// Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). +// +// WSO2 Inc. 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. + +public function testExprFunctions(int a, int b, int c) { // warning * 2 + [1,2].forEach(element => ()); // warning + [1,2].forEach(element => doNothing(element + c)); +} + +function (int, int) returns int anonFunc1 = (x, y) => x + y; + +function (int, int) returns int anonFunc2 = function (int x, int y) returns int => x + y; + +public function anonFunc4(int a) => [1,2].forEach(element => doNothing(element)); // warning + +public function anonFunc3(int a) => [1,2].forEach(element => doNothing(a)); // warning + +function (int, int) returns int anonFunc5 = (x, y) => x; // warning + +function (int, int) returns int anonFunc6 = function (int x, int y) returns int => x; // warning + +public function anonFunc7(int a, int b) => [1,2].forEach(element => doNothing(b)); // warning * 2 + +type F function (int, int) returns int; + +type R record { + F f = (a, b) => a; // BUG: https://github.com/ballerina-platform/ballerina-lang/issues/43474 + F anotherF = function(int a, int b) returns int { // warning + return b; + }; +}; + +function doNothing(any a) { // warning + return; +} + +public function testInlineFunctionDecl() { + F[] _ = [ + (a, b) => a, // warning + function(int a, int b) returns int { // warning + return b; + } + ]; +} diff --git a/scan-command/src/test/resources/test-resources/core-rules/unused_func_parameters.bal b/scan-command/src/test/resources/test-resources/core-rules/unused_func_parameters.bal index 91b24e1..6eb42af 100644 --- a/scan-command/src/test/resources/test-resources/core-rules/unused_func_parameters.bal +++ b/scan-command/src/test/resources/test-resources/core-rules/unused_func_parameters.bal @@ -1,3 +1,19 @@ +// Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). +// +// WSO2 Inc. 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. + import ballerina/http; function test(int a, int b) returns int { @@ -72,6 +88,15 @@ class A { function test12(int a, int b, int... c) returns string { // warning * 2 return a.toString(); } + + function testIncludedParams(*IncludedRecord includedparam) {// warning + return; + } + + function testIncludedParams2(*IncludedRecord includedparam) { + _ = includedparam; + return; + } } service /a on new http:Listener(8080) { @@ -110,6 +135,15 @@ service /a on new http:Listener(8080) { resource function post test12(int a, int b, int... c) returns string { // warning * 2 return a.toString(); } + + function testIncludedParams(*IncludedRecord includedparam) {// warning + return; + } + + function testIncludedParams2(*IncludedRecord includedparam) { + _ = includedparam; + return; + } } object {} a = object { @@ -148,6 +182,15 @@ object {} a = object { function test12(int a, int b, int... c) returns string { // warning * 2 return a.toString(); } + + function testIncludedParams(*IncludedRecord includedparam) {// warning + return; + } + + function testIncludedParams2(*IncludedRecord includedparam) { + _ = includedparam; + return; + } }; public function main(int a, int b, int c) { // warning @@ -164,3 +207,14 @@ public function t(int a, int b, int c) { // warning * 2 var fn = function(int a2, int b2) returns int => b; // warning * 2 int _ = fn(1,2); } + +type IncludedRecord record {int a;}; + +function testIncludedParams(*IncludedRecord includedparam) {// warning + return; +} + +function testIncludedParams2(*IncludedRecord includedparam) { + _ = includedparam; + return; +} diff --git a/scan-command/src/test/resources/testng.xml b/scan-command/src/test/resources/testng.xml index 2ec7177..7406e3f 100644 --- a/scan-command/src/test/resources/testng.xml +++ b/scan-command/src/test/resources/testng.xml @@ -29,7 +29,8 @@ under the License. - + + From 797b59f57b08946085526e7fa7ee1da93138e67f Mon Sep 17 00:00:00 2001 From: Sasindu Alahakoon Date: Wed, 9 Oct 2024 14:09:06 +0530 Subject: [PATCH 09/18] Remove unused imports in static code rule tests --- .../io/ballerina/scan/internal/StaticCodeAnalyzerTest.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/scan-command/src/test/java/io/ballerina/scan/internal/StaticCodeAnalyzerTest.java b/scan-command/src/test/java/io/ballerina/scan/internal/StaticCodeAnalyzerTest.java index 88f4394..828c375 100644 --- a/scan-command/src/test/java/io/ballerina/scan/internal/StaticCodeAnalyzerTest.java +++ b/scan-command/src/test/java/io/ballerina/scan/internal/StaticCodeAnalyzerTest.java @@ -27,13 +27,10 @@ import io.ballerina.scan.Rule; import io.ballerina.scan.RuleKind; import io.ballerina.scan.Source; -import io.ballerina.scan.utils.RuleDescription; import io.ballerina.tools.text.LineRange; import org.testng.Assert; -import org.testng.annotations.Test; import java.nio.file.Path; -import java.util.List; /** * Core analyzer tests. From 927847ca09166a3872593628f7c11e0c208febce Mon Sep 17 00:00:00 2001 From: Sasindu Alahakoon Date: Wed, 9 Oct 2024 14:17:58 +0530 Subject: [PATCH 10/18] Refactor the unused function parameter analyzer --- .../scan/internal/StaticCodeAnalyzer.java | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/scan-command/src/main/java/io/ballerina/scan/internal/StaticCodeAnalyzer.java b/scan-command/src/main/java/io/ballerina/scan/internal/StaticCodeAnalyzer.java index ceaa775..cd3fc43 100644 --- a/scan-command/src/main/java/io/ballerina/scan/internal/StaticCodeAnalyzer.java +++ b/scan-command/src/main/java/io/ballerina/scan/internal/StaticCodeAnalyzer.java @@ -80,11 +80,10 @@ public void visit(FunctionSignatureNode functionSignatureNode) { reportIssue(scannerContext, document, name, CoreRule.UNUSED_FUNCTION_PARAMETERS.rule()); } }); - return; - } - - if (isUnusedNode(parameter, semanticModel)) { - reportIssue(scannerContext, document, parameter, CoreRule.UNUSED_FUNCTION_PARAMETERS.rule()); + } else { + if (isUnusedNode(parameter, semanticModel)) { + reportIssue(scannerContext, document, parameter, CoreRule.UNUSED_FUNCTION_PARAMETERS.rule()); + } } this.visitSyntaxNode(parameter); }); @@ -104,6 +103,7 @@ private void checkUnusedParametersInImplicitFunctionExpression(Node params) { reportIssue(scannerContext, document, parameter, CoreRule.UNUSED_FUNCTION_PARAMETERS.rule()); } }); + return; } if (params instanceof SimpleNameReferenceNode) { @@ -123,9 +123,6 @@ private boolean isUnusedNode(Node node, SemanticModel semanticModel) { return false; } - if (semanticModel.references(symbol.get()).size() == 1) { - return true; - } - return false; + return semanticModel.references(symbol.get()).size() == 1; } } From 7b6c4d7f1841103b2db6f9d2ce6f4e713fa87e87 Mon Sep 17 00:00:00 2001 From: Sasindu Alahakoon Date: Thu, 10 Oct 2024 14:41:18 +0530 Subject: [PATCH 11/18] Refactor the tests in unused function parameter static rule --- .../scan/internal/StaticCodeAnalyzer.java | 38 +++-- .../ballerina/scan/utils/RuleDescription.java | 32 +++++ .../scan/internal/CheckPanicAnalyzerTest.java | 20 ++- .../UnusedFunctionParamAnalyzerTest.java | 120 +++------------- .../unused_anonymous_func_parameters.bal | 4 +- .../core-rules/unused_func_parameters.bal | 133 ++---------------- scan-command/src/test/resources/testng.xml | 2 +- 7 files changed, 106 insertions(+), 243 deletions(-) diff --git a/scan-command/src/main/java/io/ballerina/scan/internal/StaticCodeAnalyzer.java b/scan-command/src/main/java/io/ballerina/scan/internal/StaticCodeAnalyzer.java index cd3fc43..966dc2c 100644 --- a/scan-command/src/main/java/io/ballerina/scan/internal/StaticCodeAnalyzer.java +++ b/scan-command/src/main/java/io/ballerina/scan/internal/StaticCodeAnalyzer.java @@ -32,7 +32,6 @@ import io.ballerina.compiler.syntax.tree.SyntaxKind; import io.ballerina.compiler.syntax.tree.SyntaxTree; import io.ballerina.projects.Document; -import io.ballerina.scan.Rule; import io.ballerina.scan.ScannerContext; import java.util.Optional; @@ -67,7 +66,7 @@ void analyze() { @Override public void visit(CheckExpressionNode checkExpressionNode) { if (checkExpressionNode.checkKeyword().kind().equals(SyntaxKind.CHECKPANIC_KEYWORD)) { - reportIssue(scannerContext, document, checkExpressionNode, CoreRule.AVOID_CHECKPANIC.rule()); + reportIssue(checkExpressionNode, CoreRule.AVOID_CHECKPANIC); } } @@ -76,17 +75,14 @@ public void visit(FunctionSignatureNode functionSignatureNode) { functionSignatureNode.parameters().forEach(parameter -> { if (parameter instanceof IncludedRecordParameterNode includedRecordParameterNode) { includedRecordParameterNode.paramName().ifPresent(name -> { - if (isUnusedNode(name, semanticModel)) { - reportIssue(scannerContext, document, name, CoreRule.UNUSED_FUNCTION_PARAMETERS.rule()); - } + reportIssueIfNodeIsUnused(name, CoreRule.UNUSED_FUNCTION_PARAMETERS); }); } else { - if (isUnusedNode(parameter, semanticModel)) { - reportIssue(scannerContext, document, parameter, CoreRule.UNUSED_FUNCTION_PARAMETERS.rule()); - } + reportIssueIfNodeIsUnused(parameter, CoreRule.UNUSED_FUNCTION_PARAMETERS); } this.visitSyntaxNode(parameter); }); + functionSignatureNode.parameters().forEach(parameterNode -> parameterNode.accept(this)); functionSignatureNode.returnTypeDesc().ifPresent(returnTypeDesc -> returnTypeDesc.accept(this)); } @@ -99,30 +95,28 @@ public void visit(ImplicitAnonymousFunctionExpressionNode implicitAnonymousFunct private void checkUnusedParametersInImplicitFunctionExpression(Node params) { if (params instanceof ImplicitAnonymousFunctionParameters parameters) { parameters.parameters().forEach(parameter -> { - if (isUnusedNode(parameter, semanticModel)) { - reportIssue(scannerContext, document, parameter, CoreRule.UNUSED_FUNCTION_PARAMETERS.rule()); - } + reportIssueIfNodeIsUnused(parameter, CoreRule.UNUSED_FUNCTION_PARAMETERS); }); return; } if (params instanceof SimpleNameReferenceNode) { - if (isUnusedNode(params, semanticModel)) { - reportIssue(scannerContext, document, params, CoreRule.UNUSED_FUNCTION_PARAMETERS.rule()); - } + reportIssueIfNodeIsUnused(params, CoreRule.UNUSED_FUNCTION_PARAMETERS); } } - private void reportIssue(ScannerContext scannerContext, Document document, Node node, Rule rule) { - scannerContext.getReporter().reportIssue(document, node.location(), rule); + private void reportIssueIfNodeIsUnused(Node node, CoreRule coreRule) { + if (isUnusedNode(node)) { + reportIssue(node, coreRule); + } } - private boolean isUnusedNode(Node node, SemanticModel semanticModel) { - Optional symbol = semanticModel.symbol(node); - if (symbol.isEmpty()) { - return false; - } + private void reportIssue(Node node, CoreRule coreRule) { + scannerContext.getReporter().reportIssue(document, node.location(), coreRule.rule()); + } - return semanticModel.references(symbol.get()).size() == 1; + private boolean isUnusedNode(Node node) { + Optional symbol = semanticModel.symbol(node); + return symbol.filter(value -> semanticModel.references(value).size() == 1).isPresent(); } } diff --git a/scan-command/src/main/java/io/ballerina/scan/utils/RuleDescription.java b/scan-command/src/main/java/io/ballerina/scan/utils/RuleDescription.java index 0563d30..0872f37 100644 --- a/scan-command/src/main/java/io/ballerina/scan/utils/RuleDescription.java +++ b/scan-command/src/main/java/io/ballerina/scan/utils/RuleDescription.java @@ -1,7 +1,39 @@ +/* + * Copyright (c) 2024, 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; +/** + * + * This class provides the descriptions for Ballerina static coding rules. + * + * @since 0.1.0 + */ public class RuleDescription { + + /** + * Description for the rule that advises avoiding the use of 'checkpanic'. + */ public static final String AVOID_CHECKPANIC = "Avoid checkpanic"; + + /** + * Description for the rule that warns against unused function parameters. + */ public static final String UNUSED_FUNCTION_PARAMETERS = "Unused function parameters"; private RuleDescription() { diff --git a/scan-command/src/test/java/io/ballerina/scan/internal/CheckPanicAnalyzerTest.java b/scan-command/src/test/java/io/ballerina/scan/internal/CheckPanicAnalyzerTest.java index 7184148..4739555 100644 --- a/scan-command/src/test/java/io/ballerina/scan/internal/CheckPanicAnalyzerTest.java +++ b/scan-command/src/test/java/io/ballerina/scan/internal/CheckPanicAnalyzerTest.java @@ -1,3 +1,21 @@ +/* + * Copyright (c) 2024, 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.internal; import io.ballerina.projects.Document; @@ -9,7 +27,7 @@ import java.util.List; -public class CheckPanicAnalyzerTest extends StaticCodeAnalyzerTest { +public class CheckpanicAnalyzerTest extends StaticCodeAnalyzerTest { @Test(description = "test checkpanic analyzer") void testCheckpanicAnalyzer() { diff --git a/scan-command/src/test/java/io/ballerina/scan/internal/UnusedFunctionParamAnalyzerTest.java b/scan-command/src/test/java/io/ballerina/scan/internal/UnusedFunctionParamAnalyzerTest.java index 891ee9f..19181b8 100644 --- a/scan-command/src/test/java/io/ballerina/scan/internal/UnusedFunctionParamAnalyzerTest.java +++ b/scan-command/src/test/java/io/ballerina/scan/internal/UnusedFunctionParamAnalyzerTest.java @@ -20,125 +20,49 @@ void testUnusedFunctionParameterAnalyzer() { scannerContext, document.module().getCompilation().getSemanticModel()); staticCodeAnalyzer.analyze(); List issues = scannerContext.getReporter().getIssues(); - Assert.assertEquals(issues.size(), 59); + Assert.assertEquals(issues.size(), 21); - assertIssue(issues.get(0), documentName, 24, 29, 24, 34, "ballerina:2", 2, + assertIssue(issues.get(0), documentName, 28, 29, 28, 34, "ballerina:2", 2, RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(1), documentName, 28, 29, 28, 34, "ballerina:2", 2, + assertIssue(issues.get(1), documentName, 36, 29, 36, 38, "ballerina:2", 2, RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(2), documentName, 30, 29, 30, 34, "ballerina:2", 2, + assertIssue(issues.get(2), documentName, 40, 29, 40, 38, "ballerina:2", 2, RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(3), documentName, 30, 36, 30, 41, "ballerina:2", 2, + assertIssue(issues.get(3), documentName, 42, 22, 42, 27, "ballerina:2", 2, RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(4), documentName, 34, 29, 34, 34, "ballerina:2", 2, + assertIssue(issues.get(4), documentName, 42, 29, 42, 37, "ballerina:2", 2, RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(5), documentName, 34, 36, 34, 41, "ballerina:2", 2, + assertIssue(issues.get(5), documentName, 44, 22, 44, 27, "ballerina:2", 2, RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(6), documentName, 40, 29, 40, 38, "ballerina:2", 2, + assertIssue(issues.get(6), documentName, 44, 29, 44, 37, "ballerina:2", 2, RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(7), documentName, 44, 29, 44, 38, "ballerina:2", 2, + assertIssue(issues.get(7), documentName, 53, 33, 53, 38, "ballerina:2", 2, RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(8), documentName, 48, 23, 48, 28, "ballerina:2", 2, + assertIssue(issues.get(8), documentName, 57, 33, 57, 42, "ballerina:2", 2, RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(9), documentName, 48, 30, 48, 38, "ballerina:2", 2, + assertIssue(issues.get(9), documentName, 61, 26, 61, 31, "ballerina:2", 2, RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(10), documentName, 50, 23, 50, 28, "ballerina:2", 2, + assertIssue(issues.get(10), documentName, 61, 33, 61, 41, "ballerina:2", 2, RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(11), documentName, 50, 30, 50, 38, "ballerina:2", 2, + assertIssue(issues.get(11), documentName, 63, 48, 63, 67, "ballerina:2", 2, RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(12), documentName, 61, 33, 61, 38, "ballerina:2", 2, + assertIssue(issues.get(12), documentName, 80, 48, 80, 61, "ballerina:2", 2, RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(13), documentName, 65, 33, 65, 38, "ballerina:2", 2, + assertIssue(issues.get(13), documentName, 90, 28, 90, 33, "ballerina:2", 2, RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(14), documentName, 67, 33, 67, 38, "ballerina:2", 2, + assertIssue(issues.get(14), documentName, 92, 18, 92, 25, "ballerina:2", 2, RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(15), documentName, 67, 40, 67, 45, "ballerina:2", 2, + assertIssue(issues.get(15), documentName, 96, 19, 96, 24, "ballerina:2", 2, RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(16), documentName, 71, 33, 71, 38, "ballerina:2", 2, + assertIssue(issues.get(16), documentName, 100, 18, 100, 23, "ballerina:2", 2, RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(17), documentName, 71, 40, 71, 45, "ballerina:2", 2, + assertIssue(issues.get(17), documentName, 100, 32, 100, 37, "ballerina:2", 2, RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(18), documentName, 77, 33, 77, 42, "ballerina:2", 2, + assertIssue(issues.get(18), documentName, 101, 22, 101, 28, "ballerina:2", 2, RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(19), documentName, 81, 33, 81, 42, "ballerina:2", 2, + assertIssue(issues.get(19), documentName, 101, 30, 101, 36, "ballerina:2", 2, RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(20), documentName, 85, 27, 85, 32, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(21), documentName, 85, 34, 85, 42, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(22), documentName, 87, 27, 87, 32, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(23), documentName, 87, 34, 87, 42, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(24), documentName, 91, 48, 91, 61, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(25), documentName, 108, 46, 108, 51, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(26), documentName, 112, 47, 112, 52, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(27), documentName, 114, 47, 114, 52, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(28), documentName, 114, 54, 114, 59, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(29), documentName, 118, 47, 118, 52, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(30), documentName, 118, 54, 118, 59, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(31), documentName, 124, 47, 124, 56, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(32), documentName, 128, 47, 128, 56, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(33), documentName, 132, 41, 132, 46, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(34), documentName, 132, 48, 132, 56, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(35), documentName, 134, 41, 134, 46, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(36), documentName, 134, 48, 134, 56, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(37), documentName, 138, 48, 138, 61, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(38), documentName, 155, 33, 155, 38, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(39), documentName, 159, 33, 159, 38, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(40), documentName, 161, 33, 161, 38, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(41), documentName, 161, 40, 161, 45, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(42), documentName, 165, 33, 165, 38, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(43), documentName, 165, 40, 165, 45, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(44), documentName, 171, 33, 171, 42, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(45), documentName, 175, 33, 175, 42, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(46), documentName, 179, 27, 179, 32, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(47), documentName, 179, 34, 179, 42, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(48), documentName, 181, 27, 181, 32, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(49), documentName, 181, 34, 181, 42, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(50), documentName, 185, 48, 185, 61, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(51), documentName, 195, 28, 195, 33, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(52), documentName, 197, 18, 197, 25, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(53), documentName, 201, 19, 201, 24, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(54), documentName, 205, 18, 205, 23, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(55), documentName, 205, 32, 205, 37, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(56), documentName, 206, 22, 206, 28, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(57), documentName, 206, 30, 206, 36, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(58), documentName, 212, 44, 212, 57, "ballerina:2", 2, + assertIssue(issues.get(20), documentName, 107, 44, 107, 57, "ballerina:2", 2, RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); } diff --git a/scan-command/src/test/resources/test-resources/core-rules/unused_anonymous_func_parameters.bal b/scan-command/src/test/resources/test-resources/core-rules/unused_anonymous_func_parameters.bal index 678e744..a898e9a 100644 --- a/scan-command/src/test/resources/test-resources/core-rules/unused_anonymous_func_parameters.bal +++ b/scan-command/src/test/resources/test-resources/core-rules/unused_anonymous_func_parameters.bal @@ -23,9 +23,9 @@ function (int, int) returns int anonFunc1 = (x, y) => x + y; function (int, int) returns int anonFunc2 = function (int x, int y) returns int => x + y; -public function anonFunc4(int a) => [1,2].forEach(element => doNothing(element)); // warning +public function anonFunc3(int a) => [1,2].forEach(element => doNothing(element)); // warning -public function anonFunc3(int a) => [1,2].forEach(element => doNothing(a)); // warning +public function anonFunc4(int a) => [1,2].forEach(element => doNothing(a)); // warning function (int, int) returns int anonFunc5 = (x, y) => x; // warning diff --git a/scan-command/src/test/resources/test-resources/core-rules/unused_func_parameters.bal b/scan-command/src/test/resources/test-resources/core-rules/unused_func_parameters.bal index 6eb42af..e8b0d23 100644 --- a/scan-command/src/test/resources/test-resources/core-rules/unused_func_parameters.bal +++ b/scan-command/src/test/resources/test-resources/core-rules/unused_func_parameters.bal @@ -16,6 +16,10 @@ import ballerina/http; +type TestObjType object { + function t(int, int) returns int; +}; + function test(int a, int b) returns int { return a + b; } @@ -26,29 +30,19 @@ function test3(int a, int b, int c) returns int { // warning return a + b; } -function test4(int a, int b, int c) returns int => a + b; // warning - -function test5(int a, int b, int c, int d) returns int { // warning * 2 - return test(a, b); -} - -function test6(int a, int b, int c, int d) returns int => test(a, b); // warning * 2 - -function test7(int a, int b, int c = 3) returns int { +function test4(int a, int b, int c = 3) returns int { return a + b + c; } -function test8(int a, int b, int c = 3) returns int { //warning +function test5(int a, int b, int c = 3) returns int { //warning return a + b; } -function test9(int a, int b, int c = 3) returns int => a + b; // warning - -function test10(int a, int b, int c = 3) returns int => c + test(a, b); +function test6(int a, int b, int c = 3) returns int => a + b; // warning -function test11(int a, int b, int... c) returns string => a.toString(); // warning * 2 +function test7(int a, int b, int... c) returns string => a.toString(); // warning * 2 -function test12(int a, int b, int... c) returns string { // warning * 2 +function test8(int a, int b, int... c) returns string { // warning * 2 return a.toString(); } @@ -57,44 +51,22 @@ class A { return a + b; } - function test2(int a, int b) returns int => a + b; - function test3(int a, int b, int c) returns int { // warning return a + b; } - function test4(int a, int b, int c) returns int => a + b; // warning - - function test5(int a, int b, int c, int d) returns int { // warning * 2 - return test(a, b); - } - - function test6(int a, int b, int c, int d) returns int => test(a, b); // warning * 2 - - function test7(int a, int b, int c = 3) returns int { - return a + b + c; - } - - function test8(int a, int b, int c = 3) returns int { // warning + function test6(int a, int b, int c = 3) returns int { // warning return a + b; } - function test9(int a, int b, int c = 3) returns int => a + b; // warning - - function test10(int a, int b, int c = 3) returns int => c + test(a, b); - - function test11(int a, int b, int... c) returns string => a.toString(); // warning * 2 - - function test12(int a, int b, int... c) returns string { // warning * 2 - return a.toString(); - } + function test7(int a, int b, int... c) returns string => a.toString(); // warning * 2 - function testIncludedParams(*IncludedRecord includedparam) {// warning + function testIncludedParams(*IncludedRecord includedRecordParam) {// warning return; } - function testIncludedParams2(*IncludedRecord includedparam) { - _ = includedparam; + function testIncludedParams2(*IncludedRecord includedRecordParam) { + _ = includedRecordParam; return; } } @@ -106,36 +78,6 @@ service /a on new http:Listener(8080) { resource function get test2(int a, int b) returns int => a + b; - resource function get test3(int a, int b, int c) returns int { // warning - return a + b; - } - - resource function post test4(int a, int b, int c) returns int => a + b; // warning - - resource function post test5(int a, int b, int c, int d) returns int { // warning * 2 - return test(a, b); - } - - resource function post test6(int a, int b, int c, int d) returns int => test(a, b); // warning * 2 - - resource function post test7(int a, int b, int c = 3) returns int { - return a + b + c; - } - - resource function post test8(int a, int b, int c = 3) returns int { // warning - return a + b; - } - - resource function post test9(int a, int b, int c = 3) returns int => a + b; // warning - - resource function post test10(int a, int b, int c = 3) returns int => c + test(a, b); - - resource function post test11(int a, int b, int... c) returns string => a.toString(); // warning * 2 - - resource function post test12(int a, int b, int... c) returns string { // warning * 2 - return a.toString(); - } - function testIncludedParams(*IncludedRecord includedparam) {// warning return; } @@ -146,53 +88,6 @@ service /a on new http:Listener(8080) { } } -object {} a = object { - function test(int a, int b) returns int { - return a + b; - } - - function test2(int a, int b) returns int => a + b; - - function test3(int a, int b, int c) returns int { // warning - return a + b; - } - - function test4(int a, int b, int c) returns int => a + b; // warning - - function test5(int a, int b, int c, int d) returns int { // warning * 2 - return test(a, b); - } - - function test6(int a, int b, int c, int d) returns int => test(a, b); // warning * 2 - - function test7(int a, int b, int c = 3) returns int { - return a + b + c; - } - - function test8(int a, int b, int c = 3) returns int { // warning - return a + b; - } - - function test9(int a, int b, int c = 3) returns int => a + b; // warning - - function test10(int a, int b, int c = 3) returns int => c + test(a, b); - - function test11(int a, int b, int... c) returns string => a.toString(); // warning * 2 - - function test12(int a, int b, int... c) returns string { // warning * 2 - return a.toString(); - } - - function testIncludedParams(*IncludedRecord includedparam) {// warning - return; - } - - function testIncludedParams2(*IncludedRecord includedparam) { - _ = includedparam; - return; - } -}; - public function main(int a, int b, int c) { // warning _ = test(a, c); [1,2].forEach(element => ()); // warning diff --git a/scan-command/src/test/resources/testng.xml b/scan-command/src/test/resources/testng.xml index 7406e3f..bffdd5d 100644 --- a/scan-command/src/test/resources/testng.xml +++ b/scan-command/src/test/resources/testng.xml @@ -29,7 +29,7 @@ under the License. - + From cb595f5bd709feb164f9bd13dee8f845686c3054 Mon Sep 17 00:00:00 2001 From: Sasindu Alahakoon Date: Thu, 10 Oct 2024 15:38:28 +0530 Subject: [PATCH 12/18] Update tests in function parameter rules --- .../scan/internal/StaticCodeAnalyzer.java | 44 +++++++++++-------- .../UnusedFunctionParamAnalyzerTest.java | 34 +++++++------- .../unused_anonymous_func_parameters.bal | 15 +++++-- .../core-rules/unused_func_parameters.bal | 32 ++------------ 4 files changed, 56 insertions(+), 69 deletions(-) diff --git a/scan-command/src/main/java/io/ballerina/scan/internal/StaticCodeAnalyzer.java b/scan-command/src/main/java/io/ballerina/scan/internal/StaticCodeAnalyzer.java index 966dc2c..0aeb817 100644 --- a/scan-command/src/main/java/io/ballerina/scan/internal/StaticCodeAnalyzer.java +++ b/scan-command/src/main/java/io/ballerina/scan/internal/StaticCodeAnalyzer.java @@ -21,6 +21,8 @@ import io.ballerina.compiler.api.SemanticModel; import io.ballerina.compiler.api.symbols.Symbol; import io.ballerina.compiler.syntax.tree.CheckExpressionNode; +import io.ballerina.compiler.syntax.tree.ExplicitAnonymousFunctionExpressionNode; +import io.ballerina.compiler.syntax.tree.FunctionDefinitionNode; import io.ballerina.compiler.syntax.tree.FunctionSignatureNode; import io.ballerina.compiler.syntax.tree.ImplicitAnonymousFunctionExpressionNode; import io.ballerina.compiler.syntax.tree.ImplicitAnonymousFunctionParameters; @@ -71,38 +73,44 @@ public void visit(CheckExpressionNode checkExpressionNode) { } @Override - public void visit(FunctionSignatureNode functionSignatureNode) { - functionSignatureNode.parameters().forEach(parameter -> { - if (parameter instanceof IncludedRecordParameterNode includedRecordParameterNode) { - includedRecordParameterNode.paramName().ifPresent(name -> { - reportIssueIfNodeIsUnused(name, CoreRule.UNUSED_FUNCTION_PARAMETERS); - }); - } else { - reportIssueIfNodeIsUnused(parameter, CoreRule.UNUSED_FUNCTION_PARAMETERS); - } - this.visitSyntaxNode(parameter); - }); - functionSignatureNode.parameters().forEach(parameterNode -> parameterNode.accept(this)); - functionSignatureNode.returnTypeDesc().ifPresent(returnTypeDesc -> returnTypeDesc.accept(this)); + public void visit(FunctionDefinitionNode functionDefinitionNode) { + checkUnusedFunctionParameters(functionDefinitionNode.functionSignature()); + this.visitSyntaxNode(functionDefinitionNode); } @Override - public void visit(ImplicitAnonymousFunctionExpressionNode implicitAnonymousFunctionExpressionNode) { - checkUnusedParametersInImplicitFunctionExpression(implicitAnonymousFunctionExpressionNode.params()); - this.visitSyntaxNode(implicitAnonymousFunctionExpressionNode.expression()); + public void visit(ExplicitAnonymousFunctionExpressionNode explicitAnonymousFunctionExpressionNode) { + checkUnusedFunctionParameters(explicitAnonymousFunctionExpressionNode.functionSignature()); + this.visitSyntaxNode(explicitAnonymousFunctionExpressionNode); } - private void checkUnusedParametersInImplicitFunctionExpression(Node params) { + @Override + public void visit(ImplicitAnonymousFunctionExpressionNode implicitAnonymousFunctionExpressionNode) { + Node params = implicitAnonymousFunctionExpressionNode.params(); if (params instanceof ImplicitAnonymousFunctionParameters parameters) { parameters.parameters().forEach(parameter -> { reportIssueIfNodeIsUnused(parameter, CoreRule.UNUSED_FUNCTION_PARAMETERS); }); return; } - if (params instanceof SimpleNameReferenceNode) { reportIssueIfNodeIsUnused(params, CoreRule.UNUSED_FUNCTION_PARAMETERS); } + + this.visitSyntaxNode(implicitAnonymousFunctionExpressionNode.expression()); + } + + private void checkUnusedFunctionParameters(FunctionSignatureNode functionSignatureNode) { + functionSignatureNode.parameters().forEach(parameter -> { + if (parameter instanceof IncludedRecordParameterNode includedRecordParameterNode) { + includedRecordParameterNode.paramName().ifPresent(name -> { + reportIssueIfNodeIsUnused(name, CoreRule.UNUSED_FUNCTION_PARAMETERS); + }); + } else { + reportIssueIfNodeIsUnused(parameter, CoreRule.UNUSED_FUNCTION_PARAMETERS); + } + this.visitSyntaxNode(parameter); + }); } private void reportIssueIfNodeIsUnused(Node node, CoreRule coreRule) { diff --git a/scan-command/src/test/java/io/ballerina/scan/internal/UnusedFunctionParamAnalyzerTest.java b/scan-command/src/test/java/io/ballerina/scan/internal/UnusedFunctionParamAnalyzerTest.java index 19181b8..ff532c4 100644 --- a/scan-command/src/test/java/io/ballerina/scan/internal/UnusedFunctionParamAnalyzerTest.java +++ b/scan-command/src/test/java/io/ballerina/scan/internal/UnusedFunctionParamAnalyzerTest.java @@ -20,7 +20,7 @@ void testUnusedFunctionParameterAnalyzer() { scannerContext, document.module().getCompilation().getSemanticModel()); staticCodeAnalyzer.analyze(); List issues = scannerContext.getReporter().getIssues(); - Assert.assertEquals(issues.size(), 21); + Assert.assertEquals(issues.size(), 17); assertIssue(issues.get(0), documentName, 28, 29, 28, 34, "ballerina:2", 2, RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); @@ -44,25 +44,17 @@ void testUnusedFunctionParameterAnalyzer() { RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); assertIssue(issues.get(10), documentName, 61, 33, 61, 41, "ballerina:2", 2, RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(11), documentName, 63, 48, 63, 67, "ballerina:2", 2, + assertIssue(issues.get(11), documentName, 72, 19, 72, 24, "ballerina:2", 2, RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(12), documentName, 80, 48, 80, 61, "ballerina:2", 2, + assertIssue(issues.get(12), documentName, 76, 18, 76, 23, "ballerina:2", 2, RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(13), documentName, 90, 28, 90, 33, "ballerina:2", 2, + assertIssue(issues.get(13), documentName, 76, 32, 76, 37, "ballerina:2", 2, RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(14), documentName, 92, 18, 92, 25, "ballerina:2", 2, + assertIssue(issues.get(14), documentName, 77, 22, 77, 28, "ballerina:2", 2, RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(15), documentName, 96, 19, 96, 24, "ballerina:2", 2, + assertIssue(issues.get(15), documentName, 77, 30, 77, 36, "ballerina:2", 2, RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(16), documentName, 100, 18, 100, 23, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(17), documentName, 100, 32, 100, 37, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(18), documentName, 101, 22, 101, 28, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(19), documentName, 101, 30, 101, 36, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(20), documentName, 107, 44, 107, 57, "ballerina:2", 2, + assertIssue(issues.get(16), documentName, 83, 44, 83, 63, "ballerina:2", 2, RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); } @@ -76,7 +68,7 @@ void testUnusedAnonymousFunctionParameterAnalyzer() { .getSemanticModel()); staticCodeAnalyzer.analyze(); List issues = scannerContext.getReporter().getIssues(); - Assert.assertEquals(issues.size(), 13); + Assert.assertEquals(issues.size(), 15); assertIssue(issues.get(0), documentName, 16, 34, 16, 39, "ballerina:2", 2, RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); @@ -98,11 +90,15 @@ void testUnusedAnonymousFunctionParameterAnalyzer() { RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); assertIssue(issues.get(9), documentName, 39, 26, 39, 31, "ballerina:2", 2, RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(10), documentName, 44, 19, 44, 24, "ballerina:2", 2, + assertIssue(issues.get(10), documentName, 46, 12, 46, 13, "ballerina:2", 2, + RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(11), documentName, 47, 17, 47, 22, "ballerina:2", 2, + RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + assertIssue(issues.get(12), documentName, 53, 28, 53, 33, "ballerina:2", 2, RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(11), documentName, 50, 12, 50, 13, "ballerina:2", 2, + assertIssue(issues.get(13), documentName, 56, 19, 56, 26, "ballerina:2", 2, RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); - assertIssue(issues.get(12), documentName, 51, 17, 51, 22, "ballerina:2", 2, + assertIssue(issues.get(14), documentName, 60, 19, 60, 24, "ballerina:2", 2, RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); } } diff --git a/scan-command/src/test/resources/test-resources/core-rules/unused_anonymous_func_parameters.bal b/scan-command/src/test/resources/test-resources/core-rules/unused_anonymous_func_parameters.bal index a898e9a..295561f 100644 --- a/scan-command/src/test/resources/test-resources/core-rules/unused_anonymous_func_parameters.bal +++ b/scan-command/src/test/resources/test-resources/core-rules/unused_anonymous_func_parameters.bal @@ -42,10 +42,6 @@ type R record { }; }; -function doNothing(any a) { // warning - return; -} - public function testInlineFunctionDecl() { F[] _ = [ (a, b) => a, // warning @@ -54,3 +50,14 @@ public function testInlineFunctionDecl() { } ]; } + +public function main(int a, int b, int c) { // warning + _ = doNothing(a); + _ = doNothing(c); + [1,2].forEach((element) => ()); // warning + [1,2].forEach((element) => doNothing(element)); +} + +function doNothing(any a) { // warning + return; +} diff --git a/scan-command/src/test/resources/test-resources/core-rules/unused_func_parameters.bal b/scan-command/src/test/resources/test-resources/core-rules/unused_func_parameters.bal index e8b0d23..3894afc 100644 --- a/scan-command/src/test/resources/test-resources/core-rules/unused_func_parameters.bal +++ b/scan-command/src/test/resources/test-resources/core-rules/unused_func_parameters.bal @@ -17,7 +17,7 @@ import ballerina/http; type TestObjType object { - function t(int, int) returns int; + function t(int a, int a2) returns int; }; function test(int a, int b) returns int { @@ -60,15 +60,6 @@ class A { } function test7(int a, int b, int... c) returns string => a.toString(); // warning * 2 - - function testIncludedParams(*IncludedRecord includedRecordParam) {// warning - return; - } - - function testIncludedParams2(*IncludedRecord includedRecordParam) { - _ = includedRecordParam; - return; - } } service /a on new http:Listener(8080) { @@ -77,21 +68,6 @@ service /a on new http:Listener(8080) { } resource function get test2(int a, int b) returns int => a + b; - - function testIncludedParams(*IncludedRecord includedparam) {// warning - return; - } - - function testIncludedParams2(*IncludedRecord includedparam) { - _ = includedparam; - return; - } -} - -public function main(int a, int b, int c) { // warning - _ = test(a, c); - [1,2].forEach(element => ()); // warning - [1,2].forEach(element => doNothing(element)); } function doNothing(int a) { // warning @@ -105,11 +81,11 @@ public function t(int a, int b, int c) { // warning * 2 type IncludedRecord record {int a;}; -function testIncludedParams(*IncludedRecord includedparam) {// warning +function testIncludedParams(*IncludedRecord includedRecordParam) {// warning return; } -function testIncludedParams2(*IncludedRecord includedparam) { - _ = includedparam; +function testIncludedParams2(*IncludedRecord includedRecordParam) { + _ = includedRecordParam; return; } From e4bcc287b16b59dc81f119f984bcdbcd3e61d8d0 Mon Sep 17 00:00:00 2001 From: Sasindu Alahakoon Date: Fri, 11 Oct 2024 11:40:43 +0530 Subject: [PATCH 13/18] Rename unused function parameter analyzer rule description --- .../io/ballerina/scan/internal/CoreRule.java | 4 +- .../scan/internal/StaticCodeAnalyzer.java | 8 +- .../ballerina/scan/utils/RuleDescription.java | 13 +--- .../scan/internal/CheckPanicAnalyzerTest.java | 5 ++ .../ballerina/scan/internal/CoreRuleTest.java | 4 +- .../scan/internal/StaticCodeAnalyzerTest.java | 2 +- .../UnusedFunctionParamAnalyzerTest.java | 77 ++++++++++--------- .../unix/list-rules-output.txt | 6 +- .../unix/print-rules-to-console.txt | 6 +- .../windows/list-rules-output.txt | 6 +- .../windows/print-rules-to-console.txt | 6 +- .../unused_anonymous_func_parameters.bal | 4 + 12 files changed, 74 insertions(+), 67 deletions(-) diff --git a/scan-command/src/main/java/io/ballerina/scan/internal/CoreRule.java b/scan-command/src/main/java/io/ballerina/scan/internal/CoreRule.java index 222d820..1c8e8f0 100644 --- a/scan-command/src/main/java/io/ballerina/scan/internal/CoreRule.java +++ b/scan-command/src/main/java/io/ballerina/scan/internal/CoreRule.java @@ -32,8 +32,8 @@ * */ enum CoreRule { AVOID_CHECKPANIC(RuleFactory.createRule(1, RuleDescription.AVOID_CHECKPANIC, RuleKind.CODE_SMELL)), - UNUSED_FUNCTION_PARAMETERS(RuleFactory.createRule(2, - RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL)); + UNUSED_FUNCTION_PARAMETER(RuleFactory.createRule(2, + RuleDescription.UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL)); private final Rule rule; diff --git a/scan-command/src/main/java/io/ballerina/scan/internal/StaticCodeAnalyzer.java b/scan-command/src/main/java/io/ballerina/scan/internal/StaticCodeAnalyzer.java index 0aeb817..782667d 100644 --- a/scan-command/src/main/java/io/ballerina/scan/internal/StaticCodeAnalyzer.java +++ b/scan-command/src/main/java/io/ballerina/scan/internal/StaticCodeAnalyzer.java @@ -89,12 +89,12 @@ public void visit(ImplicitAnonymousFunctionExpressionNode implicitAnonymousFunct Node params = implicitAnonymousFunctionExpressionNode.params(); if (params instanceof ImplicitAnonymousFunctionParameters parameters) { parameters.parameters().forEach(parameter -> { - reportIssueIfNodeIsUnused(parameter, CoreRule.UNUSED_FUNCTION_PARAMETERS); + reportIssueIfNodeIsUnused(parameter, CoreRule.UNUSED_FUNCTION_PARAMETER); }); return; } if (params instanceof SimpleNameReferenceNode) { - reportIssueIfNodeIsUnused(params, CoreRule.UNUSED_FUNCTION_PARAMETERS); + reportIssueIfNodeIsUnused(params, CoreRule.UNUSED_FUNCTION_PARAMETER); } this.visitSyntaxNode(implicitAnonymousFunctionExpressionNode.expression()); @@ -104,10 +104,10 @@ private void checkUnusedFunctionParameters(FunctionSignatureNode functionSignatu functionSignatureNode.parameters().forEach(parameter -> { if (parameter instanceof IncludedRecordParameterNode includedRecordParameterNode) { includedRecordParameterNode.paramName().ifPresent(name -> { - reportIssueIfNodeIsUnused(name, CoreRule.UNUSED_FUNCTION_PARAMETERS); + reportIssueIfNodeIsUnused(name, CoreRule.UNUSED_FUNCTION_PARAMETER); }); } else { - reportIssueIfNodeIsUnused(parameter, CoreRule.UNUSED_FUNCTION_PARAMETERS); + reportIssueIfNodeIsUnused(parameter, CoreRule.UNUSED_FUNCTION_PARAMETER); } this.visitSyntaxNode(parameter); }); diff --git a/scan-command/src/main/java/io/ballerina/scan/utils/RuleDescription.java b/scan-command/src/main/java/io/ballerina/scan/utils/RuleDescription.java index 0872f37..9c2dbd3 100644 --- a/scan-command/src/main/java/io/ballerina/scan/utils/RuleDescription.java +++ b/scan-command/src/main/java/io/ballerina/scan/utils/RuleDescription.java @@ -19,22 +19,13 @@ package io.ballerina.scan.utils; /** - * - * This class provides the descriptions for Ballerina static coding rules. + * {@code RuleDescription} provides descriptions for Ballerina static coding rules. * * @since 0.1.0 */ public class RuleDescription { - - /** - * Description for the rule that advises avoiding the use of 'checkpanic'. - */ public static final String AVOID_CHECKPANIC = "Avoid checkpanic"; - - /** - * Description for the rule that warns against unused function parameters. - */ - public static final String UNUSED_FUNCTION_PARAMETERS = "Unused function parameters"; + public static final String UNUSED_FUNCTION_PARAMETER = "Unused function parameter"; private RuleDescription() { } diff --git a/scan-command/src/test/java/io/ballerina/scan/internal/CheckPanicAnalyzerTest.java b/scan-command/src/test/java/io/ballerina/scan/internal/CheckPanicAnalyzerTest.java index 4739555..d3a5b8e 100644 --- a/scan-command/src/test/java/io/ballerina/scan/internal/CheckPanicAnalyzerTest.java +++ b/scan-command/src/test/java/io/ballerina/scan/internal/CheckPanicAnalyzerTest.java @@ -27,6 +27,11 @@ import java.util.List; +/** + * Checkpanic analyzer tests. + * + * @since 0.1.0 + */ public class CheckpanicAnalyzerTest extends StaticCodeAnalyzerTest { @Test(description = "test checkpanic analyzer") diff --git a/scan-command/src/test/java/io/ballerina/scan/internal/CoreRuleTest.java b/scan-command/src/test/java/io/ballerina/scan/internal/CoreRuleTest.java index 4c91df6..637d798 100644 --- a/scan-command/src/test/java/io/ballerina/scan/internal/CoreRuleTest.java +++ b/scan-command/src/test/java/io/ballerina/scan/internal/CoreRuleTest.java @@ -46,10 +46,10 @@ void testCheckpanicRule() { @Test(description = "test unused function parameters test") void testUnusedFunctionParameterRule() { - Rule rule = CoreRule.UNUSED_FUNCTION_PARAMETERS.rule(); + Rule rule = CoreRule.UNUSED_FUNCTION_PARAMETER.rule(); Assert.assertEquals(rule.id(), "ballerina:2"); Assert.assertEquals(rule.numericId(), 2); - Assert.assertEquals(rule.description(), RuleDescription.UNUSED_FUNCTION_PARAMETERS); + Assert.assertEquals(rule.description(), RuleDescription.UNUSED_FUNCTION_PARAMETER); Assert.assertEquals(rule.kind(), RuleKind.CODE_SMELL); } } diff --git a/scan-command/src/test/java/io/ballerina/scan/internal/StaticCodeAnalyzerTest.java b/scan-command/src/test/java/io/ballerina/scan/internal/StaticCodeAnalyzerTest.java index 828c375..32faf32 100644 --- a/scan-command/src/test/java/io/ballerina/scan/internal/StaticCodeAnalyzerTest.java +++ b/scan-command/src/test/java/io/ballerina/scan/internal/StaticCodeAnalyzerTest.java @@ -33,7 +33,7 @@ import java.nio.file.Path; /** - * Core analyzer tests. + * Static code analyzer test. * * @since 0.1.0 */ diff --git a/scan-command/src/test/java/io/ballerina/scan/internal/UnusedFunctionParamAnalyzerTest.java b/scan-command/src/test/java/io/ballerina/scan/internal/UnusedFunctionParamAnalyzerTest.java index ff532c4..a0e8155 100644 --- a/scan-command/src/test/java/io/ballerina/scan/internal/UnusedFunctionParamAnalyzerTest.java +++ b/scan-command/src/test/java/io/ballerina/scan/internal/UnusedFunctionParamAnalyzerTest.java @@ -9,13 +9,18 @@ import java.util.List; +/** + * Unused function parameters analyzer tests. + * + * @since 0.1.0 + */ public class UnusedFunctionParamAnalyzerTest extends StaticCodeAnalyzerTest { @Test(description = "test unused function parameters analyzer") void testUnusedFunctionParameterAnalyzer() { String documentName = "unused_func_parameters.bal"; Document document = loadDocument(documentName); - ScannerContextImpl scannerContext = new ScannerContextImpl(List.of(CoreRule.UNUSED_FUNCTION_PARAMETERS.rule())); + ScannerContextImpl scannerContext = new ScannerContextImpl(List.of(CoreRule.UNUSED_FUNCTION_PARAMETER.rule())); StaticCodeAnalyzer staticCodeAnalyzer = new StaticCodeAnalyzer(document, scannerContext, document.module().getCompilation().getSemanticModel()); staticCodeAnalyzer.analyze(); @@ -23,82 +28,84 @@ void testUnusedFunctionParameterAnalyzer() { Assert.assertEquals(issues.size(), 17); assertIssue(issues.get(0), documentName, 28, 29, 28, 34, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + RuleDescription.UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); assertIssue(issues.get(1), documentName, 36, 29, 36, 38, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + RuleDescription.UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); assertIssue(issues.get(2), documentName, 40, 29, 40, 38, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + RuleDescription.UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); assertIssue(issues.get(3), documentName, 42, 22, 42, 27, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + RuleDescription.UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); assertIssue(issues.get(4), documentName, 42, 29, 42, 37, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + RuleDescription.UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); assertIssue(issues.get(5), documentName, 44, 22, 44, 27, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + RuleDescription.UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); assertIssue(issues.get(6), documentName, 44, 29, 44, 37, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + RuleDescription.UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); assertIssue(issues.get(7), documentName, 53, 33, 53, 38, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + RuleDescription.UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); assertIssue(issues.get(8), documentName, 57, 33, 57, 42, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + RuleDescription.UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); assertIssue(issues.get(9), documentName, 61, 26, 61, 31, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + RuleDescription.UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); assertIssue(issues.get(10), documentName, 61, 33, 61, 41, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + RuleDescription.UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); assertIssue(issues.get(11), documentName, 72, 19, 72, 24, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + RuleDescription.UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); assertIssue(issues.get(12), documentName, 76, 18, 76, 23, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + RuleDescription.UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); assertIssue(issues.get(13), documentName, 76, 32, 76, 37, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + RuleDescription.UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); assertIssue(issues.get(14), documentName, 77, 22, 77, 28, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + RuleDescription.UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); assertIssue(issues.get(15), documentName, 77, 30, 77, 36, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + RuleDescription.UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); assertIssue(issues.get(16), documentName, 83, 44, 83, 63, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + RuleDescription.UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); } @Test(description = "test unused anonymous function parameters analyzer") void testUnusedAnonymousFunctionParameterAnalyzer() { String documentName = "unused_anonymous_func_parameters.bal"; Document document = loadDocument(documentName); - ScannerContextImpl scannerContext = new ScannerContextImpl(List.of(CoreRule.UNUSED_FUNCTION_PARAMETERS.rule())); + ScannerContextImpl scannerContext = new ScannerContextImpl(List.of(CoreRule.UNUSED_FUNCTION_PARAMETER.rule())); StaticCodeAnalyzer staticCodeAnalyzer = new StaticCodeAnalyzer(document, scannerContext, document.module().getCompilation() .getSemanticModel()); staticCodeAnalyzer.analyze(); List issues = scannerContext.getReporter().getIssues(); - Assert.assertEquals(issues.size(), 15); + Assert.assertEquals(issues.size(), 16); assertIssue(issues.get(0), documentName, 16, 34, 16, 39, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + RuleDescription.UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); assertIssue(issues.get(1), documentName, 16, 41, 16, 46, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + RuleDescription.UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); assertIssue(issues.get(2), documentName, 17, 17, 17, 24, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + RuleDescription.UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); assertIssue(issues.get(3), documentName, 25, 26, 25, 31, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + RuleDescription.UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); assertIssue(issues.get(4), documentName, 27, 50, 27, 57, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + RuleDescription.UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); assertIssue(issues.get(5), documentName, 29, 48, 29, 49, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + RuleDescription.UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); assertIssue(issues.get(6), documentName, 31, 61, 31, 66, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + RuleDescription.UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); assertIssue(issues.get(7), documentName, 33, 26, 33, 31, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + RuleDescription.UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); assertIssue(issues.get(8), documentName, 33, 57, 33, 64, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + RuleDescription.UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); assertIssue(issues.get(9), documentName, 39, 26, 39, 31, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + RuleDescription.UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); assertIssue(issues.get(10), documentName, 46, 12, 46, 13, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + RuleDescription.UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); assertIssue(issues.get(11), documentName, 47, 17, 47, 22, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + RuleDescription.UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); assertIssue(issues.get(12), documentName, 53, 28, 53, 33, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + RuleDescription.UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); assertIssue(issues.get(13), documentName, 56, 19, 56, 26, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + RuleDescription.UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); assertIssue(issues.get(14), documentName, 60, 19, 60, 24, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETERS, RuleKind.CODE_SMELL); + RuleDescription.UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); + assertIssue(issues.get(15), documentName, 66, 46, 66, 47, "ballerina:2", 2, + RuleDescription.UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); } } diff --git a/scan-command/src/test/resources/command-outputs/unix/list-rules-output.txt b/scan-command/src/test/resources/command-outputs/unix/list-rules-output.txt index c9d415b..5ad2e66 100644 --- a/scan-command/src/test/resources/command-outputs/unix/list-rules-output.txt +++ b/scan-command/src/test/resources/command-outputs/unix/list-rules-output.txt @@ -1,8 +1,8 @@ Loading scan tool configurations from src/test/resources/test-resources/bal-project-with-config-file/Scan.toml - RuleID | Rule Kind | Rule Description - ---------------------------------------------------------------------------------------------- + RuleID | Rule Kind | Rule Description + --------------------------------------------------------------------------------------------- ballerina:1 | CODE_SMELL | Avoid checkpanic - ballerina:2 | CODE_SMELL | Unused function parameters + ballerina:2 | CODE_SMELL | Unused function parameter ballerina/example_module_static_code_analyzer:1 | CODE_SMELL | rule 1 ballerina/example_module_static_code_analyzer:2 | BUG | rule 2 ballerina/example_module_static_code_analyzer:3 | VULNERABILITY | rule 3 diff --git a/scan-command/src/test/resources/command-outputs/unix/print-rules-to-console.txt b/scan-command/src/test/resources/command-outputs/unix/print-rules-to-console.txt index 629d17e..431cad6 100644 --- a/scan-command/src/test/resources/command-outputs/unix/print-rules-to-console.txt +++ b/scan-command/src/test/resources/command-outputs/unix/print-rules-to-console.txt @@ -1,8 +1,8 @@ Loading scan tool configurations from src/test/resources/test-resources/bal-project-with-config-file/Scan.toml - RuleID | Rule Kind | Rule Description - ---------------------------------------------------------------------------------------------- + RuleID | Rule Kind | Rule Description + --------------------------------------------------------------------------------------------- ballerina:1 | CODE_SMELL | Avoid checkpanic - ballerina:2 | CODE_SMELL | Unused function parameters + ballerina:2 | CODE_SMELL | Unused function parameter ballerina/example_module_static_code_analyzer:1 | CODE_SMELL | rule 1 ballerina/example_module_static_code_analyzer:2 | BUG | rule 2 ballerina/example_module_static_code_analyzer:3 | VULNERABILITY | rule 3 diff --git a/scan-command/src/test/resources/command-outputs/windows/list-rules-output.txt b/scan-command/src/test/resources/command-outputs/windows/list-rules-output.txt index 8c1508c..e384b4e 100644 --- a/scan-command/src/test/resources/command-outputs/windows/list-rules-output.txt +++ b/scan-command/src/test/resources/command-outputs/windows/list-rules-output.txt @@ -1,8 +1,8 @@ Loading scan tool configurations from src\test\resources\test-resources\bal-project-with-config-file\Scan.toml - RuleID | Rule Kind | Rule Description - ---------------------------------------------------------------------------------------------- + RuleID | Rule Kind | Rule Description + --------------------------------------------------------------------------------------------- ballerina:1 | CODE_SMELL | Avoid checkpanic - ballerina:2 | CODE_SMELL | Unused function parameters + ballerina:2 | CODE_SMELL | Unused function parameter ballerina/example_module_static_code_analyzer:1 | CODE_SMELL | rule 1 ballerina/example_module_static_code_analyzer:2 | BUG | rule 2 ballerina/example_module_static_code_analyzer:3 | VULNERABILITY | rule 3 diff --git a/scan-command/src/test/resources/command-outputs/windows/print-rules-to-console.txt b/scan-command/src/test/resources/command-outputs/windows/print-rules-to-console.txt index 4667823..cab063a 100644 --- a/scan-command/src/test/resources/command-outputs/windows/print-rules-to-console.txt +++ b/scan-command/src/test/resources/command-outputs/windows/print-rules-to-console.txt @@ -1,8 +1,8 @@ Loading scan tool configurations from src\test\resources\test-resources\bal-project-with-config-file\Scan.toml - RuleID | Rule Kind | Rule Description - ---------------------------------------------------------------------------------------------- + RuleID | Rule Kind | Rule Description + --------------------------------------------------------------------------------------------- ballerina:1 | CODE_SMELL | Avoid checkpanic - ballerina:2 | CODE_SMELL | Unused function parameters + ballerina:2 | CODE_SMELL | Unused function parameter ballerina/example_module_static_code_analyzer:1 | CODE_SMELL | rule 1 ballerina/example_module_static_code_analyzer:2 | BUG | rule 2 ballerina/example_module_static_code_analyzer:3 | VULNERABILITY | rule 3 diff --git a/scan-command/src/test/resources/test-resources/core-rules/unused_anonymous_func_parameters.bal b/scan-command/src/test/resources/test-resources/core-rules/unused_anonymous_func_parameters.bal index 295561f..9043b24 100644 --- a/scan-command/src/test/resources/test-resources/core-rules/unused_anonymous_func_parameters.bal +++ b/scan-command/src/test/resources/test-resources/core-rules/unused_anonymous_func_parameters.bal @@ -61,3 +61,7 @@ public function main(int a, int b, int c) { // warning function doNothing(any a) { // warning return; } + +function (int a , int b) returns int v = (x, y) => x + y; + +function (int a , int b) returns int v2 = (x, y) => x; // warning From cc3f86b0fb8353e2daedbd3d014a568f1cbb4c6c Mon Sep 17 00:00:00 2001 From: Sasindu Alahakoon Date: Fri, 11 Oct 2024 11:58:14 +0530 Subject: [PATCH 14/18] Update the checkpanic expression test class name --- ...nalyzerTest.java => CheckpanicExpressionAnalyzerTest.java} | 4 ++-- scan-command/src/test/resources/testng.xml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) rename scan-command/src/test/java/io/ballerina/scan/internal/{CheckPanicAnalyzerTest.java => CheckpanicExpressionAnalyzerTest.java} (93%) diff --git a/scan-command/src/test/java/io/ballerina/scan/internal/CheckPanicAnalyzerTest.java b/scan-command/src/test/java/io/ballerina/scan/internal/CheckpanicExpressionAnalyzerTest.java similarity index 93% rename from scan-command/src/test/java/io/ballerina/scan/internal/CheckPanicAnalyzerTest.java rename to scan-command/src/test/java/io/ballerina/scan/internal/CheckpanicExpressionAnalyzerTest.java index d3a5b8e..c8fb935 100644 --- a/scan-command/src/test/java/io/ballerina/scan/internal/CheckPanicAnalyzerTest.java +++ b/scan-command/src/test/java/io/ballerina/scan/internal/CheckpanicExpressionAnalyzerTest.java @@ -28,11 +28,11 @@ import java.util.List; /** - * Checkpanic analyzer tests. + * Checkpanic expression analyzer tests. * * @since 0.1.0 */ -public class CheckpanicAnalyzerTest extends StaticCodeAnalyzerTest { +public class CheckpanicExpressionAnalyzerTest extends StaticCodeAnalyzerTest { @Test(description = "test checkpanic analyzer") void testCheckpanicAnalyzer() { diff --git a/scan-command/src/test/resources/testng.xml b/scan-command/src/test/resources/testng.xml index bffdd5d..3b99e76 100644 --- a/scan-command/src/test/resources/testng.xml +++ b/scan-command/src/test/resources/testng.xml @@ -29,7 +29,7 @@ under the License. - + From 2ef4c5747b40c5b981129114b00f7459782c2ca5 Mon Sep 17 00:00:00 2001 From: Sasindu Alahakoon Date: Fri, 11 Oct 2024 14:38:57 +0530 Subject: [PATCH 15/18] Rename the static code analysis test files --- .../src/main/java/io/ballerina/scan/internal/CoreRule.java | 5 ++--- .../{CheckpanicExpressionAnalyzerTest.java => Rule001.java} | 4 ++-- .../{UnusedFunctionParamAnalyzerTest.java => Rule002.java} | 6 +++--- .../java/io/ballerina/scan/utils/RuleDescription.java | 2 +- .../{rule_checkpanic.bal => rule001_rule_checkpanic.bal} | 0 ...ers.bal => rule002_unused_anonymous_func_parameters.bal} | 0 ...nc_parameters.bal => rule002_unused_func_parameters.bal} | 0 scan-command/src/test/resources/testng.xml | 4 ++-- 8 files changed, 10 insertions(+), 11 deletions(-) rename scan-command/src/test/java/io/ballerina/scan/internal/{CheckpanicExpressionAnalyzerTest.java => Rule001.java} (93%) rename scan-command/src/test/java/io/ballerina/scan/internal/{UnusedFunctionParamAnalyzerTest.java => Rule002.java} (97%) rename scan-command/src/{main => test}/java/io/ballerina/scan/utils/RuleDescription.java (91%) rename scan-command/src/test/resources/test-resources/core-rules/{rule_checkpanic.bal => rule001_rule_checkpanic.bal} (100%) rename scan-command/src/test/resources/test-resources/core-rules/{unused_anonymous_func_parameters.bal => rule002_unused_anonymous_func_parameters.bal} (100%) rename scan-command/src/test/resources/test-resources/core-rules/{unused_func_parameters.bal => rule002_unused_func_parameters.bal} (100%) diff --git a/scan-command/src/main/java/io/ballerina/scan/internal/CoreRule.java b/scan-command/src/main/java/io/ballerina/scan/internal/CoreRule.java index 1c8e8f0..0990326 100644 --- a/scan-command/src/main/java/io/ballerina/scan/internal/CoreRule.java +++ b/scan-command/src/main/java/io/ballerina/scan/internal/CoreRule.java @@ -20,7 +20,6 @@ import io.ballerina.scan.Rule; import io.ballerina.scan.RuleKind; -import io.ballerina.scan.utils.RuleDescription; import java.util.ArrayList; import java.util.List; @@ -31,9 +30,9 @@ * @since 0.1.0 * */ enum CoreRule { - AVOID_CHECKPANIC(RuleFactory.createRule(1, RuleDescription.AVOID_CHECKPANIC, RuleKind.CODE_SMELL)), + AVOID_CHECKPANIC(RuleFactory.createRule(1, "Avoid checkpanic", RuleKind.CODE_SMELL)), UNUSED_FUNCTION_PARAMETER(RuleFactory.createRule(2, - RuleDescription.UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL)); + "Unused function parameter", RuleKind.CODE_SMELL)); private final Rule rule; diff --git a/scan-command/src/test/java/io/ballerina/scan/internal/CheckpanicExpressionAnalyzerTest.java b/scan-command/src/test/java/io/ballerina/scan/internal/Rule001.java similarity index 93% rename from scan-command/src/test/java/io/ballerina/scan/internal/CheckpanicExpressionAnalyzerTest.java rename to scan-command/src/test/java/io/ballerina/scan/internal/Rule001.java index c8fb935..26c357d 100644 --- a/scan-command/src/test/java/io/ballerina/scan/internal/CheckpanicExpressionAnalyzerTest.java +++ b/scan-command/src/test/java/io/ballerina/scan/internal/Rule001.java @@ -32,11 +32,11 @@ * * @since 0.1.0 */ -public class CheckpanicExpressionAnalyzerTest extends StaticCodeAnalyzerTest { +public class Rule001 extends StaticCodeAnalyzerTest { @Test(description = "test checkpanic analyzer") void testCheckpanicAnalyzer() { - String documentName = "rule_checkpanic.bal"; + String documentName = "rule001_rule_checkpanic.bal"; Document document = loadDocument(documentName); ScannerContextImpl scannerContext = new ScannerContextImpl(List.of(CoreRule.AVOID_CHECKPANIC.rule())); StaticCodeAnalyzer staticCodeAnalyzer = new StaticCodeAnalyzer(document, diff --git a/scan-command/src/test/java/io/ballerina/scan/internal/UnusedFunctionParamAnalyzerTest.java b/scan-command/src/test/java/io/ballerina/scan/internal/Rule002.java similarity index 97% rename from scan-command/src/test/java/io/ballerina/scan/internal/UnusedFunctionParamAnalyzerTest.java rename to scan-command/src/test/java/io/ballerina/scan/internal/Rule002.java index a0e8155..e4d7ab3 100644 --- a/scan-command/src/test/java/io/ballerina/scan/internal/UnusedFunctionParamAnalyzerTest.java +++ b/scan-command/src/test/java/io/ballerina/scan/internal/Rule002.java @@ -14,11 +14,11 @@ * * @since 0.1.0 */ -public class UnusedFunctionParamAnalyzerTest extends StaticCodeAnalyzerTest { +public class Rule002 extends StaticCodeAnalyzerTest { @Test(description = "test unused function parameters analyzer") void testUnusedFunctionParameterAnalyzer() { - String documentName = "unused_func_parameters.bal"; + String documentName = "rule002_unused_func_parameters.bal"; Document document = loadDocument(documentName); ScannerContextImpl scannerContext = new ScannerContextImpl(List.of(CoreRule.UNUSED_FUNCTION_PARAMETER.rule())); StaticCodeAnalyzer staticCodeAnalyzer = new StaticCodeAnalyzer(document, @@ -65,7 +65,7 @@ void testUnusedFunctionParameterAnalyzer() { @Test(description = "test unused anonymous function parameters analyzer") void testUnusedAnonymousFunctionParameterAnalyzer() { - String documentName = "unused_anonymous_func_parameters.bal"; + String documentName = "rule002_unused_anonymous_func_parameters.bal"; Document document = loadDocument(documentName); ScannerContextImpl scannerContext = new ScannerContextImpl(List.of(CoreRule.UNUSED_FUNCTION_PARAMETER.rule())); StaticCodeAnalyzer staticCodeAnalyzer = new StaticCodeAnalyzer(document, diff --git a/scan-command/src/main/java/io/ballerina/scan/utils/RuleDescription.java b/scan-command/src/test/java/io/ballerina/scan/utils/RuleDescription.java similarity index 91% rename from scan-command/src/main/java/io/ballerina/scan/utils/RuleDescription.java rename to scan-command/src/test/java/io/ballerina/scan/utils/RuleDescription.java index 9c2dbd3..762056e 100644 --- a/scan-command/src/main/java/io/ballerina/scan/utils/RuleDescription.java +++ b/scan-command/src/test/java/io/ballerina/scan/utils/RuleDescription.java @@ -19,7 +19,7 @@ package io.ballerina.scan.utils; /** - * {@code RuleDescription} provides descriptions for Ballerina static coding rules. + * {@code RuleDescription} represents the descriptions for static code analysis rules. * * @since 0.1.0 */ diff --git a/scan-command/src/test/resources/test-resources/core-rules/rule_checkpanic.bal b/scan-command/src/test/resources/test-resources/core-rules/rule001_rule_checkpanic.bal similarity index 100% rename from scan-command/src/test/resources/test-resources/core-rules/rule_checkpanic.bal rename to scan-command/src/test/resources/test-resources/core-rules/rule001_rule_checkpanic.bal diff --git a/scan-command/src/test/resources/test-resources/core-rules/unused_anonymous_func_parameters.bal b/scan-command/src/test/resources/test-resources/core-rules/rule002_unused_anonymous_func_parameters.bal similarity index 100% rename from scan-command/src/test/resources/test-resources/core-rules/unused_anonymous_func_parameters.bal rename to scan-command/src/test/resources/test-resources/core-rules/rule002_unused_anonymous_func_parameters.bal diff --git a/scan-command/src/test/resources/test-resources/core-rules/unused_func_parameters.bal b/scan-command/src/test/resources/test-resources/core-rules/rule002_unused_func_parameters.bal similarity index 100% rename from scan-command/src/test/resources/test-resources/core-rules/unused_func_parameters.bal rename to scan-command/src/test/resources/test-resources/core-rules/rule002_unused_func_parameters.bal diff --git a/scan-command/src/test/resources/testng.xml b/scan-command/src/test/resources/testng.xml index 3b99e76..d29e26a 100644 --- a/scan-command/src/test/resources/testng.xml +++ b/scan-command/src/test/resources/testng.xml @@ -29,8 +29,8 @@ under the License. - - + + From 301a9ecd0c8a77bb2397e2cac6c49c9821d9cde4 Mon Sep 17 00:00:00 2001 From: anupama-pathirage Date: Mon, 21 Oct 2024 16:25:06 -0500 Subject: [PATCH 16/18] Add issue template --- .github/ISSUE_TEMPLATE/bug.yml | 31 +++++++++++++++++++++ .github/ISSUE_TEMPLATE/config.yml | 11 ++++++++ .github/ISSUE_TEMPLATE/improvement.yml | 25 +++++++++++++++++ .github/ISSUE_TEMPLATE/new-feature.yml | 32 ++++++++++++++++++++++ .github/ISSUE_TEMPLATE/task.yml | 18 ++++++++++++ .github/ISSUE_TEMPLATE/type_bug.md | 28 ------------------- .github/ISSUE_TEMPLATE/type_improvement.md | 26 ------------------ .github/ISSUE_TEMPLATE/type_new_feature.md | 26 ------------------ .github/ISSUE_TEMPLATE/type_task.md | 24 ---------------- 9 files changed, 117 insertions(+), 104 deletions(-) create mode 100644 .github/ISSUE_TEMPLATE/bug.yml create mode 100644 .github/ISSUE_TEMPLATE/config.yml create mode 100644 .github/ISSUE_TEMPLATE/improvement.yml create mode 100644 .github/ISSUE_TEMPLATE/new-feature.yml create mode 100644 .github/ISSUE_TEMPLATE/task.yml delete mode 100644 .github/ISSUE_TEMPLATE/type_bug.md delete mode 100644 .github/ISSUE_TEMPLATE/type_improvement.md delete mode 100644 .github/ISSUE_TEMPLATE/type_new_feature.md delete mode 100644 .github/ISSUE_TEMPLATE/type_task.md diff --git a/.github/ISSUE_TEMPLATE/bug.yml b/.github/ISSUE_TEMPLATE/bug.yml new file mode 100644 index 0000000..1c1bfe3 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/bug.yml @@ -0,0 +1,31 @@ +name: "🐞 Report a Bug" +description: Create an issue if something does not work as expected. +labels: ["Type/Bug"] +body: + - type: textarea + id: background + attributes: + label: Description + description: Please share a clear and concise description of the problem. + placeholder: Description + - type: textarea + id: steps + attributes: + label: Steps to Reproduce + description: List the steps you followed when you encountered the issue. Provide sample source code to reproduce the issue where applicable. + validations: + required: true + - type: input + id: version + attributes: + label: Version + description: Enter product/component version. + validations: + required: true + - type: textarea + id: environment + attributes: + label: Environment Details (with versions) + description: Mention the environment details (OS, Client, etc.) that the product is running on. + validations: + required: false diff --git a/.github/ISSUE_TEMPLATE/config.yml b/.github/ISSUE_TEMPLATE/config.yml new file mode 100644 index 0000000..aa6f24c --- /dev/null +++ b/.github/ISSUE_TEMPLATE/config.yml @@ -0,0 +1,11 @@ +blank_issues_enabled: false +contact_links: +- name: '📚 Documentation Issue' + about: Request a new article, missing topic, or report an issue if a topic is incorrect in the current documentation. + url: https://github.com/ballerina-platform/ballerina-dev-website/issues/new/choose +- name: General Question + url: https://stackoverflow.com/questions/tagged/ballerina + about: "If you have a question then please ask on Stack Overflow using the #ballerina tag." +- name: Chat on Ballerina Discord Channel + url: https://discord.gg/ballerinalang + about: "Chat about anything else with the community." diff --git a/.github/ISSUE_TEMPLATE/improvement.yml b/.github/ISSUE_TEMPLATE/improvement.yml new file mode 100644 index 0000000..053bfa8 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/improvement.yml @@ -0,0 +1,25 @@ +name: "🚀 Improvement Request" +description: Suggest an improvement to the product. +labels: ["Type/Improvement"] +body: + - type: textarea + id: limitation + attributes: + label: Current Limitation + description: Describe the the current limitation. + validations: + required: true + - type: textarea + id: suggestion + attributes: + label: Suggested Improvement + description: Describe the the improvement you suggest. + validations: + required: true + - type: input + id: version + attributes: + label: Version + description: Enter component version. + validations: + required: false diff --git a/.github/ISSUE_TEMPLATE/new-feature.yml b/.github/ISSUE_TEMPLATE/new-feature.yml new file mode 100644 index 0000000..39dd56f --- /dev/null +++ b/.github/ISSUE_TEMPLATE/new-feature.yml @@ -0,0 +1,32 @@ +name: "💡 New Feature Request" +description: Suggest new functionality and features for the product. +labels: ["Type/NewFeature"] +body: + - type: textarea + id: problem + attributes: + label: Problem + description: What is the problem this feature will solve? + validations: + required: true + - type: textarea + id: solution + attributes: + label: Proposed Solution + description: Describe the solution you'd like to have. + validations: + required: true + - type: textarea + id: alternatives + attributes: + label: Alternatives + description: Describe any alternatives have you considered + validations: + required: false + - type: input + id: version + attributes: + label: Version + description: Enter product/component version. + validations: + required: false diff --git a/.github/ISSUE_TEMPLATE/task.yml b/.github/ISSUE_TEMPLATE/task.yml new file mode 100644 index 0000000..ff238a0 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/task.yml @@ -0,0 +1,18 @@ +name: "✍️ Create a Task" +description: Create a new task. +labels: ["Type/Task"] +body: + - type: textarea + id: description + attributes: + label: Description + description: A clear description of what needs to be done. + validations: + required: true + - type: input + id: version + attributes: + label: Version + description: Enter product/component version. + validations: + required: false diff --git a/.github/ISSUE_TEMPLATE/type_bug.md b/.github/ISSUE_TEMPLATE/type_bug.md deleted file mode 100644 index 4124685..0000000 --- a/.github/ISSUE_TEMPLATE/type_bug.md +++ /dev/null @@ -1,28 +0,0 @@ ---- -name: "Bug Report" -about: "Report a bug if something is not working as expected" -labels: 'Type/Bug' - ---- - -**Description:** - - - -**Steps to reproduce:** - -**Affected Versions:** - -**OS, DB, other environment details and versions:** - -**Related Issues (optional):** - - - -**Suggested Labels (optional):** - - - -**Suggested Assignees (optional):** - - \ No newline at end of file diff --git a/.github/ISSUE_TEMPLATE/type_improvement.md b/.github/ISSUE_TEMPLATE/type_improvement.md deleted file mode 100644 index 46df96a..0000000 --- a/.github/ISSUE_TEMPLATE/type_improvement.md +++ /dev/null @@ -1,26 +0,0 @@ ---- -name: "Improvement Request" -about: "Create an improvement request for an existing feature" -labels: 'Type/Improvement' - ---- - -**Description:** - - - -**Describe your problem(s)** - -**Describe your solution(s)** - -**Related Issues (optional):** - - - -**Suggested Labels (optional):** - - - -**Suggested Assignees (optional):** - - \ No newline at end of file diff --git a/.github/ISSUE_TEMPLATE/type_new_feature.md b/.github/ISSUE_TEMPLATE/type_new_feature.md deleted file mode 100644 index 45c55d2..0000000 --- a/.github/ISSUE_TEMPLATE/type_new_feature.md +++ /dev/null @@ -1,26 +0,0 @@ ---- -name: "New Feature Request" -about: "Create a new feature request" -labels: 'Type/NewFeature' - ---- - -**Description:** - - - -**Describe your problem(s)** - -**Describe your solution(s)** - -**Related Issues (optional):** - - - -**Suggested Labels (optional):** - - - -**Suggested Assignees (optional):** - - \ No newline at end of file diff --git a/.github/ISSUE_TEMPLATE/type_task.md b/.github/ISSUE_TEMPLATE/type_task.md deleted file mode 100644 index bcec65d..0000000 --- a/.github/ISSUE_TEMPLATE/type_task.md +++ /dev/null @@ -1,24 +0,0 @@ ---- -name: "Task" -about: "Create a task which you want to keep track" -labels: 'Type/Task' - ---- - -**Description:** - - - -**Describe your task(s)** - -**Related Issues (optional):** - - - -**Suggested Labels (optional):** - - - -**Suggested Assignees (optional):** - - \ No newline at end of file From 9fab3e84106e7b004c155224d850ee98859c3bb0 Mon Sep 17 00:00:00 2001 From: Sasindu Alahakoon Date: Wed, 30 Oct 2024 11:44:32 +0530 Subject: [PATCH 17/18] Change the test file names in rule-002 --- .../scan/internal/ProjectAnalyzer.java | 5 +- .../ballerina/scan/internal/CoreRuleTest.java | 8 +- .../{Rule001.java => Rule001Test.java} | 10 +- .../{Rule002.java => Rule002Test.java} | 92 +++++++++++-------- .../ballerina/scan/utils/RuleDescription.java | 32 ------- scan-command/src/test/resources/testng.xml | 4 +- 6 files changed, 70 insertions(+), 81 deletions(-) rename scan-command/src/test/java/io/ballerina/scan/internal/{Rule001.java => Rule001Test.java} (86%) rename scan-command/src/test/java/io/ballerina/scan/internal/{Rule002.java => Rule002Test.java} (58%) delete mode 100644 scan-command/src/test/java/io/ballerina/scan/utils/RuleDescription.java diff --git a/scan-command/src/main/java/io/ballerina/scan/internal/ProjectAnalyzer.java b/scan-command/src/main/java/io/ballerina/scan/internal/ProjectAnalyzer.java index 86489cd..84b7867 100644 --- a/scan-command/src/main/java/io/ballerina/scan/internal/ProjectAnalyzer.java +++ b/scan-command/src/main/java/io/ballerina/scan/internal/ProjectAnalyzer.java @@ -22,6 +22,7 @@ import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; +import io.ballerina.compiler.api.SemanticModel; import io.ballerina.projects.CompilerPluginCache; import io.ballerina.projects.Document; import io.ballerina.projects.DocumentConfig; @@ -111,10 +112,10 @@ List analyze(List inbuiltRules) { } private Consumer analyzeDocument(Module module, ScannerContextImpl scannerContext) { + SemanticModel semanticModel = module.getCompilation().getSemanticModel(); return documentId -> { Document document = module.document(documentId); - StaticCodeAnalyzer analyzer = new StaticCodeAnalyzer(document, scannerContext, - module.getCompilation().getSemanticModel()); + StaticCodeAnalyzer analyzer = new StaticCodeAnalyzer(document, scannerContext, semanticModel); analyzer.analyze(); }; } diff --git a/scan-command/src/test/java/io/ballerina/scan/internal/CoreRuleTest.java b/scan-command/src/test/java/io/ballerina/scan/internal/CoreRuleTest.java index 637d798..1dd3c15 100644 --- a/scan-command/src/test/java/io/ballerina/scan/internal/CoreRuleTest.java +++ b/scan-command/src/test/java/io/ballerina/scan/internal/CoreRuleTest.java @@ -20,7 +20,6 @@ import io.ballerina.scan.Rule; import io.ballerina.scan.RuleKind; -import io.ballerina.scan.utils.RuleDescription; import org.testng.Assert; import org.testng.annotations.Test; @@ -30,6 +29,9 @@ * @since 0.1.0 */ public class CoreRuleTest { + public static final String AVOID_CHECKPANIC = "Avoid checkpanic"; + public static final String UNUSED_FUNCTION_PARAMETER = "Unused function parameter"; + @Test(description = "test all rules") void testAllRules() { Assert.assertEquals(CoreRule.rules().size(), 2); @@ -40,7 +42,7 @@ void testCheckpanicRule() { Rule rule = CoreRule.AVOID_CHECKPANIC.rule(); Assert.assertEquals(rule.id(), "ballerina:1"); Assert.assertEquals(rule.numericId(), 1); - Assert.assertEquals(rule.description(), RuleDescription.AVOID_CHECKPANIC); + Assert.assertEquals(rule.description(), AVOID_CHECKPANIC); Assert.assertEquals(rule.kind(), RuleKind.CODE_SMELL); } @@ -49,7 +51,7 @@ void testUnusedFunctionParameterRule() { Rule rule = CoreRule.UNUSED_FUNCTION_PARAMETER.rule(); Assert.assertEquals(rule.id(), "ballerina:2"); Assert.assertEquals(rule.numericId(), 2); - Assert.assertEquals(rule.description(), RuleDescription.UNUSED_FUNCTION_PARAMETER); + Assert.assertEquals(rule.description(), UNUSED_FUNCTION_PARAMETER); Assert.assertEquals(rule.kind(), RuleKind.CODE_SMELL); } } diff --git a/scan-command/src/test/java/io/ballerina/scan/internal/Rule001.java b/scan-command/src/test/java/io/ballerina/scan/internal/Rule001Test.java similarity index 86% rename from scan-command/src/test/java/io/ballerina/scan/internal/Rule001.java rename to scan-command/src/test/java/io/ballerina/scan/internal/Rule001Test.java index 26c357d..315316a 100644 --- a/scan-command/src/test/java/io/ballerina/scan/internal/Rule001.java +++ b/scan-command/src/test/java/io/ballerina/scan/internal/Rule001Test.java @@ -21,21 +21,21 @@ import io.ballerina.projects.Document; import io.ballerina.scan.Issue; import io.ballerina.scan.RuleKind; -import io.ballerina.scan.utils.RuleDescription; import org.testng.Assert; import org.testng.annotations.Test; import java.util.List; /** - * Checkpanic expression analyzer tests. + * Checkpanic usage analyzer tests. * * @since 0.1.0 */ -public class Rule001 extends StaticCodeAnalyzerTest { +public class Rule001Test extends StaticCodeAnalyzerTest { + public static final String AVOID_CHECKPANIC = "Avoid checkpanic"; @Test(description = "test checkpanic analyzer") - void testCheckpanicAnalyzer() { + void testCheckpanicUsage() { String documentName = "rule001_rule_checkpanic.bal"; Document document = loadDocument(documentName); ScannerContextImpl scannerContext = new ScannerContextImpl(List.of(CoreRule.AVOID_CHECKPANIC.rule())); @@ -46,6 +46,6 @@ void testCheckpanicAnalyzer() { Assert.assertEquals(issues.size(), 1); assertIssue(issues.get(0), documentName, 20, 17, 20, 39, "ballerina:1", 1, - RuleDescription.AVOID_CHECKPANIC, RuleKind.CODE_SMELL); + AVOID_CHECKPANIC, RuleKind.CODE_SMELL); } } diff --git a/scan-command/src/test/java/io/ballerina/scan/internal/Rule002.java b/scan-command/src/test/java/io/ballerina/scan/internal/Rule002Test.java similarity index 58% rename from scan-command/src/test/java/io/ballerina/scan/internal/Rule002.java rename to scan-command/src/test/java/io/ballerina/scan/internal/Rule002Test.java index e4d7ab3..caa365e 100644 --- a/scan-command/src/test/java/io/ballerina/scan/internal/Rule002.java +++ b/scan-command/src/test/java/io/ballerina/scan/internal/Rule002Test.java @@ -1,9 +1,26 @@ +/* + * Copyright (c) 2024, 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.internal; import io.ballerina.projects.Document; import io.ballerina.scan.Issue; import io.ballerina.scan.RuleKind; -import io.ballerina.scan.utils.RuleDescription; import org.testng.Assert; import org.testng.annotations.Test; @@ -14,10 +31,11 @@ * * @since 0.1.0 */ -public class Rule002 extends StaticCodeAnalyzerTest { +public class Rule002Test extends StaticCodeAnalyzerTest { + public static final String UNUSED_FUNCTION_PARAMETER = "Unused function parameter"; @Test(description = "test unused function parameters analyzer") - void testUnusedFunctionParameterAnalyzer() { + void testUnusedFunctionParameter() { String documentName = "rule002_unused_func_parameters.bal"; Document document = loadDocument(documentName); ScannerContextImpl scannerContext = new ScannerContextImpl(List.of(CoreRule.UNUSED_FUNCTION_PARAMETER.rule())); @@ -28,43 +46,43 @@ void testUnusedFunctionParameterAnalyzer() { Assert.assertEquals(issues.size(), 17); assertIssue(issues.get(0), documentName, 28, 29, 28, 34, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); + UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); assertIssue(issues.get(1), documentName, 36, 29, 36, 38, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); + UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); assertIssue(issues.get(2), documentName, 40, 29, 40, 38, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); + UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); assertIssue(issues.get(3), documentName, 42, 22, 42, 27, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); + UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); assertIssue(issues.get(4), documentName, 42, 29, 42, 37, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); + UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); assertIssue(issues.get(5), documentName, 44, 22, 44, 27, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); + UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); assertIssue(issues.get(6), documentName, 44, 29, 44, 37, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); + UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); assertIssue(issues.get(7), documentName, 53, 33, 53, 38, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); + UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); assertIssue(issues.get(8), documentName, 57, 33, 57, 42, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); + UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); assertIssue(issues.get(9), documentName, 61, 26, 61, 31, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); + UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); assertIssue(issues.get(10), documentName, 61, 33, 61, 41, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); + UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); assertIssue(issues.get(11), documentName, 72, 19, 72, 24, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); + UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); assertIssue(issues.get(12), documentName, 76, 18, 76, 23, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); + UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); assertIssue(issues.get(13), documentName, 76, 32, 76, 37, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); + UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); assertIssue(issues.get(14), documentName, 77, 22, 77, 28, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); + UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); assertIssue(issues.get(15), documentName, 77, 30, 77, 36, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); + UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); assertIssue(issues.get(16), documentName, 83, 44, 83, 63, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); + UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); } @Test(description = "test unused anonymous function parameters analyzer") - void testUnusedAnonymousFunctionParameterAnalyzer() { + void testUnusedAnonymousFunctionParameter() { String documentName = "rule002_unused_anonymous_func_parameters.bal"; Document document = loadDocument(documentName); ScannerContextImpl scannerContext = new ScannerContextImpl(List.of(CoreRule.UNUSED_FUNCTION_PARAMETER.rule())); @@ -76,36 +94,36 @@ void testUnusedAnonymousFunctionParameterAnalyzer() { Assert.assertEquals(issues.size(), 16); assertIssue(issues.get(0), documentName, 16, 34, 16, 39, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); + UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); assertIssue(issues.get(1), documentName, 16, 41, 16, 46, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); + UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); assertIssue(issues.get(2), documentName, 17, 17, 17, 24, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); + UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); assertIssue(issues.get(3), documentName, 25, 26, 25, 31, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); + UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); assertIssue(issues.get(4), documentName, 27, 50, 27, 57, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); + UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); assertIssue(issues.get(5), documentName, 29, 48, 29, 49, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); + UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); assertIssue(issues.get(6), documentName, 31, 61, 31, 66, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); + UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); assertIssue(issues.get(7), documentName, 33, 26, 33, 31, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); + UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); assertIssue(issues.get(8), documentName, 33, 57, 33, 64, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); + UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); assertIssue(issues.get(9), documentName, 39, 26, 39, 31, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); + UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); assertIssue(issues.get(10), documentName, 46, 12, 46, 13, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); + UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); assertIssue(issues.get(11), documentName, 47, 17, 47, 22, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); + UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); assertIssue(issues.get(12), documentName, 53, 28, 53, 33, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); + UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); assertIssue(issues.get(13), documentName, 56, 19, 56, 26, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); + UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); assertIssue(issues.get(14), documentName, 60, 19, 60, 24, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); + UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); assertIssue(issues.get(15), documentName, 66, 46, 66, 47, "ballerina:2", 2, - RuleDescription.UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); + UNUSED_FUNCTION_PARAMETER, RuleKind.CODE_SMELL); } } diff --git a/scan-command/src/test/java/io/ballerina/scan/utils/RuleDescription.java b/scan-command/src/test/java/io/ballerina/scan/utils/RuleDescription.java deleted file mode 100644 index 762056e..0000000 --- a/scan-command/src/test/java/io/ballerina/scan/utils/RuleDescription.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright (c) 2024, 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; - -/** - * {@code RuleDescription} represents the descriptions for static code analysis rules. - * - * @since 0.1.0 - */ -public class RuleDescription { - public static final String AVOID_CHECKPANIC = "Avoid checkpanic"; - public static final String UNUSED_FUNCTION_PARAMETER = "Unused function parameter"; - - private RuleDescription() { - } -} diff --git a/scan-command/src/test/resources/testng.xml b/scan-command/src/test/resources/testng.xml index d29e26a..cbdaee0 100644 --- a/scan-command/src/test/resources/testng.xml +++ b/scan-command/src/test/resources/testng.xml @@ -29,8 +29,8 @@ under the License. - - + + From 833c0b376934f60d9e66c91e0238e4dc187e8de6 Mon Sep 17 00:00:00 2001 From: Sasindu Alahakoon Date: Thu, 31 Oct 2024 11:03:35 +0530 Subject: [PATCH 18/18] Update ballerina version into 2201.10.2 --- gradle.properties | 2 +- scan-command/tool-scan/Ballerina.toml | 2 +- scan-command/tool-scan/Dependencies.toml | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/gradle.properties b/gradle.properties index 5f6a820..19d07b4 100644 --- a/gradle.properties +++ b/gradle.properties @@ -10,7 +10,7 @@ enterprisePluginVersion=3.18.1 # Dependency versions picoCLIVersion=4.7.5 gsonVersion=2.10.1 -ballerinaLangVersion=2201.10.1 +ballerinaLangVersion=2201.10.2 puppycrawlCheckstyleVersion=10.12.1 apacheCommonsLang3Version=3.0 commonsIoVersion=2.15.1 diff --git a/scan-command/tool-scan/Ballerina.toml b/scan-command/tool-scan/Ballerina.toml index 190051a..72c0113 100644 --- a/scan-command/tool-scan/Ballerina.toml +++ b/scan-command/tool-scan/Ballerina.toml @@ -2,7 +2,7 @@ org = "ballerina" name = "tool_scan" version = "0.1.0" -distribution = "2201.10.1" +distribution = "2201.10.2" authors = ["Ballerina"] keywords = ["scan", "static code analysis"] license = ["Apache-2.0"] diff --git a/scan-command/tool-scan/Dependencies.toml b/scan-command/tool-scan/Dependencies.toml index b9ab987..0137f0f 100644 --- a/scan-command/tool-scan/Dependencies.toml +++ b/scan-command/tool-scan/Dependencies.toml @@ -5,7 +5,7 @@ [ballerina] dependencies-toml-version = "2" -distribution-version = "2201.10.1" +distribution-version = "2201.10.2" [[package]] org = "ballerina"