javarefactoringcheckstylecode-cleanupmaven-checkstyle-plugin

Checkstyle for unused member variables?


Need the checkstyle configuration for unused member variables.

There exists a checkstyle like below, but's just for local variables.

<module name="UnusedParameterCheck"/>

( Needed a similar check for member variables ) e.g.

class MyService {
    private String foo;
    private String unusedBar; // violation, should fail    
    public String getFoo() {         
        return foo;     
    } 
}

I am using the latest version of checkstyle

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-checkstyle-plugin</artifactId>
    <version>3.3.0</version>
    <executions>
        <execution>
            <id>checkstyle</id>
            <phase>compile</phase>
            <goals>
                <goal>check</goal>
            </goals>
            <configuration>
                <failOnViolation>true</failOnViolation>
                <configLocation>checkstyle.xml</configLocation>
                <consoleOutput>true</consoleOutput>
                <sourceDirectories>
                    <sourceDirectory>src</sourceDirectory>
                </sourceDirectories>
            </configuration>
        </execution>
    </executions>
</plugin>

Was not able to get a proper solution online!

Please help!

Tried searching online but did not find the solution!


Solution

  • There is no such check at this time (8/2023). A request was made at https://github.com/checkstyle/checkstyle/issues/12596 which is still looking for approval and implementation.