From 583a095213f498e0ca4ecbaebc78e230ae030255 Mon Sep 17 00:00:00 2001 From: Eric Le Goff Date: Thu, 22 Jul 2021 13:41:47 +0200 Subject: [PATCH 01/14] Prepare a built-in profile --- .../org/elegoff/rust/checks/CheckList.java | 19 +++++++----- .../elegoff/rust/checks/LineLengthCheck.java | 31 +++++++++++++++++++ .../elegoff/I10n/rust/rules/LineLength.html | 1 + .../elegoff/I10n/rust/rules/LineLength.json | 15 +++++++++ .../I10n/rust/rules/built-in-profile.json | 6 ++++ .../elegoff/rust/checks/CheckListTest.java | 2 +- .../rust/language/RustQualityProfile.java | 9 +++++- .../rust/rules/RustRulesDefinition.java | 14 ++++++++- .../org/elegoff/l10n/rust/rules/rules.json | 1 - .../rust/language/RustQualityProfileTest.java | 2 +- 10 files changed, 87 insertions(+), 13 deletions(-) create mode 100644 rust-checks/src/main/java/org/elegoff/rust/checks/LineLengthCheck.java create mode 100644 rust-checks/src/main/resources/org/elegoff/I10n/rust/rules/LineLength.html create mode 100644 rust-checks/src/main/resources/org/elegoff/I10n/rust/rules/LineLength.json create mode 100644 rust-checks/src/main/resources/org/elegoff/I10n/rust/rules/built-in-profile.json delete mode 100644 sonar-rust-plugin/src/main/resources/org/elegoff/l10n/rust/rules/rules.json diff --git a/rust-checks/src/main/java/org/elegoff/rust/checks/CheckList.java b/rust-checks/src/main/java/org/elegoff/rust/checks/CheckList.java index ea3a5532..0d973f77 100644 --- a/rust-checks/src/main/java/org/elegoff/rust/checks/CheckList.java +++ b/rust-checks/src/main/java/org/elegoff/rust/checks/CheckList.java @@ -2,17 +2,17 @@ * Sonar Rust Plugin (Community) * Copyright (C) 2021 Eric Le Goff * http://github.com/elegoff/sonar-rust - * + *

* This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 3 of the License, or (at your option) any later version. - * + *

* This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. - * + *

