MCPcopy Create free account
hub / github.com/cppcheck-opensource/cppcheck / checkSuspiciousSemicolon

Method checkSuspiciousSemicolon

lib/checkother.cpp:260–282  ·  view source on GitHub ↗

--------------------------------------------------------------------------- Check for suspicious occurrences of 'if(); {}'. ---------------------------------------------------------------------------

Source from the content-addressed store, hash-verified

258// Check for suspicious occurrences of 'if(); {}'.
259//---------------------------------------------------------------------------
260void CheckOtherImpl::checkSuspiciousSemicolon()
261{
262 if (!mSettings.certainty.isEnabled(Certainty::inconclusive) || !mSettings.severity.isEnabled(Severity::warning))
263 return;
264
265 const SymbolDatabase* const symbolDatabase = mTokenizer->getSymbolDatabase();
266
267 logChecker("CheckOther::checkSuspiciousSemicolon"); // warning,inconclusive
268
269 // Look for "if(); {}", "for(); {}" or "while(); {}"
270 for (const Scope &scope : symbolDatabase->scopeList) {
271 if (scope.type == ScopeType::eIf || scope.type == ScopeType::eElse || scope.type == ScopeType::eWhile || scope.type == ScopeType::eFor) {
272 // Ensure the semicolon is at the same line number as the if/for/while statement
273 // and the {..} block follows it without an extra empty line.
274 if (Token::simpleMatch(scope.bodyStart, "{ ; } {") &&
275 scope.bodyStart->previous()->linenr() == scope.bodyStart->tokAt(2)->linenr() &&
276 scope.bodyStart->linenr()+1 >= scope.bodyStart->tokAt(3)->linenr() &&
277 !scope.bodyStart->tokAt(3)->isExpandedMacro()) {
278 suspiciousSemicolonError(scope.classDef);
279 }
280 }
281 }
282}
283
284void CheckOtherImpl::suspiciousSemicolonError(const Token* tok)
285{

Callers 1

runChecksMethod · 0.80

Calls 3

simpleMatchFunction · 0.70
isEnabledMethod · 0.45
tokAtMethod · 0.45

Tested by

no test coverage detected