This revision was automatically updated to reflect the committed changes. Closed by commit rGbd7628461b3f: [clang-tidy] Ignore unevaluated exprs in rvalue-reference-param-not-moved (authored by ccotter, committed by PiotrZSL).
Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D146929/new/ https://reviews.llvm.org/D146929 Files: clang-tools-extra/clang-tidy/bugprone/UseAfterMoveCheck.cpp clang-tools-extra/clang-tidy/cppcoreguidelines/RvalueReferenceParamNotMovedCheck.cpp clang-tools-extra/clang-tidy/utils/Matchers.h clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/rvalue-reference-param-not-moved.cpp
Index: clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/rvalue-reference-param-not-moved.cpp =================================================================== --- clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/rvalue-reference-param-not-moved.cpp +++ clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/rvalue-reference-param-not-moved.cpp @@ -156,6 +156,13 @@ Obj moved = std::move((o)); } +void does_not_move_in_evaluated(Obj&& o) { + // CHECK-MESSAGES: :[[@LINE-1]]:39: warning: rvalue reference parameter 'o' is never moved from inside the function body [cppcoreguidelines-rvalue-reference-param-not-moved] + using result_t = decltype(std::move(o)); + unsigned size = sizeof(std::move(o)); + Obj moved = o; +} + template <typename T1, typename T2> struct mypair { T1 first; Index: clang-tools-extra/clang-tidy/utils/Matchers.h =================================================================== --- clang-tools-extra/clang-tidy/utils/Matchers.h +++ clang-tools-extra/clang-tidy/utils/Matchers.h @@ -10,6 +10,7 @@ #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_MATCHERS_H #include "TypeTraits.h" +#include "clang/AST/ExprConcepts.h" #include "clang/ASTMatchers/ASTMatchers.h" #include <optional> @@ -48,6 +49,23 @@ return pointerType(pointee(qualType(isConstQualified()))); } +AST_MATCHER(Expr, hasUnevaluatedContext) { + if (isa<CXXNoexceptExpr>(Node) || isa<RequiresExpr>(Node)) + return true; + if (const auto *UnaryExpr = dyn_cast<UnaryExprOrTypeTraitExpr>(&Node)) { + switch (UnaryExpr->getKind()) { + case UETT_SizeOf: + case UETT_AlignOf: + return true; + default: + return false; + } + } + if (const auto *TypeIDExpr = dyn_cast<CXXTypeidExpr>(&Node)) + return !TypeIDExpr->isPotentiallyEvaluated(); + return false; +} + // A matcher implementation that matches a list of type name regular expressions // against a NamedDecl. If a regular expression contains the substring "::" // matching will occur against the qualified name, otherwise only the typename. Index: clang-tools-extra/clang-tidy/cppcoreguidelines/RvalueReferenceParamNotMovedCheck.cpp =================================================================== --- clang-tools-extra/clang-tidy/cppcoreguidelines/RvalueReferenceParamNotMovedCheck.cpp +++ clang-tools-extra/clang-tidy/cppcoreguidelines/RvalueReferenceParamNotMovedCheck.cpp @@ -7,6 +7,7 @@ //===----------------------------------------------------------------------===// #include "RvalueReferenceParamNotMovedCheck.h" +#include "../utils/Matchers.h" #include "clang/AST/ASTContext.h" #include "clang/ASTMatchers/ASTMatchFinder.h" @@ -14,6 +15,8 @@ namespace clang::tidy::cppcoreguidelines { +using matchers::hasUnevaluatedContext; + namespace { AST_MATCHER_P(LambdaExpr, valueCapturesVar, DeclarationMatcher, VarMatcher) { return std::find_if(Node.capture_begin(), Node.capture_end(), @@ -39,16 +42,18 @@ StatementMatcher MoveCallMatcher = callExpr( + argumentCountIs(1), anyOf(callee(functionDecl(hasName("::std::move"))), callee(unresolvedLookupExpr(hasAnyDeclaration( namedDecl(hasUnderlyingDecl(hasName("::std::move"))))))), - argumentCountIs(1), hasArgument( 0, argumentOf( AllowPartialMove, declRefExpr(to(equalsBoundNode("param"))).bind("ref"))), unless(hasAncestor( - lambdaExpr(valueCapturesVar(equalsBoundNode("param")))))) + lambdaExpr(valueCapturesVar(equalsBoundNode("param"))))), + unless(anyOf(hasAncestor(typeLoc()), + hasAncestor(expr(hasUnevaluatedContext()))))) .bind("move-call"); Finder->addMatcher( Index: clang-tools-extra/clang-tidy/bugprone/UseAfterMoveCheck.cpp =================================================================== --- clang-tools-extra/clang-tidy/bugprone/UseAfterMoveCheck.cpp +++ clang-tools-extra/clang-tidy/bugprone/UseAfterMoveCheck.cpp @@ -10,13 +10,13 @@ #include "clang/AST/Expr.h" #include "clang/AST/ExprCXX.h" -#include "clang/AST/ExprConcepts.h" #include "clang/ASTMatchers/ASTMatchers.h" #include "clang/Analysis/CFG.h" #include "clang/Lex/Lexer.h" #include "llvm/ADT/STLExtras.h" #include "../utils/ExprSequence.h" +#include "../utils/Matchers.h" #include <optional> using namespace clang::ast_matchers; @@ -24,24 +24,9 @@ namespace clang::tidy::bugprone { -namespace { +using matchers::hasUnevaluatedContext; -AST_MATCHER(Expr, hasUnevaluatedContext) { - if (isa<CXXNoexceptExpr>(Node) || isa<RequiresExpr>(Node)) - return true; - if (const auto *UnaryExpr = dyn_cast<UnaryExprOrTypeTraitExpr>(&Node)) { - switch (UnaryExpr->getKind()) { - case UETT_SizeOf: - case UETT_AlignOf: - return true; - default: - return false; - } - } - if (const auto *TypeIDExpr = dyn_cast<CXXTypeidExpr>(&Node)) - return !TypeIDExpr->isPotentiallyEvaluated(); - return false; -} +namespace { /// Contains information about a use-after-move. struct UseAfterMove { @@ -86,7 +71,6 @@ } // namespace - // Matches nodes that are // - Part of a decltype argument or class template argument (we check this by // seeing if they are children of a TypeLoc), or
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits