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.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 * <div class="wrapper"><pre class="prettyprint"><code class="language-java">
34 * if (valid())
35 * return false;
36 * else
37 * return true;
38 * </code></pre></div>
39 *
40 * <p>
41 * could be written as
42 * </p>
43 * <div class="wrapper"><pre class="prettyprint"><code class="language-java">
44 * return !valid();
45 * </code></pre></div>
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 @Override
66 public int[] getAcceptableTokens() {
67 return getRequiredTokens();
68 }
69
70 @Override
71 public int[] getDefaultTokens() {
72 return getRequiredTokens();
73 }
74
75 @Override
76 public int[] getRequiredTokens() {
77 return new int[] {TokenTypes.LITERAL_IF};
78 }
79
80 @Override
81 public void visitToken(DetailAST ast) {
82 // LITERAL_IF has the following four or five children:
83 // '('
84 // condition
85 // ')'
86 // thenStatement
87 // [ LITERAL_ELSE (with the elseStatement as a child) ]
88
89 // don't bother if this is not if then else
90 final DetailAST elseLiteral =
91 ast.findFirstToken(TokenTypes.LITERAL_ELSE);
92 if (elseLiteral != null) {
93 final DetailAST elseStatement = elseLiteral.getFirstChild();
94
95 // skip '(' and ')'
96 final DetailAST condition = ast.getFirstChild().getNextSibling();
97 final DetailAST thenStatement = condition.getNextSibling().getNextSibling();
98
99 if (canReturnOrYieldOnlyBooleanLiteral(thenStatement)
100 && canReturnOrYieldOnlyBooleanLiteral(elseStatement)) {
101 log(ast, MSG_KEY);
102 }
103 }
104 }
105
106 /**
107 * Returns if an AST is a return or a yield statement with a boolean literal
108 * or a compound statement that contains only such a return or a yield statement.
109 *
110 * <p>Returns {@code true} iff ast represents
111 * <pre>
112 * return/yield true/false;
113 * </pre>
114 * or
115 * <pre>
116 * {
117 * return/yield true/false;
118 * }
119 * </pre>
120 *
121 * @param ast the syntax tree to check
122 * @return if ast is a return or a yield statement with a boolean literal.
123 */
124 private static boolean canReturnOrYieldOnlyBooleanLiteral(DetailAST ast) {
125 boolean result = true;
126 if (!isBooleanLiteralReturnOrYieldStatement(ast)) {
127 final DetailAST firstStatement = ast.getFirstChild();
128 result = isBooleanLiteralReturnOrYieldStatement(firstStatement);
129 }
130 return result;
131 }
132
133 /**
134 * Returns if an AST is a return or a yield statement with a boolean literal.
135 *
136 * <p>Returns {@code true} iff ast represents
137 * <pre>
138 * return/yield true/false;
139 * </pre>
140 *
141 * @param ast the syntax tree to check
142 * @return if ast is a return or a yield statement with a boolean literal.
143 */
144 private static boolean isBooleanLiteralReturnOrYieldStatement(DetailAST ast) {
145 boolean booleanReturnStatement = false;
146
147 if (ast != null && (ast.getType() == TokenTypes.LITERAL_RETURN
148 || ast.getType() == TokenTypes.LITERAL_YIELD)) {
149 final DetailAST expr = ast.getFirstChild();
150
151 if (expr.getType() != TokenTypes.SEMI) {
152 final DetailAST value = expr.getFirstChild();
153 booleanReturnStatement = TokenUtil.isBooleanLiteralType(value.getType());
154 }
155 }
156 return booleanReturnStatement;
157 }
158 }