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

[java] UseTryWithResources false positive when AutoCloseable helper used #2650

Closed
amarkevich opened this issue Jul 16, 2020 · 4 comments · Fixed by #3181
Closed

[java] UseTryWithResources false positive when AutoCloseable helper used #2650

amarkevich opened this issue Jul 16, 2020 · 4 comments · Fixed by #3181
Labels
a:false-positive PMD flags a piece of code that is not problematic
Milestone

Comments

@amarkevich
Copy link

amarkevich commented Jul 16, 2020

Affects PMD Version:

maven-pmd-plugin 3.13.0 (pmd 6.21.0)

Rule: UseTryWithResources

Description:

PMD Failure: UseTryWithResources when resource is not AutoCloseable and closed via external class which implement AutoCloseable

Code Sample demonstrating the issue:

public class PMDUseTryWithResources {

    static class ResourceCloser implements AutoCloseable {
        public static void close(NotAutoCloseable notAutoCloseable) {
            notAutoCloseable.close();
        }

        @Override
        public void close() throws Exception {
            // nothing
        }
    }

    interface NotAutoCloseable {
        void close();
    }

    public void useTryWithResources() {
        NotAutoCloseable notAutoCloseable = null;
        try {
            notAutoCloseable = new NotAutoCloseable() {
                @Override
                public void close() {
                }
            };
        } finally {
            if (notAutoCloseable != null) {
                ResourceCloser.close(notAutoCloseable);
            }
        }
    }
}

Steps to reproduce:

  1. mvn install

Running PMD through: Maven

@amarkevich amarkevich added the a:bug PMD crashes or fails to analyse a file. label Jul 16, 2020
@adangel adangel changed the title False positive: UseTryWithResources when AutoCloseable helper used [java] UseTryWithResources false positive when AutoCloseable helper used Aug 23, 2020
@adangel
Copy link
Member

adangel commented Aug 23, 2020

Maybe the problem is, that your helper class ResourceCloser implements unnecessarily AutoClosable.

The rule searches for any method call with the name close, regardless whether it's static or not. So it finds ResourceCloser.close(notAutoCloseable);and tells you, you should use try-with-resources for ResourceCloser...

@amarkevich
Copy link
Author

Actually AutoClosable logic is used for another case.
Expected behavior: do not report a problem when static 'close' method used

@adangel
Copy link
Member

adangel commented Sep 12, 2020

Expected behavior: do not report a problem when static 'close' method used

I actually disagree - if the thing, that is being closed, is AutoCloseable, then one should use try-with-resources. That's the point. If you use IOUtils.closeQuietly(resource), the rule should still trigger, even though the close method is static.

We have two cases:

  1. Non-static close methods: resource.close() - if resource is of type AutoCloseable, then the rule should trigger. In that case, there are no arguments to the close method. If the resource is not a autocloseable resource, then the close call should be ignored.

  2. Static close (helper) methods: ResourceClose.close(resource). In that case, it doesn't matter, whether ResourceClose is AutoCloseable or not. What matters is only, whether the (first) argument is AutoCloseable - in that case "resource". If the resource is AutoCloseable, then the rule should trigger. If it isn't, then the close call should be ignored.

Currently, the rule always checks both for the type "AutoCloseable" - the thing, on which the close method is called (might be the resource or in static case the helper class) and also (if present) the argument.

The rule should probably be changed to check the argument - if present (and assume, it's a static method call) and otherwise check the method call target.

The rule is a XPath rule:

//TryStatement[FinallyStatement//Name[
tokenize(@Image, '\.')[last()] = $closeMethods
][
pmd-java:typeIs('java.lang.AutoCloseable')
or
../../PrimarySuffix/Arguments//PrimaryPrefix[pmd-java:typeIs('java.lang.AutoCloseable')]
]]

The type check in line 1842 should probably only be done, if there are no arguments.

@adangel adangel added a:false-positive PMD flags a piece of code that is not problematic help-wanted and removed a:bug PMD crashes or fails to analyse a file. labels Sep 12, 2020
oowekyala added a commit to oowekyala/pmd that referenced this issue Mar 29, 2021
oowekyala added a commit to oowekyala/pmd that referenced this issue Mar 29, 2021
- Test case for pmd#2882
- Test case for pmd#2650
- Update ci file
- Update release notes
@adangel adangel linked a pull request Apr 10, 2021 that will close this issue
4 tasks
@adangel adangel added this to the 7.0.0 milestone Apr 10, 2021
@adangel adangel mentioned this issue Jan 23, 2023
55 tasks
@adangel
Copy link
Member

adangel commented Apr 22, 2023

This has been fixed with PMD 7.0.0-rc1.

@adangel adangel closed this as completed Apr 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a:false-positive PMD flags a piece of code that is not problematic
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants