Skip to content

[java] ReturnEmptyArrayRatherThanNull can not detect return null surrounded by if statement #2540

@wuchiuwong

Description

@wuchiuwong

Affects PMD Version:
6.22.0

Rule:

Description:
ReturnEmptyArrayRatherThanNull can not detect return null surrounded by if statement
AST analyse result of the code sample:

//MethodDeclaration
[
(./ResultType/Type[@ArrayType='true'])
and
(./Block/BlockStatement/Statement/ReturnStatement/Expression/PrimaryExpression/PrimaryPrefix/Literal/NullLiteral)
]

AST chain to return null in the following code sample is: Block -> BlockStatement -> Statement -> IfStatement -> Statement -> ReturnStatement -> ... -> NullLiteral

Code Sample demonstrating the issue:

public byte []readBytes() throws IOException {
	int tag = is.read();
    if (tag == 'N')
		return null;

Expected outcome:
false-negative

Running PMD through:
CLI

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-negativePMD doesn't flag a problematic piece of code

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions