Skip to content

fix(S5838): wire quick fix for Map.get isEqualTo/isNotEqualTo#5608

Open
asm0dey wants to merge 0 commit intoSonarSource:masterfrom
asm0dey:master
Open

fix(S5838): wire quick fix for Map.get isEqualTo/isNotEqualTo#5608
asm0dey wants to merge 0 commit intoSonarSource:masterfrom
asm0dey:master

Conversation

@asm0dey
Copy link
Copy Markdown

@asm0dey asm0dey commented May 7, 2026

Follow-up to #5607 addressing review feedback that landed after merge.

Summary

  • Add ActualKeyExpectedQuickFix to actually wire the quick fix for assertThat(map.get(k)).isEqualTo(v)assertThat(map).containsEntry(k, v) (and isNotEqualTodoesNotContainEntry)
  • Drop two duplicate simplifier registrations introduced in Add quick fix for Map.get(key) isEqualTo/isNotEqualTo(value) #5607 around the new MAP_GET entries (PATH_GET_PARENT_AND_PARENT_FILE in IS_EQUAL_TO, TRIM/isEmptyString in IS_NOT_EQUAL_TO); findFirst made them dead code
  • Replace stale [[quickfixes=!]] annotation on the Map.get quick-fix fixture with real fix@/edit@ annotations and add the isNotEqualTo case
  • Add Compliant negative test for List.get(int) to lock in the ofSubTypes(JAVA_UTIL_MAP) gate

@sonar-review-alpha
Copy link
Copy Markdown
Contributor

sonar-review-alpha Bot commented May 7, 2026

Summary

This PR completes the quick-fix implementation for S5838's Map.get simplification, converting assertThat(map.get(k)).isEqualTo(v) to assertThat(map).containsEntry(k, v) (and isNotEqualTo variant).

The changes:

  • Wire the ActualKeyExpectedQuickFix class to apply the quick fix for both isEqualTo and isNotEqualTo assertions on Map.get
  • Clean up dead code: remove two duplicate simplifier registrations in IS_EQUAL_TO and IS_NOT_EQUAL_TO that became unreachable after earlier changes
  • Update test fixtures from stale [[quickfixes=!]] notation to proper fix@/edit@ annotations with both assertion variants
  • Add a negative test for List.get(int) to verify the type guard (ofSubTypes(JAVA_UTIL_MAP)) works correctly

What reviewers should know

Start with: The simplifier registrations and their cleanup — understand what duplicate code was removed and why it became dead code.

Type safety is key: The fix only applies to Map.get(key), not List.get(index). Verify the ofSubTypes(JAVA_UTIL_MAP) gate is properly enforced, especially with the new negative test for List.get.

Test fixtures: Look for how the annotations changed from [[quickfixes=!]] to actual fix@/edit@ locations. Both isEqualTo and isNotEqualTo cases should be covered now.

Quick-fix wiring: The ActualKeyExpectedQuickFix class handles the actual transformation. Confirm it correctly builds the containsEntry/doesNotContainEntry calls and preserves the map, key, and value arguments in the right order.


  • Generate Walkthrough
  • Generate Diagram

🗣️ Give feedback

sonar-review-alpha[bot]

This comment was marked as outdated.

Copy link
Copy Markdown
Contributor

@sonar-review-alpha sonar-review-alpha Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall this is a well-scoped follow-up with a clear goal. The dead-code cleanup is correct and the type gate is sound. One structural concern below needs verification before merging.

Note — the following observations could not be attached as inline comments:

  • java-checks/src/main/java/org/sonar/java/checks/tests/AssertJChainSimplificationIndex.java:631-638: The MAP_GET entries on lines 155 and 198 currently call msgWithActualCustom, which hard-codes NoQuickFix via the single-argument WithContextSimplification constructor (line 637 — no second arg).

If the PR wires ActualKeyExpectedQuickFix inside msgWithActualCustom, every other caller (TO_STRING, isInstanceOf, isNotInstanceOf) would also get a quick fix, which is likely unintended.

Please confirm one of the following:

  • The MAP_GET entries are changed away from msgWithActualCustom to a dedicated factory (e.g. msgWithActualKeyExpected) that passes new ActualKeyExpectedQuickFix(...) to the two-argument WithContextSimplification constructor.
  • OR msgWithActualCustom gains an overload and only the MAP_GET call sites use it.

Without this, the new ActualKeyExpectedQuickFix class will exist but its apply() result will be silently discarded by NoQuickFix.

  • java-checks/src/main/java/org/sonar/java/checks/tests/AssertJChainSimplificationIndex.java:156: This entry is a verbatim duplicate of line 154: both register PATH_GET_PARENT_AND_PARENT_FILEhasParentRaw under IS_EQUAL_TO. Because checkPredicatesForSimplification calls .findFirst() (see AssertJChainSimplificationCheck.java:120), the second occurrence is unreachable — this is confirmed dead code introduced by #5607. The PR should delete this line.
  • java-checks/src/main/java/org/sonar/java/checks/tests/AssertJChainSimplificationIndex.java:199: Same dead-code pattern: this entry is an exact duplicate of line 197 (TRIM/isEmptyStringisNotBlank under IS_NOT_EQUAL_TO). The MAP_GET entry inserted between them at line 198 split the original pair, but findFirst() still stops at the first match, making line 199 unreachable. The PR should delete this line.
  • java-checks-test-sources/default/src/test/java/checks/tests/AssertJChainSimplificationCheckTest_QuickFix.java:109: After the PR adds ActualKeyExpectedQuickFix, the [[quickfixes=!]] annotation here must be replaced with real fix@/edit@ annotations. Two separate things to verify:
  1. isEqualTo case (this line): the edits must remove .get(x) from the subject and replace isEqualTo(y) with containsEntry(x, y). Column offsets in the annotations must be exact.
  2. isNotEqualTo case (not yet in this file): the PR should add a symmetric test for doesNotContainEntry.

Also confirm the isNotEqualTo quick-fix annotation passes the key argument correctly — the transformation is identical in structure but the predicate name differs.

🗣️ Give feedback

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant