mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-07-25 09:55:29 +02:00
[306563 ] - fixed false positives
This commit is contained in:
parent
e3b26be436
commit
b239297541
3 changed files with 60 additions and 9 deletions
|
@ -25,8 +25,9 @@
|
|||
category="org.eclipse.cdt.codan.core.categories.ProgrammingProblems"
|
||||
defaultSeverity="Warning"
|
||||
id="org.eclipse.cdt.codan.internal.checkers.StatementHasNoEffectProblem"
|
||||
name="Statement has no effect">
|
||||
</problem>
|
||||
name="Statement has no effect"
|
||||
messagePattern="Statement has no effect"
|
||||
/>
|
||||
</checker>
|
||||
|
||||
<checker
|
||||
|
|
|
@ -36,10 +36,9 @@ import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTBinaryExpression;
|
|||
*
|
||||
*/
|
||||
public class StatementHasNoEffectChecker extends AbstractIndexAstChecker {
|
||||
private static final String ER_ID = "org.eclipse.cdt.codan.internal.checkers.StatementHasNoEffectProblem";
|
||||
private static final String ER_ID = "org.eclipse.cdt.codan.internal.checkers.StatementHasNoEffectProblem"; //$NON-NLS-1$
|
||||
|
||||
public void processAst(IASTTranslationUnit ast) {
|
||||
// traverse the ast using the visitor pattern.
|
||||
ast.accept(new CheckStmpVisitor());
|
||||
}
|
||||
|
||||
|
@ -52,7 +51,7 @@ public class StatementHasNoEffectChecker extends AbstractIndexAstChecker {
|
|||
if (stmt instanceof IASTExpressionStatement) {
|
||||
if (hasNoEffect(((IASTExpressionStatement) stmt)
|
||||
.getExpression())) {
|
||||
reportProblem(ER_ID, stmt, "Statement has no effect");
|
||||
reportProblem(ER_ID, stmt);
|
||||
}
|
||||
return PROCESS_SKIP;
|
||||
}
|
||||
|
@ -70,8 +69,20 @@ public class StatementHasNoEffectChecker extends AbstractIndexAstChecker {
|
|||
private boolean hasNoEffect(IASTExpression e) {
|
||||
if (e instanceof IASTBinaryExpression) {
|
||||
IASTBinaryExpression binExpr = (IASTBinaryExpression) e;
|
||||
if (binExpr.getOperator() == IASTBinaryExpression.op_assign)
|
||||
switch (binExpr.getOperator()) {
|
||||
case IASTBinaryExpression.op_assign:
|
||||
case IASTBinaryExpression.op_binaryAndAssign:
|
||||
case IASTBinaryExpression.op_binaryOrAssign:
|
||||
case IASTBinaryExpression.op_binaryXorAssign:
|
||||
case IASTBinaryExpression.op_divideAssign:
|
||||
case IASTBinaryExpression.op_plusAssign:
|
||||
case IASTBinaryExpression.op_minusAssign:
|
||||
case IASTBinaryExpression.op_multiplyAssign:
|
||||
case IASTBinaryExpression.op_moduloAssign:
|
||||
case IASTBinaryExpression.op_shiftLeftAssign:
|
||||
case IASTBinaryExpression.op_shiftRightAssign:
|
||||
return false;
|
||||
}
|
||||
if (binExpr instanceof CPPASTBinaryExpression) {
|
||||
// unfortunately ICPPASTBinaryExpression does not have
|
||||
// getOverload public method
|
||||
|
@ -97,6 +108,8 @@ public class StatementHasNoEffectChecker extends AbstractIndexAstChecker {
|
|||
case IASTUnaryExpression.op_postFixIncr:
|
||||
case IASTUnaryExpression.op_prefixIncr:
|
||||
return false;
|
||||
case IASTUnaryExpression.op_bracketedPrimary:
|
||||
return hasNoEffect(unaryExpr.getOperand());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -25,7 +25,7 @@ public class StatementHasNoEffectCheckerTest extends CheckerTestCase {
|
|||
}
|
||||
</code>
|
||||
*/
|
||||
public void test1() {
|
||||
public void testUnaryExpression() {
|
||||
load("test1.c");
|
||||
runOnFile();
|
||||
checkErrorLine(3);
|
||||
|
@ -40,7 +40,7 @@ public class StatementHasNoEffectCheckerTest extends CheckerTestCase {
|
|||
}
|
||||
</code>
|
||||
*/
|
||||
public void test2() {
|
||||
public void testBinaryExpression() {
|
||||
load("test2.c");
|
||||
runOnFile();
|
||||
checkErrorLine(4);
|
||||
|
@ -55,9 +55,46 @@ public class StatementHasNoEffectCheckerTest extends CheckerTestCase {
|
|||
}
|
||||
</code>
|
||||
*/
|
||||
public void test3() {
|
||||
public void testNormalAssignment() {
|
||||
load("test3.c");
|
||||
runOnFile();
|
||||
checkNoErrors();
|
||||
}
|
||||
/*-
|
||||
<code file="test4.c">
|
||||
main() {
|
||||
int a,b;
|
||||
|
||||
(a=b); // no errors here
|
||||
a+=b;
|
||||
a<<=b;
|
||||
a-=b;
|
||||
a++;
|
||||
b--;
|
||||
--a;
|
||||
++b;
|
||||
a%=2;
|
||||
a>>=2;
|
||||
}
|
||||
</code>
|
||||
*/
|
||||
public void testFalsePositives() {
|
||||
load("test4.c");
|
||||
runOnFile();
|
||||
checkNoErrors();
|
||||
}
|
||||
|
||||
/*-
|
||||
<code file="test5.c">
|
||||
main() {
|
||||
int a;
|
||||
a; // error here on line 3
|
||||
}
|
||||
</code>
|
||||
*/
|
||||
public void testIdExpression() {
|
||||
load("test5.c");
|
||||
runOnFile();
|
||||
checkErrorLine(3);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue