Skip to content

Commit

Permalink
[CodeQL] More fancy switches
Browse files Browse the repository at this point in the history
  • Loading branch information
hurricup committed Sep 1, 2024
1 parent bec7f4b commit 693e151
Show file tree
Hide file tree
Showing 16 changed files with 305 additions and 334 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2015-2023 Alexandr Evstigneev
* Copyright 2015-2024 Alexandr Evstigneev
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -545,28 +545,18 @@ private Map<Class<? extends HTMLMasonCompositeElement>, List<HTMLMasonCompositeE

PsiTreeUtil.processElements(HTMLMasonFileImpl.this, element ->
{
if (element instanceof HTMLMasonOnceBlock) {
onceResult.add((HTMLMasonCompositeElement)element);
}
else if (element instanceof HTMLMasonSharedBlock) {
sharedResult.add((HTMLMasonCompositeElement)element);
}
else if (element instanceof HTMLMasonCleanupBlock) {
cleanupResult.add((HTMLMasonCompositeElement)element);
}
else if (element instanceof HTMLMasonInitBlock &&
HTMLMasonFileImpl.this.equals(PsiTreeUtil.getParentOfType(element, HTMLMasonArgsContainer.class))) {
initResult.add((HTMLMasonCompositeElement)element);
}
else if (element instanceof HTMLMasonArgsBlock &&
HTMLMasonFileImpl.this.equals(PsiTreeUtil.getParentOfType(element, HTMLMasonArgsContainer.class))) {
argsResult.add((HTMLMasonCompositeElement)element);
}
else if (element instanceof HTMLMasonMethodDefinition) {
methodsResult.add((HTMLMasonCompositeElement)element);
}
else if (element instanceof HTMLMasonSubcomponentDefitnition) {
subComponentsResult.add((HTMLMasonCompositeElement)element);
switch (element) {
case HTMLMasonOnceBlock block -> onceResult.add(block);
case HTMLMasonSharedBlock block -> sharedResult.add(block);
case HTMLMasonCleanupBlock block -> cleanupResult.add(block);
case HTMLMasonInitBlock block
when HTMLMasonFileImpl.this.equals(PsiTreeUtil.getParentOfType(element, HTMLMasonArgsContainer.class)) -> initResult.add(block);
case HTMLMasonArgsBlock block
when HTMLMasonFileImpl.this.equals(PsiTreeUtil.getParentOfType(element, HTMLMasonArgsContainer.class)) -> argsResult.add(block);
case HTMLMasonMethodDefinition definition -> methodsResult.add(definition);
case HTMLMasonSubcomponentDefitnition defitnition -> subComponentsResult.add(defitnition);
default -> {
}
}

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,17 +375,17 @@ else if (elementType != TokenType.WHITE_SPACE && elementType != COMMENT_LINE) {
}
PsiElement run = podSection;

// detecting first section
while (true) {
PsiElement prevSibling = run.getPrevSibling();
//noinspection IfCanBeSwitch
if (prevSibling == null) {
break;
}
if (prevSibling instanceof PodSection && ((PodSection)prevSibling).hasContent()) {
break;
}
if (prevSibling instanceof PodTitledSection) {
podSection = (PodTitledSection)prevSibling;
if (prevSibling instanceof PodTitledSection section) {
podSection = section;
}
run = prevSibling;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2015-2023 Alexandr Evstigneev
* Copyright 2015-2024 Alexandr Evstigneev
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -223,31 +223,23 @@ protected static boolean isOp(PsiElement element) {
*/
@Contract("null->null")
public static @Nullable PsiElement findPodElement(@Nullable PsiElement element) {
if (element == null) {
return null;
}
else if (element instanceof PerlBuiltInSubDefinition) {
String subName = StringUtil.notNullize(((PerlBuiltInSubDefinition)element).getName());
if ("default".equals(subName)) {
return PerlDocUtil.resolveDescriptor(SWITCH_DOC_LINK, element, false);
}
else {
return PerlDocUtil.getPerlFuncDocFromText(element, subName);
return switch (element) {
case null -> null;

Check notice on line 227 in plugin/core/src/main/java/com/perl5/lang/perl/documentation/PerlDocumentationProvider.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Duplicate branches in 'switch'

Branch in 'switch' is a duplicate of the default branch
case PerlBuiltInSubDefinition definition -> {
String subName = StringUtil.notNullize(definition.getName());
if ("default".equals(subName)) {
yield PerlDocUtil.resolveDescriptor(SWITCH_DOC_LINK, element, false);
}
else {
yield PerlDocUtil.getPerlFuncDocFromText(element, subName);
}
}
}
else if (element instanceof PerlSubElement) {
return findPodElement((PerlSubElement)element);
}
else if (element instanceof PerlFileImpl) {
return findPodElement((PerlFileImpl)element);
}
else if (element instanceof PerlNamespaceDefinitionElement) {
return findPodElement((PerlNamespaceDefinitionElement)element);
}
else if (element instanceof PerlVariable) {
return PerlDocUtil.getPerlVarDoc((PerlVariable)element);
}
return null;
case PerlSubElement subElement -> findPodElement(subElement);
case PerlFileImpl file -> findPodElement(file);
case PerlNamespaceDefinitionElement definitionElement -> findPodElement(definitionElement);
case PerlVariable variable -> PerlDocUtil.getPerlVarDoc(variable);
default -> null;
};
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,29 +156,33 @@ else if (finalElement instanceof PerlDerefExpression) {
}

private static @NotNull PerlValue computeValue(@NotNull PsiElement element) {
if (element instanceof PerlVariableDeclarationExpr) {
if (((PerlVariableDeclarationExpr)element).isParenthesized()) {
return PerlArrayValue.builder().addPsiElements(Arrays.asList(element.getChildren())).build();
switch (element) {
case PerlVariableDeclarationExpr declarationExpr -> {
if (declarationExpr.isParenthesized()) {
return PerlArrayValue.builder().addPsiElements(Arrays.asList(element.getChildren())).build();
}
PsiElement[] children = element.getChildren();
return children.length == 1 ? from(children[0]) : UNKNOWN_VALUE;
}
case PerlReturnExpr returnExpr -> {
PsiPerlExpr expr = returnExpr.getReturnValueExpr();
return expr == null ? UNDEF_VALUE : from(expr);
}
case PerlVariableMixin variableMixin -> {
PerlVariableNameElement variableNameElement = variableMixin.getVariableNameElement();
return variableNameElement == null ? UNKNOWN_VALUE : PerlResolveUtil.inferVariableValue(variableMixin);
}
case PerlString string -> {
return PerlScalarValue.create(ElementManipulators.getValueText(string));
}
case PerlImplicitVariableDeclaration implicitVariableDeclaration -> {
return implicitVariableDeclaration.getDeclaredValue();
}
case PerlMethod method -> {
return computeValue(method);
}
default -> {
}
PsiElement[] children = element.getChildren();
return children.length == 1 ? from(children[0]) : UNKNOWN_VALUE;
}
if (element instanceof PerlReturnExpr) {
PsiPerlExpr expr = ((PerlReturnExpr)element).getReturnValueExpr();
return expr == null ? UNDEF_VALUE : from(expr);
}
else if (element instanceof PerlVariableMixin variableMixin) {
PerlVariableNameElement variableNameElement = variableMixin.getVariableNameElement();
return variableNameElement == null ? UNKNOWN_VALUE : PerlResolveUtil.inferVariableValue(variableMixin);
}
else if (element instanceof PerlString) {
return PerlScalarValue.create(ElementManipulators.getValueText(element));
}
else if (element instanceof PerlImplicitVariableDeclaration implicitVariableDeclaration) {
return implicitVariableDeclaration.getDeclaredValue();
}
else if (element instanceof PerlMethod method) {
return computeValue(method);
}

IElementType elementType = PsiUtilCore.getElementType(element);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2015-2022 Alexandr Evstigneev
* Copyright 2015-2024 Alexandr Evstigneev
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -85,16 +85,15 @@ public static boolean processSubDefinitionLookupElement(@NotNull String nameToUs
public static boolean processImportedEntityLookupElement(@NotNull PsiElement element,
@NotNull PerlExportDescriptor exportDescriptor,
@NotNull PerlCompletionProcessor completionProcessor) {
if (element instanceof PerlSubDefinitionElement subDefinitionElement) {
return processSubDefinitionLookupElement(subDefinitionElement, exportDescriptor, completionProcessor);
}
else if (element instanceof PerlSubDeclarationElement subDeclarationElement) {
return processSubDeclarationLookupElement(subDeclarationElement, exportDescriptor, completionProcessor);
}
else if (element instanceof PerlGlobVariableElement globVariableElement) {
return processGlobLookupElement(globVariableElement, exportDescriptor, completionProcessor);
}
throw new RuntimeException("Don't know how to make lookup element for " + element.getClass());
return switch (element) {
case PerlSubDefinitionElement subDefinitionElement ->
processSubDefinitionLookupElement(subDefinitionElement, exportDescriptor, completionProcessor);
case PerlSubDeclarationElement subDeclarationElement ->
processSubDeclarationLookupElement(subDeclarationElement, exportDescriptor, completionProcessor);
case PerlGlobVariableElement globVariableElement ->
processGlobLookupElement(globVariableElement, exportDescriptor, completionProcessor);
default -> throw new RuntimeException("Don't know how to make lookup element for " + element.getClass());
};
}

public static boolean processSubDeclarationLookupElement(@NotNull PerlSubDeclarationElement subDeclaration,
Expand Down Expand Up @@ -226,23 +225,21 @@ public static boolean processSubsCompletionsForCallValue(@NotNull PerlSimpleComp
completionProcessor.getLeafElement(), new PerlNamespaceItemProcessor<>() {
@Override
public boolean processItem(@NotNull PsiNamedElement element) {
if (element instanceof PerlImplicitSubDefinition implicitSubDefinition && implicitSubDefinition.isAnonymous()) {
return completionProcessor.result();
}
if (element instanceof PerlSubDefinitionElement subDefinitionElement &&
!subDefinitionElement.isAnonymous() && (isStatic && subDefinitionElement.isStatic() || subDefinitionElement.isMethod())) {
return processSubDefinitionLookupElement(subDefinitionElement, completionProcessor);
}
if (element instanceof PerlSubDeclarationElement subDeclarationElement &&
(isStatic && subDeclarationElement.isStatic() || subDeclarationElement.isMethod())) {
return processSubDeclarationLookupElement(subDeclarationElement, completionProcessor);
}
if (element instanceof PerlGlobVariableElement perlGlobVariableElement && perlGlobVariableElement.isLeftSideOfAssignment()) {
if (StringUtil.isNotEmpty(element.getName())) {
return processGlobLookupElement(perlGlobVariableElement, completionProcessor);
}
}
return completionProcessor.result();
return switch (element) {
case PerlImplicitSubDefinition implicitSubDefinition
when implicitSubDefinition.isAnonymous() -> completionProcessor.result();

Check notice on line 230 in plugin/core/src/main/java/com/perl5/lang/perl/idea/completion/util/PerlSubCompletionUtil.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Duplicate branches in 'switch'

Branch in 'switch' is a duplicate of the default branch
case PerlSubDefinitionElement subDefinitionElement
when !subDefinitionElement.isAnonymous() &&
(isStatic && subDefinitionElement.isStatic() || subDefinitionElement.isMethod()) ->
processSubDefinitionLookupElement(subDefinitionElement, completionProcessor);
case PerlSubDeclarationElement subDeclarationElement
when (isStatic && subDeclarationElement.isStatic() || subDeclarationElement.isMethod()) ->
processSubDeclarationLookupElement(subDeclarationElement, completionProcessor);
case PerlGlobVariableElement perlGlobVariableElement
when perlGlobVariableElement.isLeftSideOfAssignment() && StringUtil.isNotEmpty(element.getName()) ->
processGlobLookupElement(perlGlobVariableElement, completionProcessor);
default -> completionProcessor.result();
};
}

@Override
Expand Down
Loading

0 comments on commit 693e151

Please sign in to comment.