Skip to content

False positive: EmptyLineSeparator reports violation on method call #18438

@Praveen7294

Description

@Praveen7294

reported at #18311 (comment)

I have read check documentation: https://checkstyle.org/checks/whitespace/emptylineseparator.html#EmptyLineSeparator
I have downloaded the latest checkstyle from: https://checkstyle.org/cmdline.html#Download_and_Run
I have executed the cli and showed it below, as cli describes the problem better than 1,000 words

EmptyLineSeparator reports violation on method call which wrong. It only occurs when there are more than one empty lines before a method call.

Config:

<?xml version="1.0"?>
<!DOCTYPE module PUBLIC "-//Puppy Crawl//DTD Check Configuration 1.3//EN"
   "http://www.puppycrawl.com/dtds/configuration_1_3.dtd">

<module name="Checker">
  <module name="TreeWalker">
    <module name="EmptyLineSeparator">
      <property name="tokens"
               value="PACKAGE_DEF, IMPORT, STATIC_IMPORT, CLASS_DEF, INTERFACE_DEF, ENUM_DEF,
                    STATIC_INIT, INSTANCE_INIT, METHOD_DEF, CTOR_DEF, VARIABLE_DEF, RECORD_DEF,
                    COMPACT_CTOR_DEF"/>
      <property name="allowNoEmptyLineBetweenFields" value="true"/>
      <property name="allowMultipleEmptyLines" value="false"/>
      <property name="allowMultipleEmptyLinesInsideClassMembers" value="false"/>
    </module>
  </module>
</module>

Test.java:

import org.junit.Test;                              // 1
import org.junit.Assert;                            // 2
                                                    // 3
public class Test {                                 // 4
                                                    // 5
  public void foo(int a) {}                         // 6
                                                    // 7
  @Test                                             // 8
  public void testFoo() {                           // 9
                                                    // 10
                                                    // 11
    int a = 10;                                     // 12
                                                    // 13
                                                    // 14
    Test t = new Test();                            // 15
                                                    // 16
                                                    // 17
    t.foo(10);                                      // 18
    int b = 20;                                     // 19
                                                    // 20
                                                    // 21
    Assert.assertFalse(false);                      // 22
  }                                                 // 23
}                                                   // 24

Actual Cli:

$ java -jar checkstyle-12.3.0-all.jar -c config_check.xml Test.java
Starting audit...
[ERROR] F:\GitHub\headhtmltagname\Test.java:9:25: There is more than 1 empty line after this line. [EmptyLineSeparator]
[ERROR] F:\GitHub\headhtmltagname\Test.java:12:15: There is more than 1 empty line after this line. [EmptyLineSeparator]
[ERROR] F:\GitHub\headhtmltagname\Test.java:18:6: There is more than 1 empty line after this line. [EmptyLineSeparator]
[ERROR] F:\GitHub\headhtmltagname\Test.java:22:11: There is more than 1 empty line after this line. [EmptyLineSeparator]
Audit done.
Checkstyle ends with 4 errors.

According to the reported violation, lines 18 and 22 have more than one empty line after them, but there are no empty lines after these lines.

Expected Cli:

$ java -jar checkstyle-12.3.0-all.jar -c config_check.xml Test.java
Starting audit...
[ERROR] F:\GitHub\headhtmltagname\Test.java:9:25: There is more than 1 empty line after this line. [EmptyLineSeparator]
[ERROR] F:\GitHub\headhtmltagname\Test.java:12:15: There is more than 1 empty line after this line. [EmptyLineSeparator]
[ERROR] F:\GitHub\headhtmltagname\Test.java:15:6: There is more than 1 empty line after this line. [EmptyLineSeparator]
[ERROR] F:\GitHub\headhtmltagname\Test.java:19:11: There is more than 1 empty line after this line. [EmptyLineSeparator]
Audit done.
Checkstyle ends with 4 errors.

In my opinion, it might be incorrect line referencing.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions