Added SimplifyBooleanExpressionCheck to find code like "if (b == true)".

Also fixed SimplyfyBooleanReturnCheck and introduced tests.
This commit is contained in:
Lars Kühne 2002-10-21 07:20:19 +00:00
parent 54b9235aed
commit f1ef403f5e
4 changed files with 133 additions and 5 deletions

View File

@ -0,0 +1,54 @@
package com.puppycrawl.tools.checkstyle.checks;
import com.puppycrawl.tools.checkstyle.api.Check;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code for adherence to a set of rules.
// Copyright (C) 2001-2002 Oliver Burn
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
////////////////////////////////////////////////////////////////////////////////
public class SimplifyBooleanExpressionCheck
extends Check
{
/** @see Check */
public int[] getDefaultTokens()
{
return new int[] {TokenTypes.LITERAL_TRUE, TokenTypes.LITERAL_FALSE};
}
/** @see Check */
public void visitToken(DetailAST aAST)
{
final DetailAST parent = aAST.getParent();
switch (parent.getType()) {
case TokenTypes.NOT_EQUAL:
case TokenTypes.EQUAL:
case TokenTypes.LNOT:
case TokenTypes.LOR:
case TokenTypes.LAND:
log(parent.getLineNo(), parent.getColumnNo(),
// TODO: i18n
"Expression can be simplified.");
break;
default:
break;
}
}
}

View File

@ -49,19 +49,29 @@ public class SimplifyBooleanReturnCheck
throw new IllegalArgumentException("not an if");
}
// LITERAL_IF has the following four or five children:
// '('
// condition
// ')'
// thenstatement
// [ LITERAL_ELSE (with the elseStatement as a child) ]
// don't bother if this is not if then else
if (aAST.getChildCount() != 3) {
if (aAST.getChildCount() != 5) {
return;
}
AST condition = aAST.getFirstChild();
AST thenStatement = condition.getNextSibling();
AST elseStatement = thenStatement.getNextSibling();
// skip '(' and ')'
// TODO: Introduce helpers in DetailAST
AST condition = aAST.getFirstChild().getNextSibling();
AST thenStatement = condition.getNextSibling().getNextSibling();
AST elseStatement = thenStatement.getNextSibling().getFirstChild();
if (returnsOnlyBooleanLiteral(thenStatement)
&& returnsOnlyBooleanLiteral(elseStatement))
{
log(aAST.getLineNo(), "Remove conditional logic.");
// TODO: i18n
log(aAST.getLineNo(), aAST.getColumnNo(), "Remove conditional logic.");
}
}

View File

@ -0,0 +1,31 @@
package com.puppycrawl.tools.checkstyle;
import com.puppycrawl.tools.checkstyle.checks.SimplifyBooleanExpressionCheck;
public class SimplifyBooleanExpressionCheckTest
extends BaseCheckTestCase
{
public SimplifyBooleanExpressionCheckTest(String aName)
{
super(aName);
}
public void testIt()
throws Exception
{
final CheckConfiguration checkConfig = new CheckConfiguration();
checkConfig.setClassname(SimplifyBooleanExpressionCheck.class.getName());
final Checker c = createChecker(checkConfig);
final String fname = getPath("InputSimplifyBoolean.java");
final String[] expected = {
"20:18: Expression can be simplified.",
"41:36: Expression can be simplified.",
"42:36: Expression can be simplified.",
"43:16: Expression can be simplified.",
"43:32: Expression can be simplified.",
// TODO: Change Check.log to avoid duplicate messages
"43:32: Expression can be simplified.",
};
verify(c, fname, expected);
}
}

View File

@ -0,0 +1,33 @@
package com.puppycrawl.tools.checkstyle;
import com.puppycrawl.tools.checkstyle.checks.SimplifyBooleanReturnCheck;
/*
* Created by IntelliJ IDEA.
* User: lk
* Date: Oct 21, 2002
* Time: 8:59:11 AM
* To change this template use Options | File Templates.
*/
public class SimplifyBooleanReturnCheckTest
extends BaseCheckTestCase
{
public SimplifyBooleanReturnCheckTest(String aName)
{
super(aName);
}
public void testIt()
throws Exception
{
final CheckConfiguration checkConfig = new CheckConfiguration();
checkConfig.setClassname(SimplifyBooleanReturnCheck.class.getName());
final Checker c = createChecker(checkConfig);
final String fname = getPath("InputSimplifyBoolean.java");
final String[] expected = {
"20:9: Remove conditional logic.",
"33:9: Remove conditional logic.",
};
verify(c, fname, expected);
}
}