@@ -2360,6 +2360,14 @@ bool CheckClass::isConstMemberFunc(const Scope *scope, const Token *tok)
23602360
23612361const std::set<std::string> CheckClass::stl_containers_not_const = { " map" , " unordered_map" , " std :: map|unordered_map <" }; // start pattern
23622362
2363+ static bool isNonConstPtrCast (const Token* tok)
2364+ {
2365+ if (!tok || !tok->isCast ())
2366+ return false ;
2367+ const ValueType* vt = tok->valueType ();
2368+ return !vt || (vt->pointer > 0 && !vt->isConst (vt->pointer ));
2369+ }
2370+
23632371bool CheckClass::checkConstFunc (const Scope *scope, const Function *func, MemberAccess& memberAccessed) const
23642372{
23652373 if (mTokenizer ->hasIfdef (func->functionScope ->bodyStart , func->functionScope ->bodyEnd ))
@@ -2450,9 +2458,7 @@ bool CheckClass::checkConstFunc(const Scope *scope, const Function *func, Member
24502458 }
24512459
24522460 // non const pointer cast
2453- if (tok1->valueType () && tok1->valueType ()->pointer > 0 && tok1->astParent () && tok1->astParent ()->isCast () &&
2454- !(tok1->astParent ()->valueType () &&
2455- (tok1->astParent ()->valueType ()->pointer == 0 || tok1->astParent ()->valueType ()->isConst (tok1->astParent ()->valueType ()->pointer ))))
2461+ if (tok1->valueType () && tok1->valueType ()->pointer > 0 && isNonConstPtrCast (tok1->astParent ()))
24562462 return false ;
24572463
24582464 const Token* lhs = tok1->previous ();
@@ -2463,6 +2469,12 @@ bool CheckClass::checkConstFunc(const Scope *scope, const Function *func, Member
24632469 else if (lhs->str () == " :" && lhs->astParent () && lhs->astParent ()->astParent () && lhs->astParent ()->str () == " ?" )
24642470 lhs = lhs->astParent ()->astParent ();
24652471 if (lhs->str () == " &" ) {
2472+ const Token* parent = lhs->astParent ();
2473+ while (Token::Match (parent, " [+(]" )) {
2474+ if (isNonConstPtrCast (parent))
2475+ return false ;
2476+ parent = parent->astParent ();
2477+ }
24662478 const Token* const top = lhs->astTop ();
24672479 if (top->isAssignmentOp ()) {
24682480 if (Token::simpleMatch (top->astOperand2 (), " {" ) && !top->astOperand2 ()->previous ()->function ()) // TODO: check usage in init list
0 commit comments