@@ -1692,71 +1692,76 @@ void CheckClass::checkConst()
16921692
16931693 for (func = scope->functionList .begin (); func != scope->functionList .end (); ++func) {
16941694 // does the function have a body?
1695- if (func->type == Function::eFunction && func->hasBody () && !func->isFriend () && !func->isStatic () && !func->isVirtual ()) {
1696- // get last token of return type
1697- const Token *previous = func->tokenDef ->previous ();
1695+ if (func->type != Function::eFunction || !func->hasBody ())
1696+ continue ;
16981697
1699- // does the function return a pointer or reference?
1700- if (Token::Match (previous, " *|&" )) {
1701- if (func->retDef ->str () != " const" )
1702- continue ;
1703- } else if (Token::Match (previous->previous (), " *|& >" )) {
1704- const Token *temp = previous->previous ();
1705-
1706- bool foundConst = false ;
1707- while (!Token::Match (temp->previous (), " ;|}|{|public:|protected:|private:" )) {
1708- temp = temp->previous ();
1709- if (temp->str () == " const" ) {
1710- foundConst = true ;
1711- break ;
1712- }
1713- }
1698+ // don't warn for friend/static/virtual methods
1699+ if (func->isFriend () || func->isStatic () || func->isVirtual ())
1700+ continue ;
17141701
1715- if (!foundConst)
1716- continue ;
1717- } else if (func->isOperator () && Token::Match (previous, " ;|{|}|public:|private:|protected:" )) { // Operator without return type: conversion operator
1718- const std::string& opName = func->tokenDef ->str ();
1719- if (opName.compare (8 , 5 , " const" ) != 0 && opName.back () == ' &' )
1720- continue ;
1721- } else {
1722- // don't warn for unknown types..
1723- // LPVOID, HDC, etc
1724- if (previous->str ().size () > 2 && !previous->type () && previous->isUpperCaseName ())
1725- continue ;
1726- }
1702+ // get last token of return type
1703+ const Token *previous = func->tokenDef ->previous ();
17271704
1728- // check if base class function is virtual
1729- if (!scope->definedType ->derivedFrom .empty ()) {
1730- if (func->isImplicitlyVirtual (true ))
1731- continue ;
1705+ // does the function return a pointer or reference?
1706+ if (Token::Match (previous, " *|&" )) {
1707+ if (func->retDef ->str () != " const" )
1708+ continue ;
1709+ } else if (Token::Match (previous->previous (), " *|& >" )) {
1710+ const Token *temp = previous->previous ();
1711+
1712+ bool foundConst = false ;
1713+ while (!Token::Match (temp->previous (), " ;|}|{|public:|protected:|private:" )) {
1714+ temp = temp->previous ();
1715+ if (temp->str () == " const" ) {
1716+ foundConst = true ;
1717+ break ;
1718+ }
17321719 }
17331720
1734- bool memberAccessed = false ;
1735- // if nothing non-const was found. write error..
1736- if (checkConstFunc (scope, &*func, memberAccessed)) {
1737- std::string classname = scope->className ;
1738- const Scope *nest = scope->nestedIn ;
1739- while (nest && nest->type != Scope::eGlobal) {
1740- classname = std::string (nest->className + " ::" + classname);
1741- nest = nest->nestedIn ;
1742- }
1721+ if (!foundConst)
1722+ continue ;
1723+ } else if (func->isOperator () && Token::Match (previous, " ;|{|}|public:|private:|protected:" )) { // Operator without return type: conversion operator
1724+ const std::string& opName = func->tokenDef ->str ();
1725+ if (opName.compare (8 , 5 , " const" ) != 0 && opName.back () == ' &' )
1726+ continue ;
1727+ } else {
1728+ // don't warn for unknown types..
1729+ // LPVOID, HDC, etc
1730+ if (previous->str ().size () > 2 && !previous->type () && previous->isUpperCaseName ())
1731+ continue ;
1732+ }
1733+
1734+ // check if base class function is virtual
1735+ if (!scope->definedType ->derivedFrom .empty () && !func->isImplicitlyVirtual (true ))
1736+ continue ;
17431737
1744- // get function name
1745- std::string functionName = (func->tokenDef ->isName () ? " " : " operator" ) + func->tokenDef ->str ();
1738+ bool memberAccessed = false ;
1739+ // if nothing non-const was found. write error..
1740+ if (!checkConstFunc (scope, &*func, memberAccessed))
1741+ continue ;
17461742
1747- if (func->tokenDef ->str () == " (" )
1748- functionName += " )" ;
1749- else if (func->tokenDef ->str () == " [" )
1750- functionName += " ]" ;
1743+ if (func->isConst () && (memberAccessed || func->isOperator ()))
1744+ continue ;
17511745
1752- if (!func->isConst () || (!memberAccessed && !func->isOperator ())) {
1753- if (func->isInline ())
1754- checkConstError (func->token , classname, functionName, !memberAccessed && !func->isOperator ());
1755- else // not inline
1756- checkConstError2 (func->token , func->tokenDef , classname, functionName, !memberAccessed && !func->isOperator ());
1757- }
1758- }
1746+ std::string classname = scope->className ;
1747+ const Scope *nest = scope->nestedIn ;
1748+ while (nest && nest->type != Scope::eGlobal) {
1749+ classname = std::string (nest->className + " ::" + classname);
1750+ nest = nest->nestedIn ;
17591751 }
1752+
1753+ // get function name
1754+ std::string functionName = (func->tokenDef ->isName () ? " " : " operator" ) + func->tokenDef ->str ();
1755+
1756+ if (func->tokenDef ->str () == " (" )
1757+ functionName += " )" ;
1758+ else if (func->tokenDef ->str () == " [" )
1759+ functionName += " ]" ;
1760+
1761+ if (func->isInline ())
1762+ checkConstError (func->token , classname, functionName, !memberAccessed && !func->isOperator ());
1763+ else // not inline
1764+ checkConstError2 (func->token , func->tokenDef , classname, functionName, !memberAccessed && !func->isOperator ());
17601765 }
17611766 }
17621767}
0 commit comments