--------------------------------------------------------------------------- Check for suspicious occurrences of 'if(); {}'. ---------------------------------------------------------------------------
| 258 | // Check for suspicious occurrences of 'if(); {}'. |
| 259 | //--------------------------------------------------------------------------- |
| 260 | void 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 | |
| 284 | void CheckOtherImpl::suspiciousSemicolonError(const Token* tok) |
| 285 | { |
no test coverage detected