eclipse-jdt / eclipse-jdt/eclipse.jdt.core

Spurious Resource Leak Warning When Resource Placed in Local Map

Open
#5,362 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
237
Forks
195
Avg merge
1d 12h
Merged PRs (30d)
47

Description

Using Eclipse 2025-12 (build 20251204-0850)

Settings:
Potential resource leak: Warning
Resource not managed with ownership annotations as recommended: Warning

When a try-with-resource instance is stored in a map with local scope within the try block a spurious `Resource leak: '' is never closed` warning is raised.

This does not happen for other collections - such as arrays and lists.

```
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.jar.JarFile;

import org.junit.Test;

public class TryWithResourceWhenStored {

// This is OK - no warning here
@SuppressWarnings("static-method")
@Test
public void testJars() throws IOException {

try (JarFile jar_file = new JarFile("logback-core-1.3.14.jar")) {
assertNotNull(jar_file);
}
}


// This is OK - no warning here
@SuppressWarnings("static-method")
@Test
public void testJarsInArray() throws IOException {

try (JarFile jar_file = new JarFile("logback-core-1.3.14.jar")) {

JarFile class_path_jars[] = {jar_file};
assertEquals(1, class_path_jars.length);
}
}

// This is OK - no warning here
@SuppressWarnings("static-method")
@Test
public void testJarsInList() throws IOException {

try (JarFile jar_file = new JarFile("logback-core-1.3.14.jar")) {

List class_path_jars = new ArrayList<>();

class_path_jars.add(jar_file);
assertEquals(1, class_path_jars.size());
}
}

// But this gives a warning
@SuppressWarnings("static-method")
@Test
public void testJarsInMap() throws IOException {

try (JarFile jar_file = new JarFile("logback-core-1.3.14.jar")) {

Map class_path_jars = new HashMap<>();

// Spurious warning raised "Resource leak: '' is never closed".
class_path_jars.put("slf4j-api-2.0.9.jar", jar_file);
assertEquals(1, class_path_jars.size());
}
}
}
```

Contributor guide

Open the contributing guide

Research direction

Reproduce the warning in Eclipse 2025-12 using the testJarsInMap example, then compare its analysis with testJars, testJarsInArray, and testJarsInList. Trace the resource-leak analysis for the Map.put call; done means the map case no longer reports the spurious unassigned Closeable warning while the other diagnostics remain correct.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
70/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.