001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2026 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.coding;
021
022import com.puppycrawl.tools.checkstyle.StatelessCheck;
023import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
024import com.puppycrawl.tools.checkstyle.api.DetailAST;
025import com.puppycrawl.tools.checkstyle.api.TokenTypes;
026import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
027import com.puppycrawl.tools.checkstyle.utils.ScopeUtil;
028
029/**
030 * <div>
031 * Checks if unnecessary semicolon is used after type member declaration.
032 * </div>
033 *
034 * <p>
035 * Notes:
036 * This check is not applicable to empty statements (unnecessary semicolons inside methods or
037 * init blocks),
038 * <a href="https://checkstyle.org/checks/coding/emptystatement.html">EmptyStatement</a>
039 * is responsible for it.
040 * </p>
041 *
042 * @since 8.24
043 */
044@StatelessCheck
045public final class UnnecessarySemicolonAfterTypeMemberDeclarationCheck extends AbstractCheck {
046
047    /**
048     * A key is pointing to the warning message text in "messages.properties" file.
049     */
050    public static final String MSG_SEMI = "unnecessary.semicolon";
051
052    /**
053     * Creates a new {@code UnnecessarySemicolonAfterTypeMemberDeclarationCheck} instance.
054     */
055    public UnnecessarySemicolonAfterTypeMemberDeclarationCheck() {
056        // no code by default
057    }
058
059    @Override
060    public int[] getDefaultTokens() {
061        return getAcceptableTokens();
062    }
063
064    @Override
065    public int[] getAcceptableTokens() {
066        return new int[] {
067            TokenTypes.CLASS_DEF,
068            TokenTypes.INTERFACE_DEF,
069            TokenTypes.ENUM_DEF,
070            TokenTypes.ANNOTATION_DEF,
071            TokenTypes.VARIABLE_DEF,
072            TokenTypes.ANNOTATION_FIELD_DEF,
073            TokenTypes.STATIC_INIT,
074            TokenTypes.INSTANCE_INIT,
075            TokenTypes.CTOR_DEF,
076            TokenTypes.METHOD_DEF,
077            TokenTypes.ENUM_CONSTANT_DEF,
078            TokenTypes.COMPACT_CTOR_DEF,
079            TokenTypes.RECORD_DEF,
080        };
081    }
082
083    @Override
084    public int[] getRequiredTokens() {
085        return CommonUtil.EMPTY_INT_ARRAY;
086    }
087
088    @Override
089    public void visitToken(DetailAST ast) {
090        switch (ast.getType()) {
091            case TokenTypes.CLASS_DEF,
092                 TokenTypes.INTERFACE_DEF,
093                 TokenTypes.ENUM_DEF,
094                 TokenTypes.ANNOTATION_DEF,
095                 TokenTypes.RECORD_DEF -> checkTypeDefinition(ast);
096            case TokenTypes.VARIABLE_DEF -> checkVariableDefinition(ast);
097            case TokenTypes.ENUM_CONSTANT_DEF -> checkEnumConstant(ast);
098            default -> checkTypeMember(ast);
099        }
100    }
101
102    /**
103     * Checks if type member has unnecessary semicolon.
104     *
105     * @param ast type member
106     */
107    private void checkTypeMember(DetailAST ast) {
108        if (isSemicolon(ast.getNextSibling())) {
109            log(ast.getNextSibling(), MSG_SEMI);
110        }
111    }
112
113    /**
114     * Checks if type definition has unnecessary semicolon.
115     *
116     * @param ast type definition
117     */
118    private void checkTypeDefinition(DetailAST ast) {
119        if (!ScopeUtil.isOuterMostType(ast) && isSemicolon(ast.getNextSibling())) {
120            log(ast.getNextSibling(), MSG_SEMI);
121        }
122        final DetailAST firstMember =
123            ast.findFirstToken(TokenTypes.OBJBLOCK).getFirstChild().getNextSibling();
124        if (isSemicolon(firstMember) && !ScopeUtil.isInEnumBlock(firstMember)) {
125            log(firstMember, MSG_SEMI);
126        }
127    }
128
129    /**
130     * Checks if variable definition has unnecessary semicolon.
131     *
132     * @param variableDef variable definition
133     */
134    private void checkVariableDefinition(DetailAST variableDef) {
135        if (isSemicolon(variableDef.getLastChild()) && isSemicolon(variableDef.getNextSibling())) {
136            log(variableDef.getNextSibling(), MSG_SEMI);
137        }
138    }
139
140    /**
141     * Checks if enum constant has unnecessary semicolon.
142     *
143     * @param ast enum constant
144     */
145    private void checkEnumConstant(DetailAST ast) {
146        final DetailAST next = ast.getNextSibling();
147        if (isSemicolon(next) && isSemicolon(next.getNextSibling())) {
148            log(next.getNextSibling(), MSG_SEMI);
149        }
150    }
151
152    /**
153     * Checks that {@code ast} is a semicolon.
154     *
155     * @param ast token to check
156     * @return true if ast is semicolon, false otherwise
157     */
158    private static boolean isSemicolon(DetailAST ast) {
159        return ast != null && ast.getType() == TokenTypes.SEMI;
160    }
161
162}