Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MethodParamPad ignores explicit constructor invocation #13675

Closed
nrmancuso opened this issue Sep 1, 2023 · 1 comment · Fixed by #14893
Closed

MethodParamPad ignores explicit constructor invocation #13675

nrmancuso opened this issue Sep 1, 2023 · 1 comment · Fixed by #14893

Comments

@nrmancuso
Copy link
Member

nrmancuso commented Sep 1, 2023

I have read check documentation: https://checkstyle.sourceforge.io/checks/whitespace/methodparampad.html#MethodParamPad
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

➜  src cat config.xml 
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
        "-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
        "https://checkstyle.org/dtds/configuration_1_3.dtd">
<module name="Checker">
  <property name="severity" value="error"/>
  <property name="fileExtensions" value="java, properties, xml"/>
  <module name="TreeWalker">
    <module name="MethodParamPad">
      <property name="option" value="space"/>
    </module>
  </module>
</module>

➜  src cat -n Test.java
     1  public class Test {
     2      public Test () {} // ok, whitespace between id and ()
     3  
     4      public Test (int x) {
     5          this (); // ok, 'this' seems to be ignored
     6          toString (); // ok, whitespace between id and ()
     7      }
     8      public Test (int x, int y) {
     9          this(x + y); // should be a violation
    10          toString(); // violation
    11      }
    12  
    13      public Test (int x, int y, int z) {
    14          super(); // violation, super constructor call is checked
    15          toString(); // violation
    16      }
    17  
    18      public Test (int x, int y, int z, int a) {
    19          super (); // ok, whitespace between 'super' and ()
    20          toString(); // violation
    21      }
    22  }

➜  src javac Test.java 

➜  src java -jar checkstyle-10.12.3-all.jar -c config.xml Test.java
Starting audit...
[ERROR] /home/nick/IdeaProjects/tester/src/Test.java:10:17: '(' is not preceded with whitespace. [MethodParamPad]
[ERROR] /home/nick/IdeaProjects/tester/src/Test.java:14:14: '(' is not preceded with whitespace. [MethodParamPad]
[ERROR] /home/nick/IdeaProjects/tester/src/Test.java:15:17: '(' is not preceded with whitespace. [MethodParamPad]
[ERROR] /home/nick/IdeaProjects/tester/src/Test.java:20:17: '(' is not preceded with whitespace. [MethodParamPad]
Audit done.
Checkstyle ends with 4 errors.

I would expect Checkstyle to show a violation for the following:

this(x + y);

Example:

➜  src java -jar checkstyle-10.12.3-all.jar -c config.xml Test.java
Starting audit...
[ERROR] /home/nick/IdeaProjects/tester/src/Test.java:9:13: '(' is not preceded with whitespace. [MethodParamPad]
[ERROR] /home/nick/IdeaProjects/tester/src/Test.java:10:17: '(' is not preceded with whitespace. [MethodParamPad]
[ERROR] /home/nick/IdeaProjects/tester/src/Test.java:14:14: '(' is not preceded with whitespace. [MethodParamPad]
[ERROR] /home/nick/IdeaProjects/tester/src/Test.java:15:17: '(' is not preceded with whitespace. [MethodParamPad]
[ERROR] /home/nick/IdeaProjects/tester/src/Test.java:20:17: '(' is not preceded with whitespace. [MethodParamPad]
Audit done.
Checkstyle ends with 4 errors.

The TLDR; is that if this check validates super constructor invocations, it should also validate explicit constructor invocations.

@strkkk
Copy link
Member

strkkk commented May 23, 2024

issue looks valid, I'll approve it and create PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants