Suppress a warning when using instance of with pattern matching and not sub type

INFO SonarScanner CLI 6.1.0.4477
INFO Java 21.0.5 Amazon.com Inc. (64-bit)
INFO Windows 10 10.0 amd64

Qube Server 9.9.0.65466

We have the following java code:

if ( x.getValue() instanceof Foo foo )
    this.foo = foo;

The method getValue() returns a Foo. So this is basically just a null check with pattern matching, which Java currently lacks. Though it’s still a valid Java construct.

In Debug mode the following warning is generated for this.

WARN  Use of preview features have been detected during analysis. Enable DEBUG mode to see them.
DEBUG Use of preview features:
- Expression type cannot be a subtype of the Pattern type

I do understand, that warnings can generally be ignored. Though some other warnings like when a jar is missing for the scanner MUST be looked at. It’s a bit odd to write parser code for the scanner output, that ignores certain warnings and others not.

This definitely is not a preview feature. It’s a valid Java construct, hence there should be no warning at all.

Yes, there could be a Qube/Lint rule to detect that, because it could be seen as misusing instanceof pattern matching. But that’s a different story.

Hi,

SonarQube 9.9 was released in February 2023. Perhaps it was a preview feature then? :innocent:

 
Ann

Using the latest Scanner this warning is gone.

Thanks and sorry for the false alarm.

1 Like