aboutsummaryrefslogtreecommitdiff
path: root/clang-tools-extra/clang-tidy/misc/RedundantExpressionCheck.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'clang-tools-extra/clang-tidy/misc/RedundantExpressionCheck.cpp')
-rw-r--r--clang-tools-extra/clang-tidy/misc/RedundantExpressionCheck.cpp86
1 files changed, 43 insertions, 43 deletions
diff --git a/clang-tools-extra/clang-tidy/misc/RedundantExpressionCheck.cpp b/clang-tools-extra/clang-tidy/misc/RedundantExpressionCheck.cpp
index 6baa12a..c8ae41c 100644
--- a/clang-tools-extra/clang-tidy/misc/RedundantExpressionCheck.cpp
+++ b/clang-tools-extra/clang-tidy/misc/RedundantExpressionCheck.cpp
@@ -77,8 +77,8 @@ static bool areEquivalentExpr(const Expr *Left, const Expr *Right) {
return cast<CharacterLiteral>(Left)->getValue() ==
cast<CharacterLiteral>(Right)->getValue();
case Stmt::IntegerLiteralClass: {
- llvm::APInt LeftLit = cast<IntegerLiteral>(Left)->getValue();
- llvm::APInt RightLit = cast<IntegerLiteral>(Right)->getValue();
+ const llvm::APInt LeftLit = cast<IntegerLiteral>(Left)->getValue();
+ const llvm::APInt RightLit = cast<IntegerLiteral>(Right)->getValue();
return LeftLit.getBitWidth() == RightLit.getBitWidth() &&
LeftLit == RightLit;
}
@@ -256,7 +256,7 @@ static bool rangeSubsumesRange(BinaryOperatorKind OpcodeLHS,
const APSInt &ValueLHS,
BinaryOperatorKind OpcodeRHS,
const APSInt &ValueRHS) {
- int Comparison = APSInt::compareValues(ValueLHS, ValueRHS);
+ const int Comparison = APSInt::compareValues(ValueLHS, ValueRHS);
switch (OpcodeLHS) {
case BO_EQ:
return OpcodeRHS == BO_EQ && Comparison == 0;
@@ -352,11 +352,11 @@ static bool hasSameOperatorParent(const Expr *TheExpr,
ASTContext &Context) {
// IgnoreParenImpCasts logic in reverse: skip surrounding uninteresting nodes
const DynTypedNodeList Parents = Context.getParents(*TheExpr);
- for (DynTypedNode DynParent : Parents) {
+ for (const DynTypedNode DynParent : Parents) {
if (const auto *Parent = DynParent.get<Expr>()) {
- bool Skip = isa<ParenExpr>(Parent) || isa<ImplicitCastExpr>(Parent) ||
- isa<FullExpr>(Parent) ||
- isa<MaterializeTemporaryExpr>(Parent);
+ const bool Skip =
+ isa<ParenExpr>(Parent) || isa<ImplicitCastExpr>(Parent) ||
+ isa<FullExpr>(Parent) || isa<MaterializeTemporaryExpr>(Parent);
if (Skip && hasSameOperatorParent<TExpr>(Parent, OpKind, Context))
return true;
if (checkOpKind<TExpr>(Parent, OpKind))
@@ -392,7 +392,7 @@ markDuplicateOperands(const TExpr *TheExpr,
return false;
if (collectOperands<TExpr>(Operands.second, AllOperands, OpKind))
return false;
- size_t NumOperands = AllOperands.size();
+ const size_t NumOperands = AllOperands.size();
llvm::SmallBitVector Duplicates(NumOperands);
for (size_t I = 0; I < NumOperands; I++) {
if (Duplicates[I])
@@ -463,7 +463,7 @@ AST_MATCHER_P(Expr, expandedByMacro, ArrayRef<llvm::StringLiteral>, Names) {
const LangOptions &LO = Finder->getASTContext().getLangOpts();
SourceLocation Loc = Node.getExprLoc();
while (Loc.isMacroID()) {
- StringRef MacroName = Lexer::getImmediateMacroName(Loc, SM, LO);
+ const StringRef MacroName = Lexer::getImmediateMacroName(Loc, SM, LO);
if (llvm::is_contained(Names, MacroName))
return true;
Loc = SM.getImmediateMacroCallerLoc(Loc);
@@ -476,7 +476,7 @@ AST_MATCHER_P(Expr, expandedByMacro, ArrayRef<llvm::StringLiteral>, Names) {
// Returns a matcher for integer constant expressions.
static ast_matchers::internal::Matcher<Expr>
matchIntegerConstantExpr(StringRef Id) {
- std::string CstId = (Id + "-const").str();
+ const std::string CstId = (Id + "-const").str();
return expr(isIntegerConstantExpr()).bind(CstId);
}
@@ -486,7 +486,7 @@ matchIntegerConstantExpr(StringRef Id) {
static bool retrieveIntegerConstantExpr(const MatchFinder::MatchResult &Result,
StringRef Id, APSInt &Value,
const Expr *&ConstExpr) {
- std::string CstId = (Id + "-const").str();
+ const std::string CstId = (Id + "-const").str();
ConstExpr = Result.Nodes.getNodeAs<Expr>(CstId);
if (!ConstExpr)
return false;
@@ -508,7 +508,7 @@ static bool retrieveIntegerConstantExpr(const MatchFinder::MatchResult &Result,
// Returns a matcher for symbolic expressions (matches every expression except
// ingeter constant expressions).
static ast_matchers::internal::Matcher<Expr> matchSymbolicExpr(StringRef Id) {
- std::string SymId = (Id + "-sym").str();
+ const std::string SymId = (Id + "-sym").str();
return ignoringParenImpCasts(
expr(unless(isIntegerConstantExpr())).bind(SymId));
}
@@ -517,7 +517,7 @@ static ast_matchers::internal::Matcher<Expr> matchSymbolicExpr(StringRef Id) {
// stores it into 'SymExpr'.
static bool retrieveSymbolicExpr(const MatchFinder::MatchResult &Result,
StringRef Id, const Expr *&SymExpr) {
- std::string SymId = (Id + "-sym").str();
+ const std::string SymId = (Id + "-sym").str();
if (const auto *Node = Result.Nodes.getNodeAs<Expr>(SymId)) {
SymExpr = Node;
return true;
@@ -557,11 +557,11 @@ retrieveBinOpIntegerConstantExpr(const MatchFinder::MatchResult &Result,
// Matches relational expressions: 'Expr <op> k' (i.e. x < 2, x != 3, 12 <= x).
static ast_matchers::internal::Matcher<Expr>
matchRelationalIntegerConstantExpr(StringRef Id) {
- std::string CastId = (Id + "-cast").str();
- std::string SwapId = (Id + "-swap").str();
- std::string NegateId = (Id + "-negate").str();
- std::string OverloadId = (Id + "-overload").str();
- std::string ConstId = (Id + "-const").str();
+ const std::string CastId = (Id + "-cast").str();
+ const std::string SwapId = (Id + "-swap").str();
+ const std::string NegateId = (Id + "-negate").str();
+ const std::string OverloadId = (Id + "-overload").str();
+ const std::string ConstId = (Id + "-const").str();
const auto RelationalExpr = ignoringParenImpCasts(binaryOperator(
isComparisonOperator(), expr().bind(Id),
@@ -625,7 +625,7 @@ canOverloadedOperatorArgsBeModified(const CXXOperatorCallExpr *OperatorCall,
if (!OperatorDecl)
return true;
- unsigned ParamCount = OperatorDecl->getNumParams();
+ const unsigned ParamCount = OperatorDecl->getNumParams();
// Overloaded operators declared inside a class have only one param.
// These functions must be declared const in order to not be able to modify
@@ -647,10 +647,10 @@ static bool retrieveRelationalIntegerConstantExpr(
const MatchFinder::MatchResult &Result, StringRef Id,
const Expr *&OperandExpr, BinaryOperatorKind &Opcode, const Expr *&Symbol,
APSInt &Value, const Expr *&ConstExpr) {
- std::string CastId = (Id + "-cast").str();
- std::string SwapId = (Id + "-swap").str();
- std::string NegateId = (Id + "-negate").str();
- std::string OverloadId = (Id + "-overload").str();
+ const std::string CastId = (Id + "-cast").str();
+ const std::string SwapId = (Id + "-swap").str();
+ const std::string NegateId = (Id + "-negate").str();
+ const std::string OverloadId = (Id + "-overload").str();
if (const auto *Bin = Result.Nodes.getNodeAs<BinaryOperator>(Id)) {
// Operand received with explicit comparator.
@@ -829,11 +829,11 @@ static bool areExprsFromDifferentMacros(const Expr *LhsExpr,
const SourceManager &SM = AstCtx->getSourceManager();
const LangOptions &LO = AstCtx->getLangOpts();
- std::pair<FileID, unsigned> LsrLocInfo =
+ const std::pair<FileID, unsigned> LsrLocInfo =
SM.getDecomposedLoc(SM.getExpansionLoc(Lsr.getBegin()));
- std::pair<FileID, unsigned> RsrLocInfo =
+ const std::pair<FileID, unsigned> RsrLocInfo =
SM.getDecomposedLoc(SM.getExpansionLoc(Rsr.getBegin()));
- llvm::MemoryBufferRef MB = SM.getBufferOrFake(LsrLocInfo.first);
+ const llvm::MemoryBufferRef MB = SM.getBufferOrFake(LsrLocInfo.first);
const char *LTokenPos = MB.getBufferStart() + LsrLocInfo.second;
const char *RTokenPos = MB.getBufferStart() + RsrLocInfo.second;
@@ -1097,7 +1097,7 @@ void RedundantExpressionCheck::checkArithmeticExpr(
if (const auto *ComparisonOperator = Result.Nodes.getNodeAs<BinaryOperator>(
"binop-const-compare-to-sym")) {
- BinaryOperatorKind Opcode = ComparisonOperator->getOpcode();
+ const BinaryOperatorKind Opcode = ComparisonOperator->getOpcode();
if (!retrieveBinOpIntegerConstantExpr(Result, "lhs", LhsOpcode, LhsSymbol,
LhsValue) ||
!retrieveSymbolicExpr(Result, "rhs", RhsSymbol) ||
@@ -1118,7 +1118,7 @@ void RedundantExpressionCheck::checkArithmeticExpr(
} else if (const auto *ComparisonOperator =
Result.Nodes.getNodeAs<BinaryOperator>(
"binop-const-compare-to-binop-const")) {
- BinaryOperatorKind Opcode = ComparisonOperator->getOpcode();
+ const BinaryOperatorKind Opcode = ComparisonOperator->getOpcode();
if (!retrieveBinOpIntegerConstantExpr(Result, "lhs", LhsOpcode, LhsSymbol,
LhsValue) ||
@@ -1165,7 +1165,7 @@ void RedundantExpressionCheck::checkBitwiseExpr(
const MatchFinder::MatchResult &Result) {
if (const auto *ComparisonOperator = Result.Nodes.getNodeAs<BinaryOperator>(
"binop-const-compare-to-const")) {
- BinaryOperatorKind Opcode = ComparisonOperator->getOpcode();
+ const BinaryOperatorKind Opcode = ComparisonOperator->getOpcode();
APSInt LhsValue, RhsValue;
const Expr *LhsSymbol = nullptr;
@@ -1175,9 +1175,9 @@ void RedundantExpressionCheck::checkBitwiseExpr(
!retrieveIntegerConstantExpr(Result, "rhs", RhsValue))
return;
- uint64_t LhsConstant = LhsValue.getZExtValue();
- uint64_t RhsConstant = RhsValue.getZExtValue();
- SourceLocation Loc = ComparisonOperator->getOperatorLoc();
+ const uint64_t LhsConstant = LhsValue.getZExtValue();
+ const uint64_t RhsConstant = RhsValue.getZExtValue();
+ const SourceLocation Loc = ComparisonOperator->getOperatorLoc();
// Check expression: x & k1 == k2 (i.e. x & 0xFF == 0xF00)
if (LhsOpcode == BO_And && (LhsConstant & RhsConstant) != RhsConstant) {
@@ -1208,24 +1208,24 @@ void RedundantExpressionCheck::checkBitwiseExpr(
if ((Value != 0 && ~Value != 0) || Sym->getExprLoc().isMacroID())
return;
- SourceLocation Loc = IneffectiveOperator->getOperatorLoc();
+ const SourceLocation Loc = IneffectiveOperator->getOperatorLoc();
- BinaryOperatorKind Opcode = IneffectiveOperator->getOpcode();
+ const BinaryOperatorKind Opcode = IneffectiveOperator->getOpcode();
if (exprEvaluatesToZero(Opcode, Value)) {
diag(Loc, "expression always evaluates to 0");
} else if (exprEvaluatesToBitwiseNegatedZero(Opcode, Value)) {
- SourceRange ConstExprRange(ConstExpr->getBeginLoc(),
- ConstExpr->getEndLoc());
- StringRef ConstExprText = Lexer::getSourceText(
+ const SourceRange ConstExprRange(ConstExpr->getBeginLoc(),
+ ConstExpr->getEndLoc());
+ const StringRef ConstExprText = Lexer::getSourceText(
CharSourceRange::getTokenRange(ConstExprRange), *Result.SourceManager,
Result.Context->getLangOpts());
diag(Loc, "expression always evaluates to '%0'") << ConstExprText;
} else if (exprEvaluatesToSymbolic(Opcode, Value)) {
- SourceRange SymExprRange(Sym->getBeginLoc(), Sym->getEndLoc());
+ const SourceRange SymExprRange(Sym->getBeginLoc(), Sym->getEndLoc());
- StringRef ExprText = Lexer::getSourceText(
+ const StringRef ExprText = Lexer::getSourceText(
CharSourceRange::getTokenRange(SymExprRange), *Result.SourceManager,
Result.Context->getLangOpts());
@@ -1240,7 +1240,7 @@ void RedundantExpressionCheck::checkRelationalExpr(
"comparisons-of-symbol-and-const")) {
// Matched expressions are: (x <op> k1) <REL> (x <op> k2).
// E.g.: (X < 2) && (X > 4)
- BinaryOperatorKind Opcode = ComparisonOperator->getOpcode();
+ const BinaryOperatorKind Opcode = ComparisonOperator->getOpcode();
const Expr *LhsExpr = nullptr, *RhsExpr = nullptr;
const Expr *LhsSymbol = nullptr, *RhsSymbol = nullptr;
@@ -1392,7 +1392,7 @@ void RedundantExpressionCheck::check(const MatchFinder::MatchResult &Result) {
if (Call && canOverloadedOperatorArgsBeModified(Call, true))
return;
- StringRef Message =
+ const StringRef Message =
Call ? "overloaded operator has equivalent nested operands"
: "operator has equivalent nested operands";
@@ -1405,12 +1405,12 @@ void RedundantExpressionCheck::check(const MatchFinder::MatchResult &Result) {
if (const auto *NegateOperator =
Result.Nodes.getNodeAs<UnaryOperator>("logical-bitwise-confusion")) {
- SourceLocation OperatorLoc = NegateOperator->getOperatorLoc();
+ const SourceLocation OperatorLoc = NegateOperator->getOperatorLoc();
auto Diag =
diag(OperatorLoc,
"ineffective logical negation operator used; did you mean '~'?");
- SourceLocation LogicalNotLocation = OperatorLoc.getLocWithOffset(1);
+ const SourceLocation LogicalNotLocation = OperatorLoc.getLocWithOffset(1);
if (!LogicalNotLocation.isMacroID())
Diag << FixItHint::CreateReplacement(