S2386 possible false positive

Hello, I have stumbled upon something which I believe is a S2386 false positive. Consider the following code:

public final class TestS2386 {

	public static final List<String> LIST1 = Collections.unmodifiableList(create("a"));

	public static final List<String> LIST2 = createUnmodifiable("a"); // S2386 reported here

	private TestS2386() {
	}

	private static List<String> create(String value) {
		return new ArrayList<>(Arrays.asList(value));
	}

	private static List<String> createUnmodifiable(String value) {
		return Collections.unmodifiableList(create(value));
	}

}

It declares two public static final fields holding unmodifiable containers, however, for one of them, S2386 is reported (I have marked it by a comment). I don’t see any difference between the two approaches: the combine method first creates a mutable container, which is then wrapped by Collections.unmodifiableList directly (LIST1) and indirectly (LIST2), so I see both approaches as equivalent.

I can reproduce this using Eclipse 20.09 and SonarLint 5.6.0.25634.

Hi Tomas,
I created the ticket SONARJAVA-3706 to reduce a little bit the number of false-positives.
Thanks for your feedback.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.