mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-08-09 17:25:38 +02:00
Bug 320786 - Fix false negative assignment in condition
Fix bug with expression list Change-Id: I25b226ec7fd1edcfc40fe2ebcadbd3bdb2a6a5dd Signed-off-by: Marco Stornelli <marco.stornelli@gmail.com>
This commit is contained in:
parent
b5af112f86
commit
ec6f9d204d
2 changed files with 55 additions and 0 deletions
|
@ -20,6 +20,7 @@ import org.eclipse.cdt.core.dom.ast.IASTBinaryExpression;
|
|||
import org.eclipse.cdt.core.dom.ast.IASTConditionalExpression;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTDoStatement;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTExpression;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTExpressionList;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTForStatement;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTIfStatement;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
|
||||
|
@ -52,6 +53,11 @@ public class AssignmentInConditionChecker extends AbstractIndexAstChecker {
|
|||
if (e instanceof IASTBinaryExpression) {
|
||||
IASTBinaryExpression binExpr = (IASTBinaryExpression) e;
|
||||
return binExpr.getOperator() == IASTBinaryExpression.op_assign;
|
||||
} else if (e instanceof IASTExpressionList) {
|
||||
for (IASTExpression expr : ((IASTExpressionList) e).getExpressions()) {
|
||||
if (isAssignmentExpression(expr))
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
|
|
@ -16,6 +16,7 @@ package org.eclipse.cdt.codan.core.internal.checkers;
|
|||
import org.eclipse.cdt.codan.core.tests.CheckerTestCase;
|
||||
import org.eclipse.cdt.codan.internal.core.model.CodanProblemMarker;
|
||||
import org.eclipse.core.resources.IMarker;
|
||||
import org.eclipse.core.runtime.CoreException;
|
||||
|
||||
/**
|
||||
* Test for {@see AssignmentInConditionChecker} class
|
||||
|
@ -143,4 +144,52 @@ public class AssignmentInConditionCheckerTest extends CheckerTestCase {
|
|||
loadCodeAndRun(getAboveComment());
|
||||
checkErrorLine(4);
|
||||
}
|
||||
|
||||
//void test_commaPrecededIf() {
|
||||
// int a, b;
|
||||
// if (a=some_value(), b=some_other_value(), a=b){ // warning here
|
||||
// // do something
|
||||
// }else{
|
||||
// // do something else
|
||||
// }
|
||||
//}
|
||||
//int some_value(){return 0;}
|
||||
//int ome_other_value(){return 1;}
|
||||
public void test_commaPrecededIf() throws CoreException {
|
||||
loadCodeAndRun(getAboveComment());
|
||||
checkErrorLine(3);
|
||||
}
|
||||
|
||||
//void test_commaPrecededWhile() {
|
||||
// int NO_ERROR = 0;
|
||||
// int error_code;
|
||||
// while (error_code = read_from_file(), error_code = NO_ERROR) { // warning
|
||||
// // do something
|
||||
// }
|
||||
//}
|
||||
public void test_commaPrecededWhile() throws CoreException {
|
||||
loadCodeAndRun(getAboveComment());
|
||||
checkErrorLine(4);
|
||||
}
|
||||
|
||||
//void test_commaPrecededDoWhile() {
|
||||
// int NO_ERROR = 0;
|
||||
// int error_code;
|
||||
// do{
|
||||
// // do something
|
||||
// } while (error_code = read_from_file(), error_code = NO_ERROR); // warning
|
||||
//}
|
||||
public void test_commaPrecededDoWhile() throws CoreException {
|
||||
loadCodeAndRun(getAboveComment());
|
||||
checkErrorLine(6);
|
||||
}
|
||||
|
||||
//void test_commaPrecededConditioal(){
|
||||
// int a, b, c;
|
||||
// c = (a=some_value(), b=some_other_value(), a=b)? a : b; // warning here
|
||||
//}
|
||||
public void test_commaPrecededConditioal() throws CoreException {
|
||||
loadCodeAndRun(getAboveComment());
|
||||
checkErrorLine(3);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue