| 479 | } |
| 480 | |
| 481 | void CheckOtherImpl::suspiciousFloatingPointCast() |
| 482 | { |
| 483 | if (!mSettings.severity.isEnabled(Severity::style) && !mSettings.isPremiumEnabled("suspiciousFloatingPointCast")) |
| 484 | return; |
| 485 | |
| 486 | logChecker("CheckOther::suspiciousFloatingPointCast"); // style |
| 487 | |
| 488 | const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase(); |
| 489 | for (const Scope * scope : symbolDatabase->functionScopes) { |
| 490 | const Token* tok = scope->bodyStart; |
| 491 | if (scope->function && scope->function->isConstructor()) |
| 492 | tok = scope->classDef; |
| 493 | for (; tok && tok != scope->bodyEnd; tok = tok->next()) { |
| 494 | |
| 495 | if (!tok->isCast()) |
| 496 | continue; |
| 497 | |
| 498 | const ValueType* vt = tok->valueType(); |
| 499 | if (!vt || vt->pointer || vt->reference != Reference::None || (vt->type != ValueType::FLOAT && vt->type != ValueType::DOUBLE)) |
| 500 | continue; |
| 501 | |
| 502 | using VTT = std::vector<ValueType::Type>; |
| 503 | const VTT sourceTypes = vt->type == ValueType::FLOAT ? VTT{ ValueType::DOUBLE, ValueType::LONGDOUBLE } : VTT{ ValueType::LONGDOUBLE }; |
| 504 | |
| 505 | const Token* source = tok->astOperand2() ? tok->astOperand2() : tok->astOperand1(); |
| 506 | if (!source || !source->valueType() || std::find(sourceTypes.begin(), sourceTypes.end(), source->valueType()->type) == sourceTypes.end()) |
| 507 | continue; |
| 508 | |
| 509 | const Token* parent = tok->astParent(); |
| 510 | if (!parent) |
| 511 | continue; |
| 512 | |
| 513 | const ValueType* parentVt = parent->valueType(); |
| 514 | if (!parentVt || parent->str() == "(") { |
| 515 | int argn{}; |
| 516 | if (const Token* ftok = getTokenArgumentFunction(tok, argn)) { |
| 517 | if (ftok->function()) { |
| 518 | if (const Variable* argVar = ftok->function()->getArgumentVar(argn)) |
| 519 | parentVt = argVar->valueType(); |
| 520 | } |
| 521 | } |
| 522 | } |
| 523 | if (!parentVt || std::find(sourceTypes.begin(), sourceTypes.end(), parentVt->type) == sourceTypes.end()) |
| 524 | continue; |
| 525 | |
| 526 | suspiciousFloatingPointCastError(tok); |
| 527 | } |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | void CheckOtherImpl::suspiciousFloatingPointCastError(const Token* tok) |
| 532 | { |
no test coverage detected