Skip to content

Commit

Permalink
[clang-tidy][NFC] Fix cppcoreguidelines-init-variables findings
Browse files Browse the repository at this point in the history
Fix issues found by clang-tidy in clang-tidy source directory.
  • Loading branch information
PiotrZSL committed Aug 27, 2023
1 parent 9f82209 commit cbdc3e1
Show file tree
Hide file tree
Showing 35 changed files with 59 additions and 63 deletions.
2 changes: 1 addition & 1 deletion clang-tools-extra/clang-tidy/ClangTidy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class ErrorReporter {
for (const FileByteRange &FBR : Error.Message.Ranges)
Diag << getRange(FBR);
// FIXME: explore options to support interactive fix selection.
const llvm::StringMap<Replacements> *ChosenFix;
const llvm::StringMap<Replacements> *ChosenFix = nullptr;
if (ApplyFixes != FB_NoFix &&
(ChosenFix = getFixIt(Error, ApplyFixes == FB_FixNotes))) {
for (const auto &FileAndReplacements : *ChosenFix) {
Expand Down
2 changes: 1 addition & 1 deletion clang-tools-extra/clang-tidy/ClangTidyCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ static std::optional<bool> getAsBool(StringRef Value,
return Parsed;
// To maintain backwards compatability, we support parsing numbers as
// booleans, even though its not supported in YAML.
long long Number;
long long Number = 0;
if (!Value.getAsInteger(10, Number))
return Number != 0;
return std::nullopt;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ void DurationFactoryScaleCheck::check(const MatchFinder::MatchResult &Result) {
return;

DurationScale Scale = *MaybeScale;
const Expr *Remainder;
const Expr *Remainder = nullptr;
std::optional<DurationScale> NewScale;

// We next handle the cases of multiplication and division.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ StrCatCheckResult processCall(const CallExpr *RootCall, bool IsAppend,
} // namespace

void RedundantStrcatCallsCheck::check(const MatchFinder::MatchResult& Result) {
bool IsAppend;
bool IsAppend = false;

const CallExpr* RootCall;
const CallExpr *RootCall = nullptr;
if ((RootCall = Result.Nodes.getNodeAs<CallExpr>("StrCat")))
IsAppend = false;
else if ((RootCall = Result.Nodes.getNodeAs<CallExpr>("StrAppend")))
Expand Down
2 changes: 1 addition & 1 deletion clang-tools-extra/clang-tidy/altera/UnrollLoopsCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ bool UnrollLoopsCheck::hasLargeNumIterations(const Stmt *Statement,
// If increment is binary and not one of +, -, *, /, we can't know the loop
// bounds.
if (const auto *Op = dyn_cast<BinaryOperator>(Increment)) {
int ConstantValue;
int ConstantValue = 0;
if (!extractValue(ConstantValue, Op, Context))
return true;
switch (Op->getOpcode()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ void ImplicitWideningOfMultiplicationResultCheck::handlePointerOffsetting(

// We are looking for a pointer offset operation,
// with one hand being a pointer, and another one being an offset.
const Expr *PointerExpr, *IndexExpr;
const Expr *PointerExpr = nullptr, *IndexExpr = nullptr;
if (const auto *BO = dyn_cast<BinaryOperator>(E)) {
PointerExpr = BO->getLHS();
IndexExpr = BO->getRHS();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ static int relativeCharSizesW(BuiltinType::Kind Kind) {
}

static bool isFirstWider(BuiltinType::Kind First, BuiltinType::Kind Second) {
int FirstSize, SecondSize;
int FirstSize = 0, SecondSize = 0;
if ((FirstSize = relativeIntSizes(First)) != 0 &&
(SecondSize = relativeIntSizes(Second)) != 0)
return FirstSize > SecondSize;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ bool isExprValueStored(const Expr *E, ASTContext &C) {
DynTypedNodeList P = PMap.getParents(*E);
if (P.size() != 1)
return false;
const Expr *ParentE;
const Expr *ParentE = nullptr;
while ((ParentE = P[0].get<Expr>()) && ParentE->IgnoreParenCasts() == E) {
P = PMap.getParents(P[0]);
if (P.size() != 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ void RedundantBranchConditionCheck::check(const MatchFinder::MatchResult &Result
const auto *CondVar = Result.Nodes.getNodeAs<VarDecl>(CondVarStr);
const auto *Func = Result.Nodes.getNodeAs<FunctionDecl>(FuncStr);

const DeclRefExpr *OuterIfVar, *InnerIfVar;
const DeclRefExpr *OuterIfVar = nullptr, *InnerIfVar = nullptr;
if (const auto *Inner = Result.Nodes.getNodeAs<DeclRefExpr>(InnerIfVar1Str))
InnerIfVar = Inner;
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ bool SignalHandlerCheck::checkFunctionCPP14(
return true;
}

const FunctionDecl *FBody;
const FunctionDecl *FBody = nullptr;
const Stmt *BodyS = FD->getBody(FBody);
if (!BodyS)
return false;
Expand Down
2 changes: 1 addition & 1 deletion clang-tools-extra/clang-tidy/cert/StrToNumCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ StringRef classifyReplacement(ConversionKind K) {
void StrToNumCheck::check(const MatchFinder::MatchResult &Result) {
const auto *Call = Result.Nodes.getNodeAs<CallExpr>("expr");
const FunctionDecl *FuncDecl = nullptr;
ConversionKind Conversion;
ConversionKind Conversion = ConversionKind::None;

if (const auto *ConverterFunc =
Result.Nodes.getNodeAs<FunctionDecl>("converter")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ void NarrowingConversionsCheck::handleFloatingCast(const ASTContext &Context,
// not within destination range. We convert the value to the destination
// type and check if the resulting value is infinity.
llvm::APFloat Tmp = Constant.getFloat();
bool UnusedLosesInfo;
bool UnusedLosesInfo = false;
Tmp.convert(Context.getFloatTypeSemantics(ToType->desugar()),
llvm::APFloatBase::rmNearestTiesToEven, &UnusedLosesInfo);
if (Tmp.isInfinity())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@ void PreferMemberInitializerCheck::check(
return;
}

const FieldDecl *Field;
const Expr *InitValue;
const FieldDecl *Field = nullptr;
const Expr *InitValue = nullptr;
std::tie(Field, InitValue) = isAssignmentToMemberOf(Class, S, Ctor);
if (Field) {
if (IsUseDefaultMemberInitEnabled && getLangOpts().CPlusPlus11 &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ bool MultipleInheritanceCheck::isInterface(const CXXRecordDecl *Node) {
return false;

// Short circuit the lookup if we have analyzed this record before.
bool PreviousIsInterfaceResult;
bool PreviousIsInterfaceResult = false;
if (getInterfaceStatus(Node, PreviousIsInterfaceResult))
return PreviousIsInterfaceResult;

Expand Down
4 changes: 2 additions & 2 deletions clang-tools-extra/clang-tidy/google/IntegerTypesCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ void IntegerTypesCheck::check(const MatchFinder::MatchResult &Result) {
tok::kw_signed))
return;

bool IsSigned;
unsigned Width;
bool IsSigned = false;
unsigned Width = 0;
const TargetInfo &TargetInfo = Result.Context->getTargetInfo();

// Look for uses of short, long, long long and their unsigned versions.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,8 @@ void UpgradeGoogletestCaseCheck::check(const MatchFinder::MatchResult &Result) {
if (const auto *Method = Result.Nodes.getNodeAs<CXXMethodDecl>("method")) {
ReplacementText = getNewMethodName(Method->getName());

bool IsInInstantiation;
bool IsInTemplate;
bool IsInInstantiation = false;
bool IsInTemplate = false;
bool AddFix = true;
if (const auto *Call = Result.Nodes.getNodeAs<CXXMemberCallExpr>("call")) {
const auto *Callee = llvm::cast<MemberExpr>(Call->getCallee());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ void MultiwayPathsCoveredCheck::check(const MatchFinder::MatchResult &Result) {
return;
}
const auto *Switch = Result.Nodes.getNodeAs<SwitchStmt>("switch");
std::size_t SwitchCaseCount;
bool SwitchHasDefault;
std::size_t SwitchCaseCount = 0;
bool SwitchHasDefault = false;
std::tie(SwitchCaseCount, SwitchHasDefault) = countCaseLabels(Switch);

// Checks the sanity of 'switch' statements that actually do define
Expand Down
2 changes: 1 addition & 1 deletion clang-tools-extra/clang-tidy/llvm/IncludeOrderCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ void IncludeOrderPPCallbacks::EndOfMainFile() {
// block.
for (unsigned BI = 0, BE = Blocks.size() - 1; BI != BE; ++BI) {
// Find the first include that's not in the right position.
unsigned I, E;
unsigned I = 0, E = 0;
for (I = Blocks[BI], E = Blocks[BI + 1]; I != E; ++I)
if (IncludeIndices[I] != I)
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ static llvm::SmallString<64U> skeleton(StringRef Name) {
while (Curr < End) {

const char *Prev = Curr;
UTF32 CodePoint;
UTF32 CodePoint = 0;
ConversionResult Result = convertUTF8Sequence(
reinterpret_cast<const UTF8 **>(&Curr),
reinterpret_cast<const UTF8 *>(End), &CodePoint, strictConversion);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ int main(int argc, char *argv[]) {
}

llvm::StringRef From = Values[0].trim();
llvm::UTF32 CodePoint;
llvm::UTF32 CodePoint = 0;
From.getAsInteger(16, CodePoint);

SmallVector<llvm::UTF32> To;
SmallVector<StringRef> ToN;
Values[1].split(ToN, ' ', -1, false);
for (StringRef To_ : ToN) {
llvm::UTF32 ToCodePoint;
llvm::UTF32 ToCodePoint = 0;
To_.trim().getAsInteger(16, ToCodePoint);
To.push_back(ToCodePoint);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ static bool containsMisleadingBidi(StringRef Buffer,
BidiContexts.clear();
continue;
}
llvm::UTF32 CodePoint;
llvm::UTF32 CodePoint = 0;
llvm::ConversionResult Result = llvm::convertUTF8Sequence(
(const llvm::UTF8 **)&CurPtr, (const llvm::UTF8 *)Buffer.end(),
&CodePoint, llvm::strictConversion);
Expand Down
2 changes: 1 addition & 1 deletion clang-tools-extra/clang-tidy/misc/MisleadingIdentifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ static bool hasRTLCharacters(StringRef Buffer) {
const char *CurPtr = Buffer.begin();
const char *EndPtr = Buffer.end();
while (CurPtr < EndPtr) {
llvm::UTF32 CodePoint;
llvm::UTF32 CodePoint = 0;
llvm::ConversionResult Result = llvm::convertUTF8Sequence(
(const llvm::UTF8 **)&CurPtr, (const llvm::UTF8 *)EndPtr, &CodePoint,
llvm::strictConversion);
Expand Down
2 changes: 1 addition & 1 deletion clang-tools-extra/clang-tidy/misc/MisplacedConstCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void MisplacedConstCheck::check(const MatchFinder::MatchResult &Result) {
QualType CanQT = Var->getType().getCanonicalType();

SourceLocation AliasLoc;
const char *AliasType;
const char *AliasType = nullptr;
if (const auto *Typedef = Result.Nodes.getNodeAs<TypedefDecl>("typedef")) {
AliasLoc = Typedef->getLocation();
AliasType = "typedef";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace clang::tidy::misc {
namespace {

AST_MATCHER(FunctionDecl, isPlacementOverload) {
bool New;
bool New = false;
switch (Node.getOverloadedOperator()) {
default:
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1027,7 +1027,7 @@ void RedundantExpressionCheck::checkArithmeticExpr(
const MatchFinder::MatchResult &Result) {
APSInt LhsValue, RhsValue;
const Expr *LhsSymbol = nullptr, *RhsSymbol = nullptr;
BinaryOperatorKind LhsOpcode, RhsOpcode;
BinaryOperatorKind LhsOpcode{}, RhsOpcode{};

if (const auto *ComparisonOperator = Result.Nodes.getNodeAs<BinaryOperator>(
"binop-const-compare-to-sym")) {
Expand Down Expand Up @@ -1104,7 +1104,7 @@ void RedundantExpressionCheck::checkBitwiseExpr(

APSInt LhsValue, RhsValue;
const Expr *LhsSymbol = nullptr;
BinaryOperatorKind LhsOpcode;
BinaryOperatorKind LhsOpcode{};
if (!retrieveBinOpIntegerConstantExpr(Result, "lhs", LhsOpcode, LhsSymbol,
LhsValue) ||
!retrieveIntegerConstantExpr(Result, "rhs", RhsValue))
Expand Down Expand Up @@ -1180,7 +1180,7 @@ void RedundantExpressionCheck::checkRelationalExpr(
const Expr *LhsExpr = nullptr, *RhsExpr = nullptr;
const Expr *LhsSymbol = nullptr, *RhsSymbol = nullptr;
const Expr *LhsConst = nullptr, *RhsConst = nullptr;
BinaryOperatorKind LhsOpcode, RhsOpcode;
BinaryOperatorKind LhsOpcode{}, RhsOpcode{};
APSInt LhsValue, RhsValue;

if (!retrieveRelationalIntegerConstantExpr(
Expand Down Expand Up @@ -1240,7 +1240,7 @@ void RedundantExpressionCheck::check(const MatchFinder::MatchResult &Result) {
// intentional.
if (areSidesBinaryConstExpressions(BinOp, Result.Context)) {
const Expr *LhsConst = nullptr, *RhsConst = nullptr;
BinaryOperatorKind MainOpcode, SideOpcode;
BinaryOperatorKind MainOpcode{}, SideOpcode{};

if (!retrieveConstExprFromBothSides(BinOp, MainOpcode, SideOpcode,
LhsConst, RhsConst, Result.Context))
Expand Down
4 changes: 2 additions & 2 deletions clang-tools-extra/clang-tidy/modernize/LoopConvertCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -962,8 +962,8 @@ void LoopConvertCheck::check(const MatchFinder::MatchResult &Result) {
Confidence ConfidenceLevel(Confidence::CL_Safe);
ASTContext *Context = Result.Context;

const ForStmt *Loop;
LoopFixerKind FixerKind;
const ForStmt *Loop = nullptr;
LoopFixerKind FixerKind{};
RangeDescriptor Descriptor;

if ((Loop = Nodes.getNodeAs<ForStmt>(LoopNameArray))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ void UseEqualsDefaultCheck::check(const MatchFinder::MatchResult &Result) {
bodyEmpty(Result.Context, Body);

std::vector<FixItHint> RemoveInitializers;
unsigned MemberType;
unsigned MemberType = 0;
if (const auto *Ctor = dyn_cast<CXXConstructorDecl>(SpecialFunctionDecl)) {
if (Ctor->getNumParams() == 0) {
MemberType = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ void recordFixes(const VarDecl &Var, ASTContext &Context,

std::optional<SourceLocation> firstLocAfterNewLine(SourceLocation Loc,
SourceManager &SM) {
bool Invalid;
bool Invalid = false;
const char *TextAfter = SM.getCharacterData(Loc, &Invalid);
if (Invalid) {
return std::nullopt;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ struct CognitiveComplexity final {
std::pair<unsigned, unsigned short> process() const {
assert(C != Criteria::None && "invalid criteria");

unsigned MsgId; // The id of the message to output.
unsigned short Increment; // How much of an increment?
unsigned MsgId = 0; // The id of the message to output.
unsigned short Increment = 0; // How much of an increment?

if (C == Criteria::All) {
Increment = 1 + Nesting;
Expand Down Expand Up @@ -196,8 +196,8 @@ void CognitiveComplexity::account(SourceLocation Loc, unsigned short Nesting,
Details.emplace_back(Loc, Nesting, C);
const Detail &D = Details.back();

unsigned MsgId;
unsigned short Increase;
unsigned MsgId = 0;
unsigned short Increase = 0;
std::tie(MsgId, Increase) = D.process();

Total += Increase;
Expand Down Expand Up @@ -242,9 +242,8 @@ class FunctionASTVisitor final
return Base::TraverseIfStmt(Node);

{
CognitiveComplexity::Criteria Reasons;

Reasons = CognitiveComplexity::Criteria::None;
CognitiveComplexity::Criteria Reasons =
CognitiveComplexity::Criteria::None;

// "If" increases cognitive complexity.
Reasons |= CognitiveComplexity::Criteria::Increment;
Expand Down Expand Up @@ -290,9 +289,8 @@ class FunctionASTVisitor final
return TraverseIfStmt(E, true);

{
CognitiveComplexity::Criteria Reasons;

Reasons = CognitiveComplexity::Criteria::None;
CognitiveComplexity::Criteria Reasons =
CognitiveComplexity::Criteria::None;

// "Else" increases cognitive complexity.
Reasons |= CognitiveComplexity::Criteria::Increment;
Expand Down Expand Up @@ -549,8 +547,8 @@ void FunctionCognitiveComplexityCheck::check(

// Output all the basic increments of complexity.
for (const auto &Detail : Visitor.CC.Details) {
unsigned MsgId; // The id of the message to output.
unsigned short Increase; // How much of an increment?
unsigned MsgId = 0; // The id of the message to output.
unsigned short Increase = 0; // How much of an increment?
std::tie(MsgId, Increase) = Detail.process();
assert(MsgId < Msgs.size() && "MsgId should always be valid");
// Increase, on the other hand, can be 0.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ MagicNumbersCheck::MagicNumbersCheck(StringRef Name, ClangTidyContext *Context)
IgnoredIntegerValues.resize(IgnoredIntegerValuesInput.size());
llvm::transform(IgnoredIntegerValuesInput, IgnoredIntegerValues.begin(),
[](StringRef Value) {
int64_t Res;
int64_t Res = 0;
Value.getAsInteger(10, Res);
return Res;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -613,8 +613,8 @@ void SimplifyBooleanExprCheck::reportBinOp(const ASTContext &Context,
const auto *LHS = Op->getLHS()->IgnoreParenImpCasts();
const auto *RHS = Op->getRHS()->IgnoreParenImpCasts();

const CXXBoolLiteralExpr *Bool;
const Expr *Other;
const CXXBoolLiteralExpr *Bool = nullptr;
const Expr *Other = nullptr;
if ((Bool = dyn_cast<CXXBoolLiteralExpr>(LHS)) != nullptr)
Other = RHS;
else if ((Bool = dyn_cast<CXXBoolLiteralExpr>(RHS)) != nullptr)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ static bool applyJaroWinklerHeuristic(StringRef Arg, StringRef Param,
std::ptrdiff_t L = 0;
for (std::ptrdiff_t I = 0; I < ParamLen; ++I) {
if (ParamFlags[I] == 1) {
std::ptrdiff_t J;
std::ptrdiff_t J = 0;
for (J = L; J < ArgLen; ++J)
if (ArgFlags[J] == 1) {
L = J + 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ shouldReplaceLiteralSuffix(const Expr &Literal,
// Else keep the naive literal location!

// Get the whole literal from the source buffer.
bool Invalid;
bool Invalid = false;
const StringRef LiteralSourceText = Lexer::getSourceText(
CharSourceRange::getTokenRange(*Range), SM, LO, &Invalid);
assert(!Invalid && "Failed to retrieve the source text.");
Expand Down

0 comments on commit cbdc3e1

Please sign in to comment.