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;
026
027/**
028 * <div>
029 * Detects empty statements (standalone {@code ";"} semicolon).
030 * Empty statements often introduce bugs that are hard to spot
031 * </div>
032 *
033 * @since 3.1
034 */
035@StatelessCheck
036public class EmptyStatementCheck extends AbstractCheck {
037
038    /**
039     * A key is pointing to the warning message text in "messages.properties" file.
040     */
041    public static final String MSG_KEY = "empty.statement";
042
043    /**
044     * Creates a new {@code EmptyStatementCheck} instance.
045     */
046    public EmptyStatementCheck() {
047        // no code by default
048    }
049
050    @Override
051    public int[] getDefaultTokens() {
052        return getRequiredTokens();
053    }
054
055    @Override
056    public int[] getAcceptableTokens() {
057        return getRequiredTokens();
058    }
059
060    @Override
061    public int[] getRequiredTokens() {
062        return new int[] {TokenTypes.EMPTY_STAT, TokenTypes.SEMI};
063    }
064
065    @Override
066    public void visitToken(DetailAST ast) {
067        if (ast.getType() == TokenTypes.EMPTY_STAT || isEmptyDeclaration(ast)) {
068            log(ast, MSG_KEY);
069        }
070    }
071
072    /**
073     * Checks whether a semicolon is a standalone empty declaration, i.e. it
074     * appears at the top level of a compilation unit or directly in a type body.
075     *
076     * @param semi semicolon token
077     * @return {@code true} if semicolon is an empty declaration
078     */
079    private static boolean isEmptyDeclaration(DetailAST semi) {
080        final int parentType = semi.getParent().getType();
081        return parentType == TokenTypes.COMPILATION_UNIT
082            || parentType == TokenTypes.COMPACT_COMPILATION_UNIT
083            || parentType == TokenTypes.OBJBLOCK && !isEnumConstantsTerminator(semi);
084    }
085
086    /**
087     * Checks whether a semicolon in a type body terminates the list of enum constants.
088     * Such a semicolon is part of the enum syntax and is not an empty declaration.
089     *
090     * @param semi semicolon token whose parent is an object block
091     * @return {@code true} if semicolon terminates the enum constants list
092     */
093    private static boolean isEnumConstantsTerminator(DetailAST semi) {
094        final int typeDefType = semi.getParent().getParent().getType();
095        final int previousType = semi.getPreviousSibling().getType();
096        return typeDefType == TokenTypes.ENUM_DEF
097            && (previousType == TokenTypes.LCURLY
098                || previousType == TokenTypes.ENUM_CONSTANT_DEF
099                || previousType == TokenTypes.COMMA);
100    }
101
102}