fix: retain doNothing() stubs inside lambda bodies to prevent uncompilable code#1009
Merged
Conversation
…lable empty lambda body
…da-bodies-to-avoid-uncompilable-lambda
…ion arms to prevent uncompilable code
…-inside-lambda-bodies-to-avoid-uncompilable-lambda' into fix/fix-retain-doNothing()-stubs-inside-lambda-bodies-to-avoid-uncompilable-lambda
Contributor
Author
|
I have updated the oriignal comment and Issue to include my latest commit changes. For this I added 6 tests:
|
timtebeek
reviewed
May 28, 2026
…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.
timtebeek
approved these changes
May 28, 2026
Member
timtebeek
left a comment
There was a problem hiding this comment.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What's changed?
Added a guard in
RemoveDoNothingForDefaultMocksto skip removal ofdoNothing()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 ora 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()andisSwitchExpressionArm()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, includingUpgradeSpringBoot_3_5and all other composite recipes that includeRemoveDoNothingForDefaultMocksas part of their stack.