@@ -2360,6 +2360,14 @@ bool CheckClass::isConstMemberFunc(const Scope *scope, const Token *tok)
2360
2360
2361
2361
const std::set<std::string> CheckClass::stl_containers_not_const = { " map" , " unordered_map" , " std :: map|unordered_map <" }; // start pattern
2362
2362
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
+
2363
2371
bool CheckClass::checkConstFunc (const Scope *scope, const Function *func, MemberAccess& memberAccessed) const
2364
2372
{
2365
2373
if (mTokenizer ->hasIfdef (func->functionScope ->bodyStart , func->functionScope ->bodyEnd ))
@@ -2450,9 +2458,7 @@ bool CheckClass::checkConstFunc(const Scope *scope, const Function *func, Member
2450
2458
}
2451
2459
2452
2460
// 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 ()))
2456
2462
return false ;
2457
2463
2458
2464
const Token* lhs = tok1->previous ();
@@ -2463,6 +2469,12 @@ bool CheckClass::checkConstFunc(const Scope *scope, const Function *func, Member
2463
2469
else if (lhs->str () == " :" && lhs->astParent () && lhs->astParent ()->astParent () && lhs->astParent ()->str () == " ?" )
2464
2470
lhs = lhs->astParent ()->astParent ();
2465
2471
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
+ }
2466
2478
const Token* const top = lhs->astTop ();
2467
2479
if (top->isAssignmentOp ()) {
2468
2480
if (Token::simpleMatch (top->astOperand2 (), " {" ) && !top->astOperand2 ()->previous ()->function ()) // TODO: check usage in init list
0 commit comments