PsiElementVisitor (@NotNull ProblemsHolder holder, boolean isOnTheFly) { if (!HighlightingFeature.ENHANCED_SWITCH.isAvailable(holder.getFile())) { return PsiElementVisitor.EMPTY_VISITOR; } return new JavaElementVisitor() { @Override public void visitSwitchLabeledRuleStatement(@NotNull PsiSwitchLabeledRuleStatement statement) { super.visitSwitchLabeledRuleStatement(statement); PsiSwitchBlock switchBlock = statement.getEnclosingSwitchBlock(); PsiStatement body = statement.getBody(); if (switchBlock != null && (body instanceof PsiExpressionStatement || body instanceof PsiThrowStatement)) { if (switchBlock instanceof PsiSwitchExpression && body instanceof PsiExpressionStatement) { registerProblem(statement, true); } else { registerProblem(statement, false); } } } private void registerProblem(@NotNull PsiSwitchLabeledRuleStatement statement, boolean isResultExpression) { holder.registerProblem(getProblemElement(statement), message(isResultExpression ? "inspection.switch.labeled.rule.can.be.code.block.expression.message" : "inspection.switch.labeled.rule.can.be.code.block.statement.message"), new WrapWithCodeBlockFix(isResultExpression)); } @NotNull private PsiElement getProblemElement(@NotNull PsiSwitchLabeledRuleStatement statement) { if (isOnTheFly) { if (InspectionProjectProfileManager.isInformationLevel(getShortName(), statement) || ApplicationManager.getApplication().isUnitTestMode()) { return statement; } } return ObjectUtils.notNull(ObjectUtils.tryCast(statement.getFirstChild(), PsiKeyword.class), statement); } }; }