Skip to content

[java] UselessParentheses false positive for string concatenation #2739

@jbennett2091

Description

@jbennett2091

Affects PMD Version:
6.25.0

Rule:
UselessParentheses

Description:
String Concatenation can trigger useless-parenthesis warning inappropriately under the right circumstances. Code sample is below (it's meaningless code just to demonstrate the issue). PMD detects UselessParenthesis on the "alpha+bravo" piece. Interestingly, the false-positive goes away if (any of): a) remove the 'charlie' check (apparently you need the OR-operation) or b) make it [equals("ConstantString")] (apparently, has to be comparing to a variable string).

The easy work-around is to invert the equals

Code Sample demonstrating the issue:

	public static boolean isTest(
		final String alpha,
		final String bravo)
	{
		return "Charlie".equals(alpha) || (alpha + bravo).equals(alpha);
	}

Expected outcome:

False-positive: PMD reports UselessParenthesis, but they cannot be removed

Running PMD through: [CLI | Ant | Maven | Gradle | Designer | Other]

Detected via Maven as well as Eclipse plugin

Metadata

Metadata

Assignees

No one assigned

    Labels

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

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions