fixed some `modernize-use-emplace` false negatives with `std::stack` / removed `internalStlUsage` check (#4346)

This commit is contained in:
Oliver Stöneberg 2022-08-17 09:50:14 +02:00 committed by GitHub
parent 5b4c6c1e73
commit 5f171b9673
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 11 additions and 28 deletions

View File

@ -357,7 +357,7 @@ void CheckOther::checkSwitchCaseFallThrough()
reportError(_tokenizer->tokens(), Severity::debug, "debug", errmsg.str()); reportError(_tokenizer->tokens(), Severity::debug, "debug", errmsg.str());
break; break;
} }
ifnest.push(std::make_pair(tok2->link(), false)); ifnest.emplace(tok2->link(), false);
justbreak = false; justbreak = false;
} else if (Token::simpleMatch(tok2, "while (")) { } else if (Token::simpleMatch(tok2, "while (")) {
tok2 = tok2->tokAt(1)->link()->next(); tok2 = tok2->tokAt(1)->link()->next();
@ -420,7 +420,7 @@ void CheckOther::checkSwitchCaseFallThrough()
reportError(_tokenizer->tokens(), Severity::debug, "debug", errmsg.str()); reportError(_tokenizer->tokens(), Severity::debug, "debug", errmsg.str());
break; break;
} }
ifnest.push(std::make_pair(tok2->link(), justbreak)); ifnest.emplace(tok2->link(), justbreak);
justbreak = false; justbreak = false;
} else { } else {
justbreak &= ifnest.top().second; justbreak &= ifnest.top().second;

View File

@ -343,19 +343,6 @@ void CheckInternal::checkExtraWhitespace()
} }
} }
void CheckInternal::checkStlUsage()
{
const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase();
for (const Scope *scope : symbolDatabase->functionScopes) {
for (const Token* tok = scope->bodyStart->next(); tok != scope->bodyEnd; tok = tok->next()) {
if (Token::simpleMatch(tok, ". emplace ("))
reportError(tok, Severity::error, "internalStlUsage", "The 'emplace' function shall be avoided for now. It is not available e.g. in Slackware 14.0. 'emplace_back' is fine.");
//if (Token::simpleMatch(tok, ". back ( )") && tok->astOperand1() && tok->astOperand1()->valueType() && tok->astOperand1()->valueType()->container && Token::simpleMatch(tok->astOperand1()->valueType()->container, "std :: string"))
// reportError(tok, Severity::error, "internalStlUsage", "The 'std::string::back()' function shall be avoided for now.");
}
}
}
void CheckInternal::multiComparePatternError(const Token* tok, const std::string& pattern, const std::string &funcname) void CheckInternal::multiComparePatternError(const Token* tok, const std::string& pattern, const std::string &funcname)
{ {
reportError(tok, Severity::error, "multiComparePatternError", reportError(tok, Severity::error, "multiComparePatternError",

View File

@ -55,7 +55,6 @@ public:
checkInternal.checkRedundantNextPrevious(); checkInternal.checkRedundantNextPrevious();
checkInternal.checkExtraWhitespace(); checkInternal.checkExtraWhitespace();
checkInternal.checkRedundantTokCheck(); checkInternal.checkRedundantTokCheck();
checkInternal.checkStlUsage();
} }
/** @brief %Check if a simple pattern is used inside Token::Match or Token::findmatch */ /** @brief %Check if a simple pattern is used inside Token::Match or Token::findmatch */
@ -78,9 +77,6 @@ public:
/** @brief %Check if there is a redundant check for none-nullness of parameter before Match functions, such as (tok && Token::Match(tok, "foo")) */ /** @brief %Check if there is a redundant check for none-nullness of parameter before Match functions, such as (tok && Token::Match(tok, "foo")) */
void checkRedundantTokCheck(); void checkRedundantTokCheck();
/** @brief Try to avoid some new functions that are not fully supported in Linux */
void checkStlUsage();
private: private:
void multiComparePatternError(const Token *tok, const std::string &pattern, const std::string &funcname); void multiComparePatternError(const Token *tok, const std::string &pattern, const std::string &funcname);
void simplePatternError(const Token *tok, const std::string &pattern, const std::string &funcname); void simplePatternError(const Token *tok, const std::string &pattern, const std::string &funcname);

View File

@ -699,7 +699,7 @@ void SymbolDatabase::createSymbolDatabaseFindAllScopes()
scope->checkVariable(tok->tokAt(2), AccessControl::Throw, mSettings); // check for variable declaration and add it to new scope if found scope->checkVariable(tok->tokAt(2), AccessControl::Throw, mSettings); // check for variable declaration and add it to new scope if found
tok = scopeStartTok; tok = scopeStartTok;
} else if (Token::Match(tok, "%var% {")) { } else if (Token::Match(tok, "%var% {")) {
endInitList.push(std::make_pair(tok->next()->link(), scope)); endInitList.emplace(tok->next()->link(), scope);
tok = tok->next(); tok = tok->next();
} else if (const Token *lambdaEndToken = findLambdaEndToken(tok)) { } else if (const Token *lambdaEndToken = findLambdaEndToken(tok)) {
const Token *lambdaStartToken = lambdaEndToken->link(); const Token *lambdaStartToken = lambdaEndToken->link();
@ -711,13 +711,13 @@ void SymbolDatabase::createSymbolDatabaseFindAllScopes()
tok = lambdaStartToken; tok = lambdaStartToken;
} else if (tok->str() == "{") { } else if (tok->str() == "{") {
if (inInitList()) { if (inInitList()) {
endInitList.push(std::make_pair(tok->link(), scope)); endInitList.emplace(tok->link(), scope);
} else if (isExecutableScope(tok)) { } else if (isExecutableScope(tok)) {
scopeList.emplace_back(this, tok, scope, Scope::eUnconditional, tok); scopeList.emplace_back(this, tok, scope, Scope::eUnconditional, tok);
scope->nestedList.push_back(&scopeList.back()); scope->nestedList.push_back(&scopeList.back());
scope = &scopeList.back(); scope = &scopeList.back();
} else if (scope->isExecutable()) { } else if (scope->isExecutable()) {
endInitList.push(std::make_pair(tok->link(), scope)); endInitList.emplace(tok->link(), scope);
} else { } else {
tok = tok->link(); tok = tok->link();
} }

View File

@ -3466,7 +3466,7 @@ public:
void VariableMap::enterScope() void VariableMap::enterScope()
{ {
mScopeInfo.push(std::vector<std::pair<std::string, nonneg int>>()); mScopeInfo.emplace(/*std::vector<std::pair<std::string, nonneg int>>()*/);
} }
bool VariableMap::leaveScope() bool VariableMap::leaveScope()
@ -3840,7 +3840,7 @@ void Tokenizer::setVarIdPass1()
std::stack<VarIdScopeInfo> scopeStack; std::stack<VarIdScopeInfo> scopeStack;
scopeStack.push(VarIdScopeInfo()); scopeStack.emplace(/*VarIdScopeInfo()*/);
std::stack<const Token *> functionDeclEndStack; std::stack<const Token *> functionDeclEndStack;
const Token *functionDeclEndToken = nullptr; const Token *functionDeclEndToken = nullptr;
bool initlist = false; bool initlist = false;
@ -3864,7 +3864,7 @@ void Tokenizer::setVarIdPass1()
if (!variableMap.leaveScope()) if (!variableMap.leaveScope())
cppcheckError(tok); cppcheckError(tok);
} else if (tok->str() == "{") { } else if (tok->str() == "{") {
scopeStack.push(VarIdScopeInfo(true, scopeStack.top().isStructInit || tok->strAt(-1) == "=", /*isEnum=*/ false, *variableMap.getVarId())); scopeStack.emplace(true, scopeStack.top().isStructInit || tok->strAt(-1) == "=", /*isEnum=*/ false, *variableMap.getVarId());
// check if this '{' is a start of an "if" body // check if this '{' is a start of an "if" body
const Token * ifToken = tok->previous(); const Token * ifToken = tok->previous();
@ -3918,7 +3918,7 @@ void Tokenizer::setVarIdPass1()
variableMap.enterScope(); variableMap.enterScope();
} }
initlist = false; initlist = false;
scopeStack.push(VarIdScopeInfo(isExecutable, scopeStack.top().isStructInit || tok->strAt(-1) == "=", isEnumStart(tok), *variableMap.getVarId())); scopeStack.emplace(isExecutable, scopeStack.top().isStructInit || tok->strAt(-1) == "=", isEnumStart(tok), *variableMap.getVarId());
} else { /* if (tok->str() == "}") */ } else { /* if (tok->str() == "}") */
bool isNamespace = false; bool isNamespace = false;
for (const Token *tok1 = tok->link()->previous(); tok1 && tok1->isName(); tok1 = tok1->previous()) { for (const Token *tok1 = tok->link()->previous(); tok1 && tok1->isName(); tok1 = tok1->previous()) {
@ -3952,7 +3952,7 @@ void Tokenizer::setVarIdPass1()
scopeStack.pop(); scopeStack.pop();
if (scopeStack.empty()) { // should be impossible if (scopeStack.empty()) { // should be impossible
scopeStack.push(VarIdScopeInfo()); scopeStack.emplace(/*VarIdScopeInfo()*/);
} }
} }
} }

View File

@ -69,7 +69,7 @@ bool matchglob(const std::string& pattern, const std::string& name)
} }
if (*n != '\0') { if (*n != '\0') {
// If this isn't the last possibility, save it for later // If this isn't the last possibility, save it for later
backtrack.push(std::make_pair(p, n)); backtrack.emplace(p, n);
} }
break; break;
case '?': case '?':