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

Method constStatementError

lib/checkother.cpp:2413–2463  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2411}
2412
2413void CheckOtherImpl::constStatementError(const Token *tok, const std::string &type, bool inconclusive)
2414{
2415 const Token *valueTok = tok;
2416 while (valueTok && valueTok->isCast())
2417 valueTok = valueTok->astOperand2() ? valueTok->astOperand2() : valueTok->astOperand1();
2418
2419 std::string msg;
2420 if (Token::simpleMatch(tok, "=="))
2421 msg = "Found suspicious equality comparison. Did you intend to assign a value instead?";
2422 else if (Token::Match(tok, ",|!|~|%cop%"))
2423 msg = "Found suspicious operator '" + tok->str() + "', result is not used.";
2424 else if (Token::Match(tok, "%var%"))
2425 msg = "Unused variable value '" + tok->str() + "'";
2426 else if (isConstant(valueTok)) {
2427 std::string typeStr("string");
2428 if (valueTok->isNumber())
2429 typeStr = "numeric";
2430 else if (valueTok->isBoolean())
2431 typeStr = "bool";
2432 else if (valueTok->tokType() == Token::eChar)
2433 typeStr = "character";
2434 else if (isNullOperand(valueTok))
2435 typeStr = "NULL";
2436 else if (valueTok->isEnumerator())
2437 typeStr = "enumerator";
2438 msg = "Redundant code: Found a statement that begins with " + typeStr + " constant.";
2439 }
2440 else if (!tok)
2441 msg = "Redundant code: Found a statement that begins with " + type + " constant.";
2442 else if (tok->isCast() && tok->tokType() == Token::Type::eExtendedOp) {
2443 msg = "Redundant code: Found unused cast ";
2444 msg += valueTok ? "of expression '" + valueTok->expressionString() + "'." : "expression.";
2445 }
2446 else if (tok->str() == "?" && tok->tokType() == Token::Type::eExtendedOp)
2447 msg = "Redundant code: Found unused result of ternary operator.";
2448 else if (tok->str() == "." && tok->tokType() == Token::Type::eOther)
2449 msg = "Redundant code: Found unused member access.";
2450 else if (tok->str() == "[" && tok->tokType() == Token::Type::eExtendedOp)
2451 msg = "Redundant code: Found unused array access.";
2452 else if (tok->str() == "[" && !tok->astParent())
2453 msg = "Redundant code: Found unused lambda.";
2454 else if (Token::Match(tok, "%name%|::"))
2455 msg = "Redundant code: Found unused function.";
2456 else if (Token::Match(tok->previous(), "%name% ("))
2457 msg = "Redundant code: Found unused '" + tok->strAt(-1) + "' expression.";
2458 else if (mSettings.debugwarnings) {
2459 reportError(tok, Severity::debug, "debug", "constStatementError not handled.");
2460 return;
2461 }
2462 reportError(tok, Severity::warning, "constStatement", msg, CWE398, inconclusive ? Certainty::inconclusive : Certainty::normal);
2463}
2464
2465//---------------------------------------------------------------------------
2466// Detect division by zero.

Callers 1

getErrorMessagesMethod · 0.80

Calls 10

isConstantFunction · 0.85
isNullOperandFunction · 0.85
isCastMethod · 0.80
astOperand2Method · 0.80
astOperand1Method · 0.80
astParentMethod · 0.80
simpleMatchFunction · 0.70
reportErrorFunction · 0.70
strMethod · 0.45
expressionStringMethod · 0.45

Tested by

no test coverage detected