aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEvgeny Mandrikov <mandrikov@gmail.com>2012-07-03 18:45:12 +0600
committerEvgeny Mandrikov <mandrikov@gmail.com>2012-07-03 20:59:48 +0600
commit9fe8aa8e6f0747203d0c6f3afe04be2970787fe2 (patch)
treebebb9458dd4b5ad6abf1147c745df5f94ee97ea9
parent4735e2998765431dafe8bde8ec3f02780ddc60af (diff)
downloadsonarqube-9fe8aa8e6f0747203d0c6f3afe04be2970787fe2.tar.gz
sonarqube-9fe8aa8e6f0747203d0c6f3afe04be2970787fe2.zip
SONAR-3249 Do not detect commented-out code in headers
-rw-r--r--plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/ast/check/CommentedOutCodeLineCheck.java33
-rw-r--r--plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/ast/check/CommentedOutCodeLineCheckTest.java28
-rw-r--r--plugins/sonar-squid-java-plugin/test-resources/rules/CommentedCode.java6
3 files changed, 42 insertions, 25 deletions
diff --git a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/ast/check/CommentedOutCodeLineCheck.java b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/ast/check/CommentedOutCodeLineCheck.java
index 13bae7d0304..97cb1230c7f 100644
--- a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/ast/check/CommentedOutCodeLineCheck.java
+++ b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/ast/check/CommentedOutCodeLineCheck.java
@@ -19,13 +19,12 @@
*/
package org.sonar.java.ast.check;
-import org.sonar.check.BelongsToProfile;
-
-import java.util.Arrays;
-import java.util.List;
-import java.util.Set;
-
+import com.google.common.collect.Sets;
+import com.puppycrawl.tools.checkstyle.api.DetailAST;
+import com.puppycrawl.tools.checkstyle.api.TextBlock;
+import com.puppycrawl.tools.checkstyle.api.TokenTypes;
import org.apache.commons.lang.StringUtils;
+import org.sonar.check.BelongsToProfile;
import org.sonar.check.Priority;
import org.sonar.check.Rule;
import org.sonar.java.ast.visitor.AstUtils;
@@ -35,10 +34,9 @@ import org.sonar.squid.api.CheckMessage;
import org.sonar.squid.api.SourceFile;
import org.sonar.squid.recognizer.CodeRecognizer;
-import com.google.common.collect.Sets;
-import com.puppycrawl.tools.checkstyle.api.DetailAST;
-import com.puppycrawl.tools.checkstyle.api.TextBlock;
-import com.puppycrawl.tools.checkstyle.api.TokenTypes;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Set;
/**
* @since 2.13
@@ -87,11 +85,24 @@ public class CommentedOutCodeLineCheck extends JavaAstVisitor {
}
for (List<TextBlock> listOfComments : getFileContents().getCComments().values()) {
// This list contains not only comments in C style, but also documentation comments and JSNI comments
- comments.addAll(listOfComments);
+ for (TextBlock comment : listOfComments) {
+ if (!isHeader(comment)) {
+ comments.addAll(listOfComments);
+ }
+ }
}
}
/**
+ * We assume that comment on a first line - is a header with license.
+ * However possible to imagine corner case: file may contain commented-out code starting from first line.
+ * But we assume that probability of this is really low.
+ */
+ private static boolean isHeader(TextBlock comment) {
+ return comment.getStartLineNo() == 1;
+ }
+
+ /**
* Removes documentation comments and JSNI comments from candidates for commented-out code in order to prevent false-positives.
*/
@Override
diff --git a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/ast/check/CommentedOutCodeLineCheckTest.java b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/ast/check/CommentedOutCodeLineCheckTest.java
index c0974891191..f04f10e0b3a 100644
--- a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/ast/check/CommentedOutCodeLineCheckTest.java
+++ b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/ast/check/CommentedOutCodeLineCheckTest.java
@@ -19,9 +19,6 @@
*/
package org.sonar.java.ast.check;
-import static org.hamcrest.Matchers.is;
-import static org.junit.Assert.assertThat;
-
import org.junit.Before;
import org.junit.Test;
import org.sonar.java.CheckMessages;
@@ -33,6 +30,9 @@ import org.sonar.squid.Squid;
import org.sonar.squid.api.SourceFile;
import org.sonar.squid.measures.Metric;
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertThat;
+
public class CommentedOutCodeLineCheckTest {
private Squid squid;
@@ -53,26 +53,26 @@ public class CommentedOutCodeLineCheckTest {
SourceFile sourceFile = (SourceFile) squid.search("CommentedCode.java");
CheckMessages checkMessages = new CheckMessages(sourceFile);
- checkMessages.assertNext().atLine(26).withMessage("This block of commented-out lines of code should be removed.");
- checkMessages.assertNext().atLine(27);
- checkMessages.assertNext().atLine(28);
-
- checkMessages.assertNext().atLine(32);
+ checkMessages.assertNext().atLine(32).withMessage("This block of commented-out lines of code should be removed.");
+ checkMessages.assertNext().atLine(33);
+ checkMessages.assertNext().atLine(34);
checkMessages.assertNext().atLine(38);
- checkMessages.assertNext().atLine(39);
- checkMessages.assertNext().atLine(40);
checkMessages.assertNext().atLine(44);
+ checkMessages.assertNext().atLine(45);
+ checkMessages.assertNext().atLine(46);
+
+ checkMessages.assertNext().atLine(50);
- checkMessages.assertNext().atLine(60);
+ checkMessages.assertNext().atLine(66);
- checkMessages.assertNext().atLine(69);
+ checkMessages.assertNext().atLine(75);
checkMessages.assertNoMore();
- assertThat(sourceFile.getInt(Metric.COMMENT_LINES), is(40)); // TODO but in fact 46, so without fake-JSNI
- assertThat(sourceFile.getInt(Metric.COMMENT_BLANK_LINES), is(16));
+ assertThat(sourceFile.getInt(Metric.COMMENT_LINES), is(44)); // TODO but in fact 50, so without fake-JSNI
+ assertThat(sourceFile.getInt(Metric.COMMENT_BLANK_LINES), is(17));
}
}
diff --git a/plugins/sonar-squid-java-plugin/test-resources/rules/CommentedCode.java b/plugins/sonar-squid-java-plugin/test-resources/rules/CommentedCode.java
index 41ba88687a8..49e23cc1508 100644
--- a/plugins/sonar-squid-java-plugin/test-resources/rules/CommentedCode.java
+++ b/plugins/sonar-squid-java-plugin/test-resources/rules/CommentedCode.java
@@ -1,3 +1,9 @@
+/* No detection of commented-out code in header
+ * for (Visitor visitor : visitors) {
+ * continue;
+ * }
+ */
+
/**
* No detection of commented-out code in Javadoc for class
* for (Visitor visitor : visitors) {