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.blocks; 021 022import java.util.Arrays; 023import java.util.Locale; 024import java.util.Optional; 025 026import com.puppycrawl.tools.checkstyle.StatelessCheck; 027import com.puppycrawl.tools.checkstyle.api.AbstractCheck; 028import com.puppycrawl.tools.checkstyle.api.DetailAST; 029import com.puppycrawl.tools.checkstyle.api.TokenTypes; 030import com.puppycrawl.tools.checkstyle.utils.CodePointUtil; 031import com.puppycrawl.tools.checkstyle.utils.CommonUtil; 032 033/** 034 * <div> 035 * Checks for empty blocks. 036 * </div> 037 * 038 * <p> 039 * This check does not validate sequential blocks. This check does not violate fallthrough. 040 * </p> 041 * 042 * <p> 043 * NOTE: This check processes LITERAL_CASE and LITERAL_DEFAULT separately. 044 * Verification empty block is done for single nearest {@code case} or {@code default}. 045 * </p> 046 * 047 * @since 3.0 048 */ 049@StatelessCheck 050public class EmptyBlockCheck 051 extends AbstractCheck { 052 053 /** 054 * A key is pointing to the warning message text in "messages.properties" 055 * file. 056 */ 057 public static final String MSG_KEY_BLOCK_NO_STATEMENT = "block.noStatement"; 058 059 /** 060 * A key is pointing to the warning message text in "messages.properties" 061 * file. 062 */ 063 public static final String MSG_KEY_BLOCK_EMPTY = "block.empty"; 064 065 /** Specify the policy on block contents. */ 066 private BlockOption option = BlockOption.STATEMENT; 067 068 /** 069 * Setter to specify the policy on block contents. 070 * 071 * @param optionStr string to decode option from 072 * @throws IllegalArgumentException if unable to decode 073 * @since 3.0 074 */ 075 public void setOption(String optionStr) { 076 option = BlockOption.valueOf(optionStr.trim().toUpperCase(Locale.ENGLISH)); 077 } 078 079 @Override 080 public int[] getDefaultTokens() { 081 return new int[] { 082 TokenTypes.LITERAL_WHILE, 083 TokenTypes.LITERAL_TRY, 084 TokenTypes.LITERAL_FINALLY, 085 TokenTypes.LITERAL_DO, 086 TokenTypes.LITERAL_IF, 087 TokenTypes.LITERAL_ELSE, 088 TokenTypes.LITERAL_FOR, 089 TokenTypes.INSTANCE_INIT, 090 TokenTypes.STATIC_INIT, 091 TokenTypes.LITERAL_SWITCH, 092 TokenTypes.LITERAL_SYNCHRONIZED, 093 }; 094 } 095 096 @Override 097 public int[] getAcceptableTokens() { 098 return new int[] { 099 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}