| 907 | } |
| 908 | |
| 909 | void CheckConditionImpl::identicalConditionAfterEarlyExitError(const Token *cond1, const Token* cond2, ErrorPath errorPath) |
| 910 | { |
| 911 | if (diag(cond1, cond2)) |
| 912 | return; |
| 913 | |
| 914 | const bool isReturnValue = cond2 && Token::simpleMatch(cond2->astParent(), "return"); |
| 915 | |
| 916 | const std::string cond(cond1 ? cond1->expressionString() : "x"); |
| 917 | const std::string value = (cond2 && cond2->valueType() && cond2->valueType()->type == ValueType::Type::BOOL) ? "false" : "0"; |
| 918 | |
| 919 | errorPath.emplace_back(cond1, "If condition '" + cond + "' is true, the function will return/exit"); |
| 920 | errorPath.emplace_back(cond2, (isReturnValue ? "Returning identical expression '" : "Testing identical condition '") + cond + "'"); |
| 921 | |
| 922 | reportError(std::move(errorPath), |
| 923 | Severity::warning, |
| 924 | "identicalConditionAfterEarlyExit", |
| 925 | isReturnValue |
| 926 | ? ("Identical condition and return expression '" + cond + "', return value is always " + value) |
| 927 | : ("Identical condition '" + cond + "', second condition is always false"), |
| 928 | CWE398, |
| 929 | Certainty::normal); |
| 930 | } |
| 931 | |
| 932 | //--------------------------------------------------------------------------- |
| 933 | // if ((x != 1) || (x != 3)) // expression always true |
no test coverage detected