View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ///////////////////////////////////////////////////////////////////////////////////////////////
19  
20  package com.puppycrawl.tools.checkstyle.checks.coding;
21  
22  import com.puppycrawl.tools.checkstyle.StatelessCheck;
23  import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
24  import com.puppycrawl.tools.checkstyle.api.DetailAST;
25  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
26  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
27  import com.puppycrawl.tools.checkstyle.utils.ScopeUtil;
28  
29  /**
30   * <div>
31   * Checks if unnecessary semicolon is used after type member declaration.
32   * </div>
33   *
34   * <p>
35   * Notes:
36   * This check is not applicable to empty statements (unnecessary semicolons inside methods or
37   * init blocks),
38   * <a href="https://checkstyle.org/checks/coding/emptystatement.html">EmptyStatement</a>
39   * is responsible for it.
40   * </p>
41   *
42   * @since 8.24
43   */
44  @StatelessCheck
45  public final class UnnecessarySemicolonAfterTypeMemberDeclarationCheck extends AbstractCheck {
46  
47      /**
48       * A key is pointing to the warning message text in "messages.properties"
49       * file.
50       */
51      public static final String MSG_SEMI = "unnecessary.semicolon";
52  
53      /**
54       * Creates a new {@code UnnecessarySemicolonAfterTypeMemberDeclarationCheck} instance.
55       */
56      public UnnecessarySemicolonAfterTypeMemberDeclarationCheck() {
57          // no code by default
58      }
59  
60      @Override
61      public int[] getDefaultTokens() {
62          return getAcceptableTokens();
63      }
64  
65      @Override
66      public int[] getAcceptableTokens() {
67          return new int[] {
68              TokenTypes.CLASS_DEF,
69              TokenTypes.INTERFACE_DEF,
70              TokenTypes.ENUM_DEF,
71              TokenTypes.ANNOTATION_DEF,
72              TokenTypes.VARIABLE_DEF,
73              TokenTypes.ANNOTATION_FIELD_DEF,
74              TokenTypes.STATIC_INIT,
75              TokenTypes.INSTANCE_INIT,
76              TokenTypes.CTOR_DEF,
77              TokenTypes.METHOD_DEF,
78              TokenTypes.ENUM_CONSTANT_DEF,
79              TokenTypes.COMPACT_CTOR_DEF,
80              TokenTypes.RECORD_DEF,
81          };
82      }
83  
84      @Override
85      public int[] getRequiredTokens() {
86          return CommonUtil.EMPTY_INT_ARRAY;
87      }
88  
89      @Override
90      public void visitToken(DetailAST ast) {
91          switch (ast.getType()) {
92              case TokenTypes.CLASS_DEF,
93                   TokenTypes.INTERFACE_DEF,
94                   TokenTypes.ENUM_DEF,
95                   TokenTypes.ANNOTATION_DEF,
96                   TokenTypes.RECORD_DEF -> checkTypeDefinition(ast);
97              case TokenTypes.VARIABLE_DEF -> checkVariableDefinition(ast);
98              case TokenTypes.ENUM_CONSTANT_DEF -> checkEnumConstant(ast);
99              default -> checkTypeMember(ast);
100         }
101     }
102 
103     /**
104      * Checks if type member has unnecessary semicolon.
105      *
106      * @param ast type member
107      */
108     private void checkTypeMember(DetailAST ast) {
109         if (isSemicolon(ast.getNextSibling())) {
110             log(ast.getNextSibling(), MSG_SEMI);
111         }
112     }
113 
114     /**
115      * Checks if type definition has unnecessary semicolon.
116      *
117      * @param ast type definition
118      */
119     private void checkTypeDefinition(DetailAST ast) {
120         if (!ScopeUtil.isOuterMostType(ast) && isSemicolon(ast.getNextSibling())) {
121             log(ast.getNextSibling(), MSG_SEMI);
122         }
123         final DetailAST firstMember =
124             ast.findFirstToken(TokenTypes.OBJBLOCK).getFirstChild().getNextSibling();
125         if (isSemicolon(firstMember) && !ScopeUtil.isInEnumBlock(firstMember)) {
126             log(firstMember, MSG_SEMI);
127         }
128     }
129 
130     /**
131      * Checks if variable definition has unnecessary semicolon.
132      *
133      * @param variableDef variable definition
134      */
135     private void checkVariableDefinition(DetailAST variableDef) {
136         if (isSemicolon(variableDef.getLastChild()) && isSemicolon(variableDef.getNextSibling())) {
137             log(variableDef.getNextSibling(), MSG_SEMI);
138         }
139     }
140 
141     /**
142      * Checks if enum constant has unnecessary semicolon.
143      *
144      * @param ast enum constant
145      */
146     private void checkEnumConstant(DetailAST ast) {
147         final DetailAST next = ast.getNextSibling();
148         if (isSemicolon(next) && isSemicolon(next.getNextSibling())) {
149             log(next.getNextSibling(), MSG_SEMI);
150         }
151     }
152 
153     /**
154      * Checks that {@code ast} is a semicolon.
155      *
156      * @param ast token to check
157      * @return true if ast is semicolon, false otherwise
158      */
159     private static boolean isSemicolon(DetailAST ast) {
160         return ast != null && ast.getType() == TokenTypes.SEMI;
161     }
162 
163 }