mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
[java] suggest removing unnecessary wrapping in optional chains IDEA-225716
GitOrigin-RevId: 538bc7c7d357505d2e48bdd59c06bc0a707e07ec
This commit is contained in:
committed by
intellij-monorepo-bot
parent
7f6c29ed50
commit
e14bc69538
+48
-1
@@ -89,7 +89,8 @@ public class SimplifyOptionalCallChainsInspection extends AbstractBaseJavaLocalI
|
||||
new RewrappingCase(RewrappingCase.Type.OptionalGet),
|
||||
new RewrappingCase(RewrappingCase.Type.OrElseNull),
|
||||
new MapOrElseCase(OrElseType.OrElseGet),
|
||||
new MapOrElseCase(OrElseType.OrElse)
|
||||
new MapOrElseCase(OrElseType.OrElse),
|
||||
new OptionalOfNullableOrElseNullCase()
|
||||
);
|
||||
ourMapper = new CallMapper<>();
|
||||
for (ChainSimplificationCase<?> theCase : cases) {
|
||||
@@ -930,4 +931,50 @@ public class SimplifyOptionalCallChainsInspection extends AbstractBaseJavaLocalI
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static class OptionalOfNullableOrElseNullCase implements ChainSimplificationCase<OptionalOfNullableOrElseNullCase.Context> {
|
||||
@Override
|
||||
public @NotNull String getName(@NotNull Context context) {
|
||||
return JavaBundle.message("simplify.optional.chain.inspection.fix.name.remove.redundant.optional.chain");
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull String getDescription(@NotNull Context context) {
|
||||
return JavaBundle.message("simplify.optional.chain.inspection.fix.description.optional.chain.can.be.eliminated");
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable OptionalOfNullableOrElseNullCase.Context extractContext(@NotNull Project project,
|
||||
@NotNull PsiMethodCallExpression call) {
|
||||
PsiMethodCallExpression outerCall = ExpressionUtils.getCallForQualifier(call);
|
||||
PsiExpression wrappingArgument = call.getArgumentList().getExpressions()[0];
|
||||
if (!OPTIONAL_OR_ELSE.test(outerCall)) return null;
|
||||
PsiExpression argument = outerCall.getArgumentList().getExpressions()[0];
|
||||
if (!ExpressionUtils.isNullLiteral(argument)) return null;
|
||||
return new Context(wrappingArgument, outerCall);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void apply(@NotNull Project project,
|
||||
@NotNull PsiMethodCallExpression call,
|
||||
@NotNull Context context) {
|
||||
CommentTracker ct = new CommentTracker();
|
||||
ct.replaceAndRestoreComments(context.outerCall, context.wrappingArgument);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull CallMatcher getMatcher() {
|
||||
return OPTIONAL_OF_NULLABLE;
|
||||
}
|
||||
|
||||
static class Context {
|
||||
final PsiExpression wrappingArgument;
|
||||
final PsiMethodCallExpression outerCall;
|
||||
|
||||
Context(PsiExpression wrappingArgument, PsiMethodCallExpression outerCall) {
|
||||
this.wrappingArgument = wrappingArgument;
|
||||
this.outerCall = outerCall;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
// "Remove redundant Optional chain" "true"
|
||||
import java.util.Optional;
|
||||
|
||||
class Test {
|
||||
void execute(String s) {
|
||||
/*1*/
|
||||
/*2*/
|
||||
/*4*/
|
||||
/*5*/
|
||||
System.out.println(someFunc(/*3*/)/*6*/);
|
||||
}
|
||||
|
||||
String someFunc() {throw new IllegalStateException();}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
// "Remove redundant Optional chain" "true"
|
||||
import java.util.Optional;
|
||||
|
||||
class Test {
|
||||
void execute(String s) {
|
||||
System.out.println(Optional./*1*/<caret>ofNullable(/*2*/someFunc(/*3*/)/*4*/).orElse(/*5*/null)/*6*/);
|
||||
}
|
||||
|
||||
String someFunc() {throw new IllegalStateException();}
|
||||
}
|
||||
@@ -1421,6 +1421,8 @@ simplify.optional.chain.inspection.or.else.return.fix.name=Replace null check wi
|
||||
simplify.optional.chain.inspection.or.else.return.fix.description=Null check can be eliminated
|
||||
simplify.optional.chain.inspection.or.else.non.null.fix.name=Replace null check with ifPresent()
|
||||
simplify.optional.chain.inspection.or.else.non.null.fix.description=Null check can be eliminated with 'ifPresent'
|
||||
simplify.optional.chain.inspection.fix.name.remove.redundant.optional.chain=Remove redundant Optional chain
|
||||
simplify.optional.chain.inspection.fix.description.optional.chain.can.be.eliminated=Optional chain can be eliminated
|
||||
generate.test.support.method.error.no.template.found.for.framework=No template found for {0}:{1}
|
||||
generate.test.support.method.error.method.already.exists=Method {0} already exists
|
||||
generate.test.support.method.error.cannot.generate.method=Cannot generate method: {0}
|
||||
|
||||
Reference in New Issue
Block a user