Skip to content

Commit 277fb3c

Browse files
vbvictorvinay-deshmukh
authored andcommitted
[clang-tidy][NFC] Fix misc-const-correctness warnings (13/N) (llvm#167130)
1 parent b6b45bc commit 277fb3c

19 files changed

+135
-127
lines changed

clang-tools-extra/clang-tidy/bugprone/MisplacedWideningCastCheck.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ static unsigned getMaxCalculationWidth(const ASTContext &Context,
5252
E = E->IgnoreParenImpCasts();
5353

5454
if (const auto *Bop = dyn_cast<BinaryOperator>(E)) {
55-
unsigned LHSWidth = getMaxCalculationWidth(Context, Bop->getLHS());
56-
unsigned RHSWidth = getMaxCalculationWidth(Context, Bop->getRHS());
55+
const unsigned LHSWidth = getMaxCalculationWidth(Context, Bop->getLHS());
56+
const unsigned RHSWidth = getMaxCalculationWidth(Context, Bop->getRHS());
5757
if (Bop->getOpcode() == BO_Mul)
5858
return LHSWidth + RHSWidth;
5959
if (Bop->getOpcode() == BO_Add)
@@ -79,7 +79,7 @@ static unsigned getMaxCalculationWidth(const ASTContext &Context,
7979
if (Uop->getOpcode() == UO_Not)
8080
return 1024U;
8181

82-
QualType T = Uop->getType();
82+
const QualType T = Uop->getType();
8383
return T->isIntegerType() ? Context.getIntWidth(T) : 1024U;
8484
} else if (const auto *I = dyn_cast<IntegerLiteral>(E)) {
8585
return I->getValue().getActiveBits();
@@ -190,10 +190,10 @@ void MisplacedWideningCastCheck::check(const MatchFinder::MatchResult &Result) {
190190
Calc->isTypeDependent() || Calc->isValueDependent())
191191
return;
192192

193-
ASTContext &Context = *Result.Context;
193+
const ASTContext &Context = *Result.Context;
194194

195-
QualType CastType = Cast->getType();
196-
QualType CalcType = Calc->getType();
195+
const QualType CastType = Cast->getType();
196+
const QualType CalcType = Calc->getType();
197197

198198
// Explicit truncation using cast.
199199
if (Context.getIntWidth(CastType) < Context.getIntWidth(CalcType))

clang-tools-extra/clang-tidy/bugprone/MoveForwardingReferenceCheck.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ static void replaceMoveWithForward(const UnresolvedLookupExpr *Callee,
2121
const SourceManager &SM = Context.getSourceManager();
2222
const LangOptions &LangOpts = Context.getLangOpts();
2323

24-
CharSourceRange CallRange =
24+
const CharSourceRange CallRange =
2525
Lexer::makeFileCharRange(CharSourceRange::getTokenRange(
2626
Callee->getBeginLoc(), Callee->getEndLoc()),
2727
SM, LangOpts);
@@ -39,7 +39,7 @@ static void replaceMoveWithForward(const UnresolvedLookupExpr *Callee,
3939
// std::move(). This will hopefully prevent erroneous replacements if the
4040
// code does unusual things (e.g. create an alias for std::move() in
4141
// another namespace).
42-
NestedNameSpecifier NNS = Callee->getQualifier();
42+
const NestedNameSpecifier NNS = Callee->getQualifier();
4343
switch (NNS.getKind()) {
4444
case NestedNameSpecifier::Kind::Null:
4545
// Called as "move" (i.e. presumably the code had a "using std::move;").

clang-tools-extra/clang-tidy/bugprone/MultiLevelImplicitPointerConversionCheck.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,9 @@ MultiLevelImplicitPointerConversionCheck::getCheckTraversalKind() const {
8686
void MultiLevelImplicitPointerConversionCheck::check(
8787
const MatchFinder::MatchResult &Result) {
8888
const auto *MatchedExpr = Result.Nodes.getNodeAs<ImplicitCastExpr>("expr");
89-
QualType Target = MatchedExpr->getType().getDesugaredType(*Result.Context);
90-
QualType Source =
89+
const QualType Target =
90+
MatchedExpr->getType().getDesugaredType(*Result.Context);
91+
const QualType Source =
9192
MatchedExpr->getSubExpr()->getType().getDesugaredType(*Result.Context);
9293

9394
diag(MatchedExpr->getExprLoc(), "multilevel pointer conversion from %0 to "

clang-tools-extra/clang-tidy/bugprone/MultipleNewInOneExpressionCheck.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ namespace {
5151

5252
AST_MATCHER_P(CXXTryStmt, hasHandlerFor,
5353
ast_matchers::internal::Matcher<QualType>, InnerMatcher) {
54-
for (unsigned NH = Node.getNumHandlers(), I = 0; I < NH; ++I) {
54+
const unsigned NH = Node.getNumHandlers();
55+
for (unsigned I = 0; I < NH; ++I) {
5556
const CXXCatchStmt *CatchS = Node.getHandler(I);
5657
// Check for generic catch handler (match anything).
5758
if (CatchS->getCaughtType().isNull())
@@ -66,7 +67,7 @@ AST_MATCHER_P(CXXTryStmt, hasHandlerFor,
6667
}
6768

6869
AST_MATCHER(CXXNewExpr, mayThrow) {
69-
FunctionDecl *OperatorNew = Node.getOperatorNew();
70+
const FunctionDecl *OperatorNew = Node.getOperatorNew();
7071
if (!OperatorNew)
7172
return false;
7273
return !OperatorNew->getType()->castAs<FunctionProtoType>()->isNothrow();

clang-tools-extra/clang-tidy/bugprone/NarrowingConversionsCheck.cpp

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ AST_MATCHER_P(QualType, hasAnyType, std::vector<StringRef>, Names) {
2929
if (Names.empty())
3030
return false;
3131

32-
std::string Name = Node.getLocalUnqualifiedType().getAsString();
32+
const std::string Name = Node.getLocalUnqualifiedType().getAsString();
3333
return llvm::is_contained(Names, Name);
3434
}
3535

3636
AST_MATCHER(FieldDecl, hasIntBitwidth) {
3737
assert(Node.isBitField());
3838
const ASTContext &Ctx = Node.getASTContext();
39-
unsigned IntBitWidth = Ctx.getIntWidth(Ctx.IntTy);
40-
unsigned CurrentBitWidth = Node.getBitWidthValue();
39+
const unsigned IntBitWidth = Ctx.getIntWidth(Ctx.IntTy);
40+
const unsigned CurrentBitWidth = Node.getBitWidthValue();
4141
return IntBitWidth == CurrentBitWidth;
4242
}
4343

@@ -79,7 +79,7 @@ void NarrowingConversionsCheck::registerMatchers(MatchFinder *Finder) {
7979
const auto IsCeilFloorCallExpr = expr(callExpr(callee(functionDecl(
8080
hasAnyName("::ceil", "::std::ceil", "::floor", "::std::floor")))));
8181

82-
std::vector<StringRef> IgnoreConversionFromTypesVec =
82+
const std::vector<StringRef> IgnoreConversionFromTypesVec =
8383
utils::options::parseStringList(IgnoreConversionFromTypes);
8484

8585
// We may want to exclude other types from the checks, such as `size_type`
@@ -243,7 +243,7 @@ struct IntegerRange {
243243
static IntegerRange createFromType(const ASTContext &Context,
244244
const BuiltinType &T) {
245245
if (T.isFloatingPoint()) {
246-
unsigned PrecisionBits = llvm::APFloatBase::semanticsPrecision(
246+
const unsigned PrecisionBits = llvm::APFloatBase::semanticsPrecision(
247247
Context.getFloatTypeSemantics(T.desugar()));
248248
// Contrary to two's complement integer, floating point values are
249249
// symmetric and have the same number of positive and negative values.
@@ -262,24 +262,24 @@ static IntegerRange createFromType(const ASTContext &Context,
262262
return {LowerValue, UpperValue};
263263
}
264264
assert(T.isInteger() && "Unexpected builtin type");
265-
uint64_t TypeSize = Context.getTypeSize(&T);
266-
bool IsUnsignedInteger = T.isUnsignedInteger();
265+
const uint64_t TypeSize = Context.getTypeSize(&T);
266+
const bool IsUnsignedInteger = T.isUnsignedInteger();
267267
return {llvm::APSInt::getMinValue(TypeSize, IsUnsignedInteger),
268268
llvm::APSInt::getMaxValue(TypeSize, IsUnsignedInteger)};
269269
}
270270

271271
static bool isWideEnoughToHold(const ASTContext &Context,
272272
const BuiltinType &FromType,
273273
const BuiltinType &ToType) {
274-
IntegerRange FromIntegerRange = createFromType(Context, FromType);
275-
IntegerRange ToIntegerRange = createFromType(Context, ToType);
274+
const IntegerRange FromIntegerRange = createFromType(Context, FromType);
275+
const IntegerRange ToIntegerRange = createFromType(Context, ToType);
276276
return ToIntegerRange.contains(FromIntegerRange);
277277
}
278278

279279
static bool isWideEnoughToHold(const ASTContext &Context,
280280
const llvm::APSInt &IntegerConstant,
281281
const BuiltinType &ToType) {
282-
IntegerRange ToIntegerRange = createFromType(Context, ToType);
282+
const IntegerRange ToIntegerRange = createFromType(Context, ToType);
283283
return ToIntegerRange.contains(IntegerConstant);
284284
}
285285

@@ -289,13 +289,13 @@ static bool isWideEnoughToHold(const ASTContext &Context,
289289
static bool isFloatExactlyRepresentable(const ASTContext &Context,
290290
const llvm::APFloat &FloatConstant,
291291
const QualType &DestType) {
292-
unsigned DestWidth = Context.getIntWidth(DestType);
293-
bool DestSigned = DestType->isSignedIntegerOrEnumerationType();
292+
const unsigned DestWidth = Context.getIntWidth(DestType);
293+
const bool DestSigned = DestType->isSignedIntegerOrEnumerationType();
294294
llvm::APSInt Result = llvm::APSInt(DestWidth, !DestSigned);
295295
bool IsExact = false;
296-
bool Overflows = FloatConstant.convertToInteger(
297-
Result, llvm::APFloat::rmTowardZero, &IsExact) &
298-
llvm::APFloat::opInvalidOp;
296+
const bool Overflows = FloatConstant.convertToInteger(
297+
Result, llvm::APFloat::rmTowardZero, &IsExact) &
298+
llvm::APFloat::opInvalidOp;
299299
return !Overflows && IsExact;
300300
}
301301

@@ -321,8 +321,8 @@ bool NarrowingConversionsCheck::isWarningInhibitedByEquivalentSize(
321321
// With this option, we don't warn on conversions that have equivalent width
322322
// in bits. eg. uint32 <-> int32.
323323
if (!WarnOnEquivalentBitWidth) {
324-
uint64_t FromTypeSize = Context.getTypeSize(&FromType);
325-
uint64_t ToTypeSize = Context.getTypeSize(&ToType);
324+
const uint64_t FromTypeSize = Context.getTypeSize(&FromType);
325+
const uint64_t ToTypeSize = Context.getTypeSize(&ToType);
326326
if (FromTypeSize == ToTypeSize) {
327327
return true;
328328
}
@@ -406,8 +406,8 @@ void NarrowingConversionsCheck::handleIntegralCast(const ASTContext &Context,
406406
// With this option, we don't warn on conversions that have equivalent width
407407
// in bits. eg. uint32 <-> int32.
408408
if (!WarnOnEquivalentBitWidth) {
409-
uint64_t FromTypeSize = Context.getTypeSize(FromType);
410-
uint64_t ToTypeSize = Context.getTypeSize(ToType);
409+
const uint64_t FromTypeSize = Context.getTypeSize(FromType);
410+
const uint64_t ToTypeSize = Context.getTypeSize(ToType);
411411
if (FromTypeSize == ToTypeSize)
412412
return;
413413
}
@@ -583,7 +583,7 @@ void NarrowingConversionsCheck::handleImplicitCast(
583583
return;
584584
if (handleConditionalOperator(Context, Lhs, Rhs))
585585
return;
586-
SourceLocation SourceLoc = Lhs.getExprLoc();
586+
const SourceLocation SourceLoc = Lhs.getExprLoc();
587587
switch (Cast.getCastKind()) {
588588
case CK_BooleanToSignedIntegral:
589589
handleBooleanToSignedIntegral(Context, SourceLoc, Lhs, Rhs);

clang-tools-extra/clang-tidy/bugprone/NondeterministicPointerIterationOrderCheck.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ void NondeterministicPointerIterationOrderCheck::check(
6060
TemplateArgs[0].getAsType()->isPointerType();
6161

6262
if (IsAlgoArgPointer) {
63-
SourceRange R = RangeInit->getSourceRange();
63+
const SourceRange R = RangeInit->getSourceRange();
6464
diag(R.getBegin(), "iteration of pointers is nondeterministic") << R;
6565
}
6666
}
@@ -69,7 +69,7 @@ void NondeterministicPointerIterationOrderCheck::check(
6969
const auto *SortPointers = Result.Nodes.getNodeAs<Stmt>("sortsemantic");
7070

7171
if ((SortPointers) && !(SortPointers->getBeginLoc().isMacroID())) {
72-
SourceRange R = SortPointers->getSourceRange();
72+
const SourceRange R = SortPointers->getSourceRange();
7373
diag(R.getBegin(), "sorting pointers is nondeterministic") << R;
7474
}
7575
}

0 commit comments

Comments
 (0)