001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2022 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.coding;
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;
026import com.puppycrawl.tools.checkstyle.utils.TokenUtil;
027
028/**
029 * <p>
030 * Checks for over-complicated boolean expressions. Currently, it finds code like
031 * {@code if (b == true)}, {@code b || true}, {@code !false},
032 * {@code boolean a = q > 12 ? true : false},
033 * etc.
034 * </p>
035 * <p>
036 * Rationale: Complex boolean logic makes code hard to understand and maintain.
037 * </p>
038 * <p>
039 * To configure the check:
040 * </p>
041 * <pre>
042 * &lt;module name=&quot;SimplifyBooleanExpression&quot;/&gt;
043 * </pre>
044 * <p>Example:</p>
045 * <pre>
046 * public class Test {
047 *
048 *   public void bar() {
049 *
050 *     boolean a, b;
051 *     Foo c, d, e;
052 *
053 *     if (!false) {};       // violation, can be simplified to true
054 *
055 *     if (a == true) {};    // violation, can be simplified to a
056 *     if (a == b) {};       // OK
057 *     if (a == false) {};   // violation, can be simplified to !a
058 *     if (!(a != true)) {}; // violation, can be simplified to a
059 *
060 *     e = (a || b) ? c : d;     // OK
061 *     e = (a || false) ? c : d; // violation, can be simplified to a
062 *     e = (a &amp;&amp; b) ? c : d;     // OK
063 *
064 *     int s = 12;
065 *     boolean m = s &gt; 1 ? true : false; // violation, can be simplified to s &gt; 1
066 *     boolean f = c == null ? false : c.someMethod(); // OK
067 *  }
068 *
069 * }
070 * </pre>
071 * <p>
072 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
073 * </p>
074 * <p>
075 * Violation Message Keys:
076 * </p>
077 * <ul>
078 * <li>
079 * {@code simplify.expression}
080 * </li>
081 * </ul>
082 *
083 * @since 3.0
084 */
085@StatelessCheck
086public class SimplifyBooleanExpressionCheck
087        extends AbstractCheck {
088
089    /**
090     * A key is pointing to the warning message text in "messages.properties"
091     * file.
092     */
093    public static final String MSG_KEY = "simplify.expression";
094
095    @Override
096    public int[] getDefaultTokens() {
097        return getRequiredTokens();
098    }
099
100    @Override
101    public int[] getAcceptableTokens() {
102        return getRequiredTokens();
103    }
104
105    @Override
106    public int[] getRequiredTokens() {
107        return new int[] {TokenTypes.LITERAL_TRUE, TokenTypes.LITERAL_FALSE};
108    }
109
110    @Override
111    public void visitToken(DetailAST ast) {
112        final DetailAST parent = ast.getParent();
113        switch (parent.getType()) {
114            case TokenTypes.NOT_EQUAL:
115            case TokenTypes.EQUAL:
116            case TokenTypes.LNOT:
117            case TokenTypes.LOR:
118            case TokenTypes.LAND:
119                log(parent, MSG_KEY);
120                break;
121            case TokenTypes.QUESTION:
122                final DetailAST nextSibling = ast.getNextSibling();
123                if (TokenUtil.isBooleanLiteralType(parent.getFirstChild().getType())
124                        || nextSibling != null
125                        && TokenUtil.isBooleanLiteralType(
126                        nextSibling.getNextSibling().getType())) {
127                    log(parent, MSG_KEY);
128                }
129                break;
130            default:
131                break;
132        }
133    }
134
135}