@@ -527,17 +527,30 @@ static void valueFlowAfterAssign(TokenList *tokenlist, ErrorLogger *errorLogger,
527527 continue ;
528528 std::list<ValueFlow::Value> values = tok->astOperand2 ()->values ;
529529
530+ const bool constValue = tok->astOperand2 ()->isNumber ();
531+ int indentlevel = 0 ;
530532 unsigned int number_of_if = 0 ;
533+ int varusagelevel = -1 ;
531534
532535 for (Token *tok2 = tok; tok2 && tok2 != endToken; tok2 = tok2->next ()) {
536+ if (indentlevel >= 0 && tok2->str () == " {" )
537+ ++indentlevel;
538+ else if (indentlevel >= 0 && tok2->str () == " }" )
539+ --indentlevel;
540+
533541 if (Token::Match (tok2, " sizeof|typeof|typeid (" ))
534542 tok2 = tok2->linkAt (1 );
535543
536544 // conditional block of code that assigns variable..
537- if (Token::Match (tok2, " %var% (" ) && Token::simpleMatch (tok2->linkAt (1 ), " ) {" )) {
545+ else if (Token::Match (tok2, " %var% (" ) && Token::simpleMatch (tok2->linkAt (1 ), " ) {" )) {
538546 Token * const start = tok2->linkAt (1 )->next ();
539547 Token * const end = start->link ();
540- if (Token::findmatch (start, " %varid%" , end, varid)) {
548+ bool varusage = (indentlevel >= 0 && constValue && number_of_if == 0U ) ?
549+ isVariableChanged (start,end,varid) :
550+ (nullptr != Token::findmatch (start, " %varid%" , end, varid));
551+ if (varusage) {
552+ varusagelevel = indentlevel;
553+
541554 // TODO: don't check noreturn scopes
542555 if (number_of_if > 0U || Token::findmatch (tok2, " %varid%" , start, varid)) {
543556 if (settings->debugwarnings )
@@ -586,7 +599,7 @@ static void valueFlowAfterAssign(TokenList *tokenlist, ErrorLogger *errorLogger,
586599 }
587600 }
588601
589- else if (tok2->str () == " }" ) {
602+ else if (tok2->str () == " }" && indentlevel == varusagelevel ) {
590603 ++number_of_if;
591604
592605 // Set "conditional" flag for all values
0 commit comments