diff options
author | Sergio Martins <smartins@kde.org> | 2019-09-22 20:26:27 +0100 |
---|---|---|
committer | Sergio Martins <smartins@kde.org> | 2019-09-22 20:26:27 +0100 |
commit | 842ea022194297091080b9017f1cfa12ed473fa3 (patch) | |
tree | f5a34892fcecbb4cea11bffcb09a2465678c9580 /src | |
parent | f45c77c694e4efa3192492cf0e985f0046cc4f19 (diff) |
range-loop: Fix some true-negatives that didn't suggest qAsConst()
This now warns:
QStringList list = getList();
for (const QString &s : list)
CCMAIL: dfaure@kde.org
Diffstat (limited to 'src')
-rw-r--r-- | src/QtUtils.cpp | 15 |
1 files changed, 14 insertions, 1 deletions
diff --git a/src/QtUtils.cpp b/src/QtUtils.cpp index 4bc61e6b..0d66bb1b 100644 --- a/src/QtUtils.cpp +++ b/src/QtUtils.cpp @@ -207,10 +207,12 @@ bool clazy::isQtContainer(const CXXRecordDecl *record) bool clazy::containerNeverDetaches(const clang::VarDecl *valDecl, StmtBodyRange bodyRange) // clazy:exclude=function-args-by-value { + // This helps for bug 367485 + if (!valDecl) return false; - const FunctionDecl *context = dyn_cast<FunctionDecl>(valDecl->getDeclContext()); + const auto context = dyn_cast<FunctionDecl>(valDecl->getDeclContext()); if (!context) return false; @@ -218,6 +220,17 @@ bool clazy::containerNeverDetaches(const clang::VarDecl *valDecl, StmtBodyRange if (!bodyRange.body) return false; + if (valDecl->hasInit()) { + if (auto cleanupExpr = dyn_cast<clang::ExprWithCleanups>(valDecl->getInit())) { + if (auto ce = dyn_cast<clang::CXXConstructExpr>(cleanupExpr->getSubExpr())) { + if (!ce->isListInitialization() && !ce->isStdInitListInitialization()) { + // When initing via copy or move ctor there's possible detachments. + return false; + } + } + } + } + // TODO1: Being passed to a function as const should be OK if (Utils::isPassedToFunction(bodyRange, valDecl, false)) return false; |