Skip to content

[java] UnusedAssignment: False positive when using assert #5159

Closed

Description

@cowwoc

Affects PMD Version: 7.4.0

Rule: https://docs.pmd-code.org/latest/pmd_rules_java_bestpractices.html#unusedassignment

Description:

The rule incorrectly assumes that assert statements will always be executed.

Code Sample demonstrating the issue:

{
	boolean assertionsAreEnabled = false;
	//noinspection AssertWithSideEffects
	assert assertionsAreEnabled = true;
	if (assertionsAreEnabled)
		return newInstance(value, name, getAssumeThatConfiguration());
	return MapValidatorNoOp.getInstance();
}

Expected outcome:

PMD reports a violation at line boolean assertionsAreEnabled = false; but that's wrong because if assert is not executed then the assignment is not overwritten.

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

Activity

  1. added
    a:false-positive
    on Aug 3, 2024
  2. added this to the 7.29.0 milestone on Sep 25, 2026
  3. added a commit that references this issue on Sep 25, 2026
    983d42e
  4. changed the title [-][java] UnusedAssignment false positive when using assert[/-] [+][java] UnusedAssignment: false positive when using assert[/+] on Oct 23, 2026
  5. changed the title [-][java] UnusedAssignment: false positive when using assert[/-] [+][java] UnusedAssignment: False positive when using assert[/+] on Oct 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-positive

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions