Skip to content

Fix EqualsAvoidsNull to remove redundant null checks when literal is already on left side#756

Merged
timtebeek merged 5 commits intomainfrom
fix-equals-avoids-null-test
Oct 20, 2025
Merged

Fix EqualsAvoidsNull to remove redundant null checks when literal is already on left side#756
timtebeek merged 5 commits intomainfrom
fix-equals-avoids-null-test

Conversation

@timtebeek
Copy link
Member

Summary

  • Fixes the EqualsAvoidsNull recipe to properly remove redundant null checks when the literal is already on the left side of the equals call
  • Resolves failing test: EqualsAvoidsNullTest#removeUnnecessaryNullCheckByItself

Problem

The recipe was failing to remove redundant null checks in patterns like:

if (s != null && "test".equals(s)) {}

This should be simplified to:

if ("test".equals(s)) {}

However, the recipe was not making this transformation.

Root Cause

The issue had two parts:

  1. Early return prevented null check removal: The visitMethodInvocation method would return early when hasCompatibleArgument(m) was false or when the select was already a literal. This prevented maybeHandleParentBinary (the method responsible for removing redundant null checks) from being called.

  2. Incomplete null check matching: The maybeHandleParentBinary method only checked if the null check matched the select of the method invocation (e.g., s in s.equals("test")), but didn't check if it matched the argument (e.g., s in "test".equals(s)).

Changes

  • Moved the maybeHandleParentBinary call before the early return checks to ensure null check removal is always attempted for string comparison methods
  • Extended null check matching in maybeHandleParentBinary to check both the select and argument expressions
  • Added a clarifying comment explaining that null check removal runs independently of argument swapping logic

Test plan

  • EqualsAvoidsNullTest#removeUnnecessaryNullCheckByItself now passes
  • ✅ All other tests in EqualsAvoidsNullTest continue to pass
  • ✅ No regressions introduced

🤖 Generated with Claude Code

…already on left side

The recipe was failing to remove redundant null checks in patterns like:
  if (s != null && "test".equals(s)) {}

The issue was that visitMethodInvocation would return early before calling
maybeHandleParentBinary when the select was already a literal. This prevented
the null check removal logic from running.

Additionally, maybeHandleParentBinary only checked if the null check matched
the select of the method invocation, but not the argument. For cases where the
literal is already on the left side (select), we need to check if the null
check matches the argument.

Changes:
- Moved maybeHandleParentBinary call before early returns to ensure null check
  removal is always attempted for string comparison methods
- Extended null check matching to check both select and argument expressions
- Added comment clarifying that null check removal runs independently of
  argument swapping logic

Fixes: org.openrewrite.staticanalysis.EqualsAvoidsNullTest#removeUnnecessaryNullCheckByItself

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@github-project-automation github-project-automation bot moved this to In Progress in OpenRewrite Oct 19, 2025
@timtebeek timtebeek marked this pull request as ready for review October 19, 2025 12:27
@github-project-automation github-project-automation bot moved this from In Progress to Ready to Review in OpenRewrite Oct 20, 2025
@timtebeek timtebeek merged commit b6d6d94 into main Oct 20, 2025
2 checks passed
@timtebeek timtebeek deleted the fix-equals-avoids-null-test branch October 20, 2025 08:10
@github-project-automation github-project-automation bot moved this from Ready to Review to Done in OpenRewrite Oct 20, 2025
mergify bot added a commit to robfrank/linklift that referenced this pull request Oct 30, 2025
… 2.19.0 to 2.20.0 [skip ci]

Bumps [org.openrewrite.recipe:rewrite-static-analysis](https://github.com/openrewrite/rewrite-static-analysis) from 2.19.0 to 2.20.0.
Release notes

*Sourced from [org.openrewrite.recipe:rewrite-static-analysis's releases](https://github.com/openrewrite/rewrite-static-analysis/releases).*

> 2.20.0
> ------
>
> What's Changed
> --------------
>
> * Avoid incorrect changes to Kotlin in RemoveUnusedLocalVariables by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-static-analysis#755](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/755)
> * Fix EqualsAvoidsNull to remove redundant null checks when literal is already on left side by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-static-analysis#756](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/756)
> * OpenRewrite recipe best practices by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-static-analysis#758](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/758)
> * Fix RemoveUnusedPrivateFields for Lombok annotations after visibility modifiers by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-static-analysis#759](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/759)
>
> **Full Changelog**: <openrewrite/rewrite-static-analysis@v2.19.0...v2.20.0>


Commits

* [`241a923`](openrewrite/rewrite-static-analysis@241a923) Fix RemoveUnusedPrivateFields for Lombok annotations after visibility modifie...
* [`ce75f31`](openrewrite/rewrite-static-analysis@ce75f31) OpenRewrite recipe best practices ([#758](https://redirect.github.com/openrewrite/rewrite-static-analysis/issues/758))
* [`b6d6d94`](openrewrite/rewrite-static-analysis@b6d6d94) Fix EqualsAvoidsNull to remove redundant null checks when literal is already ...
* [`4c8fa35`](openrewrite/rewrite-static-analysis@4c8fa35) Drop rarely used `fromRuntimeClasspath` for `recipeFromResources`
* [`a84f525`](openrewrite/rewrite-static-analysis@a84f525) Add second test class following commit comment
* [`c195a4f`](openrewrite/rewrite-static-analysis@c195a4f) Never finalize super classes; Fixes [#372](https://redirect.github.com/openrewrite/rewrite-static-analysis/issues/372)
* [`b92d93a`](openrewrite/rewrite-static-analysis@b92d93a) Avoid incorrect changes to Kotlin in RemoveUnusedLocalVariables ([#755](https://redirect.github.com/openrewrite/rewrite-static-analysis/issues/755))
* See full diff in [compare view](openrewrite/rewrite-static-analysis@v2.19.0...v2.20.0)
  
[![Dependabot compatibility score](https://dependabot-badges.githubapp.com/badges/compatibility\_score?dependency-name=org.openrewrite.recipe:rewrite-static-analysis&package-manager=maven&previous-version=2.19.0&new-version=2.20.0)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores)
Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`.
[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)
---
Dependabot commands and options
  
You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it
- `@dependabot merge` will merge this PR after your CI passes on it
- `@dependabot squash and merge` will squash and merge this PR after your CI passes on it
- `@dependabot cancel merge` will cancel a previously requested merge and block automerging
- `@dependabot reopen` will reopen this PR if it is closed
- `@dependabot close` will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
- `@dependabot show  ignore conditions` will show all of the ignore conditions of the specified dependency
- `@dependabot ignore this major version` will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

2 participants