Skip to content

Commit 264ca2a

Browse files
Merge branch 'main' into remove-private-field-underscores
2 parents 15a9439 + 0a470e4 commit 264ca2a

File tree

5 files changed

+90
-3
lines changed

5 files changed

+90
-3
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,21 @@ public J visitTypeCast(J.TypeCast typeCast, ExecutionContext ctx) {
112112
return visitedTypeCast;
113113
}
114114

115+
// Special case: if this cast is in a generic method call that's part of a method chain,
116+
// the cast might be necessary to control generic type inference
117+
if (parentValue instanceof J.MethodInvocation &&
118+
TypeUtils.isAssignableTo(castType, expressionType) &&
119+
!castType.equals(expressionType)) {
120+
// Check if the method returns a generic type
121+
JavaType.Method methodType = ((J.MethodInvocation) parentValue).getMethodType();
122+
if (methodType != null && methodType.getReturnType() instanceof JavaType.Parameterized) {
123+
// This cast is widening the type (e.g., BarImpl to Bar) in a generic context
124+
// which might affect how the generic type is inferred in method chains
125+
// Keep the cast to be safe
126+
return visitedTypeCast;
127+
}
128+
}
129+
115130
if (!(targetType instanceof JavaType.Array) && TypeUtils.isOfClassType(targetType, "java.lang.Object") ||
116131
TypeUtils.isOfType(targetType, expressionType) ||
117132
TypeUtils.isAssignableTo(targetType, expressionType)) {

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/InlineVariableTest.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,46 @@ List<String> testLambda(List<String> names) {
103103
);
104104
}
105105

106+
@Test
107+
void inlineSwitch() {
108+
rewriteRun(
109+
//language=java
110+
java(
111+
"""
112+
import java.util.List;
113+
import java.util.stream.Collectors;
114+
115+
class Test {
116+
String test(int n) {
117+
String s = switch (n) {
118+
case 1 -> "one";
119+
case 2 -> "two";
120+
case 3 -> "three";
121+
default -> "unknown";
122+
};
123+
return s;
124+
}
125+
}
126+
""",
127+
"""
128+
import java.util.List;
129+
import java.util.stream.Collectors;
130+
131+
class Test {
132+
String test(int n) {
133+
return switch (n) {
134+
case 1 -> "one";
135+
case 2 -> "two";
136+
case 3 -> "three";
137+
default -> "unknown";
138+
};
139+
}
140+
}
141+
"""
142+
)
143+
);
144+
}
145+
106146
@SuppressWarnings("UnnecessaryLocalVariable")
107147
@Test
108148
void preserveComments() {

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,4 +528,32 @@ void bar(Marshaller marshaller) {
528528
)
529529
);
530530
}
531+
532+
@Test
533+
void dontRemoveNecessaryDowncast() {
534+
rewriteRun(
535+
// language=java
536+
java(
537+
"""
538+
import java.util.Optional;
539+
540+
interface Bar {}
541+
class BarImpl implements Bar {}
542+
class Foo {
543+
private Bar getBar() {
544+
return new BarImpl();
545+
}
546+
547+
private BarImpl getBarImpl() {
548+
return new BarImpl();
549+
}
550+
551+
public Bar baz() {
552+
return Optional.of((Bar) getBarImpl()).orElse(getBar());
553+
}
554+
}
555+
"""
556+
)
557+
);
558+
}
531559
}

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)