[java:s2259] FP for sealed code

Sonar Qube: Community Build v25.7.0.110598
Lint: 11.20.0.84274
Java: 25
Eclipse: 2025-09

The Java rule S2259 raises an FP, if the switched class is sealed.

Here is a code example fresh from ChatGPT:

// Stable since Java 17
public sealed class Shape permits Circle, Rectangle
{
    public double area()
    {
        return 0.0;
    }
}

final class Circle extends Shape
{
    private final double radius;

    public Circle(double radius)
    {
        this.radius = radius;
    }

    @Override
    public double area()
    {
        return Math.PI * radius * radius;
    }
}

non-sealed class Rectangle extends Shape
{
    private final double width;
    private final double height;

    public Rectangle(double width, double height)
    {
        this.width = width;
        this.height = height;
    }

    @Override
    public double area()
    {
        return width * height;
    }
}

Having these classes I can switch over a Shape variable using type pattern matching.

Shape shape = …;

switch (shape) // FP!
{
    case Circle _ → System.out.println(“Circle”);
    case Rectangle _ → System.out.println(“Rectangle”);
}

For this switch s2259 raises an issue and wants me to implement a default case, which is unnecessary due to the sealed Shape class.

Hey there,

In SonarQube Cloud and current versions of SonarQube Server, java:S2259 has been moved to javabugs:S2259 and its performance improved. No further work is anticipated on the version of java:S2259 that remains in SonarQube Community Build.

If you’re able to, please let us know if you still see this issue outside of Community Build!

Thanks, Colin. I understand.