Skip to content

Commit

Permalink
Merge pull request #1114 from Bertk/fixes2
Browse files Browse the repository at this point in the history
fix sonarlint issues and reduce false positives
  • Loading branch information
guwirth authored Apr 22, 2017
2 parents 9438182 + b416d6c commit ae0bbc2
Show file tree
Hide file tree
Showing 79 changed files with 668 additions and 529 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void visitToken(Token token) {
for (int i = 0; i < lines.length; i++) {
int start = indexOfIgnoreCase(lines[i]);
if (start != -1 && !isLetterAround(lines[i], start)) {
check.getContext().createLineViolation(check, message, trivia.getToken().getLine() + i);
check.getContext().createLineViolation(check, message, trivia.getToken().getLine() + i); //NOSONAR
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@
@Rule(
key = "CommentedCode",
name = "Sections of code should not be 'commented out'",
tags = {Tag.UNUSED},
priority = Priority.BLOCKER)
tags = {Tag.BAD_PRACTICE},
priority = Priority.CRITICAL)
@ActivatedByDefault
@SqaleConstantRemediation("5min")
public class CommentedCodeCheck extends SquidCheck<Grammar> implements AstAndTokenVisitor {
Expand Down Expand Up @@ -74,15 +74,14 @@ public Set<Detector> getDetectors() {
@Override
public void visitToken(Token token) {
for (Trivia trivia : token.getTrivia()) {
if (trivia.isComment()
if (trivia.isComment() //NOSONAR
&& !trivia.getToken().getOriginalValue().startsWith("///")
&& !trivia.getToken().getOriginalValue().startsWith("//!")
&& !trivia.getToken().getOriginalValue().startsWith("/**")
&& !trivia.getToken().getOriginalValue().startsWith("/**") //NOSONAR
&& !trivia.getToken().getOriginalValue().startsWith("/*!")
&& !trivia.getToken().getOriginalValue().startsWith("/*@")
&& !trivia.getToken().getOriginalValue().startsWith("/*@") //NOSONAR
&& !trivia.getToken().getOriginalValue().startsWith("//@")) {
String lines[] = regexpToDivideStringByLine.split(getContext().getCommentAnalyser().getContents(
trivia.getToken().getOriginalValue()));
String lines[] = regexpToDivideStringByLine.split(getContext().getCommentAnalyser().getContents(trivia.getToken().getOriginalValue())); //NOSONAR

for (int lineOffset = 0; lineOffset < lines.length; lineOffset++) {
if (codeRecognizer.isLineOfCode(lines[lineOffset])) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
priority = Priority.MINOR)
@ActivatedByDefault
@NoSqale
public class FileEncodingCheck extends SquidCheck<Grammar> implements CxxCharsetAwareVisitor {
public class FileEncodingCheck extends SquidCheck<Grammar> implements CxxCharsetAwareVisitor { //NOSONAR

private Charset charset;

Expand All @@ -50,7 +50,7 @@ public void setCharset(Charset charset) {
public void visitFile(AstNode astNode) {
try {
Files.readAllLines(getContext().getFile().toPath(), charset);
} catch (IOException e) {
} catch (IOException e) { //NOSONAR
getContext().createFileViolation(this, "Not all characters of the file can be encoded with the predefined charset " + charset.name() + ".");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
@ActivatedByDefault
@SqaleConstantRemediation("5min")
//similar Vera++ rule T013 "No copyright notice found"
public class FileHeaderCheck extends SquidCheck<Grammar> implements CxxCharsetAwareVisitor {
public class FileHeaderCheck extends SquidCheck<Grammar> implements CxxCharsetAwareVisitor { //NOSONAR

private static final String DEFAULT_HEADER_FORMAT = "";
private static final String MESSAGE = "Add or update the header of this file.";
Expand Down Expand Up @@ -105,7 +105,7 @@ public void visitFile(AstNode astNode) {
List<String> lines;
try {
lines = Files.readLines(getContext().getFile(), charset);
} catch (IOException e) {
} catch (IOException e) { //NOSONAR
throw new IllegalStateException(e);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,16 @@
import org.sonar.squidbridge.checks.SquidCheck;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CodingErrorAction;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import org.sonar.api.utils.PathUtils;
import org.sonar.api.utils.WildcardPattern;
import org.sonar.squidbridge.annotations.NoSqale;
Expand All @@ -47,7 +49,7 @@
priority = Priority.MAJOR)
@RuleTemplate
@NoSqale
public class FileRegularExpressionCheck extends SquidCheck<Grammar> implements CxxCharsetAwareVisitor {
public class FileRegularExpressionCheck extends SquidCheck<Grammar> implements CxxCharsetAwareVisitor { //NOSONAR

private static final String DEFAULT_MATCH_FILE_PATTERN = "";
private static final boolean DEFAULT_INVERT_FILE_PATTERN = false;
Expand Down Expand Up @@ -96,8 +98,10 @@ public void init() {
decoder = charset.newDecoder();
decoder.onMalformedInput(CodingErrorAction.REPLACE);
decoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
} catch (Exception e) {
throw new IllegalStateException(e);
} catch (PatternSyntaxException ex) {
throw new IllegalStateException(ex);
} catch (IllegalArgumentException ex2) {
throw new IllegalStateException(ex2);
}
}

Expand All @@ -108,7 +112,6 @@ public void setCharset(Charset charset) {

@Override
public void visitFile(AstNode fileNode) {
if (fileNode != null) {
try {
if (!compare(invertFilePattern, matchFile())) {
return;
Expand All @@ -117,11 +120,10 @@ public void visitFile(AstNode fileNode) {
if (compare(invertRegularExpression, matcher.find())) {
getContext().createFileViolation(this, message);
}
} catch (Exception e) {
throw new IllegalStateException(e);
} catch (Exception e) { //NOSONAR
throw new IllegalStateException(e);
}
}
}

private boolean matchFile() {
if (!matchFilePattern.isEmpty()) {
Expand All @@ -132,18 +134,11 @@ private boolean matchFile() {
return true;
}

private CharSequence fromFile(File file) throws Exception {
FileInputStream input = null;
try {
input = new FileInputStream(file);
private CharSequence fromFile(File file) throws IOException {
try (FileInputStream input = new FileInputStream(file)) {
FileChannel channel = input.getChannel();
ByteBuffer bbuf = channel.map(FileChannel.MapMode.READ_ONLY, 0, (int) channel.size());
CharBuffer cbuf = decoder.decode(bbuf);
return cbuf;
} finally {
if (input != null) {
input.close();
}
return decoder.decode(bbuf);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public class HardcodedAccountCheck extends SquidCheck<Grammar> {
*
*/
private static final String DEFAULT_REGULAR_EXPRESSION = "\\bDSN\\b.*=.*;\\b(UID|PWD)\\b=.*;";
private static Matcher reg;
private static volatile Matcher reg = null;

@RuleProperty(
key = "regularExpression",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,8 @@
import org.sonar.squidbridge.checks.SquidCheck;
import com.sonar.sslr.api.AstNode;
import com.sonar.sslr.api.Grammar;
import org.sonar.api.server.rule.RulesDefinition;
import org.sonar.squidbridge.annotations.ActivatedByDefault;
import org.sonar.squidbridge.annotations.SqaleConstantRemediation;
import org.sonar.squidbridge.annotations.SqaleSubCharacteristic;
import org.sonar.cxx.tag.Tag;

@Rule(
Expand All @@ -42,7 +40,6 @@
tags = {Tag.CERT, Tag.SECURITY},
priority = Priority.CRITICAL)
@ActivatedByDefault
@SqaleSubCharacteristic(RulesDefinition.SubCharacteristics.ARCHITECTURE_CHANGEABILITY)
@SqaleConstantRemediation("30min")
public class HardcodedIpCheck extends SquidCheck<Grammar> {

Expand All @@ -53,7 +50,7 @@ public class HardcodedIpCheck extends SquidCheck<Grammar> {
// IPv4 with port number
// (?:^|\s)([a-z]{3,6}(?=://))?(://)?((?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.(?:25[0-5]|2[0-4]\d|[01]?\d\d?))(?::(\d{2,5}))?(?:\s|$)
private static final String DEFAULT_REGULAR_EXPRESSION = "^.*((?<![\\d|\\.])(?:\\b(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b\\.){3}\\b(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b(?!\\d|\\.)).*$";
private static Matcher IP;
private static volatile Matcher IP = null;

@RuleProperty(
key = "regularExpression",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
priority = Priority.MAJOR)
@RuleTemplate
@NoSqale
public class LineRegularExpressionCheck extends SquidCheck<Grammar> implements CxxCharsetAwareVisitor {
public class LineRegularExpressionCheck extends SquidCheck<Grammar> implements CxxCharsetAwareVisitor { //NOSONAR

private static final String DEFAULT_MATCH_FILE_PATTERN = "";
private static final boolean DEFAULT_INVERT_FILE_PATTERN = false;
Expand Down Expand Up @@ -104,7 +104,6 @@ public void setCharset(Charset charset) {

@Override
public void visitFile(AstNode fileNode) {
if (fileNode != null) {
if (compare(invertFilePattern, matchFile())) {
List<String> lines;
try {
Expand All @@ -120,7 +119,6 @@ public void visitFile(AstNode fileNode) {
}
}
}
}

private boolean matchFile() {
if (!matchFilePattern.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ private boolean isConst(AstNode node) {
}
if (decl != null) {
for (AstNode qualifier : decl.getDescendants(CxxGrammarImpl.cvQualifier)) {
if (qualifier.getToken().getType() == CxxKeyword.CONST) {
if (qualifier.getToken().getType().equals(CxxKeyword.CONST)) {
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.charset.StandardCharsets;

import org.sonar.check.Priority;
import org.sonar.check.Rule;
import org.sonar.squidbridge.checks.SquidCheck;
Expand Down Expand Up @@ -61,7 +63,7 @@ private boolean endsWithNewline(RandomAccessFile randomAccessFile) throws IOExce
if (randomAccessFile.read(chars) < 1) {
return false;
}
String ch = new String(chars);
String ch = new String(chars, StandardCharsets.UTF_8);
return "\n".equals(ch) || "\r".equals(ch);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
)
@ActivatedByDefault
@SqaleConstantRemediation("10min")
public class NestedStatementsCheck extends SquidCheck<Grammar> {
public class NestedStatementsCheck extends SquidCheck<Grammar> { //NOSONAR

private static final AstNodeType[] CHECKED_TYPES = new AstNodeType[]{
CxxGrammarImpl.selectionStatement,
Expand All @@ -55,8 +55,6 @@ public class NestedStatementsCheck extends SquidCheck<Grammar> {

private static final int DEFAULT_MAX = 3;

private static final String ELSE_TOKEN = "ELSE";

@RuleProperty(
key = "max",
description = "Maximum allowed control flow statement nesting depth.",
Expand Down Expand Up @@ -99,7 +97,7 @@ public void visitNode(AstNode node) {
nestingLevel--;
}

// Prevent re-checking of descendent nodes
// Prevent re-checking of descendant nodes
checkedNodes.addAll(watchedDescendants);
}

Expand All @@ -113,6 +111,6 @@ private void visitChildren(List<AstNode> watchedDescendants) {
* @return True if the given node is the 'if' in an 'else if' construct.
*/
private boolean isElseIf(AstNode node) {
return isIfStatement(node) && node.getParent().getPreviousAstNode().getType()==CxxKeyword.ELSE;
return isIfStatement(node) && node.getParent().getPreviousAstNode().getType().equals(CxxKeyword.ELSE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.io.StringWriter;
import org.sonar.check.Priority;
import org.sonar.check.Rule;
import org.sonar.cxx.tag.Tag;
import org.sonar.squidbridge.AstScannerExceptionHandler;
import org.sonar.squidbridge.checks.SquidCheck;
import com.sonar.sslr.api.Grammar;
Expand All @@ -33,6 +34,7 @@
@Rule(
key = "ParsingError",
name = "C++ parser failure",
tags = {Tag.TOOL_ERROR},
priority = Priority.MAJOR)
@ActivatedByDefault
@NoSqale
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.sonar.check.Priority;
import org.sonar.check.Rule;
import org.sonar.cxx.parser.CxxGrammarImpl;
import org.sonar.cxx.tag.Tag;
import org.sonar.squidbridge.checks.SquidCheck;
import com.sonar.sslr.api.AstNode;
import com.sonar.sslr.api.Grammar;
Expand All @@ -31,6 +32,7 @@
@Rule(
key = "ParsingErrorRecovery",
name = "C++ skip parser error",
tags = {Tag.TOOL_ERROR},
priority = Priority.INFO)
@ActivatedByDefault
@NoSqale
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@
@ActivatedByDefault
@SqaleConstantRemediation("5min")
//similar Vera++ rule T002
public class ReservedNamesCheck extends SquidCheck<Grammar> implements CxxCharsetAwareVisitor {
public class ReservedNamesCheck extends SquidCheck<Grammar> implements CxxCharsetAwareVisitor { //NOSONAR

private static String[] keywords;
private static volatile String[] keywords = null;
private Charset charset;

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
tags = {Tag.CONVENTION})
@ActivatedByDefault
@SqaleConstantRemediation("5min")
public class SafetyTagCheck extends SquidCheck<Grammar> implements AstAndTokenVisitor {
public class SafetyTagCheck extends SquidCheck<Grammar> implements AstAndTokenVisitor { //NOSONAR

private static final String DEFAULT_REGULAR_EXPRESSION = "<Safetykey>.*</Safetykey>";
private static final String DEFAULT_MESSAGE = "Source files implementing risk mitigations shall use special name suffix";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
key = "SwitchLastCaseIsDefault",
name = "Switch statements should end with a default case",
priority = Priority.MAJOR,
tags = {Tag.PITFALL})
tags = {Tag.BAD_PRACTICE, Tag.PITFALL})
@ActivatedByDefault
@SqaleConstantRemediation("5min")
public class SwitchLastCaseIsDefaultCheck extends SquidCheck<Grammar> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
@ActivatedByDefault
@SqaleConstantRemediation("5min")
//similar Vera++ rule L002 "Don't use tab characters"
public class TabCharacterCheck extends SquidCheck<Grammar> implements CxxCharsetAwareVisitor {
public class TabCharacterCheck extends SquidCheck<Grammar> implements CxxCharsetAwareVisitor { //NOSONAR

private static final boolean DEFAULT_CREATE_LINE_VIOLATION = false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
@ActivatedByDefault
@SqaleConstantRemediation("5min")
//similar Vera++ rule L004 "Line too long"
public class TooLongLineCheck extends SquidCheck<Grammar> implements CxxCharsetAwareVisitor {
public class TooLongLineCheck extends SquidCheck<Grammar> implements CxxCharsetAwareVisitor { //NOSONAR

private static final int DEFAULT_MAXIMUM_LINE_LENHGTH = 160;
private static final int DEFAULT_TAB_WIDTH = 8;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import java.util.List;

import org.sonar.api.server.rule.RulesDefinition;
import org.sonar.check.Priority;
import org.sonar.check.Rule;
import org.sonar.check.RuleProperty;
Expand All @@ -30,7 +29,6 @@
import org.sonar.cxx.tag.Tag;
import org.sonar.squidbridge.annotations.ActivatedByDefault;
import org.sonar.squidbridge.annotations.SqaleConstantRemediation;
import org.sonar.squidbridge.annotations.SqaleSubCharacteristic;
import com.sonar.sslr.api.AstNode;
import com.sonar.sslr.api.Grammar;
import org.sonar.squidbridge.checks.SquidCheck;
Expand All @@ -40,9 +38,8 @@
priority = Priority.MAJOR,
tags = {Tag.BRAIN_OVERLOAD})
@ActivatedByDefault
@SqaleSubCharacteristic(RulesDefinition.SubCharacteristics.READABILITY)
@SqaleConstantRemediation("1h")
public class TooManyLinesOfCodeInFunctionCheck extends SquidCheck<Grammar> {
public class TooManyLinesOfCodeInFunctionCheck extends SquidCheck<Grammar> { //NOSONAR
private static final int DEFAULT_MAXIMUM = 200;

@RuleProperty(
Expand Down
Loading

0 comments on commit ae0bbc2

Please sign in to comment.