PsiElementVisitor (@NotNull ProblemsHolder holder, boolean isOnTheFly) { return new JavaElementVisitor() { @Override public void visitSwitchStatement(@NotNull PsiSwitchStatement statement) { visitSwitchBlock(statement); } @Override public void visitSwitchExpression(@NotNull PsiSwitchExpression expression) { visitSwitchBlock(expression); } private void visitSwitchBlock(PsiSwitchBlock block) { PsiCodeBlock body = block.getBody(); if (body == null) return; PsiExpression selector = block.getExpression(); if (selector == null || !TypeUtils.isJavaLangString(selector.getType())) return; // Matching of constant selector against constant labels is processed by Constant Conditions & Exceptions if (PsiUtil.isConstantExpression(selector)) return; StringCase selectorCase = fromExpression(selector, ANALYSIS_COMPLEXITY); if (selectorCase.myHasUpper != ThreeState.NO && selectorCase.myHasLower != ThreeState.NO) return; for (PsiStatement statement : body.getStatements()) { if (statement instanceof PsiSwitchLabelStatementBase) { PsiCaseLabelElementList labels = ((PsiSwitchLabelStatementBase)statement).getCaseLabelElementList(); if (labels != null) { for (PsiCaseLabelElement label : labels.getElements()) { if (label instanceof PsiExpression) { StringCase labelCase = fromExpression((PsiExpression)label, ANALYSIS_COMPLEXITY); String errorMessage; if (selectorCase.myHasLower == ThreeState.NO && labelCase.myHasLower == ThreeState.YES) { errorMessage = InspectionGadgetsBundle.message("inspection.case.mismatch.message.label.is.lower"); } else if (selectorCase.myHasUpper == ThreeState.NO && labelCase.myHasUpper == ThreeState.YES) { errorMessage = InspectionGadgetsBundle.message("inspection.case.mismatch.message.label.is.upper"); } else { continue; } holder.registerProblem(label, errorMessage); } } } } } } @Override public void visitMethodCallExpression(@NotNull PsiMethodCallExpression call) { if (!STRING_COMPARISON_METHODS.test(call)) return; PsiExpression arg = ArrayUtil.getFirstElement(call.getArgumentList().getExpressions()); PsiExpression qualifier = call.getMethodExpression().getQualifierExpression(); StringCase argCase = fromExpression(arg, ANALYSIS_COMPLEXITY); if (argCase.myHasUpper != ThreeState.YES && argCase.myHasLower != ThreeState.YES) return; StringCase qualifierCase = fromExpression(qualifier, ANALYSIS_COMPLEXITY); PsiElement anchor = Objects.requireNonNull(call.getMethodExpression().getReferenceNameElement()); String methodName = anchor.getText(); String returnValue = switch (methodName) { case "indexOf", "lastIndexOf" -> "-1"; default -> "false"; }; String errorMessage; if (qualifierCase.myHasLower == ThreeState.NO && argCase.myHasLower == ThreeState.YES) { errorMessage = InspectionGadgetsBundle.message("inspection.case.mismatch.message.arg.is.lower", methodName, returnValue); } else if (qualifierCase.myHasUpper == ThreeState.NO && argCase.myHasUpper == ThreeState.YES) { errorMessage = InspectionGadgetsBundle.message("inspection.case.mismatch.message.arg.is.upper", methodName, returnValue); } else { return; } holder.registerProblem(anchor, errorMessage); } }; }