| 1683 | } |
| 1684 | |
| 1685 | void CheckOtherImpl::checkConstVariable() |
| 1686 | { |
| 1687 | if ((!mSettings.severity.isEnabled(Severity::style) || mTokenizer->isC()) && !mSettings.isPremiumEnabled("constVariable")) |
| 1688 | return; |
| 1689 | |
| 1690 | logChecker("CheckOther::checkConstVariable"); // style,c++ |
| 1691 | |
| 1692 | const SymbolDatabase *const symbolDatabase = mTokenizer->getSymbolDatabase(); |
| 1693 | |
| 1694 | for (const Variable *var : symbolDatabase->variableList()) { |
| 1695 | if (!var) |
| 1696 | continue; |
| 1697 | if (!var->isReference()) |
| 1698 | continue; |
| 1699 | if (var->isRValueReference()) |
| 1700 | continue; |
| 1701 | if (var->isPointer()) |
| 1702 | continue; |
| 1703 | if (var->isConst()) |
| 1704 | continue; |
| 1705 | const Scope* scope = var->scope(); |
| 1706 | if (!scope) |
| 1707 | continue; |
| 1708 | const Function* function = scope->function; |
| 1709 | if (!function && !scope->isLocal()) |
| 1710 | continue; |
| 1711 | if (function && var->isArgument()) { |
| 1712 | if (function->isImplicitlyVirtual() || function->templateDef) |
| 1713 | continue; |
| 1714 | if (function->isConstructor() && isVariableMutableInInitializer(function->constructorMemberInitialization(), scope->bodyStart, var->declarationId())) |
| 1715 | continue; |
| 1716 | } |
| 1717 | if (var->isGlobal()) |
| 1718 | continue; |
| 1719 | if (var->isStatic()) |
| 1720 | continue; |
| 1721 | if (var->isArray() && !var->isStlType()) |
| 1722 | continue; |
| 1723 | if (var->isEnumType()) |
| 1724 | continue; |
| 1725 | if (var->isVolatile()) |
| 1726 | continue; |
| 1727 | if (var->isMaybeUnused()) |
| 1728 | continue; |
| 1729 | if (var->nameToken()->isExpandedMacro()) |
| 1730 | continue; |
| 1731 | if (isStructuredBindingVariable(var)) // TODO: check all bound variables |
| 1732 | continue; |
| 1733 | if (isCastToVoid(var)) |
| 1734 | continue; |
| 1735 | if (isVariableChanged(var, mSettings)) |
| 1736 | continue; |
| 1737 | const bool hasFunction = function != nullptr; |
| 1738 | if (!hasFunction) { |
| 1739 | const Scope* functionScope = scope; |
| 1740 | do { |
| 1741 | functionScope = functionScope->nestedIn; |
| 1742 | } while (functionScope && !(function = functionScope->function)); |
no test coverage detected