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

False positive warning "Potential resource leak" inside "try with resources" #2642

Closed
iloveeclipse opened this issue Jun 26, 2024 · 4 comments · Fixed by #2718 or #2725
Closed

False positive warning "Potential resource leak" inside "try with resources" #2642

iloveeclipse opened this issue Jun 26, 2024 · 4 comments · Fixed by #2718 or #2725
Assignees
Labels
bug Something isn't working compiler Eclipse Java Compiler (ecj) related issues null Issues related to null pointer analysis regression Something was broken by a previous change
Milestone

Comments

@iloveeclipse
Copy link
Member

Given this example and enabled org.eclipse.jdt.core.compiler.problem.potentiallyUnclosedCloseable=warning option in .settings/org.eclipse.jdt.core.prefs

image

import java.io.FileWriter;
public class DemoNonCloseableWarning {
    public static void main(String[] args) throws Exception {
        try (FileWriter writer = new FileWriter("/dev/null")) {
            writer.append("\n");
        }
    }
}

we report false positive warning Potential resource leak: '<unassigned Closeable value>' may not be closed.

Of course this is option is not enabled by default, and of course it is potential, but here we have undoubtedly closed writer, so not idea where the "potential" warning is coming from.

@stephan-herrmann : FYI.

@iloveeclipse iloveeclipse added bug Something isn't working compiler Eclipse Java Compiler (ecj) related issues null Issues related to null pointer analysis regression Something was broken by a previous change labels Jun 26, 2024
@iloveeclipse
Copy link
Member Author

Fun fact: it worked at least in 3.8.2 Eclipse on Java 1.7 :-)

@stephan-herrmann stephan-herrmann self-assigned this Jun 26, 2024
@stephan-herrmann stephan-herrmann added this to the 4.33 M2 milestone Jun 26, 2024
@stephan-herrmann
Copy link
Contributor

stephan-herrmann commented Jun 26, 2024

This looks spooky on first glance:

----------
1. WARNING in /tmp/DemoNonCloseableWarning.java (at line 5)
        writer.append("\n");
        ^^^^^^^^^^^^^^^^^^^
Potential resource leak: '<unassigned Closeable value>' may not be closed
----------
1 problem (1 warning)

Not allocating the writer but calling append() triggers the warning. But wait: append returns a Writer (which could in theory be a fresh new resource).

We do have provision for this situation, see org.eclipse.jdt.internal.compiler.lookup.TypeConstants.FLUENT_RESOURCE_CLASSES. Something confuses the detection of this "fluent" method.

@iloveeclipse
Copy link
Member Author

But wait: append returns a Writer

OK, I see it now where AI hallucinates :-). Compiler assumes the return value has to be closed...

stephan-herrmann added a commit to stephan-herrmann/eclipse.jdt.core that referenced this issue Jul 16, 2024
resources"

Traverse supers to detect fluent methods

Test:
+ fine tune test230_sourcepath_vs_classpath to avoid superclass loading

Fixes eclipse-jdt#2642
@stephan-herrmann
Copy link
Contributor

OK, I see it now where AI hallucinates :-). Compiler assumes the return value has to be closed...

Don't worry: with AI in the picture any causalities between input program and warnings would be much more obscure.. ;-P

stephan-herrmann added a commit to stephan-herrmann/eclipse.jdt.core that referenced this issue Jul 18, 2024
resources"

fix regression on windows by avoiding test program execution

fixes eclipse-jdt#2642
stephan-herrmann added a commit that referenced this issue Jul 18, 2024
…ources" (#2725)

fix regression on windows by avoiding test program execution

fixes #2642
robstryker pushed a commit to robstryker/eclipse.jdt.core that referenced this issue Jul 18, 2024
…ources" (eclipse-jdt#2718)

Traverse supers to detect fluent methods

Test:
+ fine tune test230_sourcepath_vs_classpath to avoid superclass loading

Fixes eclipse-jdt#2642
gayanper pushed a commit to gayanper/eclipse.jdt.core that referenced this issue Sep 7, 2024
…ources" (eclipse-jdt#2718)

Traverse supers to detect fluent methods

Test:
+ fine tune test230_sourcepath_vs_classpath to avoid superclass loading

Fixes eclipse-jdt#2642
gayanper pushed a commit to gayanper/eclipse.jdt.core that referenced this issue Sep 7, 2024
…ources" (eclipse-jdt#2725)

fix regression on windows by avoiding test program execution

fixes eclipse-jdt#2642
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working compiler Eclipse Java Compiler (ecj) related issues null Issues related to null pointer analysis regression Something was broken by a previous change
Projects
None yet
2 participants