1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-23 22:52:11 +02:00

Bug 335901 - Suspicious semicolon is not reported in "else" clause

This commit is contained in:
Alena Laskavaia 2011-05-02 01:30:34 +00:00
parent 55b32bcd97
commit f895d0411b
4 changed files with 29 additions and 4 deletions

View file

@ -27,6 +27,7 @@ public class CheckersMessages extends NLS {
public static String GenericParameter_ParameterExceptionsItem;
public static String StatementHasNoEffectChecker_ParameterMacro;
public static String SuggestedParenthesisChecker_SuggestParanthesesAroundNot;
public static String SuspiciousSemicolonChecker_ParamAfterElse;
public static String SuspiciousSemicolonChecker_ParamElse;
public static String ProblemBindingChecker_Candidates;

View file

@ -19,8 +19,9 @@ GenericParameter_ParameterExceptions=Exceptions (value of the problem argument)
GenericParameter_ParameterExceptionsItem=Value of the argument
StatementHasNoEffectChecker_ParameterMacro=Report problem in statements that comes from macro expansion
SuggestedParenthesisChecker_SuggestParanthesesAroundNot=Suggest parenthesis around not operator
SuspiciousSemicolonChecker_ParamElse=Do not report an error if 'else' exists
SuspiciousSemicolonChecker_ParamAfterElse=Report an error if semicolon is right after else statement
SuspiciousSemicolonChecker_ParamElse=Do not report an error after 'if' when 'else' exists
ProblemBindingChecker_Candidates=Candidates are:
UnusedSymbolInFileScopeChecker_CharacterSequence=Ident character sequence:
UnusedSymbolInFileScopeChecker_Exceptions=Exceptions (ident character sequence)
UnusedSymbolInFileScopeChecker_CharacterSequence=Identifier character sequence:
UnusedSymbolInFileScopeChecker_Exceptions=Exceptions (identifier character sequence)

View file

@ -25,6 +25,7 @@ import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
public class SuspiciousSemicolonChecker extends AbstractIndexAstChecker {
public static final String ER_ID = "org.eclipse.cdt.codan.internal.checkers.SuspiciousSemicolonProblem"; //$NON-NLS-1$
public static final String PARAM_ELSE = "else"; //$NON-NLS-1$
public static final String PARAM_ALFTER_ELSE = "afterelse"; //$NON-NLS-1$
public void processAst(IASTTranslationUnit ast) {
ast.accept(new ASTVisitor() {
@ -37,11 +38,15 @@ public class SuspiciousSemicolonChecker extends AbstractIndexAstChecker {
if (statement instanceof IASTIfStatement) {
IASTStatement thenStmt = ((IASTIfStatement) statement).getThenClause();
IASTStatement elseStmt = ((IASTIfStatement) statement).getElseClause();
if (elseStmt instanceof IASTNullStatement && noMacroInvolved(elseStmt) && doReportAfterElse()) {
reportProblem(ER_ID, elseStmt);
}
if (elseStmt != null && doNotReportIfElse() == true)
return PROCESS_CONTINUE;
if (thenStmt instanceof IASTNullStatement && noMacroInvolved(thenStmt)) {
reportProblem(ER_ID, thenStmt, (Object) null);
reportProblem(ER_ID, thenStmt);
}
}
return PROCESS_CONTINUE;
}
@ -52,6 +57,10 @@ public class SuspiciousSemicolonChecker extends AbstractIndexAstChecker {
final IProblem pt = getProblemById(ER_ID, getFile());
return (Boolean) getPreference(pt, PARAM_ELSE);
}
protected boolean doReportAfterElse() {
final IProblem pt = getProblemById(ER_ID, getFile());
return (Boolean) getPreference(pt, PARAM_ALFTER_ELSE);
}
protected boolean noMacroInvolved(IASTStatement node) {
IASTNodeSelector nodeSelector = node.getTranslationUnit().getNodeSelector(node.getTranslationUnit().getFilePath());
@ -63,5 +72,6 @@ public class SuspiciousSemicolonChecker extends AbstractIndexAstChecker {
public void initPreferences(IProblemWorkingCopy problem) {
super.initPreferences(problem);
addPreference(problem, PARAM_ELSE, CheckersMessages.SuspiciousSemicolonChecker_ParamElse, Boolean.FALSE);
addPreference(problem, PARAM_ALFTER_ELSE, CheckersMessages.SuspiciousSemicolonChecker_ParamAfterElse, Boolean.FALSE);
}
}

View file

@ -11,6 +11,7 @@
package org.eclipse.cdt.codan.core.internal.checkers;
import org.eclipse.cdt.codan.core.test.CheckerTestCase;
import org.eclipse.cdt.codan.internal.checkers.SuspiciousSemicolonChecker;
public class SuspiciousSemicolonCheckerTest extends CheckerTestCase {
@Override
@ -150,4 +151,16 @@ public class SuspiciousSemicolonCheckerTest extends CheckerTestCase {
loadCodeAndRun(getAboveComment());
checkNoErrors();
}
// main() {
// if (false)
// ; // only this one is reported
// else
// ;
// }
public void testIfElse() {
setPreferenceValue(SuspiciousSemicolonChecker.ER_ID, SuspiciousSemicolonChecker.PARAM_ALFTER_ELSE, Boolean.TRUE);
loadCodeAndRun(getAboveComment());
checkErrorLines(3, 5);
}
}