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 17 - False positive SA_LOCAL_SELF_COMPARISON when using instanceof pattern matching #1992

Open
SchubP opened this issue Mar 22, 2022 · 6 comments

Comments

@SchubP
Copy link

SchubP commented Mar 22, 2022

Hi,

there seems to still be an issue with the problem stated in the title.
It was taken care of way back but is now (again?) relevant. Back then there were PRs to test/fix it: #1136 and #1435

We are using:

There are serveral other similar issues and comments here mentioning this sitiuation:

Is it maybe possible, that this check/exclusion only works for Java 14 but not 15, 16, 17, ...?
For now we will probaby disable these checks.

Many thanks in advance,
Cheers, Paul

@bsutton
Copy link

bsutton commented Apr 2, 2022

I'm seeing the same issue with jdk 17:

{
	public List<VersionRange> _rangesFor(VersionConstraint constraint) throws ArgumentException
	{
		if (constraint.isEmpty())
			return new ArrayList<VersionRange>();
		if (constraint instanceof VersionUnion union) // this line generates the false positive.
			return union.ranges;
		throw new ArgumentException("Unknown VersionConstraint type $constraint.");
	}

VersionUnion.java:281 Self comparison of constraint with itself
VersionUnion._rangesFor(VersionConstraint) [Scariest(1), High confidence]

@famod
Copy link

famod commented Apr 21, 2022

I'm also seeing this, but only when using ECJ (Eclipse compiler) via maven-compiler-plugin.
There is no such issue when using maven-compiler-plugin with forceJavacCompilerUse.

@SchubP
Copy link
Author

SchubP commented May 3, 2022

This issue is probably also related: #1944
What @famod pointed out seems interesting: all related to Eclipse/ECJ/Tycho ...

@Martin-Schwenk
Copy link

When compiling the following example

public static void main(String[] args) {
    Number n = Integer.valueOf("0");
    if (n instanceof Integer i) {
        System.out.println("int " + i);
    }
}

with ejc (and then decompiling with javap) I get

         0: ldc           #16                 // String 0
         2: invokestatic  #18                 // Method java/lang/Integer.valueOf:(Ljava/lang/String;)Ljava/lang/Integer;
         5: astore_1
         6: aload_1
         7: astore_3
         8: aload_3
         9: instanceof    #19                 // class java/lang/Integer
        12: ifeq          50
        15: aload_3
        16: checkcast     #19                 // class java/lang/Integer
        19: dup
        20: astore_2
        21: aload_3
        22: checkcast     #19                 // class java/lang/Integer
        25: if_acmpne     50
        28: getstatic     #24                 // Field java/lang/System.out:Ljava/io/PrintStream;
...
        50: return

which indeed has a reference check ("if_acmpne") that compares two reference that are always equal. The duplicate "checkcast"-pattern seeems like a bug in ejc to me.
If I compile with javac, I get

         0: ldc           #7                  // String 0
         2: invokestatic  #9                  // Method java/lang/Integer.valueOf:(Ljava/lang/String;)Ljava/lang/Integer;
         5: astore_1
         6: aload_1
         7: instanceof    #10                 // class java/lang/Integer
        10: ifeq          30
        13: aload_1
        14: checkcast     #10                 // class java/lang/Integer
        17: astore_2
        18: getstatic     #15                 // Field java/lang/System.out:Ljava/io/PrintStream;
...
        30: return

which is what I'd expect and which shouldn't trigger the spotbugs warning.
Tested with eclipse 4.24.0 and openjdk 17.0.3.

@trancexpress
Copy link
Contributor

Another bug in ecj with this snippet, not sure if there already is a spotbugs issue for it:

public class InstanceOfPatternTest {
	static String STR = "2";
	public static void main(String[] args) {
		if ( switch(STR) {
				default -> "zero";
			  }
				instanceof String s) {
			System.out.println(s);
		}
	}
}
Bug: Return value of String.hashCode() ignored in test.InstanceOfPatternTest.main(String[])

(at if ( switch(STR) {)

fniephaus added a commit to hpi-swa/trufflesqueak that referenced this issue May 4, 2023
fniephaus added a commit to hpi-swa/trufflesqueak that referenced this issue May 10, 2023
fniephaus added a commit to hpi-swa/trufflesqueak that referenced this issue May 15, 2023
wborn added a commit to wborn/openhab-webui that referenced this issue Jan 8, 2024
This cleanup includes:

* Fix deprecations
* Fix JavaDocs
* Remove redundant toString calls
* Remove redundant semicolons
* Simplify boolean expressions
* Use diamond operator
* Use enhanced for loops
* Use instanceof pattern matching
* Use isEmpty instead of 0 comparisons
* Use lambdas
* Use static inner classes
* Use StandardCharsets

Also adds the SA_LOCAL_SELF_COMPARISON suppression similar as used in other repositories for spotbugs/spotbugs#1992.

Signed-off-by: Wouter Born <github@maindrain.net>
kaikreuzer pushed a commit to openhab/openhab-webui that referenced this issue Jan 17, 2024
This cleanup includes:

* Fix deprecations
* Fix JavaDocs
* Remove redundant toString calls
* Remove redundant semicolons
* Simplify boolean expressions
* Use diamond operator
* Use enhanced for loops
* Use instanceof pattern matching
* Use isEmpty instead of 0 comparisons
* Use lambdas
* Use static inner classes
* Use StandardCharsets

Also adds the SA_LOCAL_SELF_COMPARISON suppression similar as used in
other repositories for spotbugs/spotbugs#1992.

Signed-off-by: Wouter Born <github@maindrain.net>
@gtoison
Copy link
Contributor

gtoison commented Feb 12, 2024

So this seems to be an issue with ECJ as described here: eclipse-jdt/eclipse.jdt.core#1406
There are improvements in eclipse-jdt/eclipse.jdt.core#2000 and eclipse-jdt/eclipse.jdt.core#300 and this should fix the issue
In case you still have the issue with another compiler (e.g. javac) please let us know

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants