44 const std::string BadDecl =
"badDecl";
46 varDecl(unless(hasInitializer(anything())), unless(isInstantiated()),
47 isLocalVarDecl(), unless(isStaticLocal()), isDefinition(),
48 unless(hasParent(cxxCatchStmt())),
49 unless(hasParent(declStmt(hasParent(isObjCForCollectionStmt())))),
50 optionally(hasParent(declStmt(hasParent(
51 cxxForRangeStmt(hasLoopVariable(varDecl().bind(BadDecl))))))),
52 unless(equalsBoundNode(BadDecl)))
64 const auto *MatchedDecl = Result.Nodes.getNodeAs<VarDecl>(
"vardecl");
65 const ASTContext &Context = *Result.Context;
66 const SourceManager &Source = Context.getSourceManager();
69 if (MatchedDecl->isInvalidDecl())
87 if (MatchedDecl->getEndLoc().isMacroID())
90 const QualType TypePtr = MatchedDecl->getType();
91 std::optional<const char *> InitializationString;
92 bool AddMathInclude =
false;
94 if (TypePtr->isEnumeralType()) {
95 InitializationString =
nullptr;
96 }
else if (TypePtr->isBooleanType()) {
97 InitializationString =
" = false";
98 }
else if (TypePtr->isIntegerType()) {
99 InitializationString =
" = 0";
100 }
else if (TypePtr->isFloatingType()) {
101 InitializationString =
" = NAN";
102 AddMathInclude =
true;
103 }
else if (TypePtr->isAnyPointerType() || TypePtr->isMemberPointerType()) {
104 if (getLangOpts().CPlusPlus11)
105 InitializationString =
" = nullptr";
107 InitializationString =
" = NULL";
110 if (InitializationString) {
112 diag(MatchedDecl->getLocation(),
"variable %0 is not initialized")
114 if (*InitializationString !=
nullptr)
115 Diagnostic << FixItHint::CreateInsertion(
117 *Result.SourceManager,
118 Result.Context->getLangOpts()),
119 *InitializationString);
120 if (AddMathInclude) {
121 Diagnostic << IncludeInserter.createIncludeInsertion(
122 Source.getFileID(MatchedDecl->getBeginLoc()), MathHeader);
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.