mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-07-24 09:25:31 +02:00
Bug 314576: added assignment to itself checker, contribution from Severin Gehwolf
This commit is contained in:
parent
fbdb55c7a7
commit
3a67847b2b
5 changed files with 132 additions and 1 deletions
|
@ -44,4 +44,8 @@ problem.messagePattern.UnusedReturnValue = Return has value, in function returni
|
|||
problem.name.UnusedReturnValue = Unused return value
|
||||
problem.description.NoReturn = No return statement in a function which is declared to return value
|
||||
problem.messagePattern.NoReturn = No return, in function returning non-void
|
||||
problem.name.NoReturn = No return
|
||||
problem.name.NoReturn = No return
|
||||
checker.name.AssignmentToItself = Assignment to itself
|
||||
problem.messagePattern.AssignmentToItself = Assignment to itself ''{0}''
|
||||
problem.name.AssignmentToItself = Assignment to itself
|
||||
problem.description.AssignmentToItself = Finds expression where left and right side of the assignment is the same, i.e. 'var = var'
|
|
@ -250,5 +250,19 @@
|
|||
name="Field cannot be resolved">
|
||||
</problem>
|
||||
</checker>
|
||||
<checker
|
||||
class="org.eclipse.cdt.codan.internal.checkers.AssignmentToItselfChecker"
|
||||
id="org.eclipse.cdt.codan.internal.checkers.AssignmentToItselfChecker"
|
||||
name="%checker.name.AssignmentToItself">
|
||||
<problem
|
||||
category="org.eclipse.cdt.codan.core.categories.ProgrammingProblems"
|
||||
defaultEnabled="true"
|
||||
defaultSeverity="Error"
|
||||
description="%problem.description.AssignmentToItself"
|
||||
id="org.eclipse.cdt.codan.internal.checkers.AssignmentToItselfProblem"
|
||||
messagePattern="%problem.messagePattern.AssignmentToItself"
|
||||
name="%problem.name.AssignmentToItself">
|
||||
</problem>
|
||||
</checker>
|
||||
</extension>
|
||||
</plugin>
|
||||
|
|
|
@ -0,0 +1,54 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2010 Severin Gehwolf
|
||||
* 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:
|
||||
* Severin Gehwolf - initial API and implementation
|
||||
*******************************************************************************/
|
||||
package org.eclipse.cdt.codan.internal.checkers;
|
||||
|
||||
import org.eclipse.cdt.codan.core.cxx.model.AbstractIndexAstChecker;
|
||||
import org.eclipse.cdt.core.dom.ast.ASTVisitor;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTBinaryExpression;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTExpression;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
|
||||
|
||||
/**
|
||||
* Checker that find assignment to itself cases, such a
|
||||
* a = a. It can produce some false positives such as
|
||||
* a[f()]=a[f()] - but who write codes like that?
|
||||
*/
|
||||
public class AssignmentToItselfChecker extends AbstractIndexAstChecker {
|
||||
|
||||
private static final String ER_ID = "org.eclipse.cdt.codan.internal.checkers.AssignmentToItselfProblem"; //$NON-NLS-1$
|
||||
|
||||
public void processAst(IASTTranslationUnit ast) {
|
||||
// traverse the ast using the visitor pattern.
|
||||
ast.accept(new ASTVisitor() {
|
||||
{ // constructor
|
||||
shouldVisitExpressions = true;
|
||||
}
|
||||
|
||||
// visit expressions
|
||||
public int visit(IASTExpression expression) {
|
||||
if (isAssignmentToItself(expression)) {
|
||||
reportProblem(ER_ID, expression, expression.getRawSignature());
|
||||
}
|
||||
return PROCESS_CONTINUE;
|
||||
}
|
||||
|
||||
private boolean isAssignmentToItself(IASTExpression e) {
|
||||
if (e instanceof IASTBinaryExpression) {
|
||||
IASTBinaryExpression binExpr = (IASTBinaryExpression) e;
|
||||
if (binExpr.getOperator() == IASTBinaryExpression.op_assign) {
|
||||
return binExpr.getOperand1().getRawSignature().equals(binExpr.getOperand2().getRawSignature());
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
|
@ -0,0 +1,57 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2010 Severin Gehwolf
|
||||
* 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:
|
||||
* Severin Gehwolf - initial API and implementation
|
||||
*******************************************************************************/
|
||||
package org.eclipse.cdt.codan.core.internal.checkers;
|
||||
|
||||
import org.eclipse.cdt.codan.core.test.CheckerTestCase;
|
||||
|
||||
/**
|
||||
* Test for {@see AssignmentToItselfChecker} class
|
||||
*
|
||||
*/
|
||||
public class AssignmentToItselfCheckerTest extends CheckerTestCase {
|
||||
// void main() {
|
||||
// int x = 0;
|
||||
// x = 10;
|
||||
// }
|
||||
public void testNoErrorConstants() {
|
||||
loadCodeAndRun(getAboveComment());
|
||||
checkNoErrors();
|
||||
}
|
||||
|
||||
// void main() {
|
||||
// int x = 10;
|
||||
// int s = 10;
|
||||
// x = s;
|
||||
// }
|
||||
public void testNoErrorVariables() {
|
||||
loadCodeAndRun(getAboveComment());
|
||||
checkNoErrors();
|
||||
}
|
||||
|
||||
// void main() {
|
||||
// int x = 0;
|
||||
// x = x;
|
||||
// }
|
||||
public void testSimpleVariableSelfAssignmentError() {
|
||||
loadCodeAndRun(getAboveComment());
|
||||
checkErrorLine(3);
|
||||
}
|
||||
|
||||
// void main() {
|
||||
// char str[] = "hello testing world";
|
||||
// int x = 10;
|
||||
// str[i] = str[i];
|
||||
// }
|
||||
public void testArraySelfAssignmentError() {
|
||||
loadCodeAndRun(getAboveComment());
|
||||
checkErrorLine(4);
|
||||
}
|
||||
}
|
|
@ -15,6 +15,7 @@ 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.AssignmentToItselfCheckerTest;
|
||||
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;
|
||||
|
@ -45,6 +46,7 @@ public class AutomatedIntegrationSuite extends TestSuite {
|
|||
suite.addTestSuite(ReturnCheckerTest.class);
|
||||
suite.addTestSuite(CatchByReferenceTest.class);
|
||||
suite.addTestSuite(AssignmentInConditionCheckerTest.class);
|
||||
suite.addTestSuite(AssignmentToItselfCheckerTest.class);
|
||||
// framework
|
||||
suite.addTest(CodanFastTestSuite.suite());
|
||||
// quick fixes
|
||||
|
|
Loading…
Add table
Reference in a new issue