Skip to content

Commit

Permalink
qgetenv: Do not include base parameter when it is a nullptr
Browse files Browse the repository at this point in the history
This might be specified in case one does not care about the OK param,
but wants a base of 0. Because that is the default in
qEnvironmentVariableIntValue internally, there is no point to explicitly
specify a nullptr param here.
  • Loading branch information
alex1701c committed Feb 11, 2024
1 parent d066b4f commit f41d393
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/checks/level0/qgetenv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ void QGetEnv::VisitStmt(clang::Stmt *stmt)
StringRef methodname = clazy::name(method);
std::string errorMsg;
std::string replacement;
bool shouldIncludeBaseParameter = false;
bool shouldIncludeOkParameter = false;
bool changesToBaseAutodetection = false;
if (methodname == "isEmpty") {
errorMsg = "qgetenv().isEmpty() allocates.";
replacement = "qEnvironmentVariableIsEmpty";
Expand All @@ -77,9 +78,10 @@ void QGetEnv::VisitStmt(clang::Stmt *stmt)
errorMsg = "qgetenv().toInt() is slow.";
replacement = "qEnvironmentVariableIntValue";
for (unsigned int i = 0; i < memberCall->getNumArgs(); ++i) {
if (!dyn_cast<CXXDefaultArgExpr>(memberCall->getArg(i))) {
if (i == 0) {
shouldIncludeBaseParameter = true;
auto *arg = memberCall->getArg(i);
if (i == 0 && !isa<CXXDefaultArgExpr>(arg)) {
if (!isa<CastExpr>(arg) || !isa<CXXNullPtrLiteralExpr>(dyn_cast<CastExpr>(arg)->getSubExpr())) {
shouldIncludeOkParameter = true;
}
if (i > 0) {
return; // Second toInt arg (base) is not supported by qEnvironmentVariableIntValue
Expand All @@ -91,7 +93,7 @@ void QGetEnv::VisitStmt(clang::Stmt *stmt)
}

std::string getEnvArgStr = Lexer::getSourceText(CharSourceRange::getTokenRange(qgetEnvCall->getArg(0)->getSourceRange()), sm(), lo()).str();
if (shouldIncludeBaseParameter) {
if (shouldIncludeOkParameter) {
getEnvArgStr += ", " + Lexer::getSourceText(CharSourceRange::getTokenRange(memberCall->getArg(0)->getSourceRange()), sm(), lo()).str();
}

Expand Down

0 comments on commit f41d393

Please sign in to comment.