Skip to content

fix: retain doNothing() stubs inside lambda bodies to prevent uncompilable code#1009

Merged
timtebeek merged 6 commits into
openrewrite:mainfrom
darrencilia:fix/fix-retain-doNothing()-stubs-inside-lambda-bodies-to-avoid-uncompilable-lambda
May 28, 2026
Merged

fix: retain doNothing() stubs inside lambda bodies to prevent uncompilable code#1009
timtebeek merged 6 commits into
openrewrite:mainfrom
darrencilia:fix/fix-retain-doNothing()-stubs-inside-lambda-bodies-to-avoid-uncompilable-lambda

Conversation

@darrencilia
Copy link
Copy Markdown
Contributor

@darrencilia darrencilia commented May 28, 2026

What's changed?

Added a guard in RemoveDoNothingForDefaultMocks to skip removal of doNothing() stubs when they appear as an expression lambda body or a switch expression arm, preventing a dangling -> which does not compile.

Block lambda and switch block arm cases are not guarded as removal leaves an empty block which is valid Java and can be cleaned up downstream by static analysis.

What's your motivation?

Fixes #1008

When doNothing().when(mock).method(args) is the expression body of a lambda or
a switch expression arm, removing it leaves a dangling -> which does not compile.

Anything in particular you'd like reviewers to focus on?

The two helpers isExpressionLambdaBody() and isSwitchExpressionArm() confirming the cursor level is correct for each case.

Have you considered any alternatives or workarounds?

An alternative would be to remove the entire enclosing construct when the body becomes empty. However this is a significantly more complex AST transformation and risks unintended behaviour changes. Retaining is the safer option for compile-breaking cases.

Any additional context

The bug surfaces through any recipe that chains through RemoveDoNothingForDefaultMocks, including UpgradeSpringBoot_3_5 and all other composite recipes that include RemoveDoNothingForDefaultMocks as part of their stack.

@github-project-automation github-project-automation Bot moved this to In Progress in OpenRewrite May 28, 2026
@darrencilia darrencilia marked this pull request as draft May 28, 2026 20:51
@timtebeek timtebeek self-requested a review May 28, 2026 21:14
…-inside-lambda-bodies-to-avoid-uncompilable-lambda' into fix/fix-retain-doNothing()-stubs-inside-lambda-bodies-to-avoid-uncompilable-lambda
@darrencilia
Copy link
Copy Markdown
Contributor Author

darrencilia commented May 28, 2026

I have updated the oriignal comment and Issue to include my latest commit changes.

For this I added 6 tests:

  • retainsDoNothingInsideLambdaBody - expression lambda, recipe makes no change
  • removesDoNothingInLambdaBlockLeavingEmptyBlock - block lambda sole statement, recipe removes and leaves empty block
  • removesDoNothingWhenNotSoleStatementInLambdaBlock - block lambda multiple statements, recipe removes correctly
  • retainsDoNothingInSwitchExpressionArm - switch arrow arm, recipe makes no change
  • removesDoNothingInSwitchExpressionArmBlockLeavingEmptyBlock - switch block arm sole statement, recipe removes and leaves empty block
  • removesDoNothingWhenNotSoleStatementInSwitchExpressionArmBlock - switch block arm multiple statements, recipe removes correctly

darrencilia and others added 2 commits May 29, 2026 00:00
…hingForDefaultMocks.java

Co-authored-by: Tim te Beek <timtebeek@gmail.com>
Folds isExpressionLambdaBody()/isSwitchExpressionArm() into the single call site and removes the unnecessary mi != null check.
Copy link
Copy Markdown
Member

@timtebeek timtebeek left a comment

Choose a reason for hiding this comment

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

Thanks again for the help here and the clear test examples. I've inline the small methods just to have fewer indirections when reading the code. We'll merge as soon as CI is green.

@github-project-automation github-project-automation Bot moved this from In Progress to Ready to Review in OpenRewrite May 28, 2026
@timtebeek timtebeek marked this pull request as ready for review May 28, 2026 22:18
@timtebeek timtebeek merged commit 86f2308 into openrewrite:main May 28, 2026
1 check passed
@github-project-automation github-project-automation Bot moved this from Ready to Review to Done in OpenRewrite May 28, 2026
@darrencilia darrencilia deleted the fix/fix-retain-doNothing()-stubs-inside-lambda-bodies-to-avoid-uncompilable-lambda branch May 28, 2026 22:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

fix: RemoveDoNothingForDefaultMocks incorrectly removes doNothing() in contexts where removal produces uncompilable code

2 participants