* You should have received a copy of the GNU Lesser General Public License * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. @@ -25,12 +25,15 @@ public class CheckList { public static final String REPOSITORY_KEY = "rust"; - private CheckList() { } + private CheckList() { + } - public static List> getRustChecks() { - //empty array so far, until a first rule is defined - return new ArrayList<>(); + public static List> getRustChecks() { + return Arrays.asList( + LineLengthCheck.class + ); } +} + -} diff --git a/rust-checks/src/main/java/org/elegoff/rust/checks/LineLengthCheck.java b/rust-checks/src/main/java/org/elegoff/rust/checks/LineLengthCheck.java new file mode 100644 index 00000000..7d97999d --- /dev/null +++ b/rust-checks/src/main/java/org/elegoff/rust/checks/LineLengthCheck.java @@ -0,0 +1,31 @@ +package org.elegoff.rust.checks; + +import com.sonar.sslr.api.AstNode; +import org.sonar.check.Rule; +import org.sonar.check.RuleProperty; + +@Rule(key = "LineLength") +public class LineLengthCheck extends RustCheck { + + private static final int DEFAULT_MAXIMUM_LINE_LENHGTH = 120; + + @RuleProperty( + key = "maximumLineLength", + description = "The maximum authorized line length.", + defaultValue = "" + DEFAULT_MAXIMUM_LINE_LENHGTH) + public int maximumLineLength = DEFAULT_MAXIMUM_LINE_LENHGTH; + + @Override + public void visitFile(AstNode astNode) { + String[] lines = getContext().file().content().split("\\r?\\n"); + for (int i = 0; i < lines.length; i++) { + int length = lines[i].length(); + if (length > maximumLineLength) { + addLineIssue( + "Split this " + length + " characters long line (which is greater than " + maximumLineLength + " authorized).", + i + 1); + } + } + } + +} diff --git a/rust-checks/src/main/resources/org/elegoff/I10n/rust/rules/LineLength.html b/rust-checks/src/main/resources/org/elegoff/I10n/rust/rules/LineLength.html new file mode 100644 index 00000000..2e9b6fe1 --- /dev/null +++ b/rust-checks/src/main/resources/org/elegoff/I10n/rust/rules/LineLength.html @@ -0,0 +1 @@ +

Having to scroll horizontally makes it harder to get a quick overview and understanding of any piece of code.

\ No newline at end of file diff --git a/rust-checks/src/main/resources/org/elegoff/I10n/rust/rules/LineLength.json b/rust-checks/src/main/resources/org/elegoff/I10n/rust/rules/LineLength.json new file mode 100644 index 00000000..fb8759c7 --- /dev/null +++ b/rust-checks/src/main/resources/org/elegoff/I10n/rust/rules/LineLength.json @@ -0,0 +1,15 @@ +{ + "title": "Lines should not be too long", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "1min" + }, + "tags": [ + "convention" + ], + "defaultSeverity": "Major", + "sqKey": "LineLength", + "scope": "All" +} diff --git a/rust-checks/src/main/resources/org/elegoff/I10n/rust/rules/built-in-profile.json b/rust-checks/src/main/resources/org/elegoff/I10n/rust/rules/built-in-profile.json new file mode 100644 index 00000000..b4c3571e --- /dev/null +++ b/rust-checks/src/main/resources/org/elegoff/I10n/rust/rules/built-in-profile.json @@ -0,0 +1,6 @@ +{ + "name": "Community Rust", + "ruleKeys": [ + + ] +} \ No newline at end of file diff --git a/rust-checks/src/test/java/org/elegoff/rust/checks/CheckListTest.java b/rust-checks/src/test/java/org/elegoff/rust/checks/CheckListTest.java index 1454966a..5de77bd6 100644 --- a/rust-checks/src/test/java/org/elegoff/rust/checks/CheckListTest.java +++ b/rust-checks/src/test/java/org/elegoff/rust/checks/CheckListTest.java @@ -27,6 +27,6 @@ public class CheckListTest { @Test public void testSize(){ - assertThat(CheckList.getRustChecks().isEmpty()).isTrue(); + assertThat(CheckList.getRustChecks().size()).isEqualTo(1); } } diff --git a/sonar-rust-plugin/src/main/java/org/elegoff/plugins/rust/language/RustQualityProfile.java b/sonar-rust-plugin/src/main/java/org/elegoff/plugins/rust/language/RustQualityProfile.java index 34055d1d..64077f6b 100644 --- a/sonar-rust-plugin/src/main/java/org/elegoff/plugins/rust/language/RustQualityProfile.java +++ b/sonar-rust-plugin/src/main/java/org/elegoff/plugins/rust/language/RustQualityProfile.java @@ -19,7 +19,10 @@ */ package org.elegoff.plugins.rust.language; +import org.elegoff.plugins.rust.rules.RustRulesDefinition; +import org.elegoff.rust.checks.CheckList; import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition; +import org.sonarsource.analyzer.commons.BuiltInQualityProfileJsonLoader; /** @@ -27,9 +30,13 @@ */ public final class RustQualityProfile implements BuiltInQualityProfilesDefinition { + public static final String PROFILE_PATH = RustRulesDefinition.RULES_DEFINITION_FOLDER+"/built-in-profile.json"; + static final String PROFILE_NAME = "Community Rust"; + @Override public void define(Context context) { - NewBuiltInQualityProfile profile = context.createBuiltInQualityProfile("RUST Rules", RustLanguage.KEY); + NewBuiltInQualityProfile profile = context.createBuiltInQualityProfile(PROFILE_NAME, RustLanguage.KEY); + BuiltInQualityProfileJsonLoader.load(profile, CheckList.REPOSITORY_KEY, PROFILE_PATH); profile.setDefault(true); profile.done(); } diff --git a/sonar-rust-plugin/src/main/java/org/elegoff/plugins/rust/rules/RustRulesDefinition.java b/sonar-rust-plugin/src/main/java/org/elegoff/plugins/rust/rules/RustRulesDefinition.java index d81d0220..1ba43c86 100644 --- a/sonar-rust-plugin/src/main/java/org/elegoff/plugins/rust/rules/RustRulesDefinition.java +++ b/sonar-rust-plugin/src/main/java/org/elegoff/plugins/rust/rules/RustRulesDefinition.java @@ -20,19 +20,31 @@ package org.elegoff.plugins.rust.rules; import org.elegoff.plugins.rust.language.RustLanguage; +import org.elegoff.plugins.rust.language.RustQualityProfile; +import org.elegoff.rust.checks.CheckList; +import org.elegoff.rust.checks.RustCheck; import org.sonar.api.server.rule.RulesDefinition; +import org.sonarsource.analyzer.commons.RuleMetadataLoader; + +import java.util.List; public class RustRulesDefinition implements RulesDefinition { /** * Path to the directory/folder containing the descriptor files (JSON and HTML) for the rules */ - public static final String RULES_DEFINITION_FOLDER = "org/elegoff/l10n/rust/rules"; + public static final String RULES_DEFINITION_FOLDER = "org/elegoff/I10n/rust/rules"; @Override public void define(Context context) { NewRepository repository = context.createRepository("rust", RustLanguage.KEY).setName("RUST Analyzer"); + List> checkClasses = CheckList.getRustChecks(); + RuleMetadataLoader ruleMetadataLoader = new RuleMetadataLoader(RULES_DEFINITION_FOLDER, RustQualityProfile.PROFILE_PATH); + ruleMetadataLoader.addRulesByAnnotatedClass(repository, checkClasses); + repository.rules().stream() + .forEach(r -> r.setTemplate(true)); + //Current repository is empty repository.done(); } diff --git a/sonar-rust-plugin/src/main/resources/org/elegoff/l10n/rust/rules/rules.json b/sonar-rust-plugin/src/main/resources/org/elegoff/l10n/rust/rules/rules.json deleted file mode 100644 index 9e26dfee..00000000 --- a/sonar-rust-plugin/src/main/resources/org/elegoff/l10n/rust/rules/rules.json +++ /dev/null @@ -1 +0,0 @@ -{} \ No newline at end of file diff --git a/sonar-rust-plugin/src/test/java/org/elegoff/plugins/rust/language/RustQualityProfileTest.java b/sonar-rust-plugin/src/test/java/org/elegoff/plugins/rust/language/RustQualityProfileTest.java index 9acb7512..851aa524 100644 --- a/sonar-rust-plugin/src/test/java/org/elegoff/plugins/rust/language/RustQualityProfileTest.java +++ b/sonar-rust-plugin/src/test/java/org/elegoff/plugins/rust/language/RustQualityProfileTest.java @@ -29,7 +29,7 @@ public void testDefine() { BuiltInQualityProfilesDefinition.Context context = new BuiltInQualityProfilesDefinition.Context(); RustQualityProfile qp = new RustQualityProfile(); qp.define(context); - BuiltInQualityProfilesDefinition.BuiltInQualityProfile profile = context.profile("rust", "RUST Rules"); + BuiltInQualityProfilesDefinition.BuiltInQualityProfile profile = context.profile("rust", "Community Rust"); assertNotNull(profile); assertTrue(profile.isDefault()); assertEquals(0, profile.rules().size()); From 65d42fd1ffd455079b295dd5a3e39f18b0afb951 Mon Sep 17 00:00:00 2001 From: Eric Le Goff Date: Thu, 22 Jul 2021 16:52:50 +0200 Subject: [PATCH 02/14] Add rule Function Parameters count check --- rust-checks/pom.xml | 4 + .../checks/FunctionParametersCountCheck.java | 43 +++++++ .../FunctionParametersCountCheckTest.java | 20 +++ .../rust/checks/RustCheckVerifier.java | 116 ++++++++++++++++++ .../checks/FunctionParametersCount.rs | 5 + 5 files changed, 188 insertions(+) create mode 100644 rust-checks/src/main/java/org/elegoff/rust/checks/FunctionParametersCountCheck.java create mode 100644 rust-checks/src/test/java/org/elegoff/rust/checks/FunctionParametersCountCheckTest.java create mode 100644 rust-checks/src/test/java/org/elegoff/rust/checks/RustCheckVerifier.java create mode 100644 rust-checks/src/test/resources/checks/FunctionParametersCount.rs diff --git a/rust-checks/pom.xml b/rust-checks/pom.xml index c6af89be..0995e73f 100644 --- a/rust-checks/pom.xml +++ b/rust-checks/pom.xml @@ -28,6 +28,10 @@ commons-io commons-io + + org.sonarsource.analyzer-commons + sonar-analyzer-test-commons + diff --git a/rust-checks/src/main/java/org/elegoff/rust/checks/FunctionParametersCountCheck.java b/rust-checks/src/main/java/org/elegoff/rust/checks/FunctionParametersCountCheck.java new file mode 100644 index 00000000..039d7827 --- /dev/null +++ b/rust-checks/src/main/java/org/elegoff/rust/checks/FunctionParametersCountCheck.java @@ -0,0 +1,43 @@ +package org.elegoff.rust.checks; + +import com.sonar.sslr.api.AstNode; +import com.sonar.sslr.api.AstNodeType; +import org.sonar.check.RuleProperty; +import org.sonar.rust.RustGrammar; + +import java.util.Collections; +import java.util.Set; + +public class FunctionParametersCountCheck extends RustCheck { + + private static final int DEFAULT_MAXIMUM_PARAMETER_COUNT = 8; + + @RuleProperty( + key = "maximumParameterCount", + description = " Maximum authorized number of parameters", + defaultValue = "" + DEFAULT_MAXIMUM_PARAMETER_COUNT) + public int maximumParameterCount = DEFAULT_MAXIMUM_PARAMETER_COUNT; + + @Override + public Set subscribedKinds() { + return Collections.singleton(RustGrammar.FUNCTION); + } + + @Override + public void visitNode(AstNode node) { + int numberOfParameters = getNumberOfParameters(node); + + if (numberOfParameters > maximumParameterCount) { + addIssue( + "Reduce the number of parameters that this function takes from " + numberOfParameters + " to at most " + maximumParameterCount + ".", + node); + } + } + + private static int getNumberOfParameters(AstNode node) { + AstNode parameterNameList = node.getFirstChild(RustGrammar.FUNCTION_PARAMETERS); + + return parameterNameList == null ? 0 : parameterNameList.getChildren(RustGrammar.FUNCTION_PARAM).size(); + } + +} diff --git a/rust-checks/src/test/java/org/elegoff/rust/checks/FunctionParametersCountCheckTest.java b/rust-checks/src/test/java/org/elegoff/rust/checks/FunctionParametersCountCheckTest.java new file mode 100644 index 00000000..6f68920d --- /dev/null +++ b/rust-checks/src/test/java/org/elegoff/rust/checks/FunctionParametersCountCheckTest.java @@ -0,0 +1,20 @@ +package org.elegoff.rust.checks; + +import org.junit.Test; + +import java.io.File; + +public class FunctionParametersCountCheckTest { + @Test + public void test() { + RustCheckVerifier.verify(new File("src/test/resources/checks/FunctionParametersCount.rs"), new FunctionParametersCountCheck()); + } + + @Test + public void custom() { + FunctionParametersCountCheck check = new FunctionParametersCountCheck(); + check.maximumParameterCount = 10; + + RustCheckVerifier.verifyNoIssueIgnoringExpected(new File("src/test/resources/checks/FunctionParametersCount.rs"), check); + } +} diff --git a/rust-checks/src/test/java/org/elegoff/rust/checks/RustCheckVerifier.java b/rust-checks/src/test/java/org/elegoff/rust/checks/RustCheckVerifier.java new file mode 100644 index 00000000..615fbb21 --- /dev/null +++ b/rust-checks/src/test/java/org/elegoff/rust/checks/RustCheckVerifier.java @@ -0,0 +1,116 @@ +package org.elegoff.rust.checks; + +import java.io.File; +import java.io.IOException; +import java.net.URI; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import com.sonar.sslr.api.Grammar; +import com.sonar.sslr.api.RecognitionException; +import com.sonar.sslr.impl.Parser; +import org.sonar.rust.RustFile; +import org.sonar.rust.RustParser; +import org.sonar.rust.RustParserConfiguration; +import org.sonar.rust.RustVisitorContext; +import org.sonarsource.analyzer.commons.checks.verifier.SingleFileVerifier; + +public class RustCheckVerifier { + public static void verify(File file, RustCheck check) { + createVerifier(file, check, true, Integer.MAX_VALUE).assertOneOrMoreIssues(); + } + + public static void verifyWithMarginRight(File file, RustCheck check, int marginRight) { + createVerifier(file, check, true, marginRight).assertOneOrMoreIssues(); + } + + public static void verifyNoIssue(File file, RustCheck check) { + createVerifier(file, check, true, Integer.MAX_VALUE).assertNoIssues(); + } + + public static void verifyNoIssueIgnoringExpected(File file, RustCheck check) { + createVerifier(file, check, false, Integer.MAX_VALUE).assertNoIssues(); + } + + + private static SingleFileVerifier createVerifier(File file, RustCheck check, boolean addCommentsAsExpectedIssues, int marginRight) { + SingleFileVerifier verifier = SingleFileVerifier.create(file.toPath(), StandardCharsets.UTF_8); + + RustParserConfiguration conf = RustParserConfiguration.builder() + .setCharset(StandardCharsets.UTF_8) + .build(); + + Parser parser = RustParser.create(conf); + TestRustFile RustFile = new TestRustFile(file, StandardCharsets.UTF_8); + RustVisitorContext context; + try { + context = new RustVisitorContext(RustFile, parser.parse(file)); + } catch (RecognitionException e) { + context = new RustVisitorContext(RustFile, e); + } + + for (Issue issue : check.scanFileForIssues(context)) { + SingleFileVerifier.IssueBuilder issueBuilder = verifier.reportIssue(issue.message()); + Integer line = issue.line(); + if (line != null) { + issueBuilder.onLine(line); + } else { + issueBuilder.onFile(); + } + } + + if (addCommentsAsExpectedIssues) { + parseCommentsAsExpectedIssues(verifier, RustFile); + } + + return verifier; + } + + private static void parseCommentsAsExpectedIssues(SingleFileVerifier verifier, TestRustFile RustFile) { + Pattern commentPattern = Pattern.compile("/\\*.*?\\*/"); + String lines[] = RustFile.content().split("\\r?\\n"); + for (int i = 0; i < lines.length; i++) { + String line = lines[i]; + Matcher matcher = commentPattern.matcher(line); + while (matcher.find()) { + String commentString = matcher.group(); + verifier.addComment(i + 1, matcher.start() + 1, commentString, 2, 2); + } + } + } + + private static class TestRustFile implements RustFile { + + private final File file; + private final Charset charset; + + public TestRustFile(File file, Charset charset) { + this.file = file; + this.charset = charset; + } + + @Override + public String name() { + return file.getName(); + } + + @Override + public String content() { + try { + return new String(Files.readAllBytes(file.toPath()), charset); + } catch (IOException e) { + throw new IllegalStateException("Could not read " + file, e); + } + } + + @Override + public URI uri() { + return file.toURI(); + } + + } +} + diff --git a/rust-checks/src/test/resources/checks/FunctionParametersCount.rs b/rust-checks/src/test/resources/checks/FunctionParametersCount.rs new file mode 100644 index 00000000..bc038dfc --- /dev/null +++ b/rust-checks/src/test/resources/checks/FunctionParametersCount.rs @@ -0,0 +1,5 @@ + + +fn nine_params(_p1 : i32,_p2 : i32,_p3 : i32,_p4 : i32,_p5 : i32,_p6 : i32,_p7 : i32,_p8 : i32,_p9 : i32) -> i32 {/* Noncompliant {{Reduce the number of parameters that this function takes from 9 to at most 8.}} */ + return 42; +} \ No newline at end of file From 976e61e8747d973762415d84692649cce88a65f0 Mon Sep 17 00:00:00 2001 From: Eric Le Goff Date: Fri, 23 Jul 2021 15:03:32 +0200 Subject: [PATCH 03/14] Add a second rule --- .../I10n/rust/rules/FunctionParametersCount.html | 0 .../I10n/rust/rules/FunctionParametersCount.json | 14 ++++++++++++++ .../elegoff/rust/checks/LineLengthCheckTest.java | 2 ++ .../src/test/resources/checks/lineLength.rs | 0 .../src/test/resources/checks/lineLength119.rs | 0 5 files changed, 16 insertions(+) create mode 100644 rust-checks/src/main/resources/org/elegoff/I10n/rust/rules/FunctionParametersCount.html create mode 100644 rust-checks/src/main/resources/org/elegoff/I10n/rust/rules/FunctionParametersCount.json create mode 100644 rust-checks/src/test/java/org/elegoff/rust/checks/LineLengthCheckTest.java create mode 100644 rust-checks/src/test/resources/checks/lineLength.rs create mode 100644 rust-checks/src/test/resources/checks/lineLength119.rs diff --git a/rust-checks/src/main/resources/org/elegoff/I10n/rust/rules/FunctionParametersCount.html b/rust-checks/src/main/resources/org/elegoff/I10n/rust/rules/FunctionParametersCount.html new file mode 100644 index 00000000..e69de29b diff --git a/rust-checks/src/main/resources/org/elegoff/I10n/rust/rules/FunctionParametersCount.json b/rust-checks/src/main/resources/org/elegoff/I10n/rust/rules/FunctionParametersCount.json new file mode 100644 index 00000000..1bbea5f1 --- /dev/null +++ b/rust-checks/src/main/resources/org/elegoff/I10n/rust/rules/FunctionParametersCount.json @@ -0,0 +1,14 @@ +{ + "title": "Functions should not have too many parameters", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "20min" + }, + "tags": [ + "brain-overload" + ], + "defaultSeverity": "Major", + "scope": "All" +} \ No newline at end of file diff --git a/rust-checks/src/test/java/org/elegoff/rust/checks/LineLengthCheckTest.java b/rust-checks/src/test/java/org/elegoff/rust/checks/LineLengthCheckTest.java new file mode 100644 index 00000000..1f157fbd --- /dev/null +++ b/rust-checks/src/test/java/org/elegoff/rust/checks/LineLengthCheckTest.java @@ -0,0 +1,2 @@ +package org.elegoff.rust.checks;public class LinLengthCheckTest { +} diff --git a/rust-checks/src/test/resources/checks/lineLength.rs b/rust-checks/src/test/resources/checks/lineLength.rs new file mode 100644 index 00000000..e69de29b diff --git a/rust-checks/src/test/resources/checks/lineLength119.rs b/rust-checks/src/test/resources/checks/lineLength119.rs new file mode 100644 index 00000000..e69de29b From 28c4e9a3698cd9ea443d08f33294489ac132f420 Mon Sep 17 00:00:00 2001 From: Eric Le Goff Date: Fri, 23 Jul 2021 18:38:52 +0200 Subject: [PATCH 04/14] Add unit test for LineLength rule --- .../rust/checks/LineLengthCheckTest.java | 19 ++++++++++++++++++- .../src/test/resources/checks/lineLength.rs | 3 +++ .../test/resources/checks/lineLength119.rs | 6 ++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/rust-checks/src/test/java/org/elegoff/rust/checks/LineLengthCheckTest.java b/rust-checks/src/test/java/org/elegoff/rust/checks/LineLengthCheckTest.java index 1f157fbd..a7fb610b 100644 --- a/rust-checks/src/test/java/org/elegoff/rust/checks/LineLengthCheckTest.java +++ b/rust-checks/src/test/java/org/elegoff/rust/checks/LineLengthCheckTest.java @@ -1,2 +1,19 @@ -package org.elegoff.rust.checks;public class LinLengthCheckTest { +package org.elegoff.rust.checks; + +import org.junit.Test; + +import java.io.File; + +public class LineLengthCheckTest { + @Test + public void test() { + RustCheckVerifier.verify(new File("src/test/resources/checks/lineLength.rs"), new LineLengthCheck()); + } + + @Test + public void custom() { + LineLengthCheck check = new LineLengthCheck(); + check.maximumLineLength = 119; + RustCheckVerifier.verify(new File("src/test/resources/checks/lineLength119.rs"), check); + } } diff --git a/rust-checks/src/test/resources/checks/lineLength.rs b/rust-checks/src/test/resources/checks/lineLength.rs index e69de29b..90404dd3 100644 --- a/rust-checks/src/test/resources/checks/lineLength.rs +++ b/rust-checks/src/test/resources/checks/lineLength.rs @@ -0,0 +1,3 @@ +let iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii=1; +/* Noncompliant@+1 {{Split this 121 characters long line (which is greater than 120 authorized).}} */ +let iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii=1; diff --git a/rust-checks/src/test/resources/checks/lineLength119.rs b/rust-checks/src/test/resources/checks/lineLength119.rs index e69de29b..71230fb9 100644 --- a/rust-checks/src/test/resources/checks/lineLength119.rs +++ b/rust-checks/src/test/resources/checks/lineLength119.rs @@ -0,0 +1,6 @@ + +/* Noncompliant@+1 {{Split this 120 characters long line (which is greater than 119 authorized).}} */ +let iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii=1; +/* Noncompliant@+1 {{Split this 121 characters long line (which is greater than 119 authorized).}} */ +let iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii=1; + From f310fad969e66b0b34c713a610b2ae1dbc906cd2 Mon Sep 17 00:00:00 2001 From: Eric Le Goff Date: Mon, 26 Jul 2021 11:52:10 +0200 Subject: [PATCH 05/14] Add compliant example for Function Paramaters Count rule --- .../java/org/elegoff/rust/checks/CheckList.java | 3 ++- .../checks/FunctionParametersCountCheck.java | 2 ++ .../I10n/rust/rules/FunctionParametersCount.html | 16 ++++++++++++++++ .../I10n/rust/rules/built-in-profile.json | 3 ++- .../org/elegoff/rust/checks/CheckListTest.java | 2 +- .../rust/language/RustQualityProfileTest.java | 2 +- 6 files changed, 24 insertions(+), 4 deletions(-) diff --git a/rust-checks/src/main/java/org/elegoff/rust/checks/CheckList.java b/rust-checks/src/main/java/org/elegoff/rust/checks/CheckList.java index 0d973f77..b6a3b615 100644 --- a/rust-checks/src/main/java/org/elegoff/rust/checks/CheckList.java +++ b/rust-checks/src/main/java/org/elegoff/rust/checks/CheckList.java @@ -30,7 +30,8 @@ private CheckList() { public static List> getRustChecks() { return Arrays.asList( - LineLengthCheck.class + LineLengthCheck.class, + FunctionParametersCountCheck.class ); } } diff --git a/rust-checks/src/main/java/org/elegoff/rust/checks/FunctionParametersCountCheck.java b/rust-checks/src/main/java/org/elegoff/rust/checks/FunctionParametersCountCheck.java index 039d7827..16439ba3 100644 --- a/rust-checks/src/main/java/org/elegoff/rust/checks/FunctionParametersCountCheck.java +++ b/rust-checks/src/main/java/org/elegoff/rust/checks/FunctionParametersCountCheck.java @@ -2,12 +2,14 @@ import com.sonar.sslr.api.AstNode; import com.sonar.sslr.api.AstNodeType; +import org.sonar.check.Rule; import org.sonar.check.RuleProperty; import org.sonar.rust.RustGrammar; import java.util.Collections; import java.util.Set; +@Rule(key = "FunctionParametersCount") public class FunctionParametersCountCheck extends RustCheck { private static final int DEFAULT_MAXIMUM_PARAMETER_COUNT = 8; diff --git a/rust-checks/src/main/resources/org/elegoff/I10n/rust/rules/FunctionParametersCount.html b/rust-checks/src/main/resources/org/elegoff/I10n/rust/rules/FunctionParametersCount.html index e69de29b..690cfbd3 100644 --- a/rust-checks/src/main/resources/org/elegoff/I10n/rust/rules/FunctionParametersCount.html +++ b/rust-checks/src/main/resources/org/elegoff/I10n/rust/rules/FunctionParametersCount.html @@ -0,0 +1,16 @@ +

Functions with excessive parameters are difficult to use, as one needs to figure out what each parameter is.

+

In many cases, the procedure can either be split into several smaller ones, or a better data structure can be found.

+

This rule verifies that each function has at most the given number of parameters.

+

Noncompliant Code Example

+
+fn nine_params_sum(p1 : i32, p2 : i32,p3 : i32,p4 : i32,p5 : i32,p6 : i32,p7 : i32,p8 : i32,p9 : i32) -> i32 {/* Non-Compliant - too many parameters */
+    return p1 + p2 + p3 + p4 +p5 + p6 + p7 +  p8  +p9;
+}
+
+

Compliant Solution

+
+fn single_params_sum(a : &[i32] )-> i32{
+    a.iter().sum()
+}
+
+ diff --git a/rust-checks/src/main/resources/org/elegoff/I10n/rust/rules/built-in-profile.json b/rust-checks/src/main/resources/org/elegoff/I10n/rust/rules/built-in-profile.json index b4c3571e..5b0288c6 100644 --- a/rust-checks/src/main/resources/org/elegoff/I10n/rust/rules/built-in-profile.json +++ b/rust-checks/src/main/resources/org/elegoff/I10n/rust/rules/built-in-profile.json @@ -1,6 +1,7 @@ { "name": "Community Rust", "ruleKeys": [ - + "LineLength", + "FunctionParametersCount" ] } \ No newline at end of file diff --git a/rust-checks/src/test/java/org/elegoff/rust/checks/CheckListTest.java b/rust-checks/src/test/java/org/elegoff/rust/checks/CheckListTest.java index 5de77bd6..db243f29 100644 --- a/rust-checks/src/test/java/org/elegoff/rust/checks/CheckListTest.java +++ b/rust-checks/src/test/java/org/elegoff/rust/checks/CheckListTest.java @@ -27,6 +27,6 @@ public class CheckListTest { @Test public void testSize(){ - assertThat(CheckList.getRustChecks().size()).isEqualTo(1); + assertThat(CheckList.getRustChecks().size()).isEqualTo(2); } } diff --git a/sonar-rust-plugin/src/test/java/org/elegoff/plugins/rust/language/RustQualityProfileTest.java b/sonar-rust-plugin/src/test/java/org/elegoff/plugins/rust/language/RustQualityProfileTest.java index 851aa524..e914ca82 100644 --- a/sonar-rust-plugin/src/test/java/org/elegoff/plugins/rust/language/RustQualityProfileTest.java +++ b/sonar-rust-plugin/src/test/java/org/elegoff/plugins/rust/language/RustQualityProfileTest.java @@ -32,7 +32,7 @@ public void testDefine() { BuiltInQualityProfilesDefinition.BuiltInQualityProfile profile = context.profile("rust", "Community Rust"); assertNotNull(profile); assertTrue(profile.isDefault()); - assertEquals(0, profile.rules().size()); + assertEquals(2, profile.rules().size()); } From a08f338bd8c8681bdb23f8e6f6f5a0550a79a44a Mon Sep 17 00:00:00 2001 From: Eric Le Goff Date: Mon, 26 Jul 2021 11:58:35 +0200 Subject: [PATCH 06/14] rules are note template --- .../org/elegoff/plugins/rust/rules/RustRulesDefinition.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sonar-rust-plugin/src/main/java/org/elegoff/plugins/rust/rules/RustRulesDefinition.java b/sonar-rust-plugin/src/main/java/org/elegoff/plugins/rust/rules/RustRulesDefinition.java index 1ba43c86..3d9eec1e 100644 --- a/sonar-rust-plugin/src/main/java/org/elegoff/plugins/rust/rules/RustRulesDefinition.java +++ b/sonar-rust-plugin/src/main/java/org/elegoff/plugins/rust/rules/RustRulesDefinition.java @@ -43,7 +43,7 @@ public void define(Context context) { RuleMetadataLoader ruleMetadataLoader = new RuleMetadataLoader(RULES_DEFINITION_FOLDER, RustQualityProfile.PROFILE_PATH); ruleMetadataLoader.addRulesByAnnotatedClass(repository, checkClasses); repository.rules().stream() - .forEach(r -> r.setTemplate(true)); + .forEach(r -> r.setTemplate(false)); //Current repository is empty repository.done(); From 65b4a8853893ea6ab0278f57198f7510ea8cce9c Mon Sep 17 00:00:00 2001 From: Eric Le Goff Date: Mon, 26 Jul 2021 12:08:26 +0200 Subject: [PATCH 07/14] non template rules --- .../org/elegoff/plugins/rust/rules/RustRulesDefinition.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sonar-rust-plugin/src/main/java/org/elegoff/plugins/rust/rules/RustRulesDefinition.java b/sonar-rust-plugin/src/main/java/org/elegoff/plugins/rust/rules/RustRulesDefinition.java index 3d9eec1e..7c77a18e 100644 --- a/sonar-rust-plugin/src/main/java/org/elegoff/plugins/rust/rules/RustRulesDefinition.java +++ b/sonar-rust-plugin/src/main/java/org/elegoff/plugins/rust/rules/RustRulesDefinition.java @@ -37,7 +37,7 @@ public class RustRulesDefinition implements RulesDefinition { @Override public void define(Context context) { - NewRepository repository = context.createRepository("rust", RustLanguage.KEY).setName("RUST Analyzer"); + NewRepository repository = context.createRepository("community-rust", RustLanguage.KEY).setName("RUST Analyzer"); List> checkClasses = CheckList.getRustChecks(); RuleMetadataLoader ruleMetadataLoader = new RuleMetadataLoader(RULES_DEFINITION_FOLDER, RustQualityProfile.PROFILE_PATH); From 23e8fd0bc6b2d1b784c22fa64b02a509ba011ed5 Mon Sep 17 00:00:00 2001 From: Eric Le Goff Date: Mon, 26 Jul 2021 12:10:57 +0200 Subject: [PATCH 08/14] dont use Immutable --- .../src/main/java/org/elegoff/rust/checks/RustCheck.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rust-checks/src/main/java/org/elegoff/rust/checks/RustCheck.java b/rust-checks/src/main/java/org/elegoff/rust/checks/RustCheck.java index b9ee6b66..15b3dc3c 100644 --- a/rust-checks/src/main/java/org/elegoff/rust/checks/RustCheck.java +++ b/rust-checks/src/main/java/org/elegoff/rust/checks/RustCheck.java @@ -21,11 +21,11 @@ import com.sonar.sslr.api.AstNode; import com.sonar.sslr.api.Token; -import org.sonar.api.internal.google.common.collect.ImmutableList; import org.sonar.rust.RustVisitor; import org.sonar.rust.RustVisitorContext; import java.util.ArrayList; +import java.util.Collections; import java.util.List; public class RustCheck extends RustVisitor { @@ -34,7 +34,7 @@ public class RustCheck extends RustVisitor { public List scanFileForIssues(RustVisitorContext context) { issues.clear(); scanFile(context); - return ImmutableList.copyOf(issues); + return Collections.unmodifiableList(new ArrayList<>(issues)); } public void addIssue(String message, AstNode node) { From fca715bc6246617155f952d2366088e0185b2792 Mon Sep 17 00:00:00 2001 From: Eric Le Goff Date: Mon, 26 Jul 2021 13:43:33 +0200 Subject: [PATCH 09/14] Rename rule repository --- .../src/main/java/org/elegoff/rust/checks/CheckList.java | 2 +- .../org/elegoff/plugins/rust/rules/RustRulesDefinition.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/rust-checks/src/main/java/org/elegoff/rust/checks/CheckList.java b/rust-checks/src/main/java/org/elegoff/rust/checks/CheckList.java index b6a3b615..d7a85c69 100644 --- a/rust-checks/src/main/java/org/elegoff/rust/checks/CheckList.java +++ b/rust-checks/src/main/java/org/elegoff/rust/checks/CheckList.java @@ -23,7 +23,7 @@ import java.util.*; public class CheckList { - public static final String REPOSITORY_KEY = "rust"; + public static final String REPOSITORY_KEY = "community-rust"; private CheckList() { } diff --git a/sonar-rust-plugin/src/main/java/org/elegoff/plugins/rust/rules/RustRulesDefinition.java b/sonar-rust-plugin/src/main/java/org/elegoff/plugins/rust/rules/RustRulesDefinition.java index 7c77a18e..f82b764d 100644 --- a/sonar-rust-plugin/src/main/java/org/elegoff/plugins/rust/rules/RustRulesDefinition.java +++ b/sonar-rust-plugin/src/main/java/org/elegoff/plugins/rust/rules/RustRulesDefinition.java @@ -37,7 +37,7 @@ public class RustRulesDefinition implements RulesDefinition { @Override public void define(Context context) { - NewRepository repository = context.createRepository("community-rust", RustLanguage.KEY).setName("RUST Analyzer"); + NewRepository repository = context.createRepository(CheckList.REPOSITORY_KEY, RustLanguage.KEY).setName("RUST Analyzer"); List> checkClasses = CheckList.getRustChecks(); RuleMetadataLoader ruleMetadataLoader = new RuleMetadataLoader(RULES_DEFINITION_FOLDER, RustQualityProfile.PROFILE_PATH); From 3315be14c70b785fc520c05cea5745eca2ad1be6 Mon Sep 17 00:00:00 2001 From: Eric Le Goff Date: Tue, 27 Jul 2021 20:38:35 +0200 Subject: [PATCH 10/14] Add rule for empty enums --- .../org/elegoff/rust/checks/CheckList.java | 3 +- .../elegoff/rust/checks/EmptyEnumCheck.java | 41 +++++++++++++++++++ .../elegoff/I10n/rust/rules/EmptyEnum.html | 11 +++++ .../elegoff/I10n/rust/rules/EmptyEnum.json | 15 +++++++ .../I10n/rust/rules/built-in-profile.json | 3 +- .../elegoff/rust/checks/CheckListTest.java | 2 +- .../rust/checks/EmptyEnumCheckTest.java | 13 ++++++ .../FunctionParametersCountCheckTest.java | 4 +- .../rust/checks/LineLengthCheckTest.java | 4 +- .../src/test/resources/checks/empty_enum.rs | 14 +++++++ ...etersCount.rs => function_params_count.rs} | 0 .../checks/{lineLength.rs => line_length.rs} | 0 .../{lineLength119.rs => line_length_119.rs} | 0 .../java/org/sonar/rust/RustLexerTest.java | 2 +- .../rust/language/RustQualityProfileTest.java | 2 +- 15 files changed, 105 insertions(+), 9 deletions(-) create mode 100644 rust-checks/src/main/java/org/elegoff/rust/checks/EmptyEnumCheck.java create mode 100644 rust-checks/src/main/resources/org/elegoff/I10n/rust/rules/EmptyEnum.html create mode 100644 rust-checks/src/main/resources/org/elegoff/I10n/rust/rules/EmptyEnum.json create mode 100644 rust-checks/src/test/java/org/elegoff/rust/checks/EmptyEnumCheckTest.java create mode 100644 rust-checks/src/test/resources/checks/empty_enum.rs rename rust-checks/src/test/resources/checks/{FunctionParametersCount.rs => function_params_count.rs} (100%) rename rust-checks/src/test/resources/checks/{lineLength.rs => line_length.rs} (100%) rename rust-checks/src/test/resources/checks/{lineLength119.rs => line_length_119.rs} (100%) diff --git a/rust-checks/src/main/java/org/elegoff/rust/checks/CheckList.java b/rust-checks/src/main/java/org/elegoff/rust/checks/CheckList.java index d7a85c69..6d8c5b77 100644 --- a/rust-checks/src/main/java/org/elegoff/rust/checks/CheckList.java +++ b/rust-checks/src/main/java/org/elegoff/rust/checks/CheckList.java @@ -31,7 +31,8 @@ private CheckList() { public static List> getRustChecks() { return Arrays.asList( LineLengthCheck.class, - FunctionParametersCountCheck.class + FunctionParametersCountCheck.class, + EmptyEnumCheck.class ); } } diff --git a/rust-checks/src/main/java/org/elegoff/rust/checks/EmptyEnumCheck.java b/rust-checks/src/main/java/org/elegoff/rust/checks/EmptyEnumCheck.java new file mode 100644 index 00000000..c347283c --- /dev/null +++ b/rust-checks/src/main/java/org/elegoff/rust/checks/EmptyEnumCheck.java @@ -0,0 +1,41 @@ +package org.elegoff.rust.checks; + +import com.sonar.sslr.api.AstNode; +import com.sonar.sslr.api.AstNodeType; +import org.sonar.check.Rule; +import org.sonar.rust.RustGrammar; +import org.sonar.rust.api.RustKeyword; +import org.sonar.rust.api.RustPunctuator; +import org.sonar.sslr.internal.matchers.AstCreator; + +import java.util.Collections; +import java.util.List; +import java.util.Set; + +@Rule(key = "EmptyEnum") +public class EmptyEnumCheck extends RustCheck { + @Override + public Set subscribedKinds() { + return Collections.singleton(RustGrammar.ENUMERATION); + } + + @Override + public void visitNode(AstNode node) { + List test = node.getChildren(); + + AstNode enumItems = node.getFirstChild(RustGrammar.ENUM_ITEMS); + + if (enumItems == null) { + raiseIssue(node); + } + + + } + + private void raiseIssue(AstNode node) { + addIssue("Either remove or fill this empty enumeration.", node); + } + +} + + diff --git a/rust-checks/src/main/resources/org/elegoff/I10n/rust/rules/EmptyEnum.html b/rust-checks/src/main/resources/org/elegoff/I10n/rust/rules/EmptyEnum.html new file mode 100644 index 00000000..2c93bfa3 --- /dev/null +++ b/rust-checks/src/main/resources/org/elegoff/I10n/rust/rules/EmptyEnum.html @@ -0,0 +1,11 @@ +

Empty enums should be avoided. Use a never_type instead.

+

Noncompliant Code Example

+
+enum Foo {}
+
+

Compliant Code Example

+
+#![feature(never_type)]
+
+struct Foo(!);
+
diff --git a/rust-checks/src/main/resources/org/elegoff/I10n/rust/rules/EmptyEnum.json b/rust-checks/src/main/resources/org/elegoff/I10n/rust/rules/EmptyEnum.json new file mode 100644 index 00000000..f829fdd7 --- /dev/null +++ b/rust-checks/src/main/resources/org/elegoff/I10n/rust/rules/EmptyEnum.json @@ -0,0 +1,15 @@ + +{ + "title": "Enum should not be left empty", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "5min" + }, + "tags": [ + "suspicious" + ], + "defaultSeverity": "Major", + "scope": "All" +} \ No newline at end of file diff --git a/rust-checks/src/main/resources/org/elegoff/I10n/rust/rules/built-in-profile.json b/rust-checks/src/main/resources/org/elegoff/I10n/rust/rules/built-in-profile.json index 5b0288c6..5d84fb13 100644 --- a/rust-checks/src/main/resources/org/elegoff/I10n/rust/rules/built-in-profile.json +++ b/rust-checks/src/main/resources/org/elegoff/I10n/rust/rules/built-in-profile.json @@ -2,6 +2,7 @@ "name": "Community Rust", "ruleKeys": [ "LineLength", - "FunctionParametersCount" + "FunctionParametersCount", + "EmptyEnum" ] } \ No newline at end of file diff --git a/rust-checks/src/test/java/org/elegoff/rust/checks/CheckListTest.java b/rust-checks/src/test/java/org/elegoff/rust/checks/CheckListTest.java index db243f29..1df0f9de 100644 --- a/rust-checks/src/test/java/org/elegoff/rust/checks/CheckListTest.java +++ b/rust-checks/src/test/java/org/elegoff/rust/checks/CheckListTest.java @@ -27,6 +27,6 @@ public class CheckListTest { @Test public void testSize(){ - assertThat(CheckList.getRustChecks().size()).isEqualTo(2); + assertThat(CheckList.getRustChecks().size()).isEqualTo(3); } } diff --git a/rust-checks/src/test/java/org/elegoff/rust/checks/EmptyEnumCheckTest.java b/rust-checks/src/test/java/org/elegoff/rust/checks/EmptyEnumCheckTest.java new file mode 100644 index 00000000..59d9a185 --- /dev/null +++ b/rust-checks/src/test/java/org/elegoff/rust/checks/EmptyEnumCheckTest.java @@ -0,0 +1,13 @@ +package org.elegoff.rust.checks; + +import org.junit.Test; + +import java.io.File; + +public class EmptyEnumCheckTest { + + @Test + public void test() { + RustCheckVerifier.verify(new File("src/test/resources/checks/empty_enum.rs"), new EmptyEnumCheck()); + } +} diff --git a/rust-checks/src/test/java/org/elegoff/rust/checks/FunctionParametersCountCheckTest.java b/rust-checks/src/test/java/org/elegoff/rust/checks/FunctionParametersCountCheckTest.java index 6f68920d..b2156b93 100644 --- a/rust-checks/src/test/java/org/elegoff/rust/checks/FunctionParametersCountCheckTest.java +++ b/rust-checks/src/test/java/org/elegoff/rust/checks/FunctionParametersCountCheckTest.java @@ -7,7 +7,7 @@ public class FunctionParametersCountCheckTest { @Test public void test() { - RustCheckVerifier.verify(new File("src/test/resources/checks/FunctionParametersCount.rs"), new FunctionParametersCountCheck()); + RustCheckVerifier.verify(new File("src/test/resources/checks/function_params_count.rs"), new FunctionParametersCountCheck()); } @Test @@ -15,6 +15,6 @@ public void custom() { FunctionParametersCountCheck check = new FunctionParametersCountCheck(); check.maximumParameterCount = 10; - RustCheckVerifier.verifyNoIssueIgnoringExpected(new File("src/test/resources/checks/FunctionParametersCount.rs"), check); + RustCheckVerifier.verifyNoIssueIgnoringExpected(new File("src/test/resources/checks/function_params_count.rs"), check); } } diff --git a/rust-checks/src/test/java/org/elegoff/rust/checks/LineLengthCheckTest.java b/rust-checks/src/test/java/org/elegoff/rust/checks/LineLengthCheckTest.java index a7fb610b..966dbdfa 100644 --- a/rust-checks/src/test/java/org/elegoff/rust/checks/LineLengthCheckTest.java +++ b/rust-checks/src/test/java/org/elegoff/rust/checks/LineLengthCheckTest.java @@ -7,13 +7,13 @@ public class LineLengthCheckTest { @Test public void test() { - RustCheckVerifier.verify(new File("src/test/resources/checks/lineLength.rs"), new LineLengthCheck()); + RustCheckVerifier.verify(new File("src/test/resources/checks/line_length.rs"), new LineLengthCheck()); } @Test public void custom() { LineLengthCheck check = new LineLengthCheck(); check.maximumLineLength = 119; - RustCheckVerifier.verify(new File("src/test/resources/checks/lineLength119.rs"), check); + RustCheckVerifier.verify(new File("src/test/resources/checks/line_length_119.rs"), check); } } diff --git a/rust-checks/src/test/resources/checks/empty_enum.rs b/rust-checks/src/test/resources/checks/empty_enum.rs new file mode 100644 index 00000000..4f91a744 --- /dev/null +++ b/rust-checks/src/test/resources/checks/empty_enum.rs @@ -0,0 +1,14 @@ + +enum WebEvent { + // An `enum` may either be `unit-like`, + PageLoad, + PageUnload, + // like tuple structs, + KeyPress(char), + Paste(String), + // or c-like structures. + Click { x: i64, y: i64 }, +} + +/* Noncompliant@+1 {{Either remove or fill this empty enumeration.}}*/ +enum My_Empty_num {} \ No newline at end of file diff --git a/rust-checks/src/test/resources/checks/FunctionParametersCount.rs b/rust-checks/src/test/resources/checks/function_params_count.rs similarity index 100% rename from rust-checks/src/test/resources/checks/FunctionParametersCount.rs rename to rust-checks/src/test/resources/checks/function_params_count.rs diff --git a/rust-checks/src/test/resources/checks/lineLength.rs b/rust-checks/src/test/resources/checks/line_length.rs similarity index 100% rename from rust-checks/src/test/resources/checks/lineLength.rs rename to rust-checks/src/test/resources/checks/line_length.rs diff --git a/rust-checks/src/test/resources/checks/lineLength119.rs b/rust-checks/src/test/resources/checks/line_length_119.rs similarity index 100% rename from rust-checks/src/test/resources/checks/lineLength119.rs rename to rust-checks/src/test/resources/checks/line_length_119.rs diff --git a/rust-frontend/src/test/java/org/sonar/rust/RustLexerTest.java b/rust-frontend/src/test/java/org/sonar/rust/RustLexerTest.java index 5fd88432..1b5d8aaf 100644 --- a/rust-frontend/src/test/java/org/sonar/rust/RustLexerTest.java +++ b/rust-frontend/src/test/java/org/sonar/rust/RustLexerTest.java @@ -69,7 +69,7 @@ public void testTokens() { public void testParsing() { - String sexpr = "if sif_ok {}"; + String sexpr = "enum Test {}"; diff --git a/sonar-rust-plugin/src/test/java/org/elegoff/plugins/rust/language/RustQualityProfileTest.java b/sonar-rust-plugin/src/test/java/org/elegoff/plugins/rust/language/RustQualityProfileTest.java index e914ca82..700010e8 100644 --- a/sonar-rust-plugin/src/test/java/org/elegoff/plugins/rust/language/RustQualityProfileTest.java +++ b/sonar-rust-plugin/src/test/java/org/elegoff/plugins/rust/language/RustQualityProfileTest.java @@ -32,7 +32,7 @@ public void testDefine() { BuiltInQualityProfilesDefinition.BuiltInQualityProfile profile = context.profile("rust", "Community Rust"); assertNotNull(profile); assertTrue(profile.isDefault()); - assertEquals(2, profile.rules().size()); + assertEquals(3, profile.rules().size()); } From ee327b428d76483b690157b064ad6ce217939094 Mon Sep 17 00:00:00 2001 From: Eric Le Goff Date: Tue, 27 Jul 2021 20:56:57 +0200 Subject: [PATCH 11/14] fix missing sqkey --- .../main/resources/org/elegoff/I10n/rust/rules/EmptyEnum.json | 1 + 1 file changed, 1 insertion(+) diff --git a/rust-checks/src/main/resources/org/elegoff/I10n/rust/rules/EmptyEnum.json b/rust-checks/src/main/resources/org/elegoff/I10n/rust/rules/EmptyEnum.json index f829fdd7..5e7c1e9e 100644 --- a/rust-checks/src/main/resources/org/elegoff/I10n/rust/rules/EmptyEnum.json +++ b/rust-checks/src/main/resources/org/elegoff/I10n/rust/rules/EmptyEnum.json @@ -11,5 +11,6 @@ "suspicious" ], "defaultSeverity": "Major", + "sqKey": "EmptyEnum", "scope": "All" } \ No newline at end of file From ef5527de80ab9b2954b156472b785d2d0e8f37d4 Mon Sep 17 00:00:00 2001 From: Eric Le Goff Date: Wed, 28 Jul 2021 16:35:05 +0200 Subject: [PATCH 12/14] Add token issue with line --- .../main/java/org/elegoff/rust/checks/RustCheck.java | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/rust-checks/src/main/java/org/elegoff/rust/checks/RustCheck.java b/rust-checks/src/main/java/org/elegoff/rust/checks/RustCheck.java index 15b3dc3c..490a32c6 100644 --- a/rust-checks/src/main/java/org/elegoff/rust/checks/RustCheck.java +++ b/rust-checks/src/main/java/org/elegoff/rust/checks/RustCheck.java @@ -2,17 +2,17 @@ * Sonar Rust Plugin (Community) * Copyright (C) 2021 Eric Le Goff * http://github.com/elegoff/sonar-rust - * + *

* This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 3 of the License, or (at your option) any later version. - * + *

* This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. - * + *

* You should have received a copy of the GNU Lesser General Public License * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. @@ -42,9 +42,7 @@ public void addIssue(String message, AstNode node) { } public void addIssue(String message, Token token) { - if (token.getURI().equals(getContext().file().uri())) { - addLineIssue(message, token.getLine()); - } + addLineIssue(message, token.getLine()); } public void addLineIssue(String message, int line) { From 4bf6e9f9cca1fdf620d53846aa14585088743d90 Mon Sep 17 00:00:00 2001 From: Eric Le Goff Date: Thu, 29 Jul 2021 11:43:27 +0200 Subject: [PATCH 13/14] fix some code smells --- .../org/elegoff/rust/checks/CheckList.java | 7 +-- .../elegoff/rust/checks/EmptyEnumCheck.java | 26 +++++--- .../checks/FunctionParametersCountCheck.java | 19 ++++++ .../elegoff/rust/checks/LineLengthCheck.java | 19 ++++++ .../org/elegoff/rust/checks/RustCheck.java | 6 +- .../rust/checks/EmptyEnumCheckTest.java | 19 ++++++ .../FunctionParametersCountCheckTest.java | 19 ++++++ .../rust/checks/LineLengthCheckTest.java | 19 ++++++ .../rust/checks/RustCheckVerifier.java | 19 ++++++ .../main/java/org/sonar/rust/RustGrammar.java | 6 +- .../parser/expressions/ExpressionTest.java | 6 +- .../org/sonar/rust/parser/lexer/PathTest.java | 6 +- .../rust/clippy/ClippyJsonReportReader.java | 6 +- .../rust/rules/RustRulesDefinition.java | 8 +-- .../rust/rules/RustRulesDefinitionTest.java | 62 +++++++++++++++++++ 15 files changed, 215 insertions(+), 32 deletions(-) create mode 100644 sonar-rust-plugin/src/test/java/org/elegoff/plugins/rust/rules/RustRulesDefinitionTest.java diff --git a/rust-checks/src/main/java/org/elegoff/rust/checks/CheckList.java b/rust-checks/src/main/java/org/elegoff/rust/checks/CheckList.java index 6d8c5b77..a69c67ef 100644 --- a/rust-checks/src/main/java/org/elegoff/rust/checks/CheckList.java +++ b/rust-checks/src/main/java/org/elegoff/rust/checks/CheckList.java @@ -2,22 +2,21 @@ * Sonar Rust Plugin (Community) * Copyright (C) 2021 Eric Le Goff * http://github.com/elegoff/sonar-rust - *

+ * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 3 of the License, or (at your option) any later version. - *

+ * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. - *

+ * * You should have received a copy of the GNU Lesser General Public License * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ - package org.elegoff.rust.checks; import java.util.*; diff --git a/rust-checks/src/main/java/org/elegoff/rust/checks/EmptyEnumCheck.java b/rust-checks/src/main/java/org/elegoff/rust/checks/EmptyEnumCheck.java index c347283c..7a9affd0 100644 --- a/rust-checks/src/main/java/org/elegoff/rust/checks/EmptyEnumCheck.java +++ b/rust-checks/src/main/java/org/elegoff/rust/checks/EmptyEnumCheck.java @@ -1,12 +1,28 @@ +/** + * Sonar Rust Plugin (Community) + * Copyright (C) 2021 Eric Le Goff + * http://github.com/elegoff/sonar-rust + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ package org.elegoff.rust.checks; import com.sonar.sslr.api.AstNode; import com.sonar.sslr.api.AstNodeType; import org.sonar.check.Rule; import org.sonar.rust.RustGrammar; -import org.sonar.rust.api.RustKeyword; -import org.sonar.rust.api.RustPunctuator; -import org.sonar.sslr.internal.matchers.AstCreator; import java.util.Collections; import java.util.List; @@ -21,15 +37,11 @@ public Set subscribedKinds() { @Override public void visitNode(AstNode node) { - List test = node.getChildren(); - AstNode enumItems = node.getFirstChild(RustGrammar.ENUM_ITEMS); if (enumItems == null) { raiseIssue(node); } - - } private void raiseIssue(AstNode node) { diff --git a/rust-checks/src/main/java/org/elegoff/rust/checks/FunctionParametersCountCheck.java b/rust-checks/src/main/java/org/elegoff/rust/checks/FunctionParametersCountCheck.java index 16439ba3..2e8f6158 100644 --- a/rust-checks/src/main/java/org/elegoff/rust/checks/FunctionParametersCountCheck.java +++ b/rust-checks/src/main/java/org/elegoff/rust/checks/FunctionParametersCountCheck.java @@ -1,3 +1,22 @@ +/** + * Sonar Rust Plugin (Community) + * Copyright (C) 2021 Eric Le Goff + * http://github.com/elegoff/sonar-rust + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ package org.elegoff.rust.checks; import com.sonar.sslr.api.AstNode; diff --git a/rust-checks/src/main/java/org/elegoff/rust/checks/LineLengthCheck.java b/rust-checks/src/main/java/org/elegoff/rust/checks/LineLengthCheck.java index 7d97999d..85496c2d 100644 --- a/rust-checks/src/main/java/org/elegoff/rust/checks/LineLengthCheck.java +++ b/rust-checks/src/main/java/org/elegoff/rust/checks/LineLengthCheck.java @@ -1,3 +1,22 @@ +/** + * Sonar Rust Plugin (Community) + * Copyright (C) 2021 Eric Le Goff + * http://github.com/elegoff/sonar-rust + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ package org.elegoff.rust.checks; import com.sonar.sslr.api.AstNode; diff --git a/rust-checks/src/main/java/org/elegoff/rust/checks/RustCheck.java b/rust-checks/src/main/java/org/elegoff/rust/checks/RustCheck.java index 490a32c6..ee4d1c20 100644 --- a/rust-checks/src/main/java/org/elegoff/rust/checks/RustCheck.java +++ b/rust-checks/src/main/java/org/elegoff/rust/checks/RustCheck.java @@ -2,17 +2,17 @@ * Sonar Rust Plugin (Community) * Copyright (C) 2021 Eric Le Goff * http://github.com/elegoff/sonar-rust - *

+ * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 3 of the License, or (at your option) any later version. - *

+ * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. - *

+ * * You should have received a copy of the GNU Lesser General Public License * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. diff --git a/rust-checks/src/test/java/org/elegoff/rust/checks/EmptyEnumCheckTest.java b/rust-checks/src/test/java/org/elegoff/rust/checks/EmptyEnumCheckTest.java index 59d9a185..9331178b 100644 --- a/rust-checks/src/test/java/org/elegoff/rust/checks/EmptyEnumCheckTest.java +++ b/rust-checks/src/test/java/org/elegoff/rust/checks/EmptyEnumCheckTest.java @@ -1,3 +1,22 @@ +/** + * Sonar Rust Plugin (Community) + * Copyright (C) 2021 Eric Le Goff + * http://github.com/elegoff/sonar-rust + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ package org.elegoff.rust.checks; import org.junit.Test; diff --git a/rust-checks/src/test/java/org/elegoff/rust/checks/FunctionParametersCountCheckTest.java b/rust-checks/src/test/java/org/elegoff/rust/checks/FunctionParametersCountCheckTest.java index b2156b93..ac7f3d37 100644 --- a/rust-checks/src/test/java/org/elegoff/rust/checks/FunctionParametersCountCheckTest.java +++ b/rust-checks/src/test/java/org/elegoff/rust/checks/FunctionParametersCountCheckTest.java @@ -1,3 +1,22 @@ +/** + * Sonar Rust Plugin (Community) + * Copyright (C) 2021 Eric Le Goff + * http://github.com/elegoff/sonar-rust + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ package org.elegoff.rust.checks; import org.junit.Test; diff --git a/rust-checks/src/test/java/org/elegoff/rust/checks/LineLengthCheckTest.java b/rust-checks/src/test/java/org/elegoff/rust/checks/LineLengthCheckTest.java index 966dbdfa..7fb354ab 100644 --- a/rust-checks/src/test/java/org/elegoff/rust/checks/LineLengthCheckTest.java +++ b/rust-checks/src/test/java/org/elegoff/rust/checks/LineLengthCheckTest.java @@ -1,3 +1,22 @@ +/** + * Sonar Rust Plugin (Community) + * Copyright (C) 2021 Eric Le Goff + * http://github.com/elegoff/sonar-rust + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ package org.elegoff.rust.checks; import org.junit.Test; diff --git a/rust-checks/src/test/java/org/elegoff/rust/checks/RustCheckVerifier.java b/rust-checks/src/test/java/org/elegoff/rust/checks/RustCheckVerifier.java index 615fbb21..894d9ec3 100644 --- a/rust-checks/src/test/java/org/elegoff/rust/checks/RustCheckVerifier.java +++ b/rust-checks/src/test/java/org/elegoff/rust/checks/RustCheckVerifier.java @@ -1,3 +1,22 @@ +/** + * Sonar Rust Plugin (Community) + * Copyright (C) 2021 Eric Le Goff + * http://github.com/elegoff/sonar-rust + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ package org.elegoff.rust.checks; import java.io.File; diff --git a/rust-frontend/src/main/java/org/sonar/rust/RustGrammar.java b/rust-frontend/src/main/java/org/sonar/rust/RustGrammar.java index 969e6fc3..c205efd0 100644 --- a/rust-frontend/src/main/java/org/sonar/rust/RustGrammar.java +++ b/rust-frontend/src/main/java/org/sonar/rust/RustGrammar.java @@ -2,17 +2,17 @@ * Sonar Rust Plugin (Community) * Copyright (C) 2021 Eric Le Goff * http://github.com/elegoff/sonar-rust - *

+ * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 3 of the License, or (at your option) any later version. - *

+ * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. - *

+ * * You should have received a copy of the GNU Lesser General Public License * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. diff --git a/rust-frontend/src/test/java/org/sonar/rust/parser/expressions/ExpressionTest.java b/rust-frontend/src/test/java/org/sonar/rust/parser/expressions/ExpressionTest.java index 91075a76..22a48145 100644 --- a/rust-frontend/src/test/java/org/sonar/rust/parser/expressions/ExpressionTest.java +++ b/rust-frontend/src/test/java/org/sonar/rust/parser/expressions/ExpressionTest.java @@ -2,17 +2,17 @@ * Sonar Rust Plugin (Community) * Copyright (C) 2021 Eric Le Goff * http://github.com/elegoff/sonar-rust - *

+ * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 3 of the License, or (at your option) any later version. - *

+ * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. - *

+ * * You should have received a copy of the GNU Lesser General Public License * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. diff --git a/rust-frontend/src/test/java/org/sonar/rust/parser/lexer/PathTest.java b/rust-frontend/src/test/java/org/sonar/rust/parser/lexer/PathTest.java index fbf23301..c4c71456 100644 --- a/rust-frontend/src/test/java/org/sonar/rust/parser/lexer/PathTest.java +++ b/rust-frontend/src/test/java/org/sonar/rust/parser/lexer/PathTest.java @@ -2,17 +2,17 @@ * Sonar Rust Plugin (Community) * Copyright (C) 2021 Eric Le Goff * http://github.com/elegoff/sonar-rust - *

+ * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 3 of the License, or (at your option) any later version. - *

+ * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. - *

+ * * You should have received a copy of the GNU Lesser General Public License * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. diff --git a/sonar-rust-plugin/src/main/java/org/elegoff/plugins/rust/clippy/ClippyJsonReportReader.java b/sonar-rust-plugin/src/main/java/org/elegoff/plugins/rust/clippy/ClippyJsonReportReader.java index 5bddacf7..35ff41be 100644 --- a/sonar-rust-plugin/src/main/java/org/elegoff/plugins/rust/clippy/ClippyJsonReportReader.java +++ b/sonar-rust-plugin/src/main/java/org/elegoff/plugins/rust/clippy/ClippyJsonReportReader.java @@ -2,17 +2,17 @@ * Sonar Rust Plugin (Community) * Copyright (C) 2021 Eric Le Goff * http://github.com/elegoff/sonar-rust - *

+ * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 3 of the License, or (at your option) any later version. - *

+ * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. - *

+ * * You should have received a copy of the GNU Lesser General Public License * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. diff --git a/sonar-rust-plugin/src/main/java/org/elegoff/plugins/rust/rules/RustRulesDefinition.java b/sonar-rust-plugin/src/main/java/org/elegoff/plugins/rust/rules/RustRulesDefinition.java index f82b764d..4949b3ef 100644 --- a/sonar-rust-plugin/src/main/java/org/elegoff/plugins/rust/rules/RustRulesDefinition.java +++ b/sonar-rust-plugin/src/main/java/org/elegoff/plugins/rust/rules/RustRulesDefinition.java @@ -22,7 +22,6 @@ import org.elegoff.plugins.rust.language.RustLanguage; import org.elegoff.plugins.rust.language.RustQualityProfile; import org.elegoff.rust.checks.CheckList; -import org.elegoff.rust.checks.RustCheck; import org.sonar.api.server.rule.RulesDefinition; import org.sonarsource.analyzer.commons.RuleMetadataLoader; @@ -37,15 +36,12 @@ public class RustRulesDefinition implements RulesDefinition { @Override public void define(Context context) { - NewRepository repository = context.createRepository(CheckList.REPOSITORY_KEY, RustLanguage.KEY).setName("RUST Analyzer"); - - List> checkClasses = CheckList.getRustChecks(); + NewRepository repository = context.createRepository(CheckList.REPOSITORY_KEY, RustLanguage.KEY).setName("Community RUST"); + List> checkClasses = CheckList.getRustChecks(); RuleMetadataLoader ruleMetadataLoader = new RuleMetadataLoader(RULES_DEFINITION_FOLDER, RustQualityProfile.PROFILE_PATH); ruleMetadataLoader.addRulesByAnnotatedClass(repository, checkClasses); repository.rules().stream() .forEach(r -> r.setTemplate(false)); - - //Current repository is empty repository.done(); } } \ No newline at end of file diff --git a/sonar-rust-plugin/src/test/java/org/elegoff/plugins/rust/rules/RustRulesDefinitionTest.java b/sonar-rust-plugin/src/test/java/org/elegoff/plugins/rust/rules/RustRulesDefinitionTest.java new file mode 100644 index 00000000..76de04ea --- /dev/null +++ b/sonar-rust-plugin/src/test/java/org/elegoff/plugins/rust/rules/RustRulesDefinitionTest.java @@ -0,0 +1,62 @@ +/** + * Sonar Rust Plugin (Community) + * Copyright (C) 2021 Eric Le Goff + * http://github.com/elegoff/sonar-rust + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.elegoff.plugins.rust.rules; + +import org.elegoff.rust.checks.CheckList; +import org.junit.Test; +import org.sonar.api.server.rule.RulesDefinition; +import org.sonar.api.server.rule.RulesDefinition.Context; +import org.sonar.api.server.rule.RulesDefinition.Repository; + +import static org.fest.assertions.Assertions.assertThat; + + +public class RustRulesDefinitionTest { + + private Context context = new Context(); + + @Test + public void test() { + Repository repository = createRepository(); + + assertThat(repository.name()).isEqualTo("Community RUST"); + assertThat(repository.language()).isEqualTo("rust"); + assertThat(repository.rules()).hasSize(CheckList.getRustChecks().size()); + + RulesDefinition.Rule emptyEnumRule = repository.rule("EmptyEnum"); + assertThat(emptyEnumRule).isNotNull(); + assertThat(emptyEnumRule.name()).isEqualTo("Enum should not be left empty"); + assertThat(emptyEnumRule.template()).isFalse(); + + assertThat(repository.rule("LineLength").template()).isFalse(); + + for (RulesDefinition.Rule rule : repository.rules()) { + for (RulesDefinition.Param param : rule.params()) { + assertThat(param.description()).as("description for " + param.key()).isNotEmpty(); + } + } + } + + + private Repository createRepository() { + new RustRulesDefinition().define(context); + return context.repository("community-rust"); + } +} From c90cbb6f3618d9277527a6bb5f44a0318047a895 Mon Sep 17 00:00:00 2001 From: Eric Le Goff Date: Thu, 29 Jul 2021 11:47:00 +0200 Subject: [PATCH 14/14] remove unused import --- .../src/main/java/org/elegoff/rust/checks/EmptyEnumCheck.java | 1 - 1 file changed, 1 deletion(-) diff --git a/rust-checks/src/main/java/org/elegoff/rust/checks/EmptyEnumCheck.java b/rust-checks/src/main/java/org/elegoff/rust/checks/EmptyEnumCheck.java index 7a9affd0..15e88551 100644 --- a/rust-checks/src/main/java/org/elegoff/rust/checks/EmptyEnumCheck.java +++ b/rust-checks/src/main/java/org/elegoff/rust/checks/EmptyEnumCheck.java @@ -25,7 +25,6 @@ import org.sonar.rust.RustGrammar; import java.util.Collections; -import java.util.List; import java.util.Set; @Rule(key = "EmptyEnum")