Skip to content

Commit

Permalink
Fix code style
Browse files Browse the repository at this point in the history
  • Loading branch information
petertrr committed Dec 16, 2024
1 parent 3034dc2 commit 0c46341
Show file tree
Hide file tree
Showing 42 changed files with 89 additions and 145 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@
package org.sonarsource.slang;

import com.sonar.orchestrator.build.SonarScanner;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.assertj.core.api.Assertions.assertThat;

public class CoverageTest extends TestBase {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@
package org.sonarsource.slang;

import com.sonar.orchestrator.build.SonarScanner;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import org.junit.Rule;
Expand All @@ -33,6 +31,7 @@

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.assertj.core.api.Assertions.assertThat;

public class ExternalReportTest extends TestBase {

private static final String BASE_DIRECTORY = "projects/externalreport/";
Expand Down Expand Up @@ -74,8 +73,7 @@ public void golint() {
"pivot.go|external_golint:Names|MAJOR|5min|line:11|don't use underscores in Go names; var ascii_lowercase should be asciiLowercase\n" +
"pivot.go|external_golint:Names|MAJOR|5min|line:12|don't use underscores in Go names; var ascii_uppercase_len should be asciiUppercaseLen\n" +
"pivot.go|external_golint:Names|MAJOR|5min|line:13|don't use underscores in Go names; var ascii_lowercase_len should be asciiLowercaseLen\n" +
"pivot.go|external_golint:Names|MAJOR|5min|line:14|don't use underscores in Go names; var ascii_allowed should be asciiAllowed"
);
"pivot.go|external_golint:Names|MAJOR|5min|line:14|don't use underscores in Go names; var ascii_allowed should be asciiAllowed");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
*/
package org.sonarsource.slang;

import java.util.List;
import org.junit.Test;
import org.sonarqube.ws.Issues;

import static org.assertj.core.api.Assertions.assertThat;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.assertj.core.groups.Tuple;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -98,9 +97,8 @@ public static void stop() {
public void test_go() throws Exception {
ClientInputFile inputFile = prepareInputFile(
"package main\n"
+ "func empty() {\n" // go:S1186 (empty function)
+ "}\n"
);
+ "func empty() {\n" // go:S1186 (empty function)
+ "}\n");

assertIssues(analyzeWithSonarLint(inputFile),
tuple("go:S1186", 2, inputFile.getPath(), IssueSeverity.CRITICAL));
Expand All @@ -110,9 +108,8 @@ public void test_go() throws Exception {
public void test_go_nosonar() throws Exception {
ClientInputFile goInputFile = prepareInputFile(
"package main\n"
+ "func empty() { // NOSONAR\n" // skipped go:S1186 (empty function)
+ "}\n"
);
+ "func empty() { // NOSONAR\n" // skipped go:S1186 (empty function)
+ "}\n");
assertThat(analyzeWithSonarLint(goInputFile)).isEmpty();
}

Expand Down Expand Up @@ -163,7 +160,6 @@ public Charset getCharset() {
return StandardCharsets.UTF_8;
}


@Override
public <G> G getClientObject() {
return null;
Expand Down
4 changes: 0 additions & 4 deletions its/plugin/src/test/java/org/sonarsource/slang/TestBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,8 @@
import com.sonar.orchestrator.Orchestrator;
import com.sonar.orchestrator.build.SonarScanner;
import java.io.File;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import org.junit.ClassRule;
import org.sonarqube.ws.Issues;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

public class TestReportTest extends TestBase {

private static final Path BASE_DIRECTORY = Paths.get("projects","measures");
private static final Path BASE_DIRECTORY = Paths.get("projects", "measures");

@ClassRule
public static Orchestrator orchestrator = Tests.ORCHESTRATOR;
Expand Down
1 change: 0 additions & 1 deletion its/plugin/src/test/java/org/sonarsource/slang/Tests.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import com.sonar.orchestrator.locator.Location;
import com.sonar.orchestrator.locator.MavenLocation;
import java.io.File;

import org.apache.commons.lang.StringUtils;
import org.junit.ClassRule;
import org.junit.runner.RunWith;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

import org.apache.commons.lang.StringUtils;
import org.junit.AfterClass;
import org.junit.BeforeClass;
Expand All @@ -48,7 +47,6 @@ public class GoRulingTest {
private static Orchestrator orchestrator;
private static boolean keepSonarqubeRunning = "true".equals(System.getProperty("keepSonarqubeRunning"));


@BeforeClass
public static void setUp() {
OrchestratorBuilder builder = Orchestrator.builderEnv()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ public class CodeAfterJumpGoCheck extends CodeAfterJumpCheck {
@Override
protected boolean isValidAfterJump(Tree tree) {
return tree instanceof NativeTree &&
((NativeTree) tree).nativeKind().toString().contains(LABEL);
((NativeTree) tree).nativeKind().toString().contains(LABEL);
}

@Override
protected boolean shouldIgnore(Tree tree) {
return tree instanceof NativeTree &&
((NativeTree) tree).nativeKind().toString().equals(SEMICOLON);
((NativeTree) tree).nativeKind().toString().equals(SEMICOLON);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public class DuplicateBranchGoCheck extends DuplicateBranchCheck {
@Override
protected void checkConditionalStructure(CheckContext ctx, Tree tree, ConditionalStructure conditional) {
/*
If we enter a type switch, we may find branches with similar ASTs but different semantics.
In this case, we stop exploring the conditional structure to avoid raising FPs.
* If we enter a type switch, we may find branches with similar ASTs but different semantics.
* In this case, we stop exploring the conditional structure to avoid raising FPs.
*/
if (tree instanceof MatchTree && isTypeSwitch((MatchTree) tree)) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ public class OneStatementPerLineGoCheck extends OneStatementPerLineCheck {
@Override
protected boolean shouldIgnore(Tree tree) {
return tree instanceof NativeTree &&
((NativeTree) tree).nativeKind().toString().equals(SEMICOLON);
((NativeTree) tree).nativeKind().toString().equals(SEMICOLON);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,5 @@
* You should have received a copy of the Sonar Source-Available License
* along with this program; if not, see https://sonarsource.com/license/ssal/
*/
@ParametersAreNonnullByDefault
@javax.annotation.ParametersAreNonnullByDefault
package org.sonar.go.checks;

import javax.annotation.ParametersAreNonnullByDefault;
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


class ExternalProcessStreamConsumer {

private static final Logger LOG = LoggerFactory.getLogger(ExternalProcessStreamConsumer.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ static boolean fileMatch(File dest, byte[] expectedContent) throws IOException {

static byte[] getBytesFromResource(String executable) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
try(InputStream in = GoConverter.class.getClassLoader().getResourceAsStream(executable)) {
try (InputStream in = GoConverter.class.getClassLoader().getResourceAsStream(executable)) {
if (in == null) {
throw new IllegalStateException(executable + " binary not found on class path");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,5 @@
* You should have received a copy of the Sonar Source-Available License
* along with this program; if not, see https://sonarsource.com/license/ssal/
*/
@ParametersAreNonnullByDefault
@javax.annotation.ParametersAreNonnullByDefault
package org.sonar.go.converter;

import javax.annotation.ParametersAreNonnullByDefault;
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,9 @@ static Stream<Path> getReportPaths(SensorContext sensorContext) {
Configuration config = sensorContext.config();
Path baseDir = sensorContext.fileSystem().baseDir().toPath();
String[] reportPaths = config.getStringArray(REPORT_PATH_KEY);
return Arrays.stream(reportPaths).flatMap(reportPath ->
isWildcard(reportPath)
? getPatternPaths(baseDir, reportPath)
: getRegularPath(baseDir, reportPath));
return Arrays.stream(reportPaths).flatMap(reportPath -> isWildcard(reportPath)
? getPatternPaths(baseDir, reportPath)
: getRegularPath(baseDir, reportPath));
}

private static Stream<Path> getRegularPath(Path baseDir, String reportPath) {
Expand Down Expand Up @@ -245,14 +244,14 @@ private void add(CoverageStat coverage) {
for (int line = startLine; line <= endLine; line++) {
if (!isEmpty(line - 1)) {
lineMap.computeIfAbsent(line, key -> new LineCoverage())
.add(coverage);
.add(coverage);
}
}
}

private boolean isEmpty(int line) {
return lines != null &&
lines.get(line).trim().isEmpty();
lines.get(line).trim().isEmpty();
}

int findStartIgnoringBrace(CoverageStat coverage) {
Expand Down Expand Up @@ -307,7 +306,7 @@ void add(CoverageStat coverage) {
if (sum > Integer.MAX_VALUE) {
hits = Integer.MAX_VALUE;
} else {
hits = (int) sum;
hits = (int) sum;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,10 @@ String concat(String parentPath, String childPath) {
* See {@link GoCoverSensor#findInputFile(String, FileSystem)}
*/
public String resolve(String filePath) {
return resolvedPaths.computeIfAbsent(filePath, path ->
getAbsolutePathForOldGoVersions(path)
.orElseGet(() -> getAbsolutePath(path)
return resolvedPaths.computeIfAbsent(filePath, path -> getAbsolutePathForOldGoVersions(path)
.orElseGet(() -> getAbsolutePath(path)
.orElseGet(() -> prefixByFirstValidGoPath(path)
.orElseGet(() -> prefixByFirstGoPath(path)))));
.orElseGet(() -> prefixByFirstGoPath(path)))));
}

/**
Expand All @@ -88,7 +87,7 @@ public String resolve(String filePath) {
* unix: "_/mnt/c/src..." vs "/mnt/c/src..."
* windows: "_\c_\src..." vs "c:\src..."
*/
private static Optional<String> getAbsolutePathForOldGoVersions(String path){
private static Optional<String> getAbsolutePathForOldGoVersions(String path) {
if (path.startsWith(LINUX_ABSOLUTE_OLD_PREFIX)) {
return Optional.of(path.substring(1));
} else if (path.startsWith(WINDOWS_ABSOLUTE_OLD_PREFIX)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,5 @@
* You should have received a copy of the Sonar Source-Available License
* along with this program; if not, see https://sonarsource.com/license/ssal/
*/
@ParametersAreNonnullByDefault
@javax.annotation.ParametersAreNonnullByDefault
package org.sonar.go.coverage;

import javax.annotation.ParametersAreNonnullByDefault;
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public static void createExternalRuleRepository(Context context, String linterId

try (InputStreamReader inputStreamReader = new InputStreamReader(AbstractReportSensor.class.getClassLoader().getResourceAsStream(pathToRulesMeta), StandardCharsets.UTF_8)) {
JsonArray jsonArray = Json.parse(inputStreamReader).asArray();
for(JsonValue jsonValue : jsonArray) {
for (JsonValue jsonValue : jsonArray) {
JsonObject rule = jsonValue.asObject();
NewRule newRule = externalRepo.createRule(rule.getString("key", null))
.setName(rule.getString("name", null))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,19 @@ private ExternalKeyUtils() {
new ExternalKey("shadow", msg -> msg.contains("shadows declaration at")),
new ExternalKey("shift", msg -> msg.contains("too small for shift")),
new ExternalKey("structtags", msg -> msg.contains("struct field") && msg.contains("tag")),
new ExternalKey("tests", msg ->
msg.contains("has malformed") ||
msg.contains("refers to unknown") ||
msg.endsWith("should return nothing") ||
msg.endsWith("should be niladic")),
new ExternalKey("tests", msg -> msg.contains("has malformed") ||
msg.contains("refers to unknown") ||
msg.endsWith("should return nothing") ||
msg.endsWith("should be niladic")),
new ExternalKey("unreachable", msg -> msg.equals("unreachable code")),
new ExternalKey("unusedresult", msg -> msg.endsWith("call not used")),
new ExternalKey("unsafeptr", msg -> msg.equals("possible misuse of unsafe.Pointer"))
));

new ExternalKey("unsafeptr", msg -> msg.equals("possible misuse of unsafe.Pointer"))));

public static final List<ExternalKey> GO_LINT_KEYS = Collections.unmodifiableList(Arrays.asList(
new ExternalKey("PackageComment", msg ->
msg.startsWith("package comment should be of the form") ||
msg.startsWith("package comment should not have leading space") ||
msg.equals("package comment is detached; there should be no blank lines between it and the package statement") ||
msg.equals("should have a package comment, unless it's in another file for this package")),
new ExternalKey("PackageComment", msg -> msg.startsWith("package comment should be of the form") ||
msg.startsWith("package comment should not have leading space") ||
msg.equals("package comment is detached; there should be no blank lines between it and the package statement") ||
msg.equals("should have a package comment, unless it's in another file for this package")),
new ExternalKey("BlankImports", msg -> msg.equals("a blank import should be only in a main or test package, or have a comment justifying it")),
new ExternalKey("Imports", msg -> msg.equals("should not use dot imports")),
new ExternalKey("Exported", msg -> (msg.startsWith("exported") && msg.endsWith("or be unexported")) ||
Expand All @@ -74,25 +70,21 @@ private ExternalKeyUtils() {
new ExternalKey("Errorf", msg -> msg.contains("(fmt.Sprintf(...)) with") && msg.contains(".Errorf(...)")),
new ExternalKey("Errors", msg -> msg.startsWith("error var ") && msg.contains("should have name of the form ")),
new ExternalKey("ErrorStrings", msg -> msg.equals("error strings should not be capitalized or end with punctuation or a newline")),
new ExternalKey("ReceiverNames", msg ->
msg.contains("should be consistent with previous receiver name") ||
msg.startsWith("receiver name should not be an underscore") ||
msg.equals("receiver name should be a reflection of its identity; don't use generic names such as \"this\" or \"self\"")),
new ExternalKey("ReceiverNames", msg -> msg.contains("should be consistent with previous receiver name") ||
msg.startsWith("receiver name should not be an underscore") ||
msg.equals("receiver name should be a reflection of its identity; don't use generic names such as \"this\" or \"self\"")),
new ExternalKey("IncDec", msg -> msg.startsWith("should replace") && !msg.contains("(fmt.Sprintf(...)) with")),
new ExternalKey("ErrorReturn", msg -> msg.startsWith("error should be the last type when returning multiple items")),
new ExternalKey("UnexportedReturn", msg -> msg.contains("returns unexported type") && msg.endsWith("which can be annoying to use")),
new ExternalKey("TimeNames", msg -> msg.contains("don't use unit-specific suffix")),
new ExternalKey("ContextKeyTypes", msg -> msg.startsWith("should not use basic type") && msg.endsWith("as key in context.WithValue")),
new ExternalKey("ContextArgs", msg -> msg.equals("context.Context should be the first parameter of a function")),
new ExternalKey("Names", msg ->
msg.startsWith("don't use an underscore in package name") ||
msg.startsWith("don't use ALL_CAPS in Go names; use CamelCase") ||
msg.startsWith("don't use leading k in Go names;") ||
msg.startsWith("don't use underscores in Go names;") ||
msg.matches("(range var|struct field|[\\w]+) [\\w_]+ should be [\\w_]+") ||
msg.startsWith("don't use MixedCaps in package name;")
)
));
new ExternalKey("Names", msg -> msg.startsWith("don't use an underscore in package name") ||
msg.startsWith("don't use ALL_CAPS in Go names; use CamelCase") ||
msg.startsWith("don't use leading k in Go names;") ||
msg.startsWith("don't use underscores in Go names;") ||
msg.matches("(range var|struct field|[\\w]+) [\\w_]+ should be [\\w_]+") ||
msg.startsWith("don't use MixedCaps in package name;"))));

public static String lookup(String message, String linter) {
if (linter.equals(GoVetReportSensor.LINTER_ID) || linter.equals(GoLintReportSensor.LINTER_ID)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,5 @@
* You should have received a copy of the Sonar Source-Available License
* along with this program; if not, see https://sonarsource.com/license/ssal/
*/
@ParametersAreNonnullByDefault
@javax.annotation.ParametersAreNonnullByDefault
package org.sonar.go.externalreport;

import javax.annotation.ParametersAreNonnullByDefault;
Loading

0 comments on commit 0c46341

Please sign in to comment.