sevntu-checkstyle / sevntu.checkstyle

Additional Checkstyle checks, that could be added as extension to EclipseCS plugin and maven-checkstyle-plugin, Sonar checkstyle plugin, extension for CheckStyle IDEA plugin.
http://sevntu-checkstyle.github.io/sevntu.checkstyle/
190 stars 147 forks source link

UselessSuperCtorCall: false positive on constructor that throws exception #955

Closed rnveach closed 1 year ago

rnveach commented 1 year ago
$ cat TestClass.java
public class TestClass extends Base {
  public TestClass() throws Exception {
    super();
  }
}

class Base {
  public Base() throws Exception {
  }
}

$ cat TestConfig.xml
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
          "-//Puppy Crawl//DTD Check Configuration 1.3//EN"
          "https://checkstyle.org/dtds/configuration_1_3.dtd">

<module name="Checker">
    <property name="charset" value="UTF-8"/>

    <module name="TreeWalker">
<module name="com.github.sevntu.checkstyle.checks.coding.UselessSuperCtorCallCheck" />
    </module>
</module>

$ java -jar checkstyle-10.0-sevntu-1.42.0-all.jar -c TestConfig.xml TestClass.java
Starting audit...
[ERROR] TestClass.java:3:17: Redundant super constructor call could be removed. Class 'Test' has superclass. Java compiler automatically inserts a call to the no-argument constructor of the superclass. [UselessSuperCtorCall]
Audit done.
Checkstyle ends with 1 errors.

Eclipse complains with Default constructor cannot handle exception type Exception thrown by implicit super constructor. Must define an explicit constructor if I don't add Test constructor.

romani commented 1 year ago

What about jdk compiler output on no super ?

rnveach commented 1 year ago

Removing default constructor results in:

$ javac --version
javac 11.0.17

$ javac TestClass.java
TestClass.java:1: error: unreported exception Exception in default constructor
public class TestClass
       ^
1 error
romani commented 1 year ago

Is it enough for check to detect "throws" ? To skip violation

rnveach commented 1 year ago

I am closing this. I thought it was reporting the constructor, but its just reporting the super() call. It has been determined it can be removed and the violation goes away. My bad.

$ cat TestClass.java
public class Test extends Base {
public Test() throws Exception {
}
}

class Base {
public Base() {
}
}

$ cat TestConfig.xml
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
          "-//Puppy Crawl//DTD Check Configuration 1.3//EN"
          "https://checkstyle.org/dtds/configuration_1_3.dtd">

<module name="Checker">
    <property name="charset" value="UTF-8"/>

    <module name="TreeWalker">
<module name="com.github.sevntu.checkstyle.checks.coding.UselessSuperCtorCallCheck" />
    </module>
</module>

$ java -jar checkstyle-10.0-sevntu-1.42.0-all.jar -c TestConfig.xml TestClass.java
Starting audit...
Audit done.