aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEvgeny Mandrikov <mandrikov@gmail.com>2011-12-02 17:05:39 +0400
committerEvgeny Mandrikov <mandrikov@gmail.com>2011-12-05 12:51:15 +0400
commit7faee383694f53a6baad3a1a88218c5f4f88578e (patch)
treec339165fffa429f232a092b3007c1aa4a036bdfa
parentee09f485818f4808fa7aff7c02969c972931a180 (diff)
downloadsonarqube-7faee383694f53a6baad3a1a88218c5f4f88578e.tar.gz
sonarqube-7faee383694f53a6baad3a1a88218c5f4f88578e.zip
SONAR-2018 Add rule to detect commented-out lines of code
-rw-r--r--plugins/sonar-l10n-en-plugin/src/main/resources/org/sonar/l10n/squidjava.properties2
-rw-r--r--plugins/sonar-l10n-en-plugin/src/main/resources/org/sonar/l10n/squidjava/CommentedOutCodeLine.html8
-rw-r--r--plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/ast/visitor/CommentVisitor.java2
-rw-r--r--plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/squid/check/CommentedOutCodeLineCheck.java39
-rw-r--r--plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/SquidRuleRepository.java3
-rw-r--r--plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/squid/check/CommentedOutCodeLineCheckTest.java56
-rw-r--r--plugins/sonar-squid-java-plugin/test-resources/rules/CommentedCode.java22
-rw-r--r--sonar-squid/src/main/java/org/sonar/squid/api/SourceFile.java15
-rw-r--r--sonar-squid/src/main/java/org/sonar/squid/text/Source.java9
9 files changed, 154 insertions, 2 deletions
diff --git a/plugins/sonar-l10n-en-plugin/src/main/resources/org/sonar/l10n/squidjava.properties b/plugins/sonar-l10n-en-plugin/src/main/resources/org/sonar/l10n/squidjava.properties
index f26cabba546..c178b1b078c 100644
--- a/plugins/sonar-l10n-en-plugin/src/main/resources/org/sonar/l10n/squidjava.properties
+++ b/plugins/sonar-l10n-en-plugin/src/main/resources/org/sonar/l10n/squidjava.properties
@@ -27,3 +27,5 @@ rule.squid.UndocumentedApi.param.forClasses=Optional. If this property is not de
rule.squid.UnusedPrivateMethod.name=Unused private method
rule.squid.UnusedProtectedMethod.name=Unused protected method
+
+rule.squid.CommentedOutCodeLine.name=Avoid commented-out lines of code
diff --git a/plugins/sonar-l10n-en-plugin/src/main/resources/org/sonar/l10n/squidjava/CommentedOutCodeLine.html b/plugins/sonar-l10n-en-plugin/src/main/resources/org/sonar/l10n/squidjava/CommentedOutCodeLine.html
new file mode 100644
index 00000000000..f5e5d238f24
--- /dev/null
+++ b/plugins/sonar-l10n-en-plugin/src/main/resources/org/sonar/l10n/squidjava/CommentedOutCodeLine.html
@@ -0,0 +1,8 @@
+<p>Here are the main reasons why commented code is a code smell :</p>
+<ul>
+ <li>It always raises more questions than it gives answers</li>
+ <li>Everybody will forget very quickly how relevant the commented code is</li>
+ <li>This is distraction when going down the code as it stops the flow of eyes</li>
+ <li>It is a bad SCM engine : Subversion, CVS and Git are really more trustworthy !</li>
+ <li>The simple fact of understanding why code was commented out in the first place can take a lot of time</li>
+</ul>
diff --git a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/ast/visitor/CommentVisitor.java b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/ast/visitor/CommentVisitor.java
index 9d013afd8e5..1be27168475 100644
--- a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/ast/visitor/CommentVisitor.java
+++ b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/ast/visitor/CommentVisitor.java
@@ -49,9 +49,9 @@ public class CommentVisitor extends JavaAstVisitor {
@Override
public void visitFile(DetailAST ast) {
-
SourceFile file = (SourceFile) peekSourceCode();
file.addNoSonarTagLines(getSource().getNoSonarTagLines());
+ file.addCommentedOutCodeLines(getSource().getCommentedOutCodeLines());
file.setMeasure(Metric.HEADER_COMMENT_LINES, getSource().getMeasure(Metric.HEADER_COMMENT_LINES));
file.setMeasure(Metric.COMMENTED_OUT_CODE_LINES, getSource().getMeasure(Metric.COMMENTED_OUT_CODE_LINES));
file.setMeasure(Metric.COMMENT_LINES, getSource().getMeasure(Metric.COMMENT_LINES));
diff --git a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/squid/check/CommentedOutCodeLineCheck.java b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/squid/check/CommentedOutCodeLineCheck.java
new file mode 100644
index 00000000000..1e65f01c51b
--- /dev/null
+++ b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/squid/check/CommentedOutCodeLineCheck.java
@@ -0,0 +1,39 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2011 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar 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.
+ *
+ * Sonar 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 Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.java.squid.check;
+
+import org.sonar.check.Priority;
+import org.sonar.check.Rule;
+import org.sonar.squid.api.CheckMessage;
+import org.sonar.squid.api.SourceFile;
+
+@Rule(key = "CommentedOutCodeLine", priority = Priority.MAJOR)
+public class CommentedOutCodeLineCheck extends SquidCheck {
+
+ @Override
+ public void visitFile(SourceFile sourceFile) {
+ for (Integer line : sourceFile.getCommentedOutCodeLines()) {
+ CheckMessage message = new CheckMessage(this, "It's better to remove commented-out line of code.");
+ message.setLine(line);
+ sourceFile.log(message);
+ }
+ }
+
+}
diff --git a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/SquidRuleRepository.java b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/SquidRuleRepository.java
index fe31dba88ab..f2a93a76dbe 100644
--- a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/SquidRuleRepository.java
+++ b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/SquidRuleRepository.java
@@ -58,6 +58,7 @@ public final class SquidRuleRepository extends RuleRepository {
// AST checks
UndocumentedApiCheck.class, ContinueCheck.class, BreakCheck.class,
// Squid checks
- DITCheck.class, ClassComplexityCheck.class, MethodComplexityCheck.class, NoSonarCheck.class, EmptyFileCheck.class);
+ DITCheck.class, ClassComplexityCheck.class, MethodComplexityCheck.class, NoSonarCheck.class, EmptyFileCheck.class,
+ CommentedOutCodeLineCheck.class);
}
}
diff --git a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/squid/check/CommentedOutCodeLineCheckTest.java b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/squid/check/CommentedOutCodeLineCheckTest.java
new file mode 100644
index 00000000000..aee6c74b360
--- /dev/null
+++ b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/squid/check/CommentedOutCodeLineCheckTest.java
@@ -0,0 +1,56 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2011 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar 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.
+ *
+ * Sonar 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 Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.java.squid.check;
+
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.*;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.sonar.java.ast.JavaAstScanner;
+import org.sonar.java.ast.SquidTestUtils;
+import org.sonar.java.squid.JavaSquidConfiguration;
+import org.sonar.java.squid.SquidScanner;
+import org.sonar.squid.Squid;
+import org.sonar.squid.api.SourceFile;
+import org.sonar.squid.measures.Metric;
+
+public class CommentedOutCodeLineCheckTest {
+
+ private Squid squid;
+
+ @Before
+ public void setUp() {
+ squid = new Squid(new JavaSquidConfiguration());
+ CommentedOutCodeLineCheck check = new CommentedOutCodeLineCheck();
+ squid.registerVisitor(check);
+ JavaAstScanner scanner = squid.register(JavaAstScanner.class);
+ scanner.scanFile(SquidTestUtils.getInputFile("/rules/CommentedCode.java"));
+ squid.decorateSourceCodeTreeWith(Metric.values());
+ squid.register(SquidScanner.class).scan();
+ }
+
+ @Test
+ public void testDetection() {
+ SourceFile file = (SourceFile) squid.search("CommentedCode.java");
+ assertThat(file.getCheckMessages().size(), is(4));
+ }
+
+}
diff --git a/plugins/sonar-squid-java-plugin/test-resources/rules/CommentedCode.java b/plugins/sonar-squid-java-plugin/test-resources/rules/CommentedCode.java
new file mode 100644
index 00000000000..0a44bc77d2a
--- /dev/null
+++ b/plugins/sonar-squid-java-plugin/test-resources/rules/CommentedCode.java
@@ -0,0 +1,22 @@
+public class CommentedCode {
+ public CommentedCode() {
+ //This is a comment
+ //if (true) {
+ int i = 2;
+ }
+
+ /**
+ * No detection of commented code line in Javadoc bloc
+ *
+ * public void main(){
+ *
+ * }
+ */
+ public void analyse() {
+ /*
+ for(Visitor visitor : visitors){
+ visitor.scan(line);
+ }
+ */
+ }
+}
diff --git a/sonar-squid/src/main/java/org/sonar/squid/api/SourceFile.java b/sonar-squid/src/main/java/org/sonar/squid/api/SourceFile.java
index b9355b88a48..922c5ca26b1 100644
--- a/sonar-squid/src/main/java/org/sonar/squid/api/SourceFile.java
+++ b/sonar-squid/src/main/java/org/sonar/squid/api/SourceFile.java
@@ -26,6 +26,7 @@ import java.util.Set;
public class SourceFile extends SourceCode {
private Set<Integer> noSonarTagLines = new HashSet<Integer>();
+ private Set<Integer> commentedOutCodeLines = new HashSet<Integer>();
public SourceFile(String key) {
super(key);
@@ -52,4 +53,18 @@ public class SourceFile extends SourceCode {
public void addNoSonarTagLine(int line) {
noSonarTagLines.add(line);
}
+
+ /**
+ * @since 2.13
+ */
+ public Set<Integer> getCommentedOutCodeLines() {
+ return commentedOutCodeLines;
+ }
+
+ /**
+ * @since 2.13
+ */
+ public void addCommentedOutCodeLines(Set<Integer> commentedOutCodeLines) {
+ this.commentedOutCodeLines.addAll(commentedOutCodeLines);
+ }
}
diff --git a/sonar-squid/src/main/java/org/sonar/squid/text/Source.java b/sonar-squid/src/main/java/org/sonar/squid/text/Source.java
index 8e6b51b4714..7d4510f8db1 100644
--- a/sonar-squid/src/main/java/org/sonar/squid/text/Source.java
+++ b/sonar-squid/src/main/java/org/sonar/squid/text/Source.java
@@ -33,6 +33,7 @@ public class Source {
private List<Line> lines = new ArrayList<Line>();
private CodeRecognizer codeRecognizer;
private Set<Integer> noSonarTagLines = new HashSet<Integer>();
+ private Set<Integer> commentedOutCodeLines = new HashSet<Integer>();
public Source(Reader reader, CodeRecognizer codeRecognizer, String... additionalSingleLineCommentFlag) {
this.codeRecognizer = codeRecognizer;
@@ -87,6 +88,7 @@ public class Source {
line.setMeasure(Metric.COMMENT_LINES, 1);
} else {
line.setMeasure(Metric.COMMENTED_OUT_CODE_LINES, 1);
+ commentedOutCodeLines.add(line.getLineIndex());
}
}
}
@@ -125,4 +127,11 @@ public class Source {
public Set<Integer> getNoSonarTagLines() {
return noSonarTagLines;
}
+
+ /**
+ * @since 2.13
+ */
+ public Set<Integer> getCommentedOutCodeLines() {
+ return commentedOutCodeLines;
+ }
}