<commit_before>/*
 * Copyright 2003-2005 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.siyeh.ig.style;

import com.intellij.codeInsight.daemon.GroupNames;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.InspectionGadgetsFix;
import com.siyeh.ig.MethodInspection;
import com.siyeh.ig.fixes.RemoveModifierFix;
import com.siyeh.ig.psiutils.VariableAccessUtils;
import com.siyeh.InspectionGadgetsBundle;
import org.jetbrains.annotations.NotNull;

public class UnnecessaryFinalOnLocalVariableInspection extends MethodInspection {

  public String getGroupDisplayName() {
    return GroupNames.STYLE_GROUP_NAME;
  }

  public String buildErrorString(PsiElement location) {
    final PsiModifierList modifierList = (PsiModifierList)location.getParent();
    assert modifierList != null;
    final PsiVariable parameter = (PsiVariable)modifierList.getParent();
    assert parameter != null;
    final String parameterName = parameter.getName();
    return InspectionGadgetsBundle.message("unnecessary.final.on.local.variable.problem.descriptor", parameterName);
  }

  public BaseInspectionVisitor buildVisitor() {
    return new UnnecessaryFinalOnLocalVariableVisitor();
  }

  public InspectionGadgetsFix buildFix(PsiElement location) {
    return new RemoveModifierFix(location);
  }

  private static class UnnecessaryFinalOnLocalVariableVisitor extends BaseInspectionVisitor {

    public void visitDeclarationStatement(PsiDeclarationStatement statement) {
      super.visitDeclarationStatement(statement);
      final PsiElement[] declaredElements =
        statement.getDeclaredElements();
      if (declaredElements.length == 0) {
        return;
      }
      for (final PsiElement declaredElement : declaredElements) {
        if (!(declaredElement instanceof PsiLocalVariable)) {
          return;
        }
        final PsiLocalVariable variable = (PsiLocalVariable)declaredElement;
        if (!variable.hasModifierProperty(PsiModifier.FINAL)) {
          return;
        }
      }
      final PsiCodeBlock containingBlock =
        PsiTreeUtil.getParentOfType(statement, PsiCodeBlock.class);
      if (containingBlock == null) {
        return;
      }
      for (PsiElement declaredElement1 : declaredElements) {
        final PsiLocalVariable variable = (PsiLocalVariable)declaredElement1;
        if (VariableAccessUtils.variableIsUsedInInnerClass(variable, containingBlock)) {
          return;
        }
      }
      final PsiLocalVariable variable1 = (PsiLocalVariable)statement.getDeclaredElements()[0];
      registerModifierError(PsiModifier.FINAL, variable1);
    }

    public void visitTryStatement(@NotNull PsiTryStatement statement) {
      super.visitTryStatement(statement);
      final PsiCatchSection[] catchSections = statement.getCatchSections();
      for (PsiCatchSection catchSection : catchSections) {
        final PsiParameter parameter = catchSection.getParameter();
        final PsiCodeBlock catchBlock = catchSection.getCatchBlock();
        if (parameter == null || catchBlock == null) {
          continue;
        }
        if (parameter.hasModifierProperty(PsiModifier.FINAL) &&
            !VariableAccessUtils.variableIsUsedInInnerClass(parameter, catchBlock)) {
          registerModifierError(PsiModifier.FINAL, parameter);
        }
      }
    }

  }
}<commit_msg>fix for IDEADEV-4029<commit_after>/*
 * Copyright 2003-2005 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.siyeh.ig.style;

import com.intellij.codeInsight.daemon.GroupNames;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.InspectionGadgetsFix;
import com.siyeh.ig.MethodInspection;
import com.siyeh.ig.fixes.RemoveModifierFix;
import com.siyeh.ig.psiutils.VariableAccessUtils;
import com.siyeh.InspectionGadgetsBundle;
import org.jetbrains.annotations.NotNull;

public class UnnecessaryFinalOnLocalVariableInspection
        extends MethodInspection {

    public String getDisplayName() {
        return InspectionGadgetsBundle.message(
                "unnecessary.final.on.local.variable.display.name");
    }

    public String getGroupDisplayName() {
        return GroupNames.STYLE_GROUP_NAME;
    }

    public String buildErrorString(PsiElement location) {
        final PsiModifierList modifierList =
                (PsiModifierList)location.getParent();
        assert modifierList != null;
        final PsiVariable parameter = (PsiVariable)modifierList.getParent();
        assert parameter != null;
        final String parameterName = parameter.getName();
        return InspectionGadgetsBundle.message(
                "unnecessary.final.on.local.variable.problem.descriptor",
                parameterName);
    }

    public BaseInspectionVisitor buildVisitor() {
        return new UnnecessaryFinalOnLocalVariableVisitor();
    }

    public InspectionGadgetsFix buildFix(PsiElement location) {
        return new RemoveModifierFix(location);
    }

    private static class UnnecessaryFinalOnLocalVariableVisitor
            extends BaseInspectionVisitor {

        public void visitDeclarationStatement(
                PsiDeclarationStatement statement) {
            super.visitDeclarationStatement(statement);
            final PsiElement[] declaredElements =
                    statement.getDeclaredElements();
            if (declaredElements.length == 0) {
                return;
            }
            for (final PsiElement declaredElement : declaredElements) {
                if (!(declaredElement instanceof PsiLocalVariable)) {
                    return;
                }
                final PsiLocalVariable variable =
                        (PsiLocalVariable)declaredElement;
                if (!variable.hasModifierProperty(PsiModifier.FINAL)) {
                    return;
                }
            }
            final PsiCodeBlock containingBlock =
                    PsiTreeUtil.getParentOfType(statement, PsiCodeBlock.class);
            if (containingBlock == null) {
                return;
            }
            for (PsiElement declaredElement : declaredElements) {
                final PsiLocalVariable variable =
                        (PsiLocalVariable)declaredElement;
                if (VariableAccessUtils.variableIsUsedInInnerClass(variable,
                        containingBlock)) {
                    return;
                }
            }
            final PsiLocalVariable variable1 =
                    (PsiLocalVariable)statement.getDeclaredElements()[0];
            registerModifierError(PsiModifier.FINAL, variable1);
        }

        public void visitTryStatement(@NotNull PsiTryStatement statement) {
            super.visitTryStatement(statement);
            final PsiCatchSection[] catchSections =
                    statement.getCatchSections();
            for (PsiCatchSection catchSection : catchSections) {
                final PsiParameter parameter = catchSection.getParameter();
                final PsiCodeBlock catchBlock = catchSection.getCatchBlock();
                if (parameter == null || catchBlock == null) {
                    continue;
                }
                if (!parameter.hasModifierProperty(PsiModifier.FINAL)) {
                    continue;
                }
                if (!VariableAccessUtils.variableIsUsedInInnerClass(
                        parameter, catchBlock)) {
                    registerModifierError(PsiModifier.FINAL, parameter);
                }
            }
        }

        public void visitForeachStatement(PsiForeachStatement statement) {
            super.visitForeachStatement(statement);
            final PsiParameter parameter = statement.getIterationParameter();
            if (!parameter.hasModifierProperty(PsiModifier.FINAL)) {
                return;
            }
            if (VariableAccessUtils.variableIsUsedInInnerClass(parameter,
                    statement)) {
                return;
            }
            registerModifierError(PsiModifier.FINAL, parameter);
        }
    }
}
{'fraction_non_alphanumeric': 0.07557823867955261, 'fraction_numerical': 0.0041263980888261486, 'mean_word_length': 2.7062374245472838, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '7064567', 'n_tokens_mistral': 2336, 'n_tokens_neox': 2275, 'n_words': 529}