Bug 565205 - Bogus Warning: Potential resource leak
Summary: Bogus Warning: Potential resource leak
Status: CLOSED INVALID
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 4.16   Edit
Hardware: PC Windows 10
: P3 normal (vote)
Target Milestone: 4.24   Edit
Assignee: JDT-Core-Inbox CLA
QA Contact: Manoj N Palat CLA
URL:
Whiteboard:
Keywords: helpwanted
Depends on:
Blocks:
 
Reported: 2020-07-14 08:32 EDT by Clovis Seragiotto CLA
Modified: 2023-10-05 03:03 EDT (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Clovis Seragiotto CLA 2020-07-14 08:32:34 EDT
class Foo implements AutoCloseable {
    static void foo(Foo foo) { foo.method(); } // warning here
    static void bar(java.io.PrintWriter pw) { pw.append('x'); } // no warning here, even though append returns this
    private Foo method() { return this; }
    @Override public void close() { /**/ }
}

Invoking a method on a resource may generate a warning or not if the method returns a reference to the resource itself. It depends.

In the example above, the compiler generates no warning for the class java.io.PrintWriter but generates a warning for the class Foo.

Which behavior is "correct" might be a matter of taste (I'd prefer no warning in this case), but both cannot be correct simultaneously.
Comment 1 Amit Munwes CLA 2020-08-04 12:48:02 EDT
This affects many "builder" classes which are closable and always return "this" to allow method chaining.
The following line produces 3 separate warnings (!) :
builder.startObject().value("hello").endObject();
Comment 2 Manoj N Palat CLA 2021-02-10 08:34:11 EST
bulk move out of 4.19 - flagging as helpwanted but keeping in radar for 4.20
Comment 3 Manoj N Palat CLA 2021-05-21 00:09:49 EDT
Bulk move out of 4.20
Comment 4 Manoj N Palat CLA 2021-08-18 20:27:48 EDT
Bulk Move out of 4.21
Comment 5 Manoj N Palat CLA 2021-08-18 20:28:31 EDT
Bulk Move out of 4.21
Comment 6 Jay Arthanareeswaran CLA 2021-11-19 01:21:40 EST
Bulk move out of 4.22.

If this is going to make it to 4.22, feel free to set the target back.
Comment 7 Manoj N Palat CLA 2022-02-16 05:09:48 EST
Bulk move out of 4.23
Comment 8 Manoj N Palat CLA 2022-02-16 05:10:09 EST
Bulk move out of 4.23
Comment 9 Manoj N Palat CLA 2022-02-16 05:10:18 EST
Bulk move out of 4.23
Comment 10 Manoj N Palat CLA 2022-02-16 05:11:14 EST
Bulk move out of 4.23