Debian Codegraph @

src/checks/level2/returning-void-expression.cpp

raw
1 /*
2 SPDX-FileCopyrightText: 2016 Sergio Martins <[email protected]>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6
7 #include "returning-void-expression.h"
8 #include "ClazyContext.h"
9 #include "ContextUtils.h"
10 #include "clazy_stl.h"
11
12 #include <clang/AST/Decl.h>
13 #include <clang/AST/Expr.h>
14 #include <clang/AST/Stmt.h>
15 #include <clang/AST/Type.h>
16 #include <clang/Basic/LLVM.h>
17 #include <llvm/Support/Casting.h>
18
19 using namespace clang;
20
21 void ReturningVoidExpression::VisitStmt(clang::Stmt *stmt)
22 {
23 auto *ret = dyn_cast<ReturnStmt>(stmt);
24 if (!ret || !clazy::hasChildren(ret)) {
25 return;
26 }
27
28 QualType qt = ret->getRetValue()->getType();
29 if (qt.isNull() || !qt->isVoidType()) {
30 return;
31 }
32
33 DeclContext *context = clazy::contextForDecl(m_context->lastDecl);
34 if (!context) {
35 return;
36 }
37
38 auto *func = dyn_cast<FunctionDecl>(context);
39 // A function template returning T won't bailout in the void check above, do it properly now:
40 if (!func || !func->getReturnType()->isVoidType()) {
41 return;
42 }
43
44 emitWarning(stmt, "Returning a void expression");
45 }