void (Map<PsiCaseLabelElement, ThreeState> labelReachability, ProblemsHolder holder) { if (labelReachability.isEmpty()) return; Set<PsiSwitchBlock> coveredSwitches = new HashSet<>(); Map<PsiCaseLabelElement, PsiSwitchBlock> unreachableLabels = new HashMap<>(); for (Map.Entry<PsiCaseLabelElement, ThreeState> entry : labelReachability.entrySet()) { if (entry.getValue() != ThreeState.YES) continue; PsiCaseLabelElement label = entry.getKey(); PsiSwitchLabelStatementBase labelStatement = PsiImplUtil.getSwitchLabel(label); if (labelStatement == null) continue; // could be a guard PsiExpression guardExpression = labelStatement.getGuardExpression(); if (guardExpression != null) { ThreeState guardReachability = labelReachability.get(guardExpression); if (guardReachability != ThreeState.YES) continue; } PsiSwitchBlock switchBlock = labelStatement.getEnclosingSwitchBlock(); if (switchBlock == null) continue; if (!canRemoveTheOnlyReachableLabel(label, switchBlock)) continue; if (!StreamEx.iterate(labelStatement, Objects::nonNull, l -> PsiTreeUtil.getPrevSiblingOfType(l, PsiSwitchLabelStatementBase.class)) .skip(1).map(PsiSwitchLabelStatementBase::getCaseLabelElementList) .nonNull().flatArray(PsiCaseLabelElementList::getElements) .append(StreamEx.iterate(label, Objects::nonNull, l -> PsiTreeUtil.getPrevSiblingOfType(l, PsiCaseLabelElement.class)).skip(1)) .allMatch(l -> labelReachability.get(l) == ThreeState.NO)) { // Add all labels after always-reachable one as unreachable StreamEx.iterate(labelStatement, Objects::nonNull, l -> PsiTreeUtil.getNextSiblingOfType(l, PsiSwitchLabelStatementBase.class)) .remove(SwitchUtils::isDefaultLabel) .skip(1).map(PsiSwitchLabelStatementBase::getCaseLabelElementList) .nonNull().flatArray(PsiCaseLabelElementList::getElements) .append(StreamEx.iterate(label, Objects::nonNull, l -> PsiTreeUtil.getNextSiblingOfType(l, PsiCaseLabelElement.class)).skip(1)) .forEach(l -> unreachableLabels.put(l, switchBlock)); continue; } coveredSwitches.add(switchBlock); LocalQuickFix unwrapFix; if ((switchBlock instanceof PsiSwitchExpression && !CodeBlockSurrounder.canSurround(((PsiSwitchExpression)switchBlock))) || SwitchUtils.findRemovableUnreachableBranches(label, switchBlock).isEmpty()) { unwrapFix = null; } else { unwrapFix = createUnwrapSwitchLabelFix(); } holder.registerProblem(label, JavaAnalysisBundle.message("dataflow.message.only.switch.label"), LocalQuickFix.notNullElements(unwrapFix)); } for (Map.Entry<PsiCaseLabelElement, ThreeState> entry : labelReachability.entrySet()) { if (entry.getValue() != ThreeState.NO) continue; PsiCaseLabelElement label = entry.getKey(); PsiSwitchLabelStatementBase labelStatement = PsiImplUtil.getSwitchLabel(label); if (labelStatement == null) continue; PsiSwitchBlock switchBlock = labelStatement.getEnclosingSwitchBlock(); if (switchBlock == null || coveredSwitches.contains(switchBlock)) continue; unreachableLabels.put(label, switchBlock); } unreachableLabels.forEach((label, switchBlock) -> { if (isThrowing(label)) return; // duplicate case label is a compilation error so no need to highlight by the inspection Set<PsiElement> suspiciousElements = SwitchBlockHighlightingModel.findSuspiciousLabelElements(switchBlock); if (!suspiciousElements.contains(label)) { holder.problem(label, JavaAnalysisBundle.message("dataflow.message.unreachable.switch.label")) .maybeFix(createDeleteLabelFix(label)).register(); } }); }