diff options
author | SĂ©rgio Martins <sergio.martins@kdab.com> | 2019-06-03 16:37:21 +0100 |
---|---|---|
committer | Sergio Martins <sergio.martins@kdab.com> | 2019-06-03 17:14:32 +0100 |
commit | 7763c1d33ea68a02b3db44feb2cd2b5bdb154bf0 (patch) | |
tree | 52a10e3ba8cba466369324feaeab96e64d2ced8f /src | |
parent | 4918d8efba9b20b273a15ab2dd215a768ff884f9 (diff) |
Introduce signal-with-return-value
It's a design smell to have signals returning values
Diffstat (limited to 'src')
-rw-r--r-- | src/Checks.h | 2 | ||||
-rw-r--r-- | src/checks/manuallevel/signal-with-return-value.cpp | 61 | ||||
-rw-r--r-- | src/checks/manuallevel/signal-with-return-value.h | 40 |
3 files changed, 103 insertions, 0 deletions
diff --git a/src/Checks.h b/src/Checks.h index 00cb6b9c..a2df0180 100644 --- a/src/Checks.h +++ b/src/Checks.h @@ -42,6 +42,7 @@ #include "checks/manuallevel/qvariant-template-instantiation.h" #include "checks/manuallevel/raw-environment-function.h" #include "checks/manuallevel/reserve-candidates.h" +#include "checks/manuallevel/signal-with-return-value.h" #include "checks/manuallevel/thread-with-slots.h" #include "checks/manuallevel/tr-non-literal.h" #include "checks/manuallevel/unneeded-cast.h" @@ -135,6 +136,7 @@ void CheckManager::registerChecks() registerCheck(check<QVariantTemplateInstantiation>("qvariant-template-instantiation", ManualCheckLevel, RegisteredCheck::Option_VisitsStmts)); registerCheck(check<RawEnvironmentFunction>("raw-environment-function", ManualCheckLevel, RegisteredCheck::Option_VisitsStmts)); registerCheck(check<ReserveCandidates>("reserve-candidates", ManualCheckLevel, RegisteredCheck::Option_VisitsStmts)); + registerCheck(check<SignalWithReturnValue>("signal-with-return-value", ManualCheckLevel, RegisteredCheck::Option_VisitsDecls)); registerCheck(check<ThreadWithSlots>("thread-with-slots", ManualCheckLevel, RegisteredCheck::Option_VisitsStmts | RegisteredCheck::Option_VisitsDecls)); registerCheck(check<TrNonLiteral>("tr-non-literal", ManualCheckLevel, RegisteredCheck::Option_VisitsStmts)); registerCheck(check<UnneededCast>("unneeded-cast", ManualCheckLevel, RegisteredCheck::Option_VisitsStmts)); diff --git a/src/checks/manuallevel/signal-with-return-value.cpp b/src/checks/manuallevel/signal-with-return-value.cpp new file mode 100644 index 00000000..25884374 --- /dev/null +++ b/src/checks/manuallevel/signal-with-return-value.cpp @@ -0,0 +1,61 @@ +/* + This file is part of the clazy static checker. + + Copyright (C) 2019 Sergio Martins <smartins@kde.org> + + 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 "signal-with-return-value.h" +#include "Utils.h" +#include "HierarchyUtils.h" +#include "QtUtils.h" +#include "TypeUtils.h" +#include "AccessSpecifierManager.h" +#include "ClazyContext.h" + +#include <clang/AST/AST.h> + +using namespace clang; +using namespace std; + + +SignalWithReturnValue::SignalWithReturnValue(const std::string &name, ClazyContext *context) + : CheckBase(name, context) +{ + context->enableAccessSpecifierManager(); +} + +void SignalWithReturnValue::VisitDecl(clang::Decl *decl) +{ + AccessSpecifierManager *accessSpecifierManager = m_context->accessSpecifierManager; + auto method = dyn_cast<CXXMethodDecl>(decl); + if (!accessSpecifierManager || !method) + return; + + if (method->isThisDeclarationADefinition() && !method->hasInlineBody()) + return; + + const bool methodIsSignal = accessSpecifierManager->qtAccessSpecifierType(method) == QtAccessSpecifier_Signal; + if (!methodIsSignal) + return; + + if (!method->getReturnType()->isVoidType()) { + if (!accessSpecifierManager->isScriptable(method)) { + emitWarning(decl, std::string(clazy::name(method)) + "() should return void. For a clean design signals shouldn't assume a single slot are connected to them."); + } + } +} diff --git a/src/checks/manuallevel/signal-with-return-value.h b/src/checks/manuallevel/signal-with-return-value.h new file mode 100644 index 00000000..b94685e1 --- /dev/null +++ b/src/checks/manuallevel/signal-with-return-value.h @@ -0,0 +1,40 @@ +/* + This file is part of the clazy static checker. + + Copyright (C) 2019 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com + Author: 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_SIGNAL_WITH_RETURN_VALUE_H +#define CLAZY_SIGNAL_WITH_RETURN_VALUE_H + +#include "checkbase.h" + + +/** + * See README-signal-with-return-value.md for more info. + */ +class SignalWithReturnValue : public CheckBase +{ +public: + explicit SignalWithReturnValue(const std::string &name, ClazyContext *context); + void VisitDecl(clang::Decl *) override; +private: +}; + +#endif |