Fix ChangeMethodInvocationReturnType changing variable type for nested matches#7993
Conversation
…d matches When the matched method invocation appeared nested inside the initializer (e.g. as an argument to another call) rather than as the initializer itself, the recipe still rewrote the declared variable type. For example `Cell c = row.createCell(i, other.getCellType())` would have `Cell` replaced with `CellType`, producing uncompilable code. Only rewrite the declared type when a variable's initializer is itself the matched method invocation. Fixes openrewrite/rewrite-apache#134
|
Note for reviewers: the failing checks are all in |
|
Root cause of the red check (confirmed across 3 runs): It's pre-existing and unrelated to this change: because this PR modifies This needs a maintainer to either merge past the known-broken C# integTest or fix the .NET RPC startup in CI. |
Unwrap parentheses and recurse into ternary branches when checking whether a variable is initialized by the matched invocation. Typecast initializers stay excluded: the cast determines the declared type.
MBoegers
left a comment
There was a problem hiding this comment.
Added a missing path where Cell xlsxCell = (xlsxRow.createCell()) and Cell xlsxCell = someFlag ? xlsxRow.createCell() : null would be missed.
|
Thank you for all @timtebeek. Waiting the mext release for remove custom fix |
What's changed?
ChangeMethodInvocationReturnTyperewrote the declared type of a variable whenever a matching method invocation appeared anywhere in the variable declaration subtree — including when the match was nested as an argument to another call, rather than being the initializer itself.visitVariableDeclarationsset amethodUpdatedflag from any descendant match and then unconditionally rewrote the declared type. This change additionally requires that one of the variables' initializer is the matched invocation before rewriting the declared type.What's your motivation?
The Apache POI 3.17 migration runs:
Against code like:
getCellType()is only an argument tocreateCell(...), but the recipe rewrote the declaration toCellType xlsxCell = ..., producing uncompilable code (Cellcannot be converted toCellType).Anything in particular you'd like reviewers to focus on?
The nested method invocation's own return-type attribution is still updated (that is correct); only the enclosing variable's declared type is now left alone when the match isn't the initializer. Added a regression test covering both the legitimate (direct initializer) and the previously-broken (nested argument) cases.
Checklist