use of org.sonar.plugins.java.api.tree.BinaryExpressionTree in project sonar-java by SonarSource.
the class ExplodedGraphWalker method handleBlockExit.
private void handleBlockExit(ProgramPoint programPosition) {
CFG.Block block = (CFG.Block) programPosition.block;
Tree terminator = block.terminator();
cleanUpProgramState(block);
boolean exitPath = node.exitPath;
if (terminator != null) {
switch(terminator.kind()) {
case IF_STATEMENT:
ExpressionTree ifCondition = ((IfStatementTree) terminator).condition();
handleBranch(block, cleanupCondition(ifCondition), verifyCondition(ifCondition));
return;
case CONDITIONAL_OR:
case CONDITIONAL_AND:
handleBranch(block, ((BinaryExpressionTree) terminator).leftOperand());
return;
case CONDITIONAL_EXPRESSION:
handleBranch(block, ((ConditionalExpressionTree) terminator).condition());
return;
case FOR_STATEMENT:
ExpressionTree condition = ((ForStatementTree) terminator).condition();
if (condition != null) {
handleBranch(block, condition, false);
return;
}
break;
case WHILE_STATEMENT:
ExpressionTree whileCondition = ((WhileStatementTree) terminator).condition();
handleBranch(block, cleanupCondition(whileCondition), verifyCondition(whileCondition));
return;
case DO_STATEMENT:
ExpressionTree doCondition = ((DoWhileStatementTree) terminator).condition();
handleBranch(block, cleanupCondition(doCondition), verifyCondition(doCondition));
return;
case SYNCHRONIZED_STATEMENT:
resetFieldValues(false);
break;
case RETURN_STATEMENT:
ExpressionTree returnExpression = ((ReturnStatementTree) terminator).expression();
if (returnExpression != null) {
programState.storeExitValue();
}
break;
case THROW_STATEMENT:
ProgramState.Pop unstack = programState.unstackValue(1);
SymbolicValue sv = unstack.values.get(0);
if (sv instanceof SymbolicValue.CaughtExceptionSymbolicValue) {
// retrowing the exception from a catch block
sv = ((SymbolicValue.CaughtExceptionSymbolicValue) sv).exception();
} else {
sv = constraintManager.createExceptionalSymbolicValue(((ThrowStatementTree) terminator).expression().symbolType());
}
programState = unstack.state.stackValue(sv);
programState.storeExitValue();
break;
default:
}
}
// unconditional jumps, for-statement, switch-statement, synchronized:
if (exitPath) {
if (block.exitBlock() != null) {
enqueue(new ProgramPoint(block.exitBlock()), programState, true);
} else {
for (CFG.Block successor : block.successors()) {
enqueue(new ProgramPoint(successor), programState, true);
}
}
} else {
for (CFG.Block successor : block.successors()) {
if (!block.isFinallyBlock() || isDirectFlowSuccessorOf(successor, block)) {
enqueue(new ProgramPoint(successor), programState, successor == block.exitBlock());
}
}
}
}
use of org.sonar.plugins.java.api.tree.BinaryExpressionTree in project sonar-java by SonarSource.
the class MapComputeIfAbsentOrPresentCheck method isNullCheck.
private static boolean isNullCheck(ExpressionTree condition) {
if (condition.is(Tree.Kind.EQUAL_TO, Tree.Kind.NOT_EQUAL_TO)) {
BinaryExpressionTree bet = (BinaryExpressionTree) condition;
ExpressionTree rightOperand = ExpressionUtils.skipParentheses(bet.rightOperand());
ExpressionTree leftOperand = ExpressionUtils.skipParentheses(bet.leftOperand());
return rightOperand.is(Tree.Kind.NULL_LITERAL) || leftOperand.is(Tree.Kind.NULL_LITERAL);
}
return false;
}
use of org.sonar.plugins.java.api.tree.BinaryExpressionTree in project sonar-java by SonarSource.
the class InvalidDateValuesCheck method visitNode.
@Override
public void visitNode(Tree tree) {
super.visitNode(tree);
if (hasSemantic() && tree.is(Tree.Kind.EQUAL_TO, Tree.Kind.NOT_EQUAL_TO)) {
BinaryExpressionTree binaryExpressionTree = (BinaryExpressionTree) tree;
String name = getThresholdToCheck(binaryExpressionTree.leftOperand());
ExpressionTree argToCheck = null;
if (name == null) {
name = getThresholdToCheck(binaryExpressionTree.rightOperand());
if (name != null) {
argToCheck = binaryExpressionTree.leftOperand();
}
} else {
argToCheck = binaryExpressionTree.rightOperand();
}
if (argToCheck != null) {
checkArgument(argToCheck, name, "\"{0}\" is not a valid value for \"{1}\".");
}
}
}
use of org.sonar.plugins.java.api.tree.BinaryExpressionTree in project sonar-java by SonarSource.
the class NullCheckWithInstanceofCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (!hasSemantic()) {
return;
}
BinaryExpressionTree binaryExpression = (BinaryExpressionTree) tree;
ExpressionTree leftOp = ExpressionUtils.skipParentheses(binaryExpression.leftOperand());
ExpressionTree rightOp = ExpressionUtils.skipParentheses(binaryExpression.rightOperand());
if ((is(Tree.Kind.EQUAL_TO, leftOp, rightOp) && nullCheckWithInstanceOf(leftOp, rightOp, binaryExpression.kind(), Tree.Kind.CONDITIONAL_OR)) || (is(Tree.Kind.NOT_EQUAL_TO, leftOp, rightOp) && nullCheckWithInstanceOf(leftOp, rightOp, binaryExpression.kind(), Tree.Kind.CONDITIONAL_AND))) {
reportIssue(treeToReport(leftOp, rightOp), "Remove this unnecessary null check; \"instanceof\" returns false for nulls.");
}
}
use of org.sonar.plugins.java.api.tree.BinaryExpressionTree in project sonar-java by SonarSource.
the class NullShouldNotBeUsedWithOptionalCheck method visitBinaryExpression.
@Override
public void visitBinaryExpression(BinaryExpressionTree binaryExpression) {
// check that an @Optional is not compared to "null"
if (binaryExpression.is(Tree.Kind.EQUAL_TO, Tree.Kind.NOT_EQUAL_TO)) {
ExpressionTree left = binaryExpression.leftOperand();
ExpressionTree right = binaryExpression.rightOperand();
if ((isOptional(left) && isNull(right)) || (isNull(left) && isOptional(right))) {
context.reportIssue(this, binaryExpression, "Remove this null-check of an \"Optional\".");
}
}
super.visitBinaryExpression(binaryExpression);
}
Aggregations