Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarco Stornelli2019-05-05 06:25:09 -0400
committerMarco Stornelli2019-05-18 02:10:33 -0400
commitec6f9d204dc2ba6359276467cc82bfba480e07f8 (patch)
tree2888797446b6933f662613e07d08b5014d8dbbab
parentb5af112f8658395bca6d208b0c84c5302bceb230 (diff)
downloadorg.eclipse.cdt-ec6f9d204dc2ba6359276467cc82bfba480e07f8.tar.gz
org.eclipse.cdt-ec6f9d204dc2ba6359276467cc82bfba480e07f8.tar.xz
org.eclipse.cdt-ec6f9d204dc2ba6359276467cc82bfba480e07f8.zip
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>
-rw-r--r--codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/AssignmentInConditionChecker.java6
-rw-r--r--codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/core/internal/checkers/AssignmentInConditionCheckerTest.java49
2 files changed, 55 insertions, 0 deletions
diff --git a/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/AssignmentInConditionChecker.java b/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/AssignmentInConditionChecker.java
index a0ee541c04..32be3cc4b2 100644
--- a/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/AssignmentInConditionChecker.java
+++ b/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/AssignmentInConditionChecker.java
@@ -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;
}
diff --git a/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/core/internal/checkers/AssignmentInConditionCheckerTest.java b/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/core/internal/checkers/AssignmentInConditionCheckerTest.java
index c7ad4ebaa6..afdaee4b1a 100644
--- a/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/core/internal/checkers/AssignmentInConditionCheckerTest.java
+++ b/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/core/internal/checkers/AssignmentInConditionCheckerTest.java
@@ -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);
+ }
}

Back to the top