Skip to content

NullTernary false positive in switch expression #5753

@Emilyaxe

Description

@Emilyaxe

NullTernary does not report a warning for the arrow-case form (default -> ...), but reports one for the equivalent yield block. Both forms are semantically identical and do not involve unboxing, so the warning on the yield block is a false positive.

public class NullTernarySwitch {
    // arrow-case expression — NO warning
    String fArrow(int n) {
        return switch (n) {
            case 0 -> "zero";
            default -> (n % 2 == 0) ? "even" : null;
        };
    }

    // block-case with yield — NullTernary is reported
    String fYield(int n) {
        return switch (n) {
            case 0 -> "zero";
            default -> {
                yield (n % 2 == 0) ? "even" : null;  // false positive
            }
        };
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions