Skip to content

Commit 0a470e4

Browse files
committed
Do not remove type arguments within ternary
Fixes #164
1 parent 3fee48a commit 0a470e4

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

src/main/java/org/openrewrite/staticanalysis/UnnecessaryExplicitTypeArguments.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
4848
}
4949

5050
Object enclosing = getCursor().getParentTreeCursor().getValue();
51+
52+
if (enclosing instanceof J.Ternary) {
53+
return m; // may be necessary for type inference
54+
}
55+
5156
JavaType inferredType = null;
5257
if (enclosing instanceof J.MethodInvocation) {
5358
if (shouldRetainOnStaticMethod(methodType)) {

src/test/java/org/openrewrite/staticanalysis/UnnecessaryExplicitTypeArgumentsTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,6 @@ void test() {
139139
);
140140
}
141141

142-
@ExpectedToFail
143142
@Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/164")
144143
@Test
145144
void doesNotRemoveNecessaryTypeArguments() {
@@ -150,8 +149,8 @@ void doesNotRemoveNecessaryTypeArguments() {
150149
import java.util.Optional;
151150
import java.util.stream.Stream;
152151
public class Test {
153-
void test() {
154-
Stream.of("hi")
152+
void test(String s) {
153+
Stream.of(s)
155154
.map(it -> it == null ? Optional.<String>empty() : Optional.of(it))
156155
.flatMap(Optional::stream)
157156
.map(this::mapper); //this requires the type information

0 commit comments

Comments
 (0)