From: Evgeny Mandrikov Date: Wed, 4 Jul 2012 18:15:05 +0000 (+0600) Subject: SONAR-3119 Report only one violation per block of commented out lines of code X-Git-Tag: 3.2~245 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=367c873a3475b8f92278a0a13578dc38b1935483;p=sonarqube.git SONAR-3119 Report only one violation per block of commented out lines of code --- 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 97cb1230c7f..be273ded716 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,6 +19,7 @@ */ package org.sonar.java.ast.check; +import com.google.common.collect.Lists; import com.google.common.collect.Sets; import com.puppycrawl.tools.checkstyle.api.DetailAST; import com.puppycrawl.tools.checkstyle.api.TextBlock; @@ -35,6 +36,7 @@ import org.sonar.squid.api.SourceFile; import org.sonar.squid.recognizer.CodeRecognizer; import java.util.Arrays; +import java.util.Collections; import java.util.List; import java.util.Set; @@ -133,18 +135,34 @@ public class CommentedOutCodeLineCheck extends JavaAstVisitor { */ @Override public void leaveFile(DetailAST ast) { - SourceFile sourceFile = (SourceFile) peekSourceCode(); + List commentedOutCodeLines = Lists.newArrayList(); for (TextBlock comment : comments) { String[] lines = comment.getText(); for (int i = 0; i < lines.length; i++) { if (codeRecognizer.isLineOfCode(lines[i])) { - CheckMessage message = new CheckMessage(this, "This block of commented-out lines of code should be removed."); - message.setLine(comment.getStartLineNo() + i); - sourceFile.log(message); + // Mark all remaining lines from this comment as a commented out lines of code + for (int j = i; j < lines.length; j++) { + commentedOutCodeLines.add(comment.getStartLineNo() + j); + } break; } } } + + // Greedy algorithm to split lines on blocks and to report only one violation per block + SourceFile sourceFile = (SourceFile) peekSourceCode(); + Collections.sort(commentedOutCodeLines); + int prev = Integer.MIN_VALUE; + for (int i = 0; i < commentedOutCodeLines.size(); i++) { + int current = commentedOutCodeLines.get(i); + if (prev + 1 < current) { + CheckMessage message = new CheckMessage(this, "This block of commented-out lines of code should be removed."); + message.setLine(current); + sourceFile.log(message); + } + prev = current; + } + comments = null; } 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 f04f10e0b3a..d75ba9c1602 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 @@ -54,19 +54,10 @@ public class CommentedOutCodeLineCheckTest { CheckMessages checkMessages = new CheckMessages(sourceFile); 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(44); - checkMessages.assertNext().atLine(45); - checkMessages.assertNext().atLine(46); - checkMessages.assertNext().atLine(50); - checkMessages.assertNext().atLine(66); - checkMessages.assertNext().atLine(75); checkMessages.assertNoMore();