@@ -4367,6 +4367,41 @@ void CheckOther::overlappingWriteFunction(const Token *tok, const std::string& f
43674367 reportError (tok, Severity::error, " overlappingWriteFunction" , " Overlapping read/write in " + funcname + " () is undefined behavior" );
43684368}
43694369
4370+ void CheckOther::checkSuspiciousComma ()
4371+ {
4372+ if (!mSettings ->severity .isEnabled (Severity::style)) {
4373+ return ;
4374+ }
4375+
4376+ logChecker (" CheckOther::warnSuspiciousComma" );
4377+
4378+ for (const Token* tok = mTokenizer ->list .front (); tok; tok = tok->next ()) {
4379+ if (tok->str () == " ," && tok->isBinaryOp ()) {
4380+ const Token * parent = tok->astParent ();
4381+ if (parent && Token::Match (parent->previous (), " if|while (" )) {
4382+ if (tok->previous ()->str () == " )" ) {
4383+ const Function * func = tok->linkAt (-1 )->previous ()->function ();
4384+ if (func && func->initArgCount > 0 ) {
4385+ const Token * r_op = tok->astOperand2 ();
4386+ if (r_op && (r_op->isVariable () ||
4387+ r_op->tokType () == Token::eNumber ||
4388+ r_op->tokType () == Token::eString ||
4389+ r_op->tokType () == Token::eChar ||
4390+ r_op->tokType () == Token::eBoolean)) {
4391+ checkSuspiciousCommaError (tok);
4392+ }
4393+ }
4394+ }
4395+ }
4396+ }
4397+ }
4398+ }
4399+
4400+ void CheckOther::checkSuspiciousCommaError (const Token *tok)
4401+ {
4402+ reportError (tok, Severity::style, " warnSuspiciousComma" , " There is an suspicious comma expression used as a condition in an if/while condition statement." );
4403+ }
4404+
43704405void CheckOther::runChecks (const Tokenizer &tokenizer, ErrorLogger *errorLogger)
43714406{
43724407 CheckOther checkOther (&tokenizer, &tokenizer.getSettings (), errorLogger);
@@ -4414,6 +4449,7 @@ void CheckOther::runChecks(const Tokenizer &tokenizer, ErrorLogger *errorLogger)
44144449 checkOther.checkAccessOfMovedVariable ();
44154450 checkOther.checkModuloOfOne ();
44164451 checkOther.checkOverlappingWrite ();
4452+ checkOther.checkSuspiciousComma ();
44174453}
44184454
44194455void CheckOther::getErrorMessages (ErrorLogger *errorLogger, const Settings *settings) const
@@ -4486,6 +4522,7 @@ void CheckOther::getErrorMessages(ErrorLogger *errorLogger, const Settings *sett
44864522 c.comparePointersError (nullptr , nullptr , nullptr );
44874523 c.redundantAssignmentError (nullptr , nullptr , " var" , false );
44884524 c.redundantInitializationError (nullptr , nullptr , " var" , false );
4525+ c.checkSuspiciousCommaError (nullptr );
44894526
44904527 const std::vector<const Token *> nullvec;
44914528 c.funcArgOrderDifferent (" function" , nullptr , nullptr , nullvec, nullvec);
0 commit comments