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

RedundantReturn: false positive in anonymous class #650

Closed
rnveach opened this Issue Mar 4, 2018 · 2 comments

Comments

Projects
None yet
1 participant
@rnveach
Copy link
Contributor

rnveach commented Mar 4, 2018

$ cat TestClass.java
public class TestClass {
    private static void writePropertiesFile(File inputFile, File outputFile)
            throws CheckstyleException {
        PrintWriter writer = null;
        try {
            writer = new PrintWriter(outputFile, StandardCharsets.UTF_8.name());

            final DetailAST top = JavaParser.parseFile(inputFile, JavaParser.Options.WITH_COMMENTS);
            final DetailAST objBlock = getClassBody(top);
            if (objBlock != null) {
                final PrintWriter writerEx = writer;

                iteratePublicStaticIntFields(objBlock, new Consumer<String>() {
                    @Override
                    public boolean accept(String s) {
                        writerEx.println(s);
                        return true; // violation
                    }
                });
            }
        }
        catch (IOException ex) {
            throw new CheckstyleException("Failed to write javadoc properties of '" + inputFile
                + "' to '" + outputFile + "'", ex);
        }
        finally {
            CommonUtils.close(writer);
        }
    }
}

$ cat TestConfig.xml
<?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">
    <property name="charset" value="UTF-8"/>

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

$ java -jar checkstyle-8.8-sevntu-1.28.0-all.jar -c TestConfig.xml TestClass.java
Starting audit...
[ERROR] TestClass.java:17: Redundant return. [RedundantReturn]
Audit done.
Checkstyle ends with 1 errors.

The line in question is not redundant and would result in non-compilable code if it was removed or changed to a single return.
This issue was found backporting JavadocPropertiesGenerator to Java 6 for checkstyle-backport-jre6 .

http://sevntu-checkstyle.github.io/sevntu.checkstyle/apidocs/com/github/sevntu/checkstyle/checks/coding/RedundantReturnCheck.html , redundant return is only return; all other forms of return is not redundant.

@rnveach

This comment has been minimized.

Copy link
Contributor

rnveach commented Mar 4, 2018

It seems it doesn't realize the separation of the 2 methods by the anonymous class.
It thinks it is still in the void method writePropertiesFile and not in the non-void method accept of Consumer.

@rnveach rnveach added the approved label Mar 4, 2018

rnveach added a commit to rnveach/sevntu.checkstyle that referenced this issue Jun 9, 2018

rnveach added a commit to rnveach/sevntu.checkstyle that referenced this issue Jun 9, 2018

rnveach added a commit to rnveach/sevntu.checkstyle that referenced this issue Jun 9, 2018

rnveach added a commit to rnveach/sevntu.checkstyle that referenced this issue Jun 12, 2018

rnveach added a commit that referenced this issue Jun 12, 2018

@rnveach

This comment has been minimized.

Copy link
Contributor

rnveach commented Jun 12, 2018

Fix was merged

@rnveach rnveach closed this Jun 12, 2018

@rnveach rnveach added this to the 1.30.0 milestone Jun 12, 2018

@rnveach rnveach added the bug label Jun 23, 2018

kariem added a commit to kariem/sevntu.checkstyle that referenced this issue Jul 26, 2018

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