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

Method checkRedundantTokCheck

lib/checkinternal.cpp:85–117  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

83}
84
85void CheckInternalImpl::checkRedundantTokCheck()
86{
87 for (const Token *tok = mTokenizer->tokens(); tok; tok = tok->next()) {
88 if (Token::Match(tok, "&& Token :: simpleMatch|Match|findsimplematch|findmatch (")) {
89 // in code like
90 // if (tok->previous() && Token::match(tok->previous(), "bla")) {}
91 // the first tok->previous() check is redundant
92 const Token *astOp1 = tok->astOperand1();
93 const Token *astOp2 = getArguments(tok->tokAt(3))[0];
94 if (Token::simpleMatch(astOp1, "&&")) {
95 astOp1 = astOp1->astOperand2();
96 }
97 if (astOp1->expressionString() == astOp2->expressionString()) {
98 checkRedundantTokCheckError(astOp2);
99 }
100 // if (!tok || !Token::match(tok, "foo"))
101 } else if (Token::Match(tok, "%oror% ! Token :: simpleMatch|Match|findsimplematch|findmatch (")) {
102 const Token *negTok = tok->next()->astParent()->astOperand1();
103 if (Token::simpleMatch(negTok, "||")) {
104 negTok = negTok->astOperand2();
105 }
106 // the first tok condition is negated
107 if (Token::simpleMatch(negTok, "!")) {
108 const Token *astOp1 = negTok->astOperand1();
109 const Token *astOp2 = getArguments(tok->tokAt(4))[0];
110
111 if (astOp1->expressionString() == astOp2->expressionString()) {
112 checkRedundantTokCheckError(astOp2);
113 }
114 }
115 }
116 }
117}
118
119
120void CheckInternalImpl::checkRedundantTokCheckError(const Token* tok)

Callers 1

runChecksMethod · 0.45

Calls 8

nextMethod · 0.80
astOperand1Method · 0.80
astOperand2Method · 0.80
astParentMethod · 0.80
getArgumentsFunction · 0.70
simpleMatchFunction · 0.70
tokAtMethod · 0.45
expressionStringMethod · 0.45

Tested by

no test coverage detected