Skip to content

Issue #21751: Fix RequireThis resource scope in finally blocks - #21764

Merged
romani merged 1 commit into
checkstyle:masterfrom
ishu7w:fix-require-this-finally-scope
Oct 3, 2026
Merged

romani merged 1 commit into
checkstyle:masterfrom
ishu7w:fix-require-this-finally-scope

Conversation

@ishu7w

@ishu7w ishu7w commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

Issue: #21751

RequireThis misses an unqualified instance-field reference in finally when a same-named resource is declared in the associated try-with-resources statement. The resource is out of scope there, but the check currently resolves the reference to it.

Create the finally block frame outside its associated resource frame. Preserve enclosing resources and local declarations. Update the existing Checker Framework suppression to follow the extracted helper. Add tests covering catch/finally/after-try references, multiple and nested resources, local and parameter shadowing, explicit this, ordinary try/finally, the default overlapping-only mode, and disabled field checking.

Validation:

  • Reproduced on Checkstyle 14.3.0 and master 80ebbfff48f257a775b516fda1f6bb3976bcc80f.
  • New regression test fails on the base with seven missing violations; all 55 RequireThis tests pass with the fix.
  • Unit suite: 6,877 tests, zero failures/errors, two skipped; integration suite: 1,378 tests, zero failures/errors/skips.
  • JaCoCo coverage checks passed.
  • JAVA_HOME=<JDK 21> MAVEN_OPTS=-Xmx4g groovy .ci/checker-framework.groovy checker-nullness-optional-interning: passed after updating the existing suppression.
  • Local before/after comparison: unchanged diagnostics in 926 Checkstyle main/test source files and 580 Apache Commons IO main/test source files. All nine additions across 45 RequireThis fixture files are expected under the shared strict CLI configuration; none removed.
  • JAVA_HOME=<JDK 21> ./mvnw -B -ntp clean verify: passed, including coverage and static analysis.
  • CI identified one surviving mutation that removes the resource-frame guard. Added an ordinary try/finally case with a shadowing local variable; existing tests cover parameter shadowing. All 55 RequireThis tests pass; focused PIT verification of createBlockFrame kills all 14 mutations with repository thresholds unchanged. The full RequireThis PIT CI job now passes.
  • Rebased onto master eddda2325e161a8852fa4c6b78a6e2143b66383b; ./mvnw -B -ntp clean verify passed again on the rebased commit.

Local regression report (original base, before rebase): https://gist.github.com/ishu7w/0386d3328d1a08e74d58b7e37be606c8

Diff Regression config: https://gist.githubusercontent.com/ishu7w/7910b787e503937fe39d33dc6f50d1f2/raw/8064d13e9a70040dd5e04d2db1b229f6c7b1a9c1/regression-config.xml

Codex assisted with investigation, implementation, and validation.

Final fixture validation: reduced InputRequireThisFinally.java to 113 lines by removing the redundant parameter-only case. Full ./mvnw -B -ntp clean verify passed on the final fixture, including resource checks, and focused PIT again killed all 14 helper mutations.

Current CI: all 135 checks pass on 17599fd. The hosted default-configuration report covers 26 projects with zero differences. It uses default overlapping-only behavior; the strict configuration is covered by the local comparison linked above, and a matching hosted report has been requested.

@ishu7w

ishu7w commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor Author

The regression configuration is linked in the PR description. Could a maintainer trigger the hosted diff report? The current workflow restricts comment triggers to owners, members, and collaborators. A local before/after comparison over 1,551 Java files found no changes in Checkstyle or Apache Commons IO main/test sources; only the expected finally-scope fixes appeared in the regression fixtures.

@ishu7w

ishu7w commented Sep 28, 2026

Copy link
Copy Markdown
Contributor Author

Full local ./mvnw -B -ntp clean verify passed after rebasing onto master eddda232: 6,877 unit tests (two skipped), 1,378 integration tests, zero failures/errors, and coverage/static checks passed.

The two currently failed CircleCI jobs fail during checkout, before executing the patch:

These appear to require CI checkout/authentication recovery rather than a change to this fix. Could a maintainer rerun them once that is resolved?

@ishu7w
ishu7w marked this pull request as ready for review September 28, 2026 15:41
@ishu7w
ishu7w force-pushed the fix-require-this-finally-scope branch from c918985 to 0b0ce03 Compare September 28, 2026 15:50
@romani

romani commented Sep 29, 2026

Copy link
Copy Markdown
Member

please fix pitest.

@romani

romani commented Sep 29, 2026

Copy link
Copy Markdown
Member

Github, generate report for RequireThis/Example1

@ishu7w
ishu7w force-pushed the fix-require-this-finally-scope branch from 0b0ce03 to 98b64d0 Compare September 29, 2026 13:21
@ishu7w

ishu7w commented Sep 29, 2026

Copy link
Copy Markdown
Contributor Author

Fixed in 98b64d0. Added ordinary try/finally cases with a shadowing parameter and local variable. The local-variable case detects the surviving mutation that removes the resource-frame guard and incorrectly skips the enclosing block scope.

All 55 RequireThis tests pass. Focused PIT verification kills all 14 mutations in createBlockFrame, including the previously surviving mutation; no production code, mutation thresholds, or suppressions were changed in this update. The full CI suite is rerunning.

@github-actions

Copy link
Copy Markdown
Contributor

@ishu7w
ishu7w force-pushed the fix-require-this-finally-scope branch from 98b64d0 to 17599fd Compare September 29, 2026 13:49
@ishu7w

ishu7w commented Sep 29, 2026

Copy link
Copy Markdown
Contributor Author

Fixed the FileLength failures in 17599fd: removed the redundant parameter-only case, bringing the fixture from 121 to 113 lines. Existing parameter coverage and the local-variable case that kills the PIT mutation remain.

Full ./mvnw -B -ntp clean verify passed on the final fixture, including resource checks. Focused PIT again killed all 14 createBlockFrame mutations. Fresh CI is running.

@romani

romani commented Sep 30, 2026

Copy link
Copy Markdown
Member

Github, generate report for RequireThis/Example1

@github-actions

Copy link
Copy Markdown
Contributor

@ishu7w

ishu7w commented Oct 2, 2026

Copy link
Copy Markdown
Contributor Author

All 135 checks now pass on 17599fd, including the full RequireThis PIT job. The hosted RequireThis/Example1 report shows zero differences across 26 projects. Its configuration uses default overlapping-only behavior, so it verifies compatibility but does not exercise the strict mode behind this bug. Could a maintainer also trigger the custom Diff Regression config linked in the PR description (validateOnlyOverlapping=false, checkMethods=false)? The local strict comparison and regression tests are already documented there.

@romani romani left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

thanks a lot

@romani
romani merged commit 61ec1f7 into checkstyle:master Oct 3, 2026
135 checks passed
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.

2 participants