<commit_before>// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.codeInspection;

import com.intellij.codeInsight.BlockUtils;
import com.intellij.openapi.project.Project;
import com.intellij.profile.codeInspection.InspectionProjectProfileManager;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiTypesUtil;
import com.siyeh.ig.psiutils.CommentTracker;
import com.siyeh.ig.psiutils.ControlFlowUtils;
import gnu.trove.TIntArrayList;
import one.util.streamex.StreamEx;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;
import java.util.StringJoiner;

import static com.intellij.util.ObjectUtils.tryCast;

public class EnhancedSwitchBackwardMigrationInspection extends AbstractBaseJavaLocalInspectionTool {
  private static final SwitchMigrationCase[] ourCases = new SwitchMigrationCase[]{
    EnhancedSwitchBackwardMigrationInspection::inspectReturningSwitch,
    EnhancedSwitchBackwardMigrationInspection::inspectVariableSavingSwitch,
    EnhancedSwitchBackwardMigrationInspection::inspectSwitchStatement
  };

  @NotNull
  @Override
  public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly) {
    return new JavaElementVisitor() {
      @Override
      public void visitSwitchExpression(PsiSwitchExpression expression) {
        if (!isNonemptyRuleFormatSwitch(expression)) return;
        if (findReplacer(expression) == null) return;
        String message = InspectionsBundle.message("inspection.switch.expression.backward.expression.migration.inspection.name");
        PsiElement problemElement =
          (InspectionProjectProfileManager.isInformationLevel(getShortName(), expression)) ? expression : expression.getFirstChild();
        holder.registerProblem(problemElement, message, new ReplaceWithOldStyleSwitchFix());
      }

      @Override
      public void visitSwitchStatement(PsiSwitchStatement statement) {
        if (!isNonemptyRuleFormatSwitch(statement)) return;
        if (findReplacer(statement) == null) return;
        String message = InspectionsBundle.message("inspection.switch.expression.backward.statement.migration.inspection.name");
        holder.registerProblem(statement.getFirstChild(), message, new ReplaceWithOldStyleSwitchFix());
      }

      private boolean isNonemptyRuleFormatSwitch(PsiSwitchBlock block) {
        PsiSwitchLabelStatementBase label = PsiTreeUtil.getChildOfType(block.getBody(), PsiSwitchLabelStatementBase.class);
        return label instanceof PsiSwitchLabeledRuleStatement;
      }
    };
  }

  private static Replacer findReplacer(@NotNull PsiSwitchBlock block) {
    for (SwitchMigrationCase migrationCase : ourCases) {<commit_msg>Narrow fix range again (IDEA-CR-53103)<commit_after>// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.codeInspection;

import com.intellij.codeInsight.BlockUtils;
import com.intellij.openapi.project.Project;
import com.intellij.profile.codeInspection.InspectionProjectProfileManager;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiTypesUtil;
import com.siyeh.ig.psiutils.CommentTracker;
import com.siyeh.ig.psiutils.ControlFlowUtils;
import gnu.trove.TIntArrayList;
import one.util.streamex.StreamEx;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;
import java.util.StringJoiner;

import static com.intellij.util.ObjectUtils.tryCast;

public class EnhancedSwitchBackwardMigrationInspection extends AbstractBaseJavaLocalInspectionTool {
  private static final SwitchMigrationCase[] ourCases = new SwitchMigrationCase[]{
    EnhancedSwitchBackwardMigrationInspection::inspectReturningSwitch,
    EnhancedSwitchBackwardMigrationInspection::inspectVariableSavingSwitch,
    EnhancedSwitchBackwardMigrationInspection::inspectSwitchStatement
  };

  @NotNull
  @Override
  public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly) {
    return new JavaElementVisitor() {
      @Override
      public void visitSwitchExpression(PsiSwitchExpression expression) {
        if (!isNonemptyRuleFormatSwitch(expression)) return;
        if (findReplacer(expression) == null) return;
        String message = InspectionsBundle.message("inspection.switch.expression.backward.expression.migration.inspection.name");
        holder.registerProblem(expression.getFirstChild(), message, new ReplaceWithOldStyleSwitchFix());
      }

      @Override
      public void visitSwitchStatement(PsiSwitchStatement statement) {
        if (!isNonemptyRuleFormatSwitch(statement)) return;
        if (findReplacer(statement) == null) return;
        String message = InspectionsBundle.message("inspection.switch.expression.backward.statement.migration.inspection.name");
        holder.registerProblem(statement.getFirstChild(), message, new ReplaceWithOldStyleSwitchFix());
      }

      private boolean isNonemptyRuleFormatSwitch(PsiSwitchBlock block) {
        PsiSwitchLabelStatementBase label = PsiTreeUtil.getChildOfType(block.getBody(), PsiSwitchLabelStatementBase.class);
        return label instanceof PsiSwitchLabeledRuleStatement;
      }
    };
  }

  private static Replacer findReplacer(@NotNull PsiSwitchBlock block) {
    for (SwitchMigrationCase migrationCase : ourCases) {
{'fraction_non_alphanumeric': 0.08159691822798108, 'fraction_numerical': 0.0043775170723165824, 'mean_word_length': 5.382122905027933, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2898022', 'n_tokens_mistral': 1602, 'n_tokens_neox': 1555, 'n_words': 308}