diff options
author | Sergio Martins <smartins@kde.org> | 2017-08-12 18:54:52 +0100 |
---|---|---|
committer | Sergio Martins <smartins@kde.org> | 2017-08-12 18:54:52 +0100 |
commit | 2bce3a146e18b99031650cad03c203058f35558f (patch) | |
tree | 8496f6c70a894922b12b3bfe9b36b971b697e70a | |
parent | e59ffbd7f592fe84c90a556b754bce67edc91f4c (diff) |
Introducing connect-3arg-lambda
For those days when you're connecting to a lambda that uses
QObjects that might be outlived by the sender.
In these cases the recommendation is to use the 4-arg overload,
which takes a context object as 3rd parameter.
-rw-r--r-- | Changelog | 2 | ||||
-rw-r--r-- | ClazySources.cmake | 1 | ||||
-rw-r--r-- | src/checks/level1/README-connect-3arg-lambda.md | 9 | ||||
-rw-r--r-- | src/checks/level1/connect-3arg-lambda.cpp | 98 | ||||
-rw-r--r-- | src/checks/level1/connect-3arg-lambda.h | 39 | ||||
-rw-r--r-- | tests/clazy/test_requested_checks.sh.expected | 17 | ||||
-rw-r--r-- | tests/connect-3arg-lambda/config.json | 7 | ||||
-rw-r--r-- | tests/connect-3arg-lambda/main.cpp | 42 | ||||
-rw-r--r-- | tests/connect-3arg-lambda/main.cpp.expected | 3 |
9 files changed, 210 insertions, 8 deletions
@@ -49,5 +49,7 @@ the plugin and man pages. * v1.3 () + - New checks: + connect-3arg-lambda - missing-qobject-macro is now a level2 check, instead of level1. Because, people can omit Q_OBJECT intentionally. diff --git a/ClazySources.cmake b/ClazySources.cmake index 3053d400..fe5a68fa 100644 --- a/ClazySources.cmake +++ b/ClazySources.cmake @@ -38,6 +38,7 @@ set(CLAZY_CHECKS_SRCS ${CMAKE_CURRENT_LIST_DIR}/src/checks/level0/wrong-qglobalstatic.cpp ${CMAKE_CURRENT_LIST_DIR}/src/checks/level1/autounexpectedqstringbuilder.cpp ${CMAKE_CURRENT_LIST_DIR}/src/checks/level1/child-event-qobject-cast.cpp + ${CMAKE_CURRENT_LIST_DIR}/src/checks/level1/connect-3arg-lambda.cpp ${CMAKE_CURRENT_LIST_DIR}/src/checks/level1/detachingtemporary.cpp ${CMAKE_CURRENT_LIST_DIR}/src/checks/level1/foreach.cpp ${CMAKE_CURRENT_LIST_DIR}/src/checks/level1/inefficient-qlist-soft.cpp diff --git a/src/checks/level1/README-connect-3arg-lambda.md b/src/checks/level1/README-connect-3arg-lambda.md new file mode 100644 index 00000000..36dbc2da --- /dev/null +++ b/src/checks/level1/README-connect-3arg-lambda.md @@ -0,0 +1,9 @@ +# connect-3arg-lambda + +Warns when using the 3-arg `QObject::connect` that takes a lambda. +The recommendation is to use the 4-arg overload, which takes a context object +so that the lambda isn't executed when the context object is deleted. + +It's very common to use lambdas to connect signals to slots with different number +of arguments. This can result in a crash if the signal is emitted after the receiver +is deleted. diff --git a/src/checks/level1/connect-3arg-lambda.cpp b/src/checks/level1/connect-3arg-lambda.cpp new file mode 100644 index 00000000..e27dfb93 --- /dev/null +++ b/src/checks/level1/connect-3arg-lambda.cpp @@ -0,0 +1,98 @@ +/* + This file is part of the clazy static checker. + + Copyright (C) 2017 Sergio Martins <sergio.martins@kdab.com> + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#include "connect-3arg-lambda.h" +#include "Utils.h" +#include "HierarchyUtils.h" +#include "QtUtils.h" +#include "TypeUtils.h" +#include "checkmanager.h" + +#include <clang/AST/AST.h> + +using namespace clang; +using namespace std; + + +Connect3argLambda::Connect3argLambda(const std::string &name, ClazyContext *context) + : CheckBase(name, context) +{ +} + + +void Connect3argLambda::VisitStmt(clang::Stmt *stmt) +{ + auto callExpr = dyn_cast<CallExpr>(stmt); + if (!callExpr) + return; + + FunctionDecl *fdecl = callExpr->getDirectCallee(); + if (!QtUtils::isConnect(fdecl) || fdecl->getNumParams() != 3) + return; + + auto lambda = HierarchyUtils::getFirstChildOfType2<LambdaExpr>(callExpr->getArg(2)); + if (!lambda) + return; + callExpr->getArg(0)->dump(); + + // The sender can be: this + auto senderThis = HierarchyUtils::unpeal<CXXThisExpr>(callExpr->getArg(0), HierarchyUtils::IgnoreImplicitCasts); + + // Or it can be: a declref (variable) + DeclRefExpr *senderDeclRef = senderThis ? nullptr : HierarchyUtils::getFirstChildOfType2<DeclRefExpr>(callExpr->getArg(0)); + + + // If this is referenced inside the lambda body, for example, by calling a member function + auto thisExprs = HierarchyUtils::getStatements<CXXThisExpr>(lambda->getBody()); + const bool lambdaHasThis = !thisExprs.empty(); + + // The variables used inside the lambda + auto declrefs = HierarchyUtils::getStatements<DeclRefExpr>(lambda->getBody()); + + // If lambda doesn't do anything interesting, don't warn + if (declrefs.empty() && !lambdaHasThis) + return; + + if (lambdaHasThis && !senderThis && QtUtils::isQObject(thisExprs[0]->getType())) { + emitWarning(stmt->getLocStart(), "Pass 'this' as the 3rd connect parameter"); + return; + } + + ValueDecl *senderDecl = senderDeclRef ? senderDeclRef->getDecl() : nullptr; + // We'll only warn if the lambda is dereferencing another QObject (besides the sender). + bool found = false; + for (auto declref : declrefs) { + ValueDecl *decl = declref->getDecl(); + if (decl == senderDecl) + continue; // It's the sender, continue. + + if (QtUtils::isQObject(decl->getType())) { + found = true; + break; + } + } + + if (found) + emitWarning(stmt->getLocStart(), "Pass a context object as 3rd connect parameter"); +} + + +REGISTER_CHECK("connect-3arg-lambda", Connect3argLambda, CheckLevel1) diff --git a/src/checks/level1/connect-3arg-lambda.h b/src/checks/level1/connect-3arg-lambda.h new file mode 100644 index 00000000..69b8a1b6 --- /dev/null +++ b/src/checks/level1/connect-3arg-lambda.h @@ -0,0 +1,39 @@ +/* + This file is part of the clazy static checker. + + Copyright (C) 2017 Sergio Martins <sergio.martins@kdab.com> + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#ifndef CLAZY_CONNECT_3ARG_LAMBDA_H +#define CLAZY_CONNECT_3ARG_LAMBDA_H + +#include "checkbase.h" + + +/** + * See README-connect-3arg-lambda.md for more info. + */ +class Connect3argLambda : public CheckBase +{ +public: + explicit Connect3argLambda(const std::string &name, ClazyContext *context); + void VisitStmt(clang::Stmt *stmt) override; +private: +}; + +#endif diff --git a/tests/clazy/test_requested_checks.sh.expected b/tests/clazy/test_requested_checks.sh.expected index 8211eaaf..b7f048f9 100644 --- a/tests/clazy/test_requested_checks.sh.expected +++ b/tests/clazy/test_requested_checks.sh.expected @@ -1,6 +1,6 @@ -Requested checks: auto-unexpected-qstringbuilder, child-event-qobject-cast, connect-non-signal, connect-not-normalized, container-anti-pattern, detaching-temporary, foreach, incorrect-emit, inefficient-qlist-soft, install-event-filter, lambda-in-connect, mutable-container-key, non-pod-global-static, post-event, qcolor-from-literal, qdatetime-utc, qdeleteall, qenums, qfileinfo-exists, qgetenv, qlatin1string-non-ascii, qmap-with-pointer-key, qstring-arg, qstring-insensitive-allocation, qstring-left, qstring-ref, qt-macros, qvariant-template-instantiation, range-loop, returning-data-from-temporary, rule-of-two-soft, strict-iterators, temporary-iterator, unused-non-trivial-variable, writing-to-temporary, wrong-qglobalstatic +Requested checks: auto-unexpected-qstringbuilder, child-event-qobject-cast, connect-3arg-lambda, connect-non-signal, connect-not-normalized, container-anti-pattern, detaching-temporary, foreach, incorrect-emit, inefficient-qlist-soft, install-event-filter, lambda-in-connect, mutable-container-key, non-pod-global-static, post-event, qcolor-from-literal, qdatetime-utc, qdeleteall, qenums, qfileinfo-exists, qgetenv, qlatin1string-non-ascii, qmap-with-pointer-key, qstring-arg, qstring-insensitive-allocation, qstring-left, qstring-ref, qt-macros, qvariant-template-instantiation, range-loop, returning-data-from-temporary, rule-of-two-soft, strict-iterators, temporary-iterator, unused-non-trivial-variable, writing-to-temporary, wrong-qglobalstatic Invalid check: foo -Requested checks: auto-unexpected-qstringbuilder, child-event-qobject-cast, connect-non-signal, connect-not-normalized, container-anti-pattern, detaching-temporary, foreach, incorrect-emit, inefficient-qlist-soft, install-event-filter, lambda-in-connect, mutable-container-key, non-pod-global-static, post-event, qcolor-from-literal, qdatetime-utc, qdeleteall, qenums, qfileinfo-exists, qgetenv, qlatin1string-non-ascii, qmap-with-pointer-key, qstring-arg, qstring-insensitive-allocation, qstring-left, qstring-ref, qt-macros, qvariant-template-instantiation, range-loop, returning-data-from-temporary, rule-of-two-soft, strict-iterators, temporary-iterator, unused-non-trivial-variable, writing-to-temporary, wrong-qglobalstatic +Requested checks: auto-unexpected-qstringbuilder, child-event-qobject-cast, connect-3arg-lambda, connect-non-signal, connect-not-normalized, container-anti-pattern, detaching-temporary, foreach, incorrect-emit, inefficient-qlist-soft, install-event-filter, lambda-in-connect, mutable-container-key, non-pod-global-static, post-event, qcolor-from-literal, qdatetime-utc, qdeleteall, qenums, qfileinfo-exists, qgetenv, qlatin1string-non-ascii, qmap-with-pointer-key, qstring-arg, qstring-insensitive-allocation, qstring-left, qstring-ref, qt-macros, qvariant-template-instantiation, range-loop, returning-data-from-temporary, rule-of-two-soft, strict-iterators, temporary-iterator, unused-non-trivial-variable, writing-to-temporary, wrong-qglobalstatic Requested checks: foreach Requested checks: foreach, writing-to-temporary Invalid check: foo @@ -8,18 +8,18 @@ Requested checks: foreach, writing-to-temporary Requested checks: old-style-connect Requested checks: old-style-connect Requested checks: foreach, old-style-connect -Requested checks: auto-unexpected-qstringbuilder, base-class-event, child-event-qobject-cast, connect-non-signal, connect-not-normalized, container-anti-pattern, container-inside-loop, copyable-polymorphic, ctor-missing-parent-argument, detaching-temporary, foreach, function-args-by-ref, function-args-by-value, global-const-char-pointer, implicit-casts, incorrect-emit, inefficient-qlist-soft, install-event-filter, lambda-in-connect, missing-qobject-macro, missing-typeinfo, mutable-container-key, non-pod-global-static, old-style-connect, post-event, qcolor-from-literal, qdatetime-utc, qdeleteall, qenums, qfileinfo-exists, qgetenv, qlatin1string-non-ascii, qmap-with-pointer-key, qstring-allocations, qstring-arg, qstring-insensitive-allocation, qstring-left, qstring-ref, qt-macros, qvariant-template-instantiation, range-loop, reserve-candidates, returning-data-from-temporary, returning-void-expression, rule-of-three, rule-of-two-soft, strict-iterators, temporary-iterator, unused-non-trivial-variable, virtual-call-ctor, writing-to-temporary, wrong-qglobalstatic +Requested checks: auto-unexpected-qstringbuilder, base-class-event, child-event-qobject-cast, connect-3arg-lambda, connect-non-signal, connect-not-normalized, container-anti-pattern, container-inside-loop, copyable-polymorphic, ctor-missing-parent-argument, detaching-temporary, foreach, function-args-by-ref, function-args-by-value, global-const-char-pointer, implicit-casts, incorrect-emit, inefficient-qlist-soft, install-event-filter, lambda-in-connect, missing-qobject-macro, missing-typeinfo, mutable-container-key, non-pod-global-static, old-style-connect, post-event, qcolor-from-literal, qdatetime-utc, qdeleteall, qenums, qfileinfo-exists, qgetenv, qlatin1string-non-ascii, qmap-with-pointer-key, qstring-allocations, qstring-arg, qstring-insensitive-allocation, qstring-left, qstring-ref, qt-macros, qvariant-template-instantiation, range-loop, reserve-candidates, returning-data-from-temporary, returning-void-expression, rule-of-three, rule-of-two-soft, strict-iterators, temporary-iterator, unused-non-trivial-variable, virtual-call-ctor, writing-to-temporary, wrong-qglobalstatic Requested checks: implicit-casts Requested checks: foreach, implicit-casts Requested checks: old-style-connect Requested checks: connect-non-signal, connect-not-normalized, container-anti-pattern, lambda-in-connect, mutable-container-key, qcolor-from-literal, qdatetime-utc, qenums, qfileinfo-exists, qgetenv, qmap-with-pointer-key, qstring-arg, qstring-insensitive-allocation, qstring-ref, qt-macros, qvariant-template-instantiation, strict-iterators, temporary-iterator, unused-non-trivial-variable, writing-to-temporary, wrong-qglobalstatic -Requested checks: auto-unexpected-qstringbuilder, child-event-qobject-cast, connect-non-signal, connect-not-normalized, container-anti-pattern, detaching-temporary, foreach, incorrect-emit, inefficient-qlist-soft, install-event-filter, lambda-in-connect, mutable-container-key, non-pod-global-static, post-event, qcolor-from-literal, qdatetime-utc, qdeleteall, qenums, qfileinfo-exists, qgetenv, qlatin1string-non-ascii, qmap-with-pointer-key, qstring-arg, qstring-insensitive-allocation, qstring-left, qstring-ref, qt-macros, qvariant-template-instantiation, range-loop, returning-data-from-temporary, rule-of-two-soft, strict-iterators, temporary-iterator, unused-non-trivial-variable, writing-to-temporary, wrong-qglobalstatic +Requested checks: auto-unexpected-qstringbuilder, child-event-qobject-cast, connect-3arg-lambda, connect-non-signal, connect-not-normalized, container-anti-pattern, detaching-temporary, foreach, incorrect-emit, inefficient-qlist-soft, install-event-filter, lambda-in-connect, mutable-container-key, non-pod-global-static, post-event, qcolor-from-literal, qdatetime-utc, qdeleteall, qenums, qfileinfo-exists, qgetenv, qlatin1string-non-ascii, qmap-with-pointer-key, qstring-arg, qstring-insensitive-allocation, qstring-left, qstring-ref, qt-macros, qvariant-template-instantiation, range-loop, returning-data-from-temporary, rule-of-two-soft, strict-iterators, temporary-iterator, unused-non-trivial-variable, writing-to-temporary, wrong-qglobalstatic Requested checks: connect-non-signal, connect-not-normalized, container-anti-pattern, lambda-in-connect, mutable-container-key, qcolor-from-literal, qdatetime-utc, qenums, qfileinfo-exists, qgetenv, qmap-with-pointer-key, qstring-arg, qstring-insensitive-allocation, qstring-ref, qt-macros, qvariant-template-instantiation, reserve-candidates, strict-iterators, temporary-iterator, unused-non-trivial-variable, writing-to-temporary, wrong-qglobalstatic Requested checks: connect-non-signal, connect-not-normalized, container-anti-pattern, lambda-in-connect, mutable-container-key, qcolor-from-literal, qdatetime-utc, qenums, qfileinfo-exists, qgetenv, qmap-with-pointer-key, qstring-arg, qstring-insensitive-allocation, qstring-ref, qt-macros, qvariant-template-instantiation, strict-iterators, temporary-iterator, unused-non-trivial-variable, writing-to-temporary, wrong-qglobalstatic Requested checks: connect-non-signal, connect-not-normalized, container-anti-pattern, foreach, implicit-casts, lambda-in-connect, mutable-container-key, qcolor-from-literal, qdatetime-utc, qenums, qfileinfo-exists, qgetenv, qmap-with-pointer-key, qstring-arg, qstring-insensitive-allocation, qstring-ref, qt-macros, qvariant-template-instantiation, strict-iterators, temporary-iterator, unused-non-trivial-variable, writing-to-temporary, wrong-qglobalstatic -Requested checks: auto-unexpected-qstringbuilder, child-event-qobject-cast, connect-non-signal, connect-not-normalized, container-anti-pattern, detaching-temporary, foreach, incorrect-emit, inefficient-qlist-soft, install-event-filter, lambda-in-connect, mutable-container-key, non-pod-global-static, post-event, qcolor-from-literal, qdatetime-utc, qdeleteall, qenums, qfileinfo-exists, qgetenv, qlatin1string-non-ascii, qmap-with-pointer-key, qstring-arg, qstring-insensitive-allocation, qstring-left, qstring-ref, qt-macros, qvariant-template-instantiation, range-loop, returning-data-from-temporary, rule-of-two-soft, strict-iterators, temporary-iterator, unused-non-trivial-variable, writing-to-temporary, wrong-qglobalstatic +Requested checks: auto-unexpected-qstringbuilder, child-event-qobject-cast, connect-3arg-lambda, connect-non-signal, connect-not-normalized, container-anti-pattern, detaching-temporary, foreach, incorrect-emit, inefficient-qlist-soft, install-event-filter, lambda-in-connect, mutable-container-key, non-pod-global-static, post-event, qcolor-from-literal, qdatetime-utc, qdeleteall, qenums, qfileinfo-exists, qgetenv, qlatin1string-non-ascii, qmap-with-pointer-key, qstring-arg, qstring-insensitive-allocation, qstring-left, qstring-ref, qt-macros, qvariant-template-instantiation, range-loop, returning-data-from-temporary, rule-of-two-soft, strict-iterators, temporary-iterator, unused-non-trivial-variable, writing-to-temporary, wrong-qglobalstatic Test9 -Requested checks: auto-unexpected-qstringbuilder, child-event-qobject-cast, connect-non-signal, connect-not-normalized, container-anti-pattern, detaching-temporary, foreach, incorrect-emit, inefficient-qlist-soft, install-event-filter, lambda-in-connect, mutable-container-key, non-pod-global-static, post-event, qcolor-from-literal, qdatetime-utc, qdeleteall, qenums, qfileinfo-exists, qgetenv, qlatin1string-non-ascii, qmap-with-pointer-key, qstring-arg, qstring-insensitive-allocation, qstring-left, qstring-ref, qt-macros, qvariant-template-instantiation, range-loop, returning-data-from-temporary, rule-of-two-soft, strict-iterators, temporary-iterator, unused-non-trivial-variable, writing-to-temporary, wrong-qglobalstatic +Requested checks: auto-unexpected-qstringbuilder, child-event-qobject-cast, connect-3arg-lambda, connect-non-signal, connect-not-normalized, container-anti-pattern, detaching-temporary, foreach, incorrect-emit, inefficient-qlist-soft, install-event-filter, lambda-in-connect, mutable-container-key, non-pod-global-static, post-event, qcolor-from-literal, qdatetime-utc, qdeleteall, qenums, qfileinfo-exists, qgetenv, qlatin1string-non-ascii, qmap-with-pointer-key, qstring-arg, qstring-insensitive-allocation, qstring-left, qstring-ref, qt-macros, qvariant-template-instantiation, range-loop, returning-data-from-temporary, rule-of-two-soft, strict-iterators, temporary-iterator, unused-non-trivial-variable, writing-to-temporary, wrong-qglobalstatic Requested checks: connect-non-signal, connect-not-normalized, container-anti-pattern, lambda-in-connect, mutable-container-key, qcolor-from-literal, qdatetime-utc, qenums, qfileinfo-exists, qgetenv, qmap-with-pointer-key, qstring-arg, qstring-insensitive-allocation, qstring-ref, qt-macros, qvariant-template-instantiation, reserve-candidates, strict-iterators, temporary-iterator, unused-non-trivial-variable, writing-to-temporary, wrong-qglobalstatic Requested checks: connect-non-signal, connect-not-normalized, container-anti-pattern, implicit-casts, lambda-in-connect, mutable-container-key, qcolor-from-literal, qdatetime-utc, qenums, qfileinfo-exists, qgetenv, qmap-with-pointer-key, qstring-arg, qstring-insensitive-allocation, qstring-ref, qt-macros, qvariant-template-instantiation, reserve-candidates, strict-iterators, temporary-iterator, unused-non-trivial-variable, writing-to-temporary, wrong-qglobalstatic Requested checks: implicit-casts @@ -53,6 +53,7 @@ Available checks and FixIts: - Checks from level1: - auto-unexpected-qstringbuilder (fix-auto-unexpected-qstringbuilder) - child-event-qobject-cast + - connect-3arg-lambda - detaching-temporary - foreach - incorrect-emit @@ -109,6 +110,6 @@ Backup your code before running them. Requested checks: connect-non-signal, connect-not-normalized, container-anti-pattern, lambda-in-connect, mutable-container-key, qcolor-from-literal, qdatetime-utc, qfileinfo-exists, qmap-with-pointer-key, qstring-arg, qstring-insensitive-allocation, qstring-ref, qt-macros, qvariant-template-instantiation, strict-iterators, temporary-iterator, unused-non-trivial-variable, writing-to-temporary, wrong-qglobalstatic Requested checks: implicit-casts Requested checks: implicit-casts -Requested checks: auto-unexpected-qstringbuilder, child-event-qobject-cast, connect-non-signal, connect-not-normalized, container-anti-pattern, detaching-temporary, foreach, incorrect-emit, inefficient-qlist-soft, install-event-filter, lambda-in-connect, mutable-container-key, non-pod-global-static, post-event, qcolor-from-literal, qdatetime-utc, qdeleteall, qenums, qfileinfo-exists, qgetenv, qlatin1string-non-ascii, qmap-with-pointer-key, qstring-arg, qstring-insensitive-allocation, qstring-left, qstring-ref, qt-macros, qvariant-template-instantiation, range-loop, returning-data-from-temporary, rule-of-two-soft, strict-iterators, temporary-iterator, unused-non-trivial-variable, writing-to-temporary, wrong-qglobalstatic +Requested checks: auto-unexpected-qstringbuilder, child-event-qobject-cast, connect-3arg-lambda, connect-non-signal, connect-not-normalized, container-anti-pattern, detaching-temporary, foreach, incorrect-emit, inefficient-qlist-soft, install-event-filter, lambda-in-connect, mutable-container-key, non-pod-global-static, post-event, qcolor-from-literal, qdatetime-utc, qdeleteall, qenums, qfileinfo-exists, qgetenv, qlatin1string-non-ascii, qmap-with-pointer-key, qstring-arg, qstring-insensitive-allocation, qstring-left, qstring-ref, qt-macros, qvariant-template-instantiation, range-loop, returning-data-from-temporary, rule-of-two-soft, strict-iterators, temporary-iterator, unused-non-trivial-variable, writing-to-temporary, wrong-qglobalstatic Requested checks: connect-non-signal, connect-not-normalized, container-anti-pattern, lambda-in-connect, mutable-container-key, qcolor-from-literal, qdatetime-utc, qfileinfo-exists, qmap-with-pointer-key, qstring-arg, qstring-insensitive-allocation, qstring-ref, qt-macros, qvariant-template-instantiation, strict-iterators, temporary-iterator, unused-non-trivial-variable, writing-to-temporary, wrong-qglobalstatic -Requested checks: auto-unexpected-qstringbuilder, child-event-qobject-cast, connect-non-signal, connect-not-normalized, container-anti-pattern, detaching-temporary, foreach, incorrect-emit, inefficient-qlist-soft, install-event-filter, lambda-in-connect, mutable-container-key, non-pod-global-static, post-event, qcolor-from-literal, qdatetime-utc, qdeleteall, qfileinfo-exists, qgetenv, qlatin1string-non-ascii, qmap-with-pointer-key, qstring-arg, qstring-insensitive-allocation, qstring-left, qstring-ref, qt-macros, qvariant-template-instantiation, range-loop, returning-data-from-temporary, rule-of-two-soft, strict-iterators, temporary-iterator, unused-non-trivial-variable, writing-to-temporary, wrong-qglobalstatic +Requested checks: auto-unexpected-qstringbuilder, child-event-qobject-cast, connect-3arg-lambda, connect-non-signal, connect-not-normalized, container-anti-pattern, detaching-temporary, foreach, incorrect-emit, inefficient-qlist-soft, install-event-filter, lambda-in-connect, mutable-container-key, non-pod-global-static, post-event, qcolor-from-literal, qdatetime-utc, qdeleteall, qfileinfo-exists, qgetenv, qlatin1string-non-ascii, qmap-with-pointer-key, qstring-arg, qstring-insensitive-allocation, qstring-left, qstring-ref, qt-macros, qvariant-template-instantiation, range-loop, returning-data-from-temporary, rule-of-two-soft, strict-iterators, temporary-iterator, unused-non-trivial-variable, writing-to-temporary, wrong-qglobalstatic diff --git a/tests/connect-3arg-lambda/config.json b/tests/connect-3arg-lambda/config.json new file mode 100644 index 00000000..e7e6e0cb --- /dev/null +++ b/tests/connect-3arg-lambda/config.json @@ -0,0 +1,7 @@ +{ + "tests" : [ + { + "filename" : "main.cpp" + } + ] +} diff --git a/tests/connect-3arg-lambda/main.cpp b/tests/connect-3arg-lambda/main.cpp new file mode 100644 index 00000000..02f4afc1 --- /dev/null +++ b/tests/connect-3arg-lambda/main.cpp @@ -0,0 +1,42 @@ +#include <QtCore/QString> +#include <QtCore/QObject> + + +void test() +{ + QObject *o1; + QObject *o2; + QObject::connect(o1, &QObject::destroyed, + [=] { o2->deleteLater(); }); // Warn + + QObject::connect(o1, &QObject::destroyed, o2, + [=] { o2->deleteLater(); }); // OK + + QObject::connect(o1, &QObject::destroyed, + [=] { o1->deleteLater(); }); // OK + + QObject::connect(o1, &QObject::destroyed, + [=] { int a; a = 1; }); // OK +} + +class MyObject : public QObject +{ +public: + void foo(); + + void test() + { + MyObject *o2; + connect(o2, &QObject::destroyed, [this] { // Warn + foo(); + }); + + connect(this, &QObject::destroyed, [this] { // OK + foo(); + }); + + connect(this, &QObject::destroyed, [o2] { // Warn + o2->foo(); + }); + } +}; diff --git a/tests/connect-3arg-lambda/main.cpp.expected b/tests/connect-3arg-lambda/main.cpp.expected new file mode 100644 index 00000000..ca688fca --- /dev/null +++ b/tests/connect-3arg-lambda/main.cpp.expected @@ -0,0 +1,3 @@ +connect-3arg-lambda/main.cpp:9:5: warning: Pass a context object as 3rd connect parameter [-Wclazy-connect-3arg-lambda] +connect-3arg-lambda/main.cpp:30:9: warning: Pass 'this' as the 3rd connect parameter [-Wclazy-connect-3arg-lambda] +connect-3arg-lambda/main.cpp:38:9: warning: Pass a context object as 3rd connect parameter [-Wclazy-connect-3arg-lambda] |