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.TokenUtil;
27
28 /**
29 * <div>
30 * Checks for over-complicated boolean return or yield statements.
31 * For example the following code
32 * </div>
33 * {@snippet lang="text" :
34 * if (valid())
35 * return false;
36 * else
37 * return true;
38 * }
39 *
40 * <p>
41 * could be written as
42 * </p>
43 * {@snippet lang="text" :
44 * return !valid();
45 * }
46 *
47 * <p>
48 * The idea for this Check has been shamelessly stolen from the equivalent
49 * <a href="https://pmd.github.io/pmd/pmd_rules_java_design.html#simplifybooleanreturns">
50 * PMD</a> rule.
51 * </p>
52 *
53 * @since 3.0
54 */
55 @StatelessCheck
56 public class SimplifyBooleanReturnCheck
57 extends AbstractCheck {
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 = "simplify.boolReturn";
64
65 /**
66 * Creates a new {@code SimplifyBooleanReturnCheck} instance.
67 */
68 public SimplifyBooleanReturnCheck() {
69 // no code by default
70 }
71
72 @Override
73 public int[] getAcceptableTokens() {
74 return getRequiredTokens();
75 }
76
77 @Override
78 public int[] getDefaultTokens() {
79 return getRequiredTokens();
80 }
81
82 @Override
83 public int[] getRequiredTokens() {
84 return new int[] {TokenTypes.LITERAL_IF};
85 }
86
87 @Override
88 public void visitToken(DetailAST ast) {
89 // LITERAL_IF has the following four or five children:
90 // '('
91 // condition
92 // ')'
93 // thenStatement
94 // [ LITERAL_ELSE (with the elseStatement as a child) ]
95
96 // don't bother if this is not if then else
97 final DetailAST elseLiteral =
98 ast.findFirstToken(TokenTypes.LITERAL_ELSE);
99 if (elseLiteral != null) {
100 final DetailAST elseStatement = elseLiteral.getFirstChild();
101
102 // skip '(' and ')'
103 final DetailAST condition = ast.getFirstChild().getNextSibling();
104 final DetailAST thenStatement = condition.getNextSibling().getNextSibling();
105
106 if (canReturnOrYieldOnlyBooleanLiteral(thenStatement)
107 && canReturnOrYieldOnlyBooleanLiteral(elseStatement)) {
108 log(ast, MSG_KEY);
109 }
110 }
111 }
112
113 /**
114 * Returns if an AST is a return or a yield statement with a boolean literal
115 * or a compound statement that contains only such a return or a yield statement.
116 *
117 * <p>Returns {@code true} iff ast represents
118 * {@snippet lang="text" :
119 * return/yield true/false;
120 * }
121 * or
122 * {@snippet lang="text" :
123 * {
124 * return/yield true/false;
125 * }
126 * }
127 *
128 * @param ast the syntax tree to check
129 * @return if ast is a return or a yield statement with a boolean literal.
130 */
131 private static boolean canReturnOrYieldOnlyBooleanLiteral(DetailAST ast) {
132 boolean result = true;
133 if (!isBooleanLiteralReturnOrYieldStatement(ast)) {
134 final DetailAST firstStatement = ast.getFirstChild();
135 result = isBooleanLiteralReturnOrYieldStatement(firstStatement);
136 }
137 return result;
138 }
139
140 /**
141 * Returns if an AST is a return or a yield statement with a boolean literal.
142 *
143 * <p>Returns {@code true} iff ast represents
144 * {@snippet lang="text" :
145 * return/yield true/false;
146 * }
147 *
148 * @param ast the syntax tree to check
149 * @return if ast is a return or a yield statement with a boolean literal.
150 */
151 private static boolean isBooleanLiteralReturnOrYieldStatement(DetailAST ast) {
152 boolean booleanReturnStatement = false;
153
154 if (ast != null && (ast.getType() == TokenTypes.LITERAL_RETURN
155 || ast.getType() == TokenTypes.LITERAL_YIELD)) {
156 final DetailAST expr = ast.getFirstChild();
157
158 if (expr.getType() != TokenTypes.SEMI) {
159 final DetailAST value = expr.getFirstChild();
160 booleanReturnStatement = TokenUtil.isBooleanLiteralType(value.getType());
161 }
162 }
163 return booleanReturnStatement;
164 }
165
166 }