Repository navigation
Keep early returns in TestMethodsShouldBeVoid and KotlinTestMethodsShouldReturnUnit - #1217
Merged
Merged
Conversation
The recipe also visited Kotlin files, where it replaced the `Int` return type with a Java `void` primitive and removed the `return` statements, printing `fun testMethod(): void { }`, which does not compile. Kotlin test methods are handled by `KotlinTestMethodsShouldReturnUnit`, so this recipe now only accepts Java compilation units.
…sShouldReturnUnit`
Both recipes deleted every `return` in a test method, so a guard like `if (skip) { return 0; }` stopped exiting and the rest of the test ran anyway. Only a `return` that is the last statement of the method is now removed (keeping its expression when it is a valid expression statement); any other `return` loses just its value. Methods are left unchanged when an early return's value has side effects, when a Kotlin lambda returns from the test method, when the method overrides another, is called or referenced elsewhere in the same file, or has no body (previously an NPE). Returns in local classes and Kotlin local functions are no longer touched.
23 of 76 tasks
…-keep-early-returns-v1 # Conflicts: # src/main/resources/META-INF/rewrite/recipes.csv
This was referenced Oct 24, 2026
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.
TestMethodsShouldBeVoidandKotlinTestMethodsShouldReturnUnitdeleted everyreturnin a test method. A guard such asif (System.getenv("SKIP") != null) { return 0; }then no longer exited, so the rest of the test ran and could fail. The recipes now only remove areturnthat is the last statement of the method (keeping a call likereturn calculate();ascalculate();), and turn every otherreturn value;intoreturn;:They leave a method unchanged when an early return's value has side effects, when it overrides another method, when it is called or referenced elsewhere in the same file (a test reused as a helper), when it has no body (previously an NPE on abstract test methods), or, in Kotlin, when a lambda returns from the test method. Returns in local classes and Kotlin local functions are no longer touched.
TestMethodsShouldBeVoidalso stops visiting Kotlin files, where it printedfun testMethod(): void { }.Fixes item C4 of #1211.