View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2025 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.blocks;
21  
22  import java.util.Arrays;
23  import java.util.Locale;
24  import java.util.Optional;
25  
26  import com.puppycrawl.tools.checkstyle.StatelessCheck;
27  import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
28  import com.puppycrawl.tools.checkstyle.api.DetailAST;
29  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
30  import com.puppycrawl.tools.checkstyle.utils.CodePointUtil;
31  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
32  
33  /**
34   * <div>
35   * Checks for empty blocks.
36   * </div>
37   *
38   * <p>
39   * This check does not validate sequential blocks. This check does not violate fallthrough.
40   * </p>
41   *
42   * <p>
43   * NOTE: This check processes LITERAL_CASE and LITERAL_DEFAULT separately.
44   * Verification empty block is done for single nearest {@code case} or {@code default}.
45   * </p>
46   *
47   * @since 3.0
48   */
49  @StatelessCheck
50  public class EmptyBlockCheck
51      extends AbstractCheck {
52  
53      /**
54       * A key is pointing to the warning message text in "messages.properties"
55       * file.
56       */
57      public static final String MSG_KEY_BLOCK_NO_STATEMENT = "block.noStatement";
58  
59      /**
60       * A key is pointing to the warning message text in "messages.properties"
61       * file.
62       */
63      public static final String MSG_KEY_BLOCK_EMPTY = "block.empty";
64  
65      /** Specify the policy on block contents. */
66      private BlockOption option = BlockOption.STATEMENT;
67  
68      /**
69       * Setter to specify the policy on block contents.
70       *
71       * @param optionStr string to decode option from
72       * @throws IllegalArgumentException if unable to decode
73       * @since 3.0
74       */
75      public void setOption(String optionStr) {
76          option = BlockOption.valueOf(optionStr.trim().toUpperCase(Locale.ENGLISH));
77      }
78  
79      @Override
80      public int[] getDefaultTokens() {
81          return new int[] {
82              TokenTypes.LITERAL_WHILE,
83              TokenTypes.LITERAL_TRY,
84              TokenTypes.LITERAL_FINALLY,
85              TokenTypes.LITERAL_DO,
86              TokenTypes.LITERAL_IF,
87              TokenTypes.LITERAL_ELSE,
88              TokenTypes.LITERAL_FOR,
89              TokenTypes.INSTANCE_INIT,
90              TokenTypes.STATIC_INIT,
91              TokenTypes.LITERAL_SWITCH,
92              TokenTypes.LITERAL_SYNCHRONIZED,
93          };
94      }
95  
96      @Override
97      public int[] getAcceptableTokens() {
98          return new int[] {
99              TokenTypes.LITERAL_WHILE,
100             TokenTypes.LITERAL_TRY,
101             TokenTypes.LITERAL_CATCH,
102             TokenTypes.LITERAL_FINALLY,
103             TokenTypes.LITERAL_DO,
104             TokenTypes.LITERAL_IF,
105             TokenTypes.LITERAL_ELSE,
106             TokenTypes.LITERAL_FOR,
107             TokenTypes.INSTANCE_INIT,
108             TokenTypes.STATIC_INIT,
109             TokenTypes.LITERAL_SWITCH,
110             TokenTypes.LITERAL_SYNCHRONIZED,
111             TokenTypes.LITERAL_CASE,
112             TokenTypes.LITERAL_DEFAULT,
113             TokenTypes.ARRAY_INIT,
114         };
115     }
116 
117     @Override
118     public int[] getRequiredTokens() {
119         return CommonUtil.EMPTY_INT_ARRAY;
120     }
121 
122     @Override
123     public void visitToken(DetailAST ast) {
124         final Optional<DetailAST> leftCurly = getLeftCurly(ast);
125         if (leftCurly.isPresent()) {
126             final DetailAST leftCurlyAST = leftCurly.orElseThrow();
127             if (option == BlockOption.STATEMENT) {
128                 final boolean emptyBlock;
129                 if (leftCurlyAST.getType() == TokenTypes.LCURLY) {
130                     final DetailAST nextSibling = leftCurlyAST.getNextSibling();
131                     emptyBlock = nextSibling.getType() != TokenTypes.CASE_GROUP
132                             && nextSibling.getType() != TokenTypes.SWITCH_RULE;
133                 }
134                 else {
135                     emptyBlock = leftCurlyAST.getChildCount() <= 1;
136                 }
137                 if (emptyBlock) {
138                     log(leftCurlyAST,
139                         MSG_KEY_BLOCK_NO_STATEMENT);
140                 }
141             }
142             else if (!hasText(leftCurlyAST)) {
143                 log(leftCurlyAST,
144                     MSG_KEY_BLOCK_EMPTY,
145                     ast.getText());
146             }
147         }
148     }
149 
150     /**
151      * Checks if SLIST token contains any text.
152      *
153      * @param slistAST a {@code DetailAST} value
154      * @return whether the SLIST token contains any text.
155      */
156     private boolean hasText(final DetailAST slistAST) {
157         final DetailAST rightCurly = slistAST.findFirstToken(TokenTypes.RCURLY);
158         final DetailAST rcurlyAST;
159 
160         if (rightCurly == null) {
161             rcurlyAST = slistAST.getParent().findFirstToken(TokenTypes.RCURLY);
162         }
163         else {
164             rcurlyAST = rightCurly;
165         }
166         final int slistLineNo = slistAST.getLineNo();
167         final int slistColNo = slistAST.getColumnNo();
168         final int rcurlyLineNo = rcurlyAST.getLineNo();
169         final int rcurlyColNo = rcurlyAST.getColumnNo();
170         boolean returnValue = false;
171         if (slistLineNo == rcurlyLineNo) {
172             // Handle braces on the same line
173             final int[] txt = Arrays.copyOfRange(getLineCodePoints(slistLineNo - 1),
174                     slistColNo + 1, rcurlyColNo);
175 
176             if (!CodePointUtil.isBlank(txt)) {
177                 returnValue = true;
178             }
179         }
180         else {
181             final int[] codePointsFirstLine = getLineCodePoints(slistLineNo - 1);
182             final int[] firstLine = Arrays.copyOfRange(codePointsFirstLine,
183                     slistColNo + 1, codePointsFirstLine.length);
184             final int[] codePointsLastLine = getLineCodePoints(rcurlyLineNo - 1);
185             final int[] lastLine = Arrays.copyOfRange(codePointsLastLine, 0, rcurlyColNo);
186             // check if all lines are also only whitespace
187             returnValue = !(CodePointUtil.isBlank(firstLine) && CodePointUtil.isBlank(lastLine))
188                     || !checkIsAllLinesAreWhitespace(slistLineNo, rcurlyLineNo);
189         }
190         return returnValue;
191     }
192 
193     /**
194      * Checks is all lines from 'lineFrom' to 'lineTo' (exclusive)
195      * contain whitespaces only.
196      *
197      * @param lineFrom
198      *            check from this line number
199      * @param lineTo
200      *            check to this line numbers
201      * @return true if lines contain only whitespaces
202      */
203     private boolean checkIsAllLinesAreWhitespace(int lineFrom, int lineTo) {
204         boolean result = true;
205         for (int i = lineFrom; i < lineTo - 1; i++) {
206             if (!CodePointUtil.isBlank(getLineCodePoints(i))) {
207                 result = false;
208                 break;
209             }
210         }
211         return result;
212     }
213 
214     /**
215      * Calculates the left curly corresponding to the block to be checked.
216      *
217      * @param ast a {@code DetailAST} value
218      * @return the left curly corresponding to the block to be checked
219      */
220     private static Optional<DetailAST> getLeftCurly(DetailAST ast) {
221         final DetailAST parent = ast.getParent();
222         final int parentType = parent.getType();
223         final Optional<DetailAST> leftCurly;
224 
225         if (parentType == TokenTypes.SWITCH_RULE) {
226             // get left curly of a case or default that is in switch rule
227             leftCurly = Optional.ofNullable(parent.findFirstToken(TokenTypes.SLIST));
228         }
229         else if (parentType == TokenTypes.CASE_GROUP) {
230             // get left curly of a case or default that is in switch statement
231             leftCurly = Optional.ofNullable(ast.getNextSibling())
232                          .map(DetailAST::getFirstChild)
233                          .filter(node -> node.getType() == TokenTypes.SLIST);
234         }
235         else if (ast.findFirstToken(TokenTypes.SLIST) != null) {
236             // we have a left curly that is part of a statement list, but not in a case or default
237             leftCurly = Optional.of(ast.findFirstToken(TokenTypes.SLIST));
238         }
239         else {
240             // get the first left curly that we can find, if it is present
241             leftCurly = Optional.ofNullable(ast.findFirstToken(TokenTypes.LCURLY));
242         }
243         return leftCurly;
244     }
245 
246 }