This revision was landed with ongoing or failed builds. This revision was automatically updated to reflect the committed changes. Closed by commit rG9182c679dde7: [clang-tidy]performance-no-automatic-move: fix false negative on `const T&&`… (authored by courbet).
Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D151092/new/ https://reviews.llvm.org/D151092 Files: clang-tools-extra/clang-tidy/performance/NoAutomaticMoveCheck.cpp clang-tools-extra/docs/ReleaseNotes.rst clang-tools-extra/test/clang-tidy/checkers/performance/no-automatic-move.cpp
Index: clang-tools-extra/test/clang-tidy/checkers/performance/no-automatic-move.cpp =================================================================== --- clang-tools-extra/test/clang-tidy/checkers/performance/no-automatic-move.cpp +++ clang-tools-extra/test/clang-tidy/checkers/performance/no-automatic-move.cpp @@ -7,30 +7,42 @@ virtual ~Obj(); }; -template <typename T> -struct StatusOr { - StatusOr(const T &); - StatusOr(T &&); -}; - struct NonTemplate { NonTemplate(const Obj &); NonTemplate(Obj &&); }; +template <typename T> struct TemplateCtorPair { + TemplateCtorPair(const T &); + TemplateCtorPair(T &&value); +}; + +template <typename T> struct UrefCtor { + template <class U = T> UrefCtor(U &&value); +}; + template <typename T> T Make(); -StatusOr<Obj> PositiveStatusOrConstValue() { +NonTemplate PositiveNonTemplate() { const Obj obj = Make<Obj>(); - return obj; - // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: constness of 'obj' prevents automatic move [performance-no-automatic-move] + return obj; // selects `NonTemplate(const Obj&)` + // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: constness of 'obj' prevents + // automatic move [performance-no-automatic-move] } -NonTemplate PositiveNonTemplateConstValue() { +TemplateCtorPair<Obj> PositiveTemplateCtorPair() { const Obj obj = Make<Obj>(); - return obj; - // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: constness of 'obj' prevents automatic move [performance-no-automatic-move] + return obj; // selects `TemplateCtorPair(const T&)` + // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: constness of 'obj' prevents + // automatic move [performance-no-automatic-move] +} + +UrefCtor<Obj> PositiveUrefCtor() { + const Obj obj = Make<Obj>(); + return obj; // selects `UrefCtor(const T&&)` + // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: constness of 'obj' prevents + // automatic move [performance-no-automatic-move] } Obj PositiveCantNrvo(bool b) { @@ -51,22 +63,18 @@ } // FIXME: Ideally we would warn here too. -StatusOr<Obj> PositiveStatusOrLifetimeExtension() { +UrefCtor<Obj> PositiveUrefCtorLifetimeExtension() { const Obj &obj = Make<Obj>(); return obj; } // Negatives. -StatusOr<Obj> Temporary() { - return Make<Obj>(); -} +UrefCtor<Obj> Temporary() { return Make<Obj>(); } -StatusOr<Obj> ConstTemporary() { - return Make<const Obj>(); -} +UrefCtor<Obj> ConstTemporary() { return Make<const Obj>(); } -StatusOr<Obj> ConvertingMoveConstructor() { +UrefCtor<Obj> ConvertingMoveConstructor() { Obj obj = Make<Obj>(); return obj; } @@ -85,21 +93,19 @@ return obj2; } -StatusOr<Obj> Ref() { +UrefCtor<Obj> Ref() { Obj &obj = Make<Obj &>(); return obj; } -StatusOr<Obj> ConstRef() { +UrefCtor<Obj> ConstRef() { const Obj &obj = Make<Obj &>(); return obj; } const Obj global; -StatusOr<Obj> Global() { - return global; -} +UrefCtor<Obj> Global() { return global; } struct FromConstRefOnly { FromConstRefOnly(const Obj &); Index: clang-tools-extra/docs/ReleaseNotes.rst =================================================================== --- clang-tools-extra/docs/ReleaseNotes.rst +++ clang-tools-extra/docs/ReleaseNotes.rst @@ -387,6 +387,10 @@ <clang-tidy/checks/performance/no-automatic-move>` when warning would be emitted for a const local variable to which NRVO is applied. +- Improved :doc:`performance-no-automatic-move + <clang-tidy/checks/performance/performance-no-automatic-move>`: warn on ``const &&`` + constructors. + Removed checks ^^^^^^^^^^^^^^ Index: clang-tools-extra/clang-tidy/performance/NoAutomaticMoveCheck.cpp =================================================================== --- clang-tools-extra/clang-tidy/performance/NoAutomaticMoveCheck.cpp +++ clang-tools-extra/clang-tidy/performance/NoAutomaticMoveCheck.cpp @@ -48,15 +48,23 @@ hasParameter(0, hasType(rValueReferenceType( pointee(type(equalsBoundNode("SrcT"))))))))))); + // A matcher for `DstT::DstT(const Src&&)`, which typically comes from an + // instantiation of `template <typename U> DstT::DstT(U&&)`. + const auto ConstRefRefCtor = cxxConstructorDecl( + parameterCountIs(1), + hasParameter(0, + hasType(rValueReferenceType(pointee(isConstQualified()))))); + Finder->addMatcher( - traverse(TK_AsIs, - returnStmt(hasReturnValue( - ignoringElidableConstructorCall(ignoringParenImpCasts( - cxxConstructExpr( - hasDeclaration(LValueRefCtor), - hasArgument(0, ignoringParenImpCasts(declRefExpr( - to(NonNrvoConstLocalVariable))))) - .bind("ctor_call")))))), + traverse( + TK_AsIs, + returnStmt(hasReturnValue( + ignoringElidableConstructorCall(ignoringParenImpCasts( + cxxConstructExpr( + hasDeclaration(anyOf(LValueRefCtor, ConstRefRefCtor)), + hasArgument(0, ignoringParenImpCasts(declRefExpr( + to(NonNrvoConstLocalVariable))))) + .bind("ctor_call")))))), this); }
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits