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 6a17ef83d3c..2a60e140205 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 @@ -14,10 +14,14 @@ import org.eclipse.cdt.codan.core.cxx.model.AbstractIndexAstChecker; import org.eclipse.cdt.core.dom.ast.ASTNodeProperty; import org.eclipse.cdt.core.dom.ast.ASTVisitor; 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.IASTForStatement; import org.eclipse.cdt.core.dom.ast.IASTIfStatement; import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit; +import org.eclipse.cdt.core.dom.ast.IASTUnaryExpression; +import org.eclipse.cdt.core.dom.ast.IASTWhileStatement; public class AssignmentInConditionChecker extends AbstractIndexAstChecker { private static final String ER_ID = "org.eclipse.cdt.codan.internal.checkers.AssignmentInConditionProblem"; //$NON-NLS-1$ @@ -51,8 +55,18 @@ public class AssignmentInConditionChecker extends AbstractIndexAstChecker { private boolean isUsedAsCondition(IASTExpression expression) { ASTNodeProperty prop = expression.getPropertyInParent(); if (prop == IASTForStatement.CONDITION - || prop == IASTIfStatement.CONDITION) + || prop == IASTIfStatement.CONDITION + || prop == IASTWhileStatement.CONDITIONEXPRESSION + || prop == IASTDoStatement.CONDITION) return true; + if (prop == IASTUnaryExpression.OPERAND) { + IASTUnaryExpression expr = (IASTUnaryExpression) expression + .getParent(); + if (expr.getOperator() == IASTUnaryExpression.op_bracketedPrimary && + expr.getPropertyInParent() == IASTConditionalExpression.LOGICAL_CONDITION) { + return true; + } + } return false; } } diff --git a/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/internal/checkers/AssignmentInConditionCheckerTest.java b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/internal/checkers/AssignmentInConditionCheckerTest.java new file mode 100644 index 00000000000..38f750b2d6f --- /dev/null +++ b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/internal/checkers/AssignmentInConditionCheckerTest.java @@ -0,0 +1,85 @@ +/******************************************************************************* + * Copyright (c) 2009, 2010 Alena Laskavaia + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Alena Laskavaia - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.codan.core.internal.checkers; + +import org.eclipse.cdt.codan.core.test.CheckerTestCase; + +/** + * Test for {@see SuggestedParenthesisChecker} class + * + */ +public class AssignmentInConditionCheckerTest extends CheckerTestCase { + // main() { + // int a=1,b=3; + // if (a=b) b=4; // error here on line 3 + // } + public void test_basic() { + loadCodeAndRun(getAboveComment()); + checkErrorLine(3); + } + + // main() { + // int a=1,b=3; + // + // if ((a=b)) b--; // no error + // } + public void test_fixed() { + loadCodeAndRun(getAboveComment()); + checkNoErrors(); + } + + // main() { + // int a=1,b=3; + // if ((a=b)!=0) b=4; // no error here on line 3 + // } + public void test3() { + loadCodeAndRun(getAboveComment()); + checkNoErrors(); + } + + // main() { + // int i,a[10]; + // if (a[i]=0) b=4; // no error here on line 3 + // } + public void test_array() { + loadCodeAndRun(getAboveComment()); + checkErrorLine(3); + } + + // main() { + // int i,b=3; + // for (i = 0; i=b; i++) { // here + // } + // } + public void test_for() { + loadCodeAndRun(getAboveComment()); + checkErrorLine(3); + } + + // main() { + // int i,b=3; + // while (i=b) { // here + // } + // } + public void test_while() { + loadCodeAndRun(getAboveComment()); + checkErrorLine(3); + } + + // main() { + // int i,b=3; + // (i=b)?i++:b++; // here + // } + public void test_tri() { + loadCodeAndRun(getAboveComment()); + checkErrorLine(3); + } +} diff --git a/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/test/AutomatedIntegrationSuite.java b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/test/AutomatedIntegrationSuite.java index 93ee37faa08..89f37c93bb6 100644 --- a/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/test/AutomatedIntegrationSuite.java +++ b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/test/AutomatedIntegrationSuite.java @@ -14,6 +14,7 @@ package org.eclipse.cdt.codan.core.test; import junit.framework.Test; import junit.framework.TestSuite; +import org.eclipse.cdt.codan.core.internal.checkers.AssignmentInConditionCheckerTest; import org.eclipse.cdt.codan.core.internal.checkers.CatchByReferenceTest; import org.eclipse.cdt.codan.core.internal.checkers.ReturnCheckerTest; import org.eclipse.cdt.codan.core.internal.checkers.StatementHasNoEffectCheckerTest; @@ -41,6 +42,7 @@ public class AutomatedIntegrationSuite extends TestSuite { suite.addTestSuite(SuggestedParenthesisCheckerTest.class); suite.addTestSuite(ReturnCheckerTest.class); suite.addTestSuite(CatchByReferenceTest.class); + suite.addTestSuite(AssignmentInConditionCheckerTest.class); suite.addTest(CodanFastTestSuite.suite()); return suite; }