Skip to content

Commit 2913066

Browse files
refactor: Remove redundant null checks before instanceof (#5736)
* refactor: Remove redundant null checks before instanceof Use this link to re-run the recipe: https://app.moderne.io/recipes/org.openrewrite.staticanalysis.RemoveRedundantNullCheckBeforeInstanceof?organizationId=ODQ2MGExMTUtNDg0My00N2EwLTgzMGMtNGE1NGExMTBmZDkw Co-authored-by: Moderne <team@moderne.io> * Apply suggestions from code review --------- Co-authored-by: Moderne <team@moderne.io>
1 parent fbcd4fc commit 2913066

5 files changed

Lines changed: 5 additions & 6 deletions

File tree

rewrite-java/src/main/java/org/openrewrite/java/MethodMatcher.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,8 +372,7 @@ private boolean matchesAllowingUnknownTypes(J.MethodInvocation method) {
372372
return false;
373373
}
374374

375-
if (method.getSelect() != null &&
376-
method.getSelect() instanceof J.Identifier &&
375+
if (method.getSelect() instanceof J.Identifier &&
377376
!matchesSelectBySimpleNameAlone(((J.Identifier) method.getSelect()))) {
378377
return false;
379378
}

rewrite-java/src/main/java/org/openrewrite/java/cleanup/UnnecessaryParenthesesVisitor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public J visitReturn(J.Return return_, P ctx) {
137137
@Override
138138
public J visitVariable(J.VariableDeclarations.NamedVariable variable, P ctx) {
139139
J.VariableDeclarations.NamedVariable v = (J.VariableDeclarations.NamedVariable) super.visitVariable(variable, ctx);
140-
if (getStyle().getAssign() && v.getInitializer() != null && v.getInitializer() instanceof J.Parentheses) {
140+
if (getStyle().getAssign() && v.getInitializer() instanceof J.Parentheses) {
141141
v = (J.VariableDeclarations.NamedVariable) new UnwrapParentheses<>((J.Parentheses<?>) v.getInitializer()).visitNonNull(v, ctx, getCursor().getParentOrThrow());
142142
}
143143
return v;

rewrite-java/src/main/java/org/openrewrite/java/tree/J.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6182,7 +6182,7 @@ public List<J.Annotation> getAllAnnotations() {
61826182
for (J.Modifier modifier : modifiers) {
61836183
allAnnotations.addAll(modifier.getAnnotations());
61846184
}
6185-
if (typeExpression != null && typeExpression instanceof J.AnnotatedType) {
6185+
if (typeExpression instanceof J.AnnotatedType) {
61866186
allAnnotations.addAll(((J.AnnotatedType) typeExpression).getAnnotations());
61876187
}
61886188
return allAnnotations;

rewrite-javascript/src/main/java/org/openrewrite/javascript/tree/JS.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2318,7 +2318,7 @@ public List<J.Annotation> getAllAnnotations() {
23182318
for (J.Modifier modifier : modifiers) {
23192319
allAnnotations.addAll(modifier.getAnnotations());
23202320
}
2321-
if (typeExpression != null && typeExpression instanceof J.AnnotatedType) {
2321+
if (typeExpression instanceof J.AnnotatedType) {
23222322
allAnnotations.addAll(((J.AnnotatedType) typeExpression).getAnnotations());
23232323
}
23242324
return allAnnotations;

rewrite-kotlin/src/main/java/org/openrewrite/kotlin/internal/KotlinTreeParserVisitor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3576,7 +3576,7 @@ private J.MethodInvocation mapType(J.MethodInvocation tree) {
35763576

35773577
private J.NewClass mapType(J.NewClass tree) {
35783578
J.NewClass n = tree;
3579-
if (n.getClazz() != null && n.getClazz() instanceof J.Identifier) {
3579+
if (n.getClazz() instanceof J.Identifier) {
35803580
if (n.getClazz().getType() instanceof JavaType.Parameterized) {
35813581
J.Identifier clazz = (J.Identifier) n.getClazz();
35823582
n = n.withClazz(clazz.withType(((JavaType.Parameterized) clazz.getType()).getType()));

0 commit comments

Comments
 (0)