Skip to content

Commit

Permalink
Issue checkstyle#15256: Fixed location of violation comments
Browse files Browse the repository at this point in the history
  • Loading branch information
SteLeo1602 authored and romani committed Sep 8, 2024
1 parent ecd4670 commit a66c329
Show file tree
Hide file tree
Showing 23 changed files with 143 additions and 141 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public final class InlineConfigParser {

/** A pattern to find the string: "// violation, explanation". */
private static final Pattern VIOLATION_WITH_EXPLANATION_PATTERN = Pattern
.compile(".*//\\s*violation,\\s.+\\s(?:['\"](.*)['\"])?$");
.compile(".*//\\s*violation,\\s+(?:.*)?$");

/** A pattern to find the string: "// X violations". */
private static final Pattern MULTIPLE_VIOLATIONS_PATTERN = Pattern
Expand Down Expand Up @@ -175,6 +175,10 @@ public final class InlineConfigParser {
private static final Pattern VIOLATIONS_SOME_LINES_BELOW_PATTERN = Pattern
.compile(".*//\\s*(\\d+) violations (\\d+) lines below:$");

/** A pattern that matches any comment by default. */
private static final Pattern VIOLATION_DEFAULT = Pattern
.compile("//.*violation.*");

/** The String "(null)". */
private static final String NULL_STRING = "(null)";

Expand Down Expand Up @@ -764,6 +768,8 @@ private static void setViolations(TestInputConfiguration.Builder inputConfigBuil
VIOLATIONS_SOME_LINES_ABOVE_PATTERN.matcher(lines.get(lineNo));
final Matcher violationsSomeLinesBelowMatcher =
VIOLATIONS_SOME_LINES_BELOW_PATTERN.matcher(lines.get(lineNo));
final Matcher violationsDefault =
VIOLATION_DEFAULT.matcher(lines.get(lineNo));
if (violationMatcher.matches()) {
final String violationMessage = violationMatcher.group(1);
final int violationLineNum = lineNo + 1;
Expand Down Expand Up @@ -796,11 +802,8 @@ else if (violationBelowWithExplanationMatcher.matches()) {
inputConfigBuilder.addViolation(violationLineNum, violationMessage);
}
else if (violationWithExplanationMatcher.matches()) {
final String violationMessage = violationWithExplanationMatcher.group(1);
final int violationLineNum = lineNo + 1;
checkWhetherViolationSpecified(specifyViolationMessage, violationMessage,
violationLineNum);
inputConfigBuilder.addViolation(violationLineNum, violationMessage);
inputConfigBuilder.addViolation(violationLineNum, null);
}
else if (violationSomeLinesAboveMatcher.matches()) {
final String violationMessage = violationSomeLinesAboveMatcher.group(2);
Expand Down Expand Up @@ -859,6 +862,10 @@ else if (useFilteredViolations) {
setFilteredViolation(inputConfigBuilder, lineNo + 1,
lines.get(lineNo), specifyViolationMessage);
}
else if (violationsDefault.matches()) {
final int violationLineNum = lineNo + 1;
inputConfigBuilder.addViolation(violationLineNum, null);
}
}

private static List<TestInputViolation> getExpectedViolationsForSpecificLine(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ public void testHonorsTokenProperty() throws Exception {
@Test
public void testWildcard() throws Exception {
final String[] expected = {
"24:1: " + getCheckMessage(MSG_ORDERING, "javax.crypto.Cipher"),
"21:1: " + getCheckMessage(MSG_ORDERING, "javax.crypto.Cipher"),
};

verifyWithInlineConfigParser(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ public boolean equals(String o) {
return true;
}
}
public static class TestClass2 { // violation, no correct `equals`
public static class TestClass2 {
public int hashCode() { // violation 'without .* of 'equals()'.'
return 1;
}
public boolean equals(String o) {
return true;
}
}
public static class TestClass3 { // violation, no `hashCode`
public static class TestClass3 {
public boolean equals(Object o) { // violation 'without .* of 'hashCode()'.'
return true;
}
Expand All @@ -47,15 +47,15 @@ public boolean equals(java.lang.Object o) {
return true;
}
}
public static class TestClass6 { // violation, no `hashCode` implementation
public static class TestClass6 {
public static int hashCode(int i) {
return 1;
}
public boolean equals(Object o) { // violation 'without .* of 'hashCode()'.'
return true;
}
}
public static class TestClass7 { // violation, no `equals` implementation
public static class TestClass7 {
public int hashCode() { // violation 'without .* of 'equals()'.'
return 1;
}
Expand All @@ -67,23 +67,23 @@ public static class TestClass8 {
public native int hashCode();
public native boolean equals(Object o);
}
public static class TestClass9 { // violation, no `equals` implementation
public static class TestClass9 {
public native int hashCode(); // violation 'without .* of 'equals()'.'
}
public static class TestClass10 { // violation, no `hashCode` implementation
public static class TestClass10 {
public native boolean equals(Object o); // violation 'without .* of 'hashCode()'.'
}
public static abstract class TestClass11 {
public abstract int hashCode();
public abstract boolean equals(Object o);
}
public static abstract class TestClass12 { // violation, no `equals` implementation
public static abstract class TestClass12 {
public int hashCode() { // violation 'without .* of 'equals()'.'
return 1;
}
public abstract boolean equals(Object o);
}
public static abstract class TestClass13 { // violation, no `hashCode` implementation
public static abstract class TestClass13 {
public abstract int hashCode();
public boolean equals(java.lang.Object o) { // violation 'without .* of 'hashCode()'.'
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
*/

package com.puppycrawl.tools.checkstyle.checks.imports.importorder;
// groups are configured as follows
// com.puppycrawl,*,java
// the trailing javax.crypto.Cipher; should be flagged as a violation.

import javax.crypto.BadPaddingException;
import java.util.List; //comment test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
WriteTag
tag = @author
tagFormat = \\S
tagSeverity = (default)info
tagSeverity = error
tokens = (default)INTERFACE_DEF, CLASS_DEF, ENUM_DEF, ANNOTATION_DEF, RECORD_DEF
*/

package com.puppycrawl.tools.checkstyle.checks.javadoc.writetag;
// violation 3 lines below , 'param @author must match pattern '\\S'.*'
// violation 3 lines below 'Javadoc tag @author=Daniel Grenner'
/**
* Testing tag writing
* @author Daniel Grenner
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
WriteTag
tag = @doubletag
tagFormat = \\S
tagSeverity = (default)info
tagSeverity = error
tokens = (default)INTERFACE_DEF, CLASS_DEF, ENUM_DEF, ANNOTATION_DEF, RECORD_DEF
*/

package com.puppycrawl.tools.checkstyle.checks.javadoc.writetag;
// violation 6 lines below , '@doubletag appeared at the same time.*'
// violation 6 lines below , '@doubletag appeared at the same time.*'
// violation 6 lines below 'Javadoc tag @doubletag=first text'
// violation 6 lines below 'Javadoc tag @doubletag=second text'
/**
* Testing tag writing
* @author Daniel Grenner
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
WriteTag
tag = @incomplete
tagFormat = \\S
tagSeverity = (default)info
tagSeverity = error
tokens = (default)INTERFACE_DEF, CLASS_DEF, ENUM_DEF, ANNOTATION_DEF, RECORD_DEF
*/

package com.puppycrawl.tools.checkstyle.checks.javadoc.writetag;
// violation 4 lines below , 'Must contain non-whitespace characters.*'
// violation 4 lines below 'Javadoc tag @incomplete=This class needs more code...'
/**
* Testing tag writing
* @author Daniel Grenner
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
WriteTag
tag = @author
tagFormat = (default)null
tagSeverity = (default)info
tagSeverity = error
tokens = (default)INTERFACE_DEF, CLASS_DEF, ENUM_DEF, ANNOTATION_DEF, RECORD_DEF
*/

package com.puppycrawl.tools.checkstyle.checks.javadoc.writetag;
// violation 3 lines below , 'Missing @author tag.*'
// violation 3 lines below 'Javadoc tag @author=Daniel Grenner'
/**
* Testing tag writing
* @author Daniel Grenner
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
WriteTag
tag = @author
tagFormat = \\S
severity = ignore
tagSeverity = error
tokens = (default)INTERFACE_DEF, CLASS_DEF, ENUM_DEF, ANNOTATION_DEF, RECORD_DEF
*/

package com.puppycrawl.tools.checkstyle.checks.javadoc.writetag;

// violation 3 lines below , 'Must contain non-whitespace characters.*'
// violation 3 lines below 'Javadoc tag @author=Daniel Grenner'
/**
* Testing tag writing
* @author Daniel Grenner
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
*/
// violation 11 lines above 'Required pattern .* missing in file.'
// location of violation is first line of file
package com.puppycrawl.tools.checkstyle.checks.regexp.regexp;

import java.awt.*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*/
// violation 11 lines above 'Required pattern .* missing'
// violation due to ignoreComments=true
// due to ignoreComments=true
package com.puppycrawl.tools.checkstyle.checks.regexp.regexp;

public class InputRegexpTrailingComment11 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*/
public class InputRegexpMultilineMultilineSupport {
void method() { // violation below
// abc - violation
// abc
// def
// abc
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
* matchAcrossLines = true
*/
public class InputRegexpMultilineMultilineSupport2 {
void method() { // violation below
// abc - violation
// def // violation below
// abc - violation
void method() {
// abc // violation
// def
// abc // violation
}

void method2() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@

package com.puppycrawl.tools.checkstyle.checks.whitespace.emptylineseparator; //no violation: trailing comment
import java.io.Serializable; // violation ''import' should be separated from previous line.'
import java.util.ArrayList; /*no violation: trailing comment*/
import java.util.ArrayList; /*ok: trailing comment*/
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.Collections;
/* no violation: block comment after token*/
/* ok: block comment after token*/

import java.io.PrintWriter;
//no violation: single-line comment after token
//ok: single-line comment after token

import javax.swing.AbstractAction; /* no violation: no trailing comment
import javax.swing.AbstractAction; /* ok: no trailing comment
*/

import org.apache.commons.beanutils.locale.converters.ByteLocaleConverter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@
*/

package com.puppycrawl.tools.checkstyle.checks.whitespace.emptylineseparator; //no violation: trailing comment
package com.puppycrawl.tools.checkstyle.checks.whitespace.emptylineseparator; //ok: trailing comment
import java.io.Serializable; // violation ''import' should be separated from previous line.'
import java.util.ArrayList; /*no violation: trailing comment*/
import java.util.ArrayList; /*ok: trailing comment*/
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.Collections;
/* no violation: block comment after token*/
/* ok: block comment after token*/

import java.io.PrintWriter;
//no violation: single-line comment after token
//ok: single-line comment after token

import javax.swing.AbstractAction; /* no violation: no trailing comment
import javax.swing.AbstractAction; /* ok: no trailing comment
*/

import org.apache.commons.beanutils.locale.converters.ByteLocaleConverter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import java.lang.Class; // no violation

// no violation
// ok below
import java.lang.Long;

/*
Expand All @@ -31,12 +31,12 @@
import java.lang.Object;

// .
// no violation
// ok below
import java.lang.Boolean;
// no violation
// ok below
import java.lang.Byte;

// no violation
// ok below
/* no violation */
import java.lang.Short;
import java.lang.Number;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

package com.puppycrawl.tools.checkstyle.checks.whitespace.emptylineseparator; // trailing comment is allowed.
// violation ''//' should be separated from previous line.'
// violation at line above

import java.util.Map;

Expand Down
Loading

0 comments on commit a66c329

Please sign in to comment.