Skip to content

Simplify AssertJ hasSize(0) to isEmpty() for Iterable#1037

Merged
timtebeek merged 1 commit into
mainfrom
tim/issue-811-relevance
Jun 20, 2026
Merged

Simplify AssertJ hasSize(0) to isEmpty() for Iterable#1037
timtebeek merged 1 commit into
mainfrom
tim/issue-811-relevance

Conversation

@timtebeek

@timtebeek timtebeek commented Jun 20, 2026

Copy link
Copy Markdown
Member

What's changed

assertThat(x).hasSize(0) is already simplified to assertThat(x).isEmpty() for String, File, Collection, and Map. But SimplifyAssertJAssertion gates on TypeUtils.isAssignableTo(requiredType, actualType), so an actual typed as the broader java.lang.Iterable was not matched by the java.util.Collection entry (Iterable is a supertype of Collection, not assignable to it).

This widens the required type to java.lang.Iterable, which subsumes the Collection case since every Collection is assignable to Iterable.

Confirmed by adding the issue's own reproduction case to AssertJBestPracticesTest.SonarDedicatedAssertions#replacements, which failed before and passes after.

The existing rule targeted java.util.Collection, so an actual typed as
the broader java.lang.Iterable was not matched. Widen the required type
to java.lang.Iterable, which subsumes the Collection case.

Fixes #811
@github-project-automation github-project-automation Bot moved this to In Progress in OpenRewrite Jun 20, 2026
@timtebeek timtebeek merged commit 9e69b1b into main Jun 20, 2026
1 check passed
@timtebeek timtebeek deleted the tim/issue-811-relevance branch June 20, 2026 12:42
@github-project-automation github-project-automation Bot moved this from In Progress to Done in OpenRewrite Jun 20, 2026
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.

Simplify AssertJ assertions related to Iterable

1 participant