001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2025 the original author or authors.
004//
005// This library is free software; you can redistribute it and/or
006// modify it under the terms of the GNU Lesser General Public
007// License as published by the Free Software Foundation; either
008// version 2.1 of the License, or (at your option) any later version.
009//
010// This library is distributed in the hope that it will be useful,
011// but WITHOUT ANY WARRANTY; without even the implied warranty of
012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013// Lesser General Public License for more details.
014//
015// You should have received a copy of the GNU Lesser General Public
016// License along with this library; if not, write to the Free Software
017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
018///////////////////////////////////////////////////////////////////////////////////////////////
019
020package com.puppycrawl.tools.checkstyle.checks.naming;
021
022import com.puppycrawl.tools.checkstyle.api.DetailAST;
023import com.puppycrawl.tools.checkstyle.api.TokenTypes;
024import com.puppycrawl.tools.checkstyle.utils.ScopeUtil;
025
026/**
027 * <div>
028 * Checks that local, non-{@code final} variable names conform to a specified pattern.
029 * A catch parameter is considered to be a local variable.
030 * </div>
031 *
032 * <p>
033 * This check does not support pattern variables. Instead, use
034 * <a href="https://checkstyle.org/checks/naming/patternvariablename.html">
035 * PatternVariableName</a>.
036 * </p>
037 *
038 * @since 3.0
039 */
040public class LocalVariableNameCheck
041    extends AbstractNameCheck {
042
043    /**
044     * Allow one character variable name in
045     * <a href="https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html">
046     * initialization expressions</a>
047     * in FOR loop if one char variable name is prohibited by {@code format} regexp.
048     */
049    private boolean allowOneCharVarInForLoop;
050
051    /** Creates a new {@code LocalVariableNameCheck} instance. */
052    public LocalVariableNameCheck() {
053        super("^([a-z][a-zA-Z0-9]*|_)$");
054    }
055
056    /**
057     * Setter to allow one character variable name in
058     * <a href="https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html">
059     * initialization expressions</a>
060     * in FOR loop if one char variable name is prohibited by {@code format} regexp.
061     *
062     * @param allow Flag for allowing or not one character name in FOR loop.
063     * @since 5.8
064     */
065    public final void setAllowOneCharVarInForLoop(boolean allow) {
066        allowOneCharVarInForLoop = allow;
067    }
068
069    @Override
070    public int[] getDefaultTokens() {
071        return getRequiredTokens();
072    }
073
074    @Override
075    public int[] getAcceptableTokens() {
076        return getRequiredTokens();
077    }
078
079    @Override
080    public int[] getRequiredTokens() {
081        return new int[] {
082            TokenTypes.VARIABLE_DEF,
083        };
084    }
085
086    @Override
087    protected final boolean mustCheckName(DetailAST ast) {
088        final boolean result;
089        if (allowOneCharVarInForLoop && isForLoopVariable(ast)) {
090            final String variableName = ast.findFirstToken(TokenTypes.IDENT).getText();
091            result = variableName.length() != 1;
092        }
093        else {
094            final DetailAST modifiersAST = ast.findFirstToken(TokenTypes.MODIFIERS);
095            final boolean isFinal = modifiersAST.findFirstToken(TokenTypes.FINAL) != null;
096            result = !isFinal && ScopeUtil.isLocalVariableDef(ast);
097        }
098        return result;
099    }
100
101    /**
102     * Checks if a variable is the loop's one.
103     *
104     * @param variableDef variable definition.
105     * @return true if a variable is the loop's one.
106     */
107    private static boolean isForLoopVariable(DetailAST variableDef) {
108        final int parentType = variableDef.getParent().getType();
109        return parentType == TokenTypes.FOR_INIT
110                || parentType == TokenTypes.FOR_EACH_CLAUSE;
111    }
112
113}