Skip to content

Commit

Permalink
Merge pull request #2791 from guwirth/td-9
Browse files Browse the repository at this point in the history
fix technical debt
  • Loading branch information
guwirth authored Oct 24, 2024
2 parents 9b7c2c2 + 84dba51 commit 13923bd
Show file tree
Hide file tree
Showing 59 changed files with 217 additions and 225 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void visitFile(@Nullable AstNode astNode) {
}

@Override
public void leaveFile(AstNode astNode) {
public void leaveFile(@Nullable AstNode astNode) {
if (!getScopeType().isPresent()) {
analyzeScopeComplexity();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import com.sonar.cxx.sslr.api.AstNode;
import com.sonar.cxx.sslr.api.Grammar;
import javax.annotation.Nullable;
import org.sonar.check.Priority;
import org.sonar.check.Rule;
import org.sonar.check.RuleProperty;
Expand Down Expand Up @@ -54,7 +55,7 @@ public void setMax(int max) {
}

@Override
public void leaveFile(AstNode astNode) {
public void leaveFile(@Nullable AstNode astNode) {
var linesOfCode = ChecksHelper.getRecursiveMeasureInt(getContext().peekSourceCode(), CxxMetric.LINES_OF_CODE);
if (linesOfCode > max) {
getContext().createFileViolation(this,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.sonar.cxx.sslr.api.AstNode;
import com.sonar.cxx.sslr.api.Grammar;
import java.util.regex.Pattern;
import javax.annotation.Nullable;
import org.sonar.api.utils.PathUtils;
import org.sonar.api.utils.WildcardPattern;
import org.sonar.check.Priority;
Expand Down Expand Up @@ -106,7 +107,7 @@ public void init() {
}

@Override
public void visitFile(AstNode fileNode) {
public void visitFile(@Nullable AstNode fileNode) {
if (!compare(invertFilePattern, matchFile())) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.sonar.cxx.sslr.api.AstNode;
import com.sonar.cxx.sslr.api.Grammar;
import java.util.regex.Pattern;
import javax.annotation.Nullable;
import org.sonar.api.utils.PathUtils;
import org.sonar.api.utils.WildcardPattern;
import org.sonar.check.Priority;
Expand Down Expand Up @@ -106,7 +107,7 @@ public void init() {
}

@Override
public void visitFile(AstNode fileNode) {
public void visitFile(@Nullable AstNode fileNode) {
if (compare(invertFilePattern, matchFile())) {
var nr = 0;
for (var line : getContext().getInputFileLines()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import com.sonar.cxx.sslr.api.AstNode;
import com.sonar.cxx.sslr.api.Grammar;
import javax.annotation.Nullable;
import org.sonar.api.utils.PathUtils;
import org.sonar.api.utils.WildcardPattern;
import org.sonar.check.Priority;
Expand Down Expand Up @@ -83,7 +84,7 @@ public String getMessage() {
}

@Override
public void visitFile(AstNode fileNode) {
public void visitFile(@Nullable AstNode fileNode) {
if (!matchFilePattern.isEmpty()) {
var pattern = WildcardPattern.create(matchFilePattern);
String path = PathUtils.sanitize(getContext().getFile().getPath());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ void test() throws IOException {
Set<CxxReportIssue> issues = MultiLocatitionSquidCheck.getMultiLocationCheckMessages(file);
assertThat(issues).isNotNull();
var softly = new SoftAssertions();
softly.assertThat(issues).hasSize(3);
softly.assertThat(issues).allSatisfy(issue -> assertThat(issue.getRuleId()).isEqualTo("ClassComplexity"));
softly.assertThat(issues)
.hasSize(3)
.allSatisfy(issue -> assertThat(issue.getRuleId()).isEqualTo("ClassComplexity"));

CxxReportIssue issue0 = issues.stream().filter(issue -> issue.getLocations().get(0).getLine().equals("9"))
.findFirst().orElseThrow(() -> new AssertionError("No issue at line 9"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ void check() throws IOException {
assertThat(issues).isNotNull();

var softly = new SoftAssertions();
softly.assertThat(issues).hasSize(5);
softly.assertThat(issues)
.hasSize(5)
.allSatisfy(issue -> assertThat(issue.getRuleId()).isEqualTo("FunctionCognitiveComplexity"));

CxxReportIssue issue0 = issues.stream().filter(issue -> issue.getLocations().get(0).getLine().equals("13"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ void check() throws IOException {
var issues = MultiLocatitionSquidCheck.getMultiLocationCheckMessages(file);
assertThat(issues).isNotNull();
var softly = new SoftAssertions();
softly.assertThat(issues).hasSize(5);
softly.assertThat(issues).allSatisfy(issue -> assertThat(issue.getRuleId()).isEqualTo("FunctionComplexity"));
softly.assertThat(issues)
.hasSize(5)
.allSatisfy(issue -> assertThat(issue.getRuleId()).isEqualTo("FunctionComplexity"));

var issue0 = issues.stream()
.filter(issue -> issue.getLocations().get(0).getLine().equals("13"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ protected void processReport(File report) {
try (var scanner = new TextScanner(report, reportEncoding)) {
var pattern = Pattern.compile(reportRegEx);
LOG.debug("Processing '{}' report '{}', Encoding='{}', Pattern='{}'",
getCompilerKey(), report, scanner.encoding(), pattern);
getCompilerKey(), report, scanner.encoding(), pattern);

while (scanner.hasNextLine()) {
var matcher = pattern.matcher(scanner.nextLine());
Expand Down Expand Up @@ -117,8 +117,8 @@ protected void processReport(File report) {
* @return true, if valid
*/
protected boolean isInputValid(@Nullable String filename,
@Nullable String line, @Nullable String column,
@Nullable String id, String msg) {
@Nullable String line, @Nullable String column,
@Nullable String id, String msg) {
if ((id == null) || id.isEmpty()) {
return false;
}
Expand Down Expand Up @@ -193,7 +193,7 @@ private String getSubSequence(Matcher matcher, String groupName) {
}
} catch (IllegalArgumentException e) {
notExistingGroupName.add(groupName);
LOG.debug("named-capturing group '{}' is not used in regex.", groupName);
LOG.debug("named-capturing group '{}' is not used in regex.", groupName, e);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ Collection<CoverageMeasure> getCoverageMeasures() {

public Set<Integer> getCoveredLines() {
var coveredLines = new HashSet<Integer>();
lineMeasures.forEach((key, value) -> {
lineMeasures.forEach((Integer key, CoverageMeasure value) -> {
if (value.getHits() != 0) {
coveredLines.add(value.getLine());
}
Expand All @@ -72,7 +72,7 @@ public Set<Integer> getCoveredLines() {

public Set<Integer> getCoveredConditions() {
var coveredConditionLines = new HashSet<Integer>();
lineMeasures.forEach((key, value) -> {
lineMeasures.forEach((Integer key, CoverageMeasure value) -> {
if (value.getCoveredConditions() != 0) {
coveredConditionLines.add(value.getLine());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,15 +201,11 @@ private void saveConditions(CoverageMeasures fileMeasuresBuilderIn) {
private void updateMeasures(String kind, String event, String line, CoverageMeasures fileMeasuresBuilderIn) {

switch (kind.toLowerCase(Locale.ENGLISH)) {
case "decision":
case "condition":
case "decision", "condition":
totalconditions += 2;
setTotalCoveredConditions(event);
break;
case "catch":
case "for-range-body":
case "switch-label":
case "try":
case "catch", "for-range-body", "switch-label", "try":
totalconditions++;
if ("full".equalsIgnoreCase(event)) {
totalcoveredconditions++;
Expand Down Expand Up @@ -237,8 +233,7 @@ private void setTotalCoveredConditions(String event) {
case "full":
totalcoveredconditions += 2;
break;
case "true":
case "false":
case "true", "false":
totalcoveredconditions++;
break;
case "none":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ public void stream(SMHierarchicCursor rootCursor) throws XMLStreamException {

private void processErrorTag(SMInputCursor errorCursor) throws XMLStreamException {
String id = requireAttributeSet(errorCursor.getAttrValue("id"),
"Missing mandatory attribute /results/errors/error[@id]");
"Missing mandatory attribute /results/errors/error[@id]");
String msg = requireAttributeSet(errorCursor.getAttrValue("msg"),
"Missing mandatory attribute /results/errors/error[@msg]");
"Missing mandatory attribute /results/errors/error[@msg]");
boolean isInconclusive = "true".equals(errorCursor.getAttrValue("inconclusive"));
String issueText = createIssueText(msg, isInconclusive);
CxxReportIssue issue = null;
Expand Down Expand Up @@ -125,7 +125,7 @@ private void processErrorTag(SMInputCursor errorCursor) throws XMLStreamExceptio
// the current module) we are not interested in this <error>
if (!isLocationInProject) {
LOG.debug("Cppcheck issue outside of project, skipping issue: {}:{} {}:{}",
file, line, id, msg);
file, line, id, msg);
return;
}

Expand Down Expand Up @@ -167,7 +167,7 @@ private String makeRelativePath(String path, String basePath) {
try {
return Path.of(basePath).relativize(Path.of(path)).toString();
} catch (IllegalArgumentException e) {
LOG.warn("Can't create relative path: basePath='{}', path='{}'", basePath, path);
LOG.warn("Can't create relative path: basePath='{}', path='{}'", basePath, path, e);
return path;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@

import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import org.sonar.api.config.Configuration;
import org.sonar.api.server.rule.RulesDefinition;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.config.Configuration;
import org.sonar.api.server.rule.RulesDefinition;
import org.sonar.cxx.sensors.utils.RulesDefinitionXmlLoader;

/**
Expand Down Expand Up @@ -71,7 +71,7 @@ public void define(Context context) {
var xml = ruleDefs.substring(0, Math.min(ruleDefs.length(), 120))
.replaceAll("\\R", "").replaceAll(">[ ]+<", "><");
LOG.error("Cannot load rule definions for 'sonar.cxx.other.rules', '{}', XML '{}...', skipping",
e.getMessage(), xml);
e.getMessage(), xml, e);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ private static boolean checkRootTag(XmlParserHelper xmlParserHelper) {
xmlParserHelper.checkRootTag("test-results");
return true;
} catch (ParseErrorException e) {
LOG.warn("One of the assemblies contains no test result, please make sure this is expected.");
LOG.warn("One of the assemblies contains no test result, please make sure this is expected.", e);
return false;
}
}
Expand Down Expand Up @@ -85,7 +85,7 @@ private static Double readExecutionTimeFromDirectlyNestedTestSuiteTags(XmlParser
}

public void parse() {
try ( var xmlParserHelper = new XmlParserHelper(file)) {
try (var xmlParserHelper = new XmlParserHelper(file)) {
if (checkRootTag(xmlParserHelper)) {
handleTestResultsTag(xmlParserHelper);
}
Expand All @@ -108,7 +108,7 @@ private void handleTestResultsTag(XmlParserHelper xmlParserHelper) {
Double executionTime = readExecutionTimeFromDirectlyNestedTestSuiteTags(xmlParserHelper);

unitTestResults.add(tests, passed, skipped, failures, errors,
executionTime != null ? (long) executionTime.doubleValue() : null);
executionTime != null ? (long) executionTime.doubleValue() : null);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class VisualStudioTestResultsFileParser implements UnitTestResultsParser

@Override
public void accept(File file, UnitTestResults unitTestResults) {
LOG.info("Parsing the Visual Studio Test Results file " + file.getAbsolutePath());
LOG.info("Parsing the Visual Studio Test Results file {}", file.getAbsolutePath());
new Parser(file, unitTestResults).parse();
}

Expand Down Expand Up @@ -117,7 +117,7 @@ private Date getRequiredDateAttribute(XmlParserHelper xmlParserHelper, String na
return dateFormat.parse(value);
} catch (ParseException e) {
throw xmlParserHelper.parseError("Expected an valid date and time instead of \"" + value
+ "\" for the attribute \"" + name + "\". " + e.getMessage());
+ "\" for the attribute \"" + name + "\". " + e.getMessage());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void define(Context context) {
try (var input = java.nio.file.Files.newInputStream(userExtensionXml.toPath())) {
xmlRuleLoader.load(repository, input, encoding);
} catch (IOException | IllegalStateException e) {
LOG.error("Cannot load Rules Definions '{}'", e.getMessage());
LOG.error("Cannot load Rules Definions '{}'", e.getMessage(), e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ public StaxParser(XmlStreamHandler streamHandler, boolean isoControlCharsAwarePa
* @exception XMLStreamException javax.xml.stream.XMLStreamException
*/
public void parse(File xmlFile) throws XMLStreamException {
try ( var input = java.nio.file.Files.newInputStream(xmlFile.toPath())) {
try (var input = java.nio.file.Files.newInputStream(xmlFile.toPath())) {
parse(input);
} catch (IOException e) {
LOG.error("Cannot access file: " + e.getMessage());
LOG.error("Cannot access file: {}", e.getMessage(), e);
}
}

Expand Down Expand Up @@ -119,7 +119,7 @@ public void parse(Reader xmlReader) throws XMLStreamException {
*/
public void parse(URL xmlUrl) throws XMLStreamException {
try {
try ( var xml = xmlUrl.openStream()) {
try (var xml = xmlUrl.openStream()) {
parse(xml);
}
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ void frameEqualityIsReflexive() {

@Test
void frameEqualityWorksAsExpected() {
assertThat(frame).isEqualTo(equalFrame);
assertThat(frame).isNotEqualTo(otherFrame);
assertThat(frame)
.isEqualTo(equalFrame)
.isNotEqualTo(otherFrame);
}

@Test
Expand All @@ -71,19 +72,19 @@ void stringRepresentationShouldResembleValgrindsStandard() {
var ioMap = new HashMap<String, ValgrindFrame>();

ioMap.put("0xDEADBEAF: main() (main.cc:1)",
new ValgrindFrame("0xDEADBEAF", "libX.so", "main()", "src", "main.cc", "1"));
new ValgrindFrame("0xDEADBEAF", "libX.so", "main()", "src", "main.cc", "1"));
ioMap.put("0xDEADBEAF: main() (main.cc:1)",
new ValgrindFrame("0xDEADBEAF", "libX.so", "main()", null, "main.cc", "1"));
new ValgrindFrame("0xDEADBEAF", "libX.so", "main()", null, "main.cc", "1"));
ioMap.put("0xDEADBEAF: main() (main.cc)",
new ValgrindFrame("0xDEADBEAF", "libX.so", "main()", null, "main.cc", ""));
new ValgrindFrame("0xDEADBEAF", "libX.so", "main()", null, "main.cc", ""));
ioMap.put("0xDEADBEAF: ??? (main.cc:1)",
new ValgrindFrame("0xDEADBEAF", "libX.so", null, "src", "main.cc", "1"));
new ValgrindFrame("0xDEADBEAF", "libX.so", null, "src", "main.cc", "1"));
ioMap.put("0xDEADBEAF: ??? (in libX.so)",
new ValgrindFrame("0xDEADBEAF", "libX.so", null, "src", null, "1"));
new ValgrindFrame("0xDEADBEAF", "libX.so", null, "src", null, "1"));
ioMap.put("0xDEADBEAF: ???",
new ValgrindFrame("0xDEADBEAF", null, null, null, null, ""));
new ValgrindFrame("0xDEADBEAF", null, null, null, null, ""));
ioMap.put("???: ???",
new ValgrindFrame(null, null, null, null, null, ""));
new ValgrindFrame(null, null, null, null, null, ""));

for (var entry : ioMap.entrySet()) {
assertThat(entry.getValue()).hasToString(entry.getKey());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ void stackEqualityIsReflexive() {

@Test
void stackEqualityWorksAsExpected() {
assertThat(stack).isEqualTo(equalStack);
assertThat(stack).isNotEqualTo(otherStack);
assertThat(stack)
.isEqualTo(equalStack)
.isNotEqualTo(otherStack);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,10 @@ public void scanInputFiles(Iterable<InputFile> inputFiles) {
private static Exception handleParseException(File file, Exception e) {
checkInterrupted(e);
if (e instanceof RecognitionException) {
LOG.error(UNABLE_TO_PARSE + file.getAbsolutePath());
LOG.error(UNABLE_TO_PARSE + "{}", file.getAbsolutePath());
LOG.error(e.getMessage());
} else {
LOG.error(UNABLE_TO_PARSE + file.getAbsolutePath(), e);
LOG.error(UNABLE_TO_PARSE + "{}", file.getAbsolutePath(), e);
}
return e;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public void visitNode(AstNode statementNode) {
}

@Override
public void leaveFile(AstNode astNode) {
public void leaveFile(@Nullable AstNode astNode) {
for (var statementsAtLine : statementsPerLine.entrySet()) {
if (statementsAtLine.getValue() > 1) {
getContext().createLineViolation(
Expand Down
Loading

0 comments on commit 13923bd

Please sign in to comment.