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 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 * Finds nested blocks (blocks that are used freely in the code). 030 * </div> 031 * 032 * <p> 033 * Rationale: Nested blocks are often leftovers from the 034 * debugging process, they confuse the reader. 035 * </p> 036 * 037 * <p> 038 * For example, this check finds the obsolete braces in 039 * </p> 040 * <div class="wrapper"><pre class="prettyprint"><code class="language-java"> 041 * public void guessTheOutput() 042 * { 043 * int whichIsWhich = 0; 044 * { 045 * whichIsWhich = 2; 046 * } 047 * System.out.println("value = " + whichIsWhich); 048 * } 049 * </code></pre></div> 050 * 051 * <p> 052 * and debugging / refactoring leftovers such as 053 * </p> 054 * <div class="wrapper"><pre class="prettyprint"><code class="language-java"> 055 * // if (conditionThatIsNotUsedAnyLonger) 056 * { 057 * System.out.println("unconditional"); 058 * } 059 * </code></pre></div> 060 * 061 * <p> 062 * A case in a switch statement does not implicitly form a block. 063 * Thus, to be able to introduce local variables that have case scope 064 * it is necessary to open a nested block. This is supported, set 065 * the allowInSwitchCase property to true and include all statements 066 * of the case in the block. 067 * </p> 068 * 069 * @since 3.1 070 */ 071@StatelessCheck 072public class AvoidNestedBlocksCheck extends AbstractCheck { 073 074 /** 075 * A key is pointing to the warning message text in "messages.properties" 076 * file. 077 */ 078 public static final String MSG_KEY_BLOCK_NESTED = "block.nested"; 079 080 /** 081 * Allow nested blocks if they are the only child of a switch case. 082 */ 083 private boolean allowInSwitchCase; 084 085 @Override 086 public int[] getDefaultTokens() { 087 return getRequiredTokens(); 088 } 089 090 @Override 091 public int[] getAcceptableTokens() { 092 return getRequiredTokens(); 093 } 094 095 @Override 096 public int[] getRequiredTokens() { 097 return new int[] {TokenTypes.SLIST}; 098 } 099 100 @Override 101 public void visitToken(DetailAST ast) { 102 final DetailAST parent = ast.getParent(); 103 if (parent.getType() == TokenTypes.SLIST 104 && (!allowInSwitchCase || hasSiblings(ast))) { 105 log(ast, MSG_KEY_BLOCK_NESTED); 106 } 107 } 108 109 /** 110 * Checks whether the AST node has any siblings or not. 111 * 112 * @param ast node to examine 113 * @return {@code true} if the node has one or more siblings 114 */ 115 private static boolean hasSiblings(DetailAST ast) { 116 return ast.getPreviousSibling() != null || ast.getNextSibling() != null; 117 } 118 119 /** 120 * Setter to allow nested blocks if they are the only child of a switch case. 121 * 122 * @param allowInSwitchCase whether nested blocks are allowed 123 * if they are the only child of a switch case. 124 * @since 3.2 125 */ 126 public void setAllowInSwitchCase(boolean allowInSwitchCase) { 127 this.allowInSwitchCase = allowInSwitchCase; 128 } 129 130}