@@ -4384,6 +4384,41 @@ void CheckOther::overlappingWriteFunction(const Token *tok, const std::string& f
43844384 reportError (tok, Severity::error, " overlappingWriteFunction" , " Overlapping read/write in " + funcname + " () is undefined behavior" );
43854385}
43864386
4387+ void CheckOther::checkSuspiciousComma ()
4388+ {
4389+ if (!mSettings ->severity .isEnabled (Severity::style)) {
4390+ return ;
4391+ }
4392+
4393+ logChecker (" CheckOther::suspiciousComma" );
4394+
4395+ for (const Token* tok = mTokenizer ->list .front (); tok; tok = tok->next ()) {
4396+ if (tok->str () == " ," && tok->isBinaryOp ()) {
4397+ const Token * parent = tok->astParent ();
4398+ if (parent && Token::Match (parent->previous (), " if|while (" )) {
4399+ if (tok->strAt (-1 ) == " )" ) {
4400+ const Function * func = tok->linkAt (-1 )->previous ()->function ();
4401+ if (func && func->initArgCount > 0 ) {
4402+ const Token * r_op = tok->astOperand2 ();
4403+ if (r_op && (r_op->isVariable () ||
4404+ r_op->tokType () == Token::eNumber ||
4405+ r_op->tokType () == Token::eString ||
4406+ r_op->tokType () == Token::eChar ||
4407+ r_op->tokType () == Token::eBoolean)) {
4408+ checkSuspiciousCommaError (tok);
4409+ }
4410+ }
4411+ }
4412+ }
4413+ }
4414+ }
4415+ }
4416+
4417+ void CheckOther::checkSuspiciousCommaError (const Token *tok)
4418+ {
4419+ reportError (tok, Severity::style, " suspiciousComma" , " There is a suspicious comma expression in an if/while condition statement." );
4420+ }
4421+
43874422void CheckOther::runChecks (const Tokenizer &tokenizer, ErrorLogger *errorLogger)
43884423{
43894424 CheckOther checkOther (&tokenizer, &tokenizer.getSettings (), errorLogger);
@@ -4431,6 +4466,7 @@ void CheckOther::runChecks(const Tokenizer &tokenizer, ErrorLogger *errorLogger)
44314466 checkOther.checkAccessOfMovedVariable ();
44324467 checkOther.checkModuloOfOne ();
44334468 checkOther.checkOverlappingWrite ();
4469+ checkOther.checkSuspiciousComma ();
44344470}
44354471
44364472void CheckOther::getErrorMessages (ErrorLogger *errorLogger, const Settings *settings) const
@@ -4503,6 +4539,7 @@ void CheckOther::getErrorMessages(ErrorLogger *errorLogger, const Settings *sett
45034539 c.comparePointersError (nullptr , nullptr , nullptr );
45044540 c.redundantAssignmentError (nullptr , nullptr , " var" , false );
45054541 c.redundantInitializationError (nullptr , nullptr , " var" , false );
4542+ c.checkSuspiciousCommaError (nullptr );
45064543
45074544 const std::vector<const Token *> nullvec;
45084545 c.funcArgOrderDifferent (" function" , nullptr , nullptr , nullvec, nullvec);
0 commit comments