Refactor: slightly improve code in WhenChecker
This commit is contained in:
@@ -35,7 +35,10 @@ import java.util.Collections;
|
|||||||
/**
|
/**
|
||||||
* @author svtk
|
* @author svtk
|
||||||
*/
|
*/
|
||||||
public class WhenChecker {
|
public final class WhenChecker {
|
||||||
|
private WhenChecker() {
|
||||||
|
}
|
||||||
|
|
||||||
public static boolean isWhenExhaustive(@NotNull JetWhenExpression expression, @NotNull BindingTrace trace) {
|
public static boolean isWhenExhaustive(@NotNull JetWhenExpression expression, @NotNull BindingTrace trace) {
|
||||||
JetExpression subjectExpression = expression.getSubjectExpression();
|
JetExpression subjectExpression = expression.getSubjectExpression();
|
||||||
if (subjectExpression == null) return false;
|
if (subjectExpression == null) return false;
|
||||||
@@ -75,22 +78,30 @@ public class WhenChecker {
|
|||||||
assert enumEntry.getKind() == ClassKind.ENUM_ENTRY;
|
assert enumEntry.getKind() == ClassKind.ENUM_ENTRY;
|
||||||
for (JetWhenEntry whenEntry : whenExpression.getEntries()) {
|
for (JetWhenEntry whenEntry : whenExpression.getEntries()) {
|
||||||
for (JetWhenCondition condition : whenEntry.getConditions()) {
|
for (JetWhenCondition condition : whenEntry.getConditions()) {
|
||||||
if (condition instanceof JetWhenConditionWithExpression) {
|
if (!(condition instanceof JetWhenConditionWithExpression)) {
|
||||||
JetExpression patternExpression = ((JetWhenConditionWithExpression) condition).getExpression();
|
continue;
|
||||||
JetSimpleNameExpression reference = getReference(patternExpression);
|
}
|
||||||
if (reference == null) continue;
|
if (isCheckForEnumEntry((JetWhenConditionWithExpression) condition, enumEntry, trace)) {
|
||||||
DeclarationDescriptor target = trace.get(BindingContext.REFERENCE_TARGET, reference);
|
return true;
|
||||||
if (target == null) continue;
|
|
||||||
ClassDescriptor classDescriptor = trace.get(BindingContext.OBJECT_DECLARATION_CLASS, (VariableDescriptor) target);
|
|
||||||
if (classDescriptor == enumEntry) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static boolean isCheckForEnumEntry(
|
||||||
|
@NotNull JetWhenConditionWithExpression whenExpression,
|
||||||
|
@NotNull ClassDescriptor enumEntry,
|
||||||
|
@NotNull BindingTrace trace
|
||||||
|
) {
|
||||||
|
JetSimpleNameExpression reference = getReference(whenExpression.getExpression());
|
||||||
|
if (reference == null) return false;
|
||||||
|
DeclarationDescriptor target = trace.get(BindingContext.REFERENCE_TARGET, reference);
|
||||||
|
if (target == null) return false;
|
||||||
|
ClassDescriptor classDescriptor = trace.get(BindingContext.OBJECT_DECLARATION_CLASS, (VariableDescriptor) target);
|
||||||
|
return classDescriptor == enumEntry;
|
||||||
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
private static JetSimpleNameExpression getReference(@Nullable JetExpression expression) {
|
private static JetSimpleNameExpression getReference(@Nullable JetExpression expression) {
|
||||||
if (expression == null) {
|
if (expression == null) {
|
||||||
|
|||||||
Reference in New Issue
Block a user