Skip to content

[java] PreserveStackTraceRule: false-positive on Pattern Matching with instanceof #5318

@VitaliiIevtushenko

Description

@VitaliiIevtushenko

Affects PMD Version: 7.7.0

Rule: PreserveStackTraceRule

Please provide the rule name and a link to the rule documentation:
https://docs.pmd-code.org/latest/pmd_rules_java_bestpractices.html#preservestacktrace

Description: using instanceof's Pattern Matching variable counts as PreserveStackTrace rule breaker

Code Sample demonstrating the issue:

        try {
          ......
        } catch (RuntimeException ex) {
            throw ex instanceof ExpirationException expirationException
                    ? expirationExceptionHandler.handle(expirationException)
                    : ex;
        }

Expected outcome:

PMD reports a violation at line throw ex instanceof ExpirationException expirationException, but that's wrong. That's a false positive.

Running PMD through: Maven

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-positivePMD flags a piece of code that is not problematic

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions