[clang-tools-extra] [clang-tidy] Fix performance-move-const-arg false negative in ternary… (PR #128402)
https://github.com/RiverDave created https://github.com/llvm/llvm-project/pull/128402 This PR aims to fix `performance-move-const-arg` #126515 ## Changes Enhanced the `performance-move-arg` check in Clang-Tidy to detect cases where std::move is used in **ternary expressions which was not being detected before** ## Testing - A new mock class has been where the changes have been tested & all tests pass >From d9dbf1b67ec6f249656c0b4af35ea48ef29cc095 Mon Sep 17 00:00:00 2001 From: Riverdave Date: Sat, 22 Feb 2025 03:57:35 -0500 Subject: [PATCH] [clang-tidy] Fix performance-move-const-arg false negative in ternary operators --- .../performance/MoveConstArgCheck.cpp | 13 +-- clang-tools-extra/docs/ReleaseNotes.rst | 4 .../checkers/performance/move-const-arg.cpp | 23 +++ 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/clang-tools-extra/clang-tidy/performance/MoveConstArgCheck.cpp b/clang-tools-extra/clang-tidy/performance/MoveConstArgCheck.cpp index 421ce003975bc..3de41e707cfd7 100644 --- a/clang-tools-extra/clang-tidy/performance/MoveConstArgCheck.cpp +++ b/clang-tools-extra/clang-tidy/performance/MoveConstArgCheck.cpp @@ -44,6 +44,12 @@ void MoveConstArgCheck::registerMatchers(MatchFinder *Finder) { unless(isInTemplateInstantiation())) .bind("call-move"); + // Match ternary expressions where either branch contains std::move + auto TernaryWithMoveMatcher = + conditionalOperator( +hasDescendant(MoveCallMatcher) + ).bind("ternary-move"); + Finder->addMatcher( expr(anyOf( castExpr(hasSourceExpression(MoveCallMatcher)), @@ -58,13 +64,16 @@ void MoveConstArgCheck::registerMatchers(MatchFinder *Finder) { qualType(rValueReferenceType()).bind("invocation-parm-type"); // Matches respective ParmVarDecl for a CallExpr or CXXConstructExpr. auto ArgumentWithParamMatcher = forEachArgumentWithParam( - MoveCallMatcher, parmVarDecl(anyOf(hasType(ConstTypeParmMatcher), + anyOf(MoveCallMatcher, TernaryWithMoveMatcher), + parmVarDecl(anyOf(hasType(ConstTypeParmMatcher), hasType(RValueTypeParmMatcher))) .bind("invocation-parm")); // Matches respective types of arguments for a CallExpr or CXXConstructExpr // and it works on calls through function pointers as well. auto ArgumentWithParamTypeMatcher = forEachArgumentWithParamType( - MoveCallMatcher, anyOf(ConstTypeParmMatcher, RValueTypeParmMatcher)); + anyOf(MoveCallMatcher, TernaryWithMoveMatcher), + anyOf(ConstTypeParmMatcher, RValueTypeParmMatcher)); + Finder->addMatcher( invocation(anyOf(ArgumentWithParamMatcher, ArgumentWithParamTypeMatcher)) diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 41ff1c1016f25..2eb65d61f5e78 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -115,6 +115,10 @@ Changes in existing checks ` check by providing additional examples and fixing some macro related false positives. +- Improved :doc:`performance-move-const-arg + ` check by fixing false negatives + on ternary operators calling ``std::move``. + Removed checks ^^ diff --git a/clang-tools-extra/test/clang-tidy/checkers/performance/move-const-arg.cpp b/clang-tools-extra/test/clang-tidy/checkers/performance/move-const-arg.cpp index 8e325b0ae6ca3..e616cbe78bc3a 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/performance/move-const-arg.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/performance/move-const-arg.cpp @@ -560,3 +560,26 @@ struct Result { // CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: passing result of std::move() as a const reference argument; no move will actually happen [performance-move-const-arg] }; } // namespace GH111450 + +namespace GH126515 { + +struct TernaryMoveCall { +TernaryMoveCall(); +TernaryMoveCall(const TernaryMoveCall&); +TernaryMoveCall operator=(const TernaryMoveCall&); + +void TernaryCheckTriviallyCopyable(const char * c) {} + +void testTernaryMove() { + TernaryMoveCall t1; + TernaryMoveCall other(false ? TernaryMoveCall() : TernaryMoveCall(std::move(t1)) ); + // CHECK-MESSAGES: :[[@LINE-1]]:69: warning: passing result of std::move() as a const reference argument; no move will actually happen [performance-move-const-arg] + // CHECK-MESSAGES: :[[@LINE-11]]:8: note: 'TernaryMoveCall' is not move assignable/constructible + + const char* a = "a"; + TernaryCheckTriviallyCopyable(true ? std::move(a) : "" ); + // CHECK-MESSAGES: :[[@LINE-1]]:40: warning: std::move of the variable 'a' of the trivially-copyable type 'const char *' has no effect; remove std::move() [performance-move-const-arg] +} + +}; +} // namespace GH126515 ___ cfe-commits mailing list cfe-commits@lists.llvm.org ht
[clang-tools-extra] [clang-tidy] Fix performance-move-const-arg false negative in ternary… (PR #128402)
https://github.com/RiverDave edited https://github.com/llvm/llvm-project/pull/128402 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Fix performance-move-const-arg false negative in ternary… (PR #128402)
https://github.com/RiverDave edited https://github.com/llvm/llvm-project/pull/128402 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Fix performance-move-const-arg false negative in ternary… (PR #128402)
RiverDave wrote: Thx @HerrCai0907, your feedback has been addresed. https://github.com/llvm/llvm-project/pull/128402 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Fix performance-move-const-arg false negative in ternary… (PR #128402)
https://github.com/RiverDave edited https://github.com/llvm/llvm-project/pull/128402 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Fix performance-move-const-arg false negative in ternary… (PR #128402)
https://github.com/RiverDave updated https://github.com/llvm/llvm-project/pull/128402 >From 5eef2a52de7de53b0fb24781f40a7b02b55025b9 Mon Sep 17 00:00:00 2001 From: Riverdave Date: Sat, 22 Feb 2025 03:57:35 -0500 Subject: [PATCH] [clang-tidy] Fix performance-move-const-arg false negative in ternary operators --- .../performance/MoveConstArgCheck.cpp | 11 +++-- clang-tools-extra/docs/ReleaseNotes.rst | 4 .../checkers/performance/move-const-arg.cpp | 23 +++ 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/clang-tools-extra/clang-tidy/performance/MoveConstArgCheck.cpp b/clang-tools-extra/clang-tidy/performance/MoveConstArgCheck.cpp index 421ce003975bc..553c1d20cbf1d 100644 --- a/clang-tools-extra/clang-tidy/performance/MoveConstArgCheck.cpp +++ b/clang-tools-extra/clang-tidy/performance/MoveConstArgCheck.cpp @@ -44,6 +44,10 @@ void MoveConstArgCheck::registerMatchers(MatchFinder *Finder) { unless(isInTemplateInstantiation())) .bind("call-move"); + // Match ternary expressions where either branch contains std::move + auto TernaryWithMoveMatcher = + conditionalOperator(hasDescendant(MoveCallMatcher)); + Finder->addMatcher( expr(anyOf( castExpr(hasSourceExpression(MoveCallMatcher)), @@ -58,13 +62,16 @@ void MoveConstArgCheck::registerMatchers(MatchFinder *Finder) { qualType(rValueReferenceType()).bind("invocation-parm-type"); // Matches respective ParmVarDecl for a CallExpr or CXXConstructExpr. auto ArgumentWithParamMatcher = forEachArgumentWithParam( - MoveCallMatcher, parmVarDecl(anyOf(hasType(ConstTypeParmMatcher), + anyOf(MoveCallMatcher, TernaryWithMoveMatcher), + parmVarDecl(anyOf(hasType(ConstTypeParmMatcher), hasType(RValueTypeParmMatcher))) .bind("invocation-parm")); // Matches respective types of arguments for a CallExpr or CXXConstructExpr // and it works on calls through function pointers as well. auto ArgumentWithParamTypeMatcher = forEachArgumentWithParamType( - MoveCallMatcher, anyOf(ConstTypeParmMatcher, RValueTypeParmMatcher)); + anyOf(MoveCallMatcher, TernaryWithMoveMatcher), + anyOf(ConstTypeParmMatcher, RValueTypeParmMatcher)); + Finder->addMatcher( invocation(anyOf(ArgumentWithParamMatcher, ArgumentWithParamTypeMatcher)) diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 41ff1c1016f25..2eb65d61f5e78 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -115,6 +115,10 @@ Changes in existing checks ` check by providing additional examples and fixing some macro related false positives. +- Improved :doc:`performance-move-const-arg + ` check by fixing false negatives + on ternary operators calling ``std::move``. + Removed checks ^^ diff --git a/clang-tools-extra/test/clang-tidy/checkers/performance/move-const-arg.cpp b/clang-tools-extra/test/clang-tidy/checkers/performance/move-const-arg.cpp index 8e325b0ae6ca3..e616cbe78bc3a 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/performance/move-const-arg.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/performance/move-const-arg.cpp @@ -560,3 +560,26 @@ struct Result { // CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: passing result of std::move() as a const reference argument; no move will actually happen [performance-move-const-arg] }; } // namespace GH111450 + +namespace GH126515 { + +struct TernaryMoveCall { +TernaryMoveCall(); +TernaryMoveCall(const TernaryMoveCall&); +TernaryMoveCall operator=(const TernaryMoveCall&); + +void TernaryCheckTriviallyCopyable(const char * c) {} + +void testTernaryMove() { + TernaryMoveCall t1; + TernaryMoveCall other(false ? TernaryMoveCall() : TernaryMoveCall(std::move(t1)) ); + // CHECK-MESSAGES: :[[@LINE-1]]:69: warning: passing result of std::move() as a const reference argument; no move will actually happen [performance-move-const-arg] + // CHECK-MESSAGES: :[[@LINE-11]]:8: note: 'TernaryMoveCall' is not move assignable/constructible + + const char* a = "a"; + TernaryCheckTriviallyCopyable(true ? std::move(a) : "" ); + // CHECK-MESSAGES: :[[@LINE-1]]:40: warning: std::move of the variable 'a' of the trivially-copyable type 'const char *' has no effect; remove std::move() [performance-move-const-arg] +} + +}; +} // namespace GH126515 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Fix performance-move-const-arg false negative in ternary… (PR #128402)
@@ -44,6 +44,12 @@ void MoveConstArgCheck::registerMatchers(MatchFinder *Finder) { unless(isInTemplateInstantiation())) .bind("call-move"); + // Match ternary expressions where either branch contains std::move + auto TernaryWithMoveMatcher = + conditionalOperator( +hasDescendant(MoveCallMatcher) + ).bind("ternary-move"); RiverDave wrote: Amazing! Appreciate your feedback, all of your comments should be addressed by now. https://github.com/llvm/llvm-project/pull/128402 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Fix performance-move-const-arg false negative in ternary… (PR #128402)
https://github.com/RiverDave edited https://github.com/llvm/llvm-project/pull/128402 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Fix performance-move-const-arg false negative in ternary… (PR #128402)
RiverDave wrote: > lgtm. please run git-clang-format also. i think there are format issue in the > new code. Thx @HerrCai0907, your feedback has been addresed. https://github.com/llvm/llvm-project/pull/128402 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Fix performance-move-const-arg false negative in ternary… (PR #128402)
https://github.com/RiverDave updated https://github.com/llvm/llvm-project/pull/128402 >From 5eef2a52de7de53b0fb24781f40a7b02b55025b9 Mon Sep 17 00:00:00 2001 From: Riverdave Date: Sat, 22 Feb 2025 03:57:35 -0500 Subject: [PATCH] [clang-tidy] Fix performance-move-const-arg false negative in ternary operators --- .../performance/MoveConstArgCheck.cpp | 11 +++-- clang-tools-extra/docs/ReleaseNotes.rst | 4 .../checkers/performance/move-const-arg.cpp | 23 +++ 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/clang-tools-extra/clang-tidy/performance/MoveConstArgCheck.cpp b/clang-tools-extra/clang-tidy/performance/MoveConstArgCheck.cpp index 421ce003975bc..553c1d20cbf1d 100644 --- a/clang-tools-extra/clang-tidy/performance/MoveConstArgCheck.cpp +++ b/clang-tools-extra/clang-tidy/performance/MoveConstArgCheck.cpp @@ -44,6 +44,10 @@ void MoveConstArgCheck::registerMatchers(MatchFinder *Finder) { unless(isInTemplateInstantiation())) .bind("call-move"); + // Match ternary expressions where either branch contains std::move + auto TernaryWithMoveMatcher = + conditionalOperator(hasDescendant(MoveCallMatcher)); + Finder->addMatcher( expr(anyOf( castExpr(hasSourceExpression(MoveCallMatcher)), @@ -58,13 +62,16 @@ void MoveConstArgCheck::registerMatchers(MatchFinder *Finder) { qualType(rValueReferenceType()).bind("invocation-parm-type"); // Matches respective ParmVarDecl for a CallExpr or CXXConstructExpr. auto ArgumentWithParamMatcher = forEachArgumentWithParam( - MoveCallMatcher, parmVarDecl(anyOf(hasType(ConstTypeParmMatcher), + anyOf(MoveCallMatcher, TernaryWithMoveMatcher), + parmVarDecl(anyOf(hasType(ConstTypeParmMatcher), hasType(RValueTypeParmMatcher))) .bind("invocation-parm")); // Matches respective types of arguments for a CallExpr or CXXConstructExpr // and it works on calls through function pointers as well. auto ArgumentWithParamTypeMatcher = forEachArgumentWithParamType( - MoveCallMatcher, anyOf(ConstTypeParmMatcher, RValueTypeParmMatcher)); + anyOf(MoveCallMatcher, TernaryWithMoveMatcher), + anyOf(ConstTypeParmMatcher, RValueTypeParmMatcher)); + Finder->addMatcher( invocation(anyOf(ArgumentWithParamMatcher, ArgumentWithParamTypeMatcher)) diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 41ff1c1016f25..2eb65d61f5e78 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -115,6 +115,10 @@ Changes in existing checks ` check by providing additional examples and fixing some macro related false positives. +- Improved :doc:`performance-move-const-arg + ` check by fixing false negatives + on ternary operators calling ``std::move``. + Removed checks ^^ diff --git a/clang-tools-extra/test/clang-tidy/checkers/performance/move-const-arg.cpp b/clang-tools-extra/test/clang-tidy/checkers/performance/move-const-arg.cpp index 8e325b0ae6ca3..e616cbe78bc3a 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/performance/move-const-arg.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/performance/move-const-arg.cpp @@ -560,3 +560,26 @@ struct Result { // CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: passing result of std::move() as a const reference argument; no move will actually happen [performance-move-const-arg] }; } // namespace GH111450 + +namespace GH126515 { + +struct TernaryMoveCall { +TernaryMoveCall(); +TernaryMoveCall(const TernaryMoveCall&); +TernaryMoveCall operator=(const TernaryMoveCall&); + +void TernaryCheckTriviallyCopyable(const char * c) {} + +void testTernaryMove() { + TernaryMoveCall t1; + TernaryMoveCall other(false ? TernaryMoveCall() : TernaryMoveCall(std::move(t1)) ); + // CHECK-MESSAGES: :[[@LINE-1]]:69: warning: passing result of std::move() as a const reference argument; no move will actually happen [performance-move-const-arg] + // CHECK-MESSAGES: :[[@LINE-11]]:8: note: 'TernaryMoveCall' is not move assignable/constructible + + const char* a = "a"; + TernaryCheckTriviallyCopyable(true ? std::move(a) : "" ); + // CHECK-MESSAGES: :[[@LINE-1]]:40: warning: std::move of the variable 'a' of the trivially-copyable type 'const char *' has no effect; remove std::move() [performance-move-const-arg] +} + +}; +} // namespace GH126515 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Fix performance-move-const-arg false negative in ternary… (PR #128402)
https://github.com/RiverDave updated https://github.com/llvm/llvm-project/pull/128402 >From 848be2ccd25fd68b6a2d2037198184b08ff5d6e2 Mon Sep 17 00:00:00 2001 From: Riverdave Date: Sat, 22 Feb 2025 03:57:35 -0500 Subject: [PATCH] [clang-tidy] Fix performance-move-const-arg false negative in ternary operators --- .../performance/MoveConstArgCheck.cpp | 11 +++-- clang-tools-extra/docs/ReleaseNotes.rst | 4 .../checkers/performance/move-const-arg.cpp | 23 +++ 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/clang-tools-extra/clang-tidy/performance/MoveConstArgCheck.cpp b/clang-tools-extra/clang-tidy/performance/MoveConstArgCheck.cpp index 421ce003975bc..553c1d20cbf1d 100644 --- a/clang-tools-extra/clang-tidy/performance/MoveConstArgCheck.cpp +++ b/clang-tools-extra/clang-tidy/performance/MoveConstArgCheck.cpp @@ -44,6 +44,10 @@ void MoveConstArgCheck::registerMatchers(MatchFinder *Finder) { unless(isInTemplateInstantiation())) .bind("call-move"); + // Match ternary expressions where either branch contains std::move + auto TernaryWithMoveMatcher = + conditionalOperator(hasDescendant(MoveCallMatcher)); + Finder->addMatcher( expr(anyOf( castExpr(hasSourceExpression(MoveCallMatcher)), @@ -58,13 +62,16 @@ void MoveConstArgCheck::registerMatchers(MatchFinder *Finder) { qualType(rValueReferenceType()).bind("invocation-parm-type"); // Matches respective ParmVarDecl for a CallExpr or CXXConstructExpr. auto ArgumentWithParamMatcher = forEachArgumentWithParam( - MoveCallMatcher, parmVarDecl(anyOf(hasType(ConstTypeParmMatcher), + anyOf(MoveCallMatcher, TernaryWithMoveMatcher), + parmVarDecl(anyOf(hasType(ConstTypeParmMatcher), hasType(RValueTypeParmMatcher))) .bind("invocation-parm")); // Matches respective types of arguments for a CallExpr or CXXConstructExpr // and it works on calls through function pointers as well. auto ArgumentWithParamTypeMatcher = forEachArgumentWithParamType( - MoveCallMatcher, anyOf(ConstTypeParmMatcher, RValueTypeParmMatcher)); + anyOf(MoveCallMatcher, TernaryWithMoveMatcher), + anyOf(ConstTypeParmMatcher, RValueTypeParmMatcher)); + Finder->addMatcher( invocation(anyOf(ArgumentWithParamMatcher, ArgumentWithParamTypeMatcher)) diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 2dcefa2ddec83..b87ea491b3ad1 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -120,6 +120,10 @@ Changes in existing checks tolerating fix-it breaking compilation when functions is used as pointers to avoid matching usage of functions within the current compilation unit. +- Improved :doc:`performance-move-const-arg + ` check by fixing false negatives + on ternary operators calling ``std::move``. + Removed checks ^^ diff --git a/clang-tools-extra/test/clang-tidy/checkers/performance/move-const-arg.cpp b/clang-tools-extra/test/clang-tidy/checkers/performance/move-const-arg.cpp index 8e325b0ae6ca3..e616cbe78bc3a 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/performance/move-const-arg.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/performance/move-const-arg.cpp @@ -560,3 +560,26 @@ struct Result { // CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: passing result of std::move() as a const reference argument; no move will actually happen [performance-move-const-arg] }; } // namespace GH111450 + +namespace GH126515 { + +struct TernaryMoveCall { +TernaryMoveCall(); +TernaryMoveCall(const TernaryMoveCall&); +TernaryMoveCall operator=(const TernaryMoveCall&); + +void TernaryCheckTriviallyCopyable(const char * c) {} + +void testTernaryMove() { + TernaryMoveCall t1; + TernaryMoveCall other(false ? TernaryMoveCall() : TernaryMoveCall(std::move(t1)) ); + // CHECK-MESSAGES: :[[@LINE-1]]:69: warning: passing result of std::move() as a const reference argument; no move will actually happen [performance-move-const-arg] + // CHECK-MESSAGES: :[[@LINE-11]]:8: note: 'TernaryMoveCall' is not move assignable/constructible + + const char* a = "a"; + TernaryCheckTriviallyCopyable(true ? std::move(a) : "" ); + // CHECK-MESSAGES: :[[@LINE-1]]:40: warning: std::move of the variable 'a' of the trivially-copyable type 'const char *' has no effect; remove std::move() [performance-move-const-arg] +} + +}; +} // namespace GH126515 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Fix performance-move-const-arg false negative in ternary… (PR #128402)
https://github.com/RiverDave updated https://github.com/llvm/llvm-project/pull/128402 >From 5eef2a52de7de53b0fb24781f40a7b02b55025b9 Mon Sep 17 00:00:00 2001 From: Riverdave Date: Sat, 22 Feb 2025 03:57:35 -0500 Subject: [PATCH] [clang-tidy] Fix performance-move-const-arg false negative in ternary operators --- .../performance/MoveConstArgCheck.cpp | 11 +++-- clang-tools-extra/docs/ReleaseNotes.rst | 4 .../checkers/performance/move-const-arg.cpp | 23 +++ 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/clang-tools-extra/clang-tidy/performance/MoveConstArgCheck.cpp b/clang-tools-extra/clang-tidy/performance/MoveConstArgCheck.cpp index 421ce003975bc..553c1d20cbf1d 100644 --- a/clang-tools-extra/clang-tidy/performance/MoveConstArgCheck.cpp +++ b/clang-tools-extra/clang-tidy/performance/MoveConstArgCheck.cpp @@ -44,6 +44,10 @@ void MoveConstArgCheck::registerMatchers(MatchFinder *Finder) { unless(isInTemplateInstantiation())) .bind("call-move"); + // Match ternary expressions where either branch contains std::move + auto TernaryWithMoveMatcher = + conditionalOperator(hasDescendant(MoveCallMatcher)); + Finder->addMatcher( expr(anyOf( castExpr(hasSourceExpression(MoveCallMatcher)), @@ -58,13 +62,16 @@ void MoveConstArgCheck::registerMatchers(MatchFinder *Finder) { qualType(rValueReferenceType()).bind("invocation-parm-type"); // Matches respective ParmVarDecl for a CallExpr or CXXConstructExpr. auto ArgumentWithParamMatcher = forEachArgumentWithParam( - MoveCallMatcher, parmVarDecl(anyOf(hasType(ConstTypeParmMatcher), + anyOf(MoveCallMatcher, TernaryWithMoveMatcher), + parmVarDecl(anyOf(hasType(ConstTypeParmMatcher), hasType(RValueTypeParmMatcher))) .bind("invocation-parm")); // Matches respective types of arguments for a CallExpr or CXXConstructExpr // and it works on calls through function pointers as well. auto ArgumentWithParamTypeMatcher = forEachArgumentWithParamType( - MoveCallMatcher, anyOf(ConstTypeParmMatcher, RValueTypeParmMatcher)); + anyOf(MoveCallMatcher, TernaryWithMoveMatcher), + anyOf(ConstTypeParmMatcher, RValueTypeParmMatcher)); + Finder->addMatcher( invocation(anyOf(ArgumentWithParamMatcher, ArgumentWithParamTypeMatcher)) diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 41ff1c1016f25..2eb65d61f5e78 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -115,6 +115,10 @@ Changes in existing checks ` check by providing additional examples and fixing some macro related false positives. +- Improved :doc:`performance-move-const-arg + ` check by fixing false negatives + on ternary operators calling ``std::move``. + Removed checks ^^ diff --git a/clang-tools-extra/test/clang-tidy/checkers/performance/move-const-arg.cpp b/clang-tools-extra/test/clang-tidy/checkers/performance/move-const-arg.cpp index 8e325b0ae6ca3..e616cbe78bc3a 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/performance/move-const-arg.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/performance/move-const-arg.cpp @@ -560,3 +560,26 @@ struct Result { // CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: passing result of std::move() as a const reference argument; no move will actually happen [performance-move-const-arg] }; } // namespace GH111450 + +namespace GH126515 { + +struct TernaryMoveCall { +TernaryMoveCall(); +TernaryMoveCall(const TernaryMoveCall&); +TernaryMoveCall operator=(const TernaryMoveCall&); + +void TernaryCheckTriviallyCopyable(const char * c) {} + +void testTernaryMove() { + TernaryMoveCall t1; + TernaryMoveCall other(false ? TernaryMoveCall() : TernaryMoveCall(std::move(t1)) ); + // CHECK-MESSAGES: :[[@LINE-1]]:69: warning: passing result of std::move() as a const reference argument; no move will actually happen [performance-move-const-arg] + // CHECK-MESSAGES: :[[@LINE-11]]:8: note: 'TernaryMoveCall' is not move assignable/constructible + + const char* a = "a"; + TernaryCheckTriviallyCopyable(true ? std::move(a) : "" ); + // CHECK-MESSAGES: :[[@LINE-1]]:40: warning: std::move of the variable 'a' of the trivially-copyable type 'const char *' has no effect; remove std::move() [performance-move-const-arg] +} + +}; +} // namespace GH126515 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Fix performance-move-const-arg false negative in ternary… (PR #128402)
https://github.com/RiverDave updated https://github.com/llvm/llvm-project/pull/128402 >From f5e26951fb93db68ee51d79d6c84eb2194e3bf74 Mon Sep 17 00:00:00 2001 From: Riverdave Date: Sat, 22 Feb 2025 03:57:35 -0500 Subject: [PATCH] [clang-tidy] Fix performance-move-const-arg false negative in ternary operators --- .../performance/MoveConstArgCheck.cpp | 14 +++ clang-tools-extra/docs/ReleaseNotes.rst | 4 .../checkers/performance/move-const-arg.cpp | 23 +++ 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/clang-tools-extra/clang-tidy/performance/MoveConstArgCheck.cpp b/clang-tools-extra/clang-tidy/performance/MoveConstArgCheck.cpp index 421ce003975bc..703ad162f99cf 100644 --- a/clang-tools-extra/clang-tidy/performance/MoveConstArgCheck.cpp +++ b/clang-tools-extra/clang-tidy/performance/MoveConstArgCheck.cpp @@ -44,6 +44,10 @@ void MoveConstArgCheck::registerMatchers(MatchFinder *Finder) { unless(isInTemplateInstantiation())) .bind("call-move"); + // Match ternary expressions where either branch contains std::move + auto TernaryWithMoveMatcher = + conditionalOperator(hasDescendant(MoveCallMatcher)); + Finder->addMatcher( expr(anyOf( castExpr(hasSourceExpression(MoveCallMatcher)), @@ -58,13 +62,15 @@ void MoveConstArgCheck::registerMatchers(MatchFinder *Finder) { qualType(rValueReferenceType()).bind("invocation-parm-type"); // Matches respective ParmVarDecl for a CallExpr or CXXConstructExpr. auto ArgumentWithParamMatcher = forEachArgumentWithParam( - MoveCallMatcher, parmVarDecl(anyOf(hasType(ConstTypeParmMatcher), - hasType(RValueTypeParmMatcher))) - .bind("invocation-parm")); + anyOf(MoveCallMatcher, TernaryWithMoveMatcher), + parmVarDecl( + anyOf(hasType(ConstTypeParmMatcher), hasType(RValueTypeParmMatcher))) + .bind("invocation-parm")); // Matches respective types of arguments for a CallExpr or CXXConstructExpr // and it works on calls through function pointers as well. auto ArgumentWithParamTypeMatcher = forEachArgumentWithParamType( - MoveCallMatcher, anyOf(ConstTypeParmMatcher, RValueTypeParmMatcher)); + anyOf(MoveCallMatcher, TernaryWithMoveMatcher), + anyOf(ConstTypeParmMatcher, RValueTypeParmMatcher)); Finder->addMatcher( invocation(anyOf(ArgumentWithParamMatcher, ArgumentWithParamTypeMatcher)) diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 2dcefa2ddec83..b87ea491b3ad1 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -120,6 +120,10 @@ Changes in existing checks tolerating fix-it breaking compilation when functions is used as pointers to avoid matching usage of functions within the current compilation unit. +- Improved :doc:`performance-move-const-arg + ` check by fixing false negatives + on ternary operators calling ``std::move``. + Removed checks ^^ diff --git a/clang-tools-extra/test/clang-tidy/checkers/performance/move-const-arg.cpp b/clang-tools-extra/test/clang-tidy/checkers/performance/move-const-arg.cpp index 8e325b0ae6ca3..e616cbe78bc3a 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/performance/move-const-arg.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/performance/move-const-arg.cpp @@ -560,3 +560,26 @@ struct Result { // CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: passing result of std::move() as a const reference argument; no move will actually happen [performance-move-const-arg] }; } // namespace GH111450 + +namespace GH126515 { + +struct TernaryMoveCall { +TernaryMoveCall(); +TernaryMoveCall(const TernaryMoveCall&); +TernaryMoveCall operator=(const TernaryMoveCall&); + +void TernaryCheckTriviallyCopyable(const char * c) {} + +void testTernaryMove() { + TernaryMoveCall t1; + TernaryMoveCall other(false ? TernaryMoveCall() : TernaryMoveCall(std::move(t1)) ); + // CHECK-MESSAGES: :[[@LINE-1]]:69: warning: passing result of std::move() as a const reference argument; no move will actually happen [performance-move-const-arg] + // CHECK-MESSAGES: :[[@LINE-11]]:8: note: 'TernaryMoveCall' is not move assignable/constructible + + const char* a = "a"; + TernaryCheckTriviallyCopyable(true ? std::move(a) : "" ); + // CHECK-MESSAGES: :[[@LINE-1]]:40: warning: std::move of the variable 'a' of the trivially-copyable type 'const char *' has no effect; remove std::move() [performance-move-const-arg] +} + +}; +} // namespace GH126515 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Fix performance-move-const-arg false negative in ternary… (PR #128402)
https://github.com/RiverDave edited https://github.com/llvm/llvm-project/pull/128402 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] detect arithmetic operations within member list initialization in modernize-use-default-member-init check (PR #129370)
https://github.com/RiverDave updated https://github.com/llvm/llvm-project/pull/129370 >From 39900908f047c2c067eea93855a5e6f644d13830 Mon Sep 17 00:00:00 2001 From: David Rivera Date: Sat, 1 Mar 2025 02:09:02 -0500 Subject: [PATCH] [clang-tidy] detect arithmetic operations within member list initialization in modernize-use-default-member-init --- .../modernize/UseDefaultMemberInitCheck.cpp | 26 +-- clang-tools-extra/docs/ReleaseNotes.rst | 4 +++ .../modernize/use-default-member-init.cpp | 26 +++ 3 files changed, 48 insertions(+), 8 deletions(-) diff --git a/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp index 6c06b0af342f6..3afc047a02b7b 100644 --- a/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp @@ -159,6 +159,12 @@ static bool sameValue(const Expr *E1, const Expr *E2) { case Stmt::UnaryOperatorClass: return sameValue(cast(E1)->getSubExpr(), cast(E2)->getSubExpr()); + case Stmt::BinaryOperatorClass: { +const auto *BinOp1 = cast(E1); +const auto *BinOp2 = cast(E2); +return sameValue(BinOp1->getLHS(), BinOp2->getLHS()) && + sameValue(BinOp1->getRHS(), BinOp2->getRHS()); + } case Stmt::CharacterLiteralClass: return cast(E1)->getValue() == cast(E2)->getValue(); @@ -194,15 +200,19 @@ void UseDefaultMemberInitCheck::storeOptions( } void UseDefaultMemberInitCheck::registerMatchers(MatchFinder *Finder) { + auto NumericLiteral = anyOf(integerLiteral(), floatLiteral()); + auto UnaryNumericLiteral = unaryOperator(hasAnyOperatorName("+", "-"), + hasUnaryOperand(NumericLiteral)); + auto EnumRef = declRefExpr(to(enumConstantDecl())); + + auto BinaryNumericExpr = binaryOperator( + hasOperands(anyOf(NumericLiteral, EnumRef, binaryOperator()), + anyOf(NumericLiteral, EnumRef, binaryOperator(; + auto InitBase = - anyOf(stringLiteral(), characterLiteral(), integerLiteral(), -unaryOperator(hasAnyOperatorName("+", "-"), - hasUnaryOperand(integerLiteral())), -floatLiteral(), -unaryOperator(hasAnyOperatorName("+", "-"), - hasUnaryOperand(floatLiteral())), -cxxBoolLiteral(), cxxNullPtrLiteralExpr(), implicitValueInitExpr(), -declRefExpr(to(enumConstantDecl(; + anyOf(stringLiteral(), characterLiteral(), NumericLiteral, +UnaryNumericLiteral, cxxBoolLiteral(), cxxNullPtrLiteralExpr(), +implicitValueInitExpr(), EnumRef, BinaryNumericExpr); auto Init = anyOf(initListExpr(anyOf(allOf(initCountIs(1), hasInit(0, InitBase)), diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 07a79d6bbe807..01892f4cde648 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -128,6 +128,10 @@ Changes in existing checks ` check by providing additional examples and fixing some macro related false positives. +- Improved :doc:`modernize-use-default-member-init + ` check by matching arithmetic + operations within member list initialization. + - Improved :doc:`performance/unnecessary-value-param ` check performance by tolerating fix-it breaking compilation when functions is used as pointers diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp index 81c980e0217e6..ff8c80b682bdb 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp @@ -518,3 +518,29 @@ class ArrayBraceInitMultipleValues { }; } // namespace PR63285 + +namespace PR122480 { + +#define ARITHMETIC_MACRO (44 - 2) + +class DefaultMemberInitWithArithmetic { + DefaultMemberInitWithArithmetic() : a{1 + 1}, b{1 + 11 + 123 + 1234}, c{2 + (4 / 2) + 3 + (7 / 11)}, d{ARITHMETIC_MACRO * 2}, e{1.2 + 3.4} {} + // CHECK-MESSAGES: :[[@LINE-1]]:39: warning: member initializer for 'a' is redundant [modernize-use-default-member-init] + // CHECK-FIXES: DefaultMemberInitWithArithmetic() {} + + int a{1 + 1}; + int b; + // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use default member initializer for 'b' [modernize-use-default-member-init] + // CHECK-FIXES: int b{1 + 11 + 123 + 1234}; + int c; + // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use default member initializer for 'c' [modernize-use-default-member-init] + // CHECK-FIXES: int c{2 + (4 / 2) + 3 + (7 / 11)} + int d; + // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use default member initializer for 'd' [modernize-use-default-member-
[clang-tools-extra] [clang-tidy] detect arithmetic operations within member list initialization in modernize-use-default-member-init check (PR #129370)
https://github.com/RiverDave deleted https://github.com/llvm/llvm-project/pull/129370 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] detect arithmetic operations within member list initialization in modernize-use-default-member-init check (PR #129370)
@@ -202,7 +208,13 @@ void UseDefaultMemberInitCheck::registerMatchers(MatchFinder *Finder) { unaryOperator(hasAnyOperatorName("+", "-"), hasUnaryOperand(floatLiteral())), cxxBoolLiteral(), cxxNullPtrLiteralExpr(), implicitValueInitExpr(), -declRefExpr(to(enumConstantDecl(; +declRefExpr(to(enumConstantDecl())), +binaryOperator(hasLHS(anyOf(integerLiteral(), floatLiteral(), RiverDave wrote: Done. I've improved not only my part but think I got rid of some redundancies to make it more readable. There's still some duplicate code like: ```cpp hasOperands(anyOf(NumericLiteral, EnumRef, binaryOperator()), anyOf(NumericLiteral, EnumRef, binaryOperator(; ``` I'm not really sure if there's another way of avoiding that. for the rest let me know if this is the right direction. https://github.com/llvm/llvm-project/pull/129370 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] detect arithmetic operations within member list initialization in modernize-use-default-member-init check (PR #129370)
https://github.com/RiverDave updated https://github.com/llvm/llvm-project/pull/129370 >From e3c5641ee3a398d8fe12c0dcd030ee84667cc86e Mon Sep 17 00:00:00 2001 From: David Rivera Date: Sat, 1 Mar 2025 02:09:02 -0500 Subject: [PATCH] [clang-tidy] detect arithmetic operations within member list initialization in modernize-use-default-member-init --- .../modernize/UseDefaultMemberInitCheck.cpp | 27 +-- clang-tools-extra/docs/ReleaseNotes.rst | 6 - .../modernize/use-default-member-init.cpp | 26 ++ 3 files changed, 50 insertions(+), 9 deletions(-) diff --git a/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp index 6c06b0af342f6..8b0d499a96811 100644 --- a/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp @@ -159,6 +159,13 @@ static bool sameValue(const Expr *E1, const Expr *E2) { case Stmt::UnaryOperatorClass: return sameValue(cast(E1)->getSubExpr(), cast(E2)->getSubExpr()); + case Stmt::BinaryOperatorClass: { +const auto *BinOp1 = cast(E1); +const auto *BinOp2 = cast(E2); +return sameValue(BinOp1->getLHS(), BinOp2->getLHS()) && + sameValue(BinOp1->getRHS(), BinOp2->getRHS()) && + BinOp1->getOpcode() == BinOp2->getOpcode(); + } case Stmt::CharacterLiteralClass: return cast(E1)->getValue() == cast(E2)->getValue(); @@ -194,15 +201,19 @@ void UseDefaultMemberInitCheck::storeOptions( } void UseDefaultMemberInitCheck::registerMatchers(MatchFinder *Finder) { + auto NumericLiteral = anyOf(integerLiteral(), floatLiteral()); + auto UnaryNumericLiteral = unaryOperator(hasAnyOperatorName("+", "-"), + hasUnaryOperand(NumericLiteral)); + auto EnumRef = declRefExpr(to(enumConstantDecl())); + + auto BinaryNumericExpr = binaryOperator( + hasOperands(anyOf(NumericLiteral, EnumRef, binaryOperator()), + anyOf(NumericLiteral, EnumRef, binaryOperator(; + auto InitBase = - anyOf(stringLiteral(), characterLiteral(), integerLiteral(), -unaryOperator(hasAnyOperatorName("+", "-"), - hasUnaryOperand(integerLiteral())), -floatLiteral(), -unaryOperator(hasAnyOperatorName("+", "-"), - hasUnaryOperand(floatLiteral())), -cxxBoolLiteral(), cxxNullPtrLiteralExpr(), implicitValueInitExpr(), -declRefExpr(to(enumConstantDecl(; + anyOf(stringLiteral(), characterLiteral(), NumericLiteral, +UnaryNumericLiteral, cxxBoolLiteral(), cxxNullPtrLiteralExpr(), +implicitValueInitExpr(), EnumRef, BinaryNumericExpr); auto Init = anyOf(initListExpr(anyOf(allOf(initCountIs(1), hasInit(0, InitBase)), diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 951b7f20af4c8..dd46494687eff 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -132,11 +132,15 @@ Changes in existing checks ` check by providing additional examples and fixing some macro related false positives. +- Improved :doc:`modernize-use-default-member-init + ` check by matching + arithmetic operations within member list initialization. + - Improved :doc:`misc-use-internal-linkage ` check by fix false positives for function or variable in header file which contains macro expansion. -- Improved :doc:`performance/unnecessary-value-param +- Improved :doc:`performance-unnecessary-value-param ` check performance by tolerating fix-it breaking compilation when functions is used as pointers to avoid matching usage of functions within the current compilation unit. diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp index 81c980e0217e6..ff8c80b682bdb 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp @@ -518,3 +518,29 @@ class ArrayBraceInitMultipleValues { }; } // namespace PR63285 + +namespace PR122480 { + +#define ARITHMETIC_MACRO (44 - 2) + +class DefaultMemberInitWithArithmetic { + DefaultMemberInitWithArithmetic() : a{1 + 1}, b{1 + 11 + 123 + 1234}, c{2 + (4 / 2) + 3 + (7 / 11)}, d{ARITHMETIC_MACRO * 2}, e{1.2 + 3.4} {} + // CHECK-MESSAGES: :[[@LINE-1]]:39: warning: member initializer for 'a' is redundant [modernize-use-default-member-init] + // CHECK-FIXES: DefaultMemberInitWithArithmetic() {} + + int a{1 + 1}; + int b; + // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use default member initializer for 'b' [modernize-use-default-member-init] + // CHECK-FIXES
[clang-tools-extra] [clang-tidy] detect arithmetic operations within member list initialization in modernize-use-default-member-init check (PR #129370)
@@ -159,6 +159,12 @@ static bool sameValue(const Expr *E1, const Expr *E2) { case Stmt::UnaryOperatorClass: return sameValue(cast(E1)->getSubExpr(), cast(E2)->getSubExpr()); + case Stmt::BinaryOperatorClass: { RiverDave wrote: That's a good edge case I hadn't thought about. yeah we totally need to make sure `BinaryOp` is the same to prevent cases where both expressions are numerically identical with different operands. should be fixed now, let me know if test cases are needed for that. https://github.com/llvm/llvm-project/pull/129370 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] detect arithmetic operations within member list initialization in modernize-use-default-member-init check (PR #129370)
https://github.com/RiverDave updated https://github.com/llvm/llvm-project/pull/129370 >From 236cd9f21bd71b118a79356eb859dda31031bf80 Mon Sep 17 00:00:00 2001 From: David Rivera Date: Sat, 1 Mar 2025 02:09:02 -0500 Subject: [PATCH] [clang-tidy] detect arithmetic operations within member list initialization in modernize-use-default-member-init --- .../modernize/UseDefaultMemberInitCheck.cpp | 27 +-- clang-tools-extra/docs/ReleaseNotes.rst | 6 - .../modernize/use-default-member-init.cpp | 26 ++ 3 files changed, 50 insertions(+), 9 deletions(-) diff --git a/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp index 6c06b0af342f6..6ce6c6af3fbb2 100644 --- a/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp @@ -159,6 +159,13 @@ static bool sameValue(const Expr *E1, const Expr *E2) { case Stmt::UnaryOperatorClass: return sameValue(cast(E1)->getSubExpr(), cast(E2)->getSubExpr()); + case Stmt::BinaryOperatorClass: { +const auto *BinOp1 = cast(E1); +const auto *BinOp2 = cast(E2); +return BinOp1->getOpcode() == BinOp2->getOpcode() && + sameValue(BinOp1->getLHS(), BinOp2->getLHS()) && + sameValue(BinOp1->getRHS(), BinOp2->getRHS()); + } case Stmt::CharacterLiteralClass: return cast(E1)->getValue() == cast(E2)->getValue(); @@ -194,15 +201,19 @@ void UseDefaultMemberInitCheck::storeOptions( } void UseDefaultMemberInitCheck::registerMatchers(MatchFinder *Finder) { + auto NumericLiteral = anyOf(integerLiteral(), floatLiteral()); + auto UnaryNumericLiteral = unaryOperator(hasAnyOperatorName("+", "-"), + hasUnaryOperand(NumericLiteral)); + auto EnumRef = declRefExpr(to(enumConstantDecl())); + + auto BinaryNumericExpr = binaryOperator( + hasOperands(anyOf(NumericLiteral, EnumRef, binaryOperator()), + anyOf(NumericLiteral, EnumRef, binaryOperator(; + auto InitBase = - anyOf(stringLiteral(), characterLiteral(), integerLiteral(), -unaryOperator(hasAnyOperatorName("+", "-"), - hasUnaryOperand(integerLiteral())), -floatLiteral(), -unaryOperator(hasAnyOperatorName("+", "-"), - hasUnaryOperand(floatLiteral())), -cxxBoolLiteral(), cxxNullPtrLiteralExpr(), implicitValueInitExpr(), -declRefExpr(to(enumConstantDecl(; + anyOf(stringLiteral(), characterLiteral(), NumericLiteral, +UnaryNumericLiteral, cxxBoolLiteral(), cxxNullPtrLiteralExpr(), +implicitValueInitExpr(), EnumRef, BinaryNumericExpr); auto Init = anyOf(initListExpr(anyOf(allOf(initCountIs(1), hasInit(0, InitBase)), diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 951b7f20af4c8..dd46494687eff 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -132,11 +132,15 @@ Changes in existing checks ` check by providing additional examples and fixing some macro related false positives. +- Improved :doc:`modernize-use-default-member-init + ` check by matching + arithmetic operations within member list initialization. + - Improved :doc:`misc-use-internal-linkage ` check by fix false positives for function or variable in header file which contains macro expansion. -- Improved :doc:`performance/unnecessary-value-param +- Improved :doc:`performance-unnecessary-value-param ` check performance by tolerating fix-it breaking compilation when functions is used as pointers to avoid matching usage of functions within the current compilation unit. diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp index 81c980e0217e6..ff8c80b682bdb 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp @@ -518,3 +518,29 @@ class ArrayBraceInitMultipleValues { }; } // namespace PR63285 + +namespace PR122480 { + +#define ARITHMETIC_MACRO (44 - 2) + +class DefaultMemberInitWithArithmetic { + DefaultMemberInitWithArithmetic() : a{1 + 1}, b{1 + 11 + 123 + 1234}, c{2 + (4 / 2) + 3 + (7 / 11)}, d{ARITHMETIC_MACRO * 2}, e{1.2 + 3.4} {} + // CHECK-MESSAGES: :[[@LINE-1]]:39: warning: member initializer for 'a' is redundant [modernize-use-default-member-init] + // CHECK-FIXES: DefaultMemberInitWithArithmetic() {} + + int a{1 + 1}; + int b; + // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use default member initializer for 'b' [modernize-use-default-member-init] + // CHECK-FIXES
[clang-tools-extra] [clang-tidy] detect arithmetic operations within member list initialization in modernize-use-default-member-init check (PR #129370)
https://github.com/RiverDave updated https://github.com/llvm/llvm-project/pull/129370 >From 64cec5f09ff2a9b2cb3811d52c66fabae90887be Mon Sep 17 00:00:00 2001 From: David Rivera Date: Sat, 1 Mar 2025 02:09:02 -0500 Subject: [PATCH] [clang-tidy] detect arithmetic operations within member list initialization in modernize-use-default-member-init --- .../modernize/UseDefaultMemberInitCheck.cpp | 26 +-- clang-tools-extra/docs/ReleaseNotes.rst | 6 - .../modernize/use-default-member-init.cpp | 26 +++ 3 files changed, 49 insertions(+), 9 deletions(-) diff --git a/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp index 6c06b0af342f6..ab6946588991e 100644 --- a/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp @@ -159,6 +159,12 @@ static bool sameValue(const Expr *E1, const Expr *E2) { case Stmt::UnaryOperatorClass: return sameValue(cast(E1)->getSubExpr(), cast(E2)->getSubExpr()); + case Stmt::BinaryOperatorClass: { +const auto *BinOp1 = cast(E1); +const auto *BinOp2 = cast(E2); +return sameValue(BinOp1->getLHS(), BinOp2->getLHS()) && + sameValue(BinOp1->getRHS(), BinOp2->getRHS()) && BinOp1->getOpcode() == BinOp2->getOpcode(); + } case Stmt::CharacterLiteralClass: return cast(E1)->getValue() == cast(E2)->getValue(); @@ -194,15 +200,19 @@ void UseDefaultMemberInitCheck::storeOptions( } void UseDefaultMemberInitCheck::registerMatchers(MatchFinder *Finder) { + auto NumericLiteral = anyOf(integerLiteral(), floatLiteral()); + auto UnaryNumericLiteral = unaryOperator(hasAnyOperatorName("+", "-"), + hasUnaryOperand(NumericLiteral)); + auto EnumRef = declRefExpr(to(enumConstantDecl())); + + auto BinaryNumericExpr = binaryOperator( + hasOperands(anyOf(NumericLiteral, EnumRef, binaryOperator()), + anyOf(NumericLiteral, EnumRef, binaryOperator(; + auto InitBase = - anyOf(stringLiteral(), characterLiteral(), integerLiteral(), -unaryOperator(hasAnyOperatorName("+", "-"), - hasUnaryOperand(integerLiteral())), -floatLiteral(), -unaryOperator(hasAnyOperatorName("+", "-"), - hasUnaryOperand(floatLiteral())), -cxxBoolLiteral(), cxxNullPtrLiteralExpr(), implicitValueInitExpr(), -declRefExpr(to(enumConstantDecl(; + anyOf(stringLiteral(), characterLiteral(), NumericLiteral, +UnaryNumericLiteral, cxxBoolLiteral(), cxxNullPtrLiteralExpr(), +implicitValueInitExpr(), EnumRef, BinaryNumericExpr); auto Init = anyOf(initListExpr(anyOf(allOf(initCountIs(1), hasInit(0, InitBase)), diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 951b7f20af4c8..dd46494687eff 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -132,11 +132,15 @@ Changes in existing checks ` check by providing additional examples and fixing some macro related false positives. +- Improved :doc:`modernize-use-default-member-init + ` check by matching + arithmetic operations within member list initialization. + - Improved :doc:`misc-use-internal-linkage ` check by fix false positives for function or variable in header file which contains macro expansion. -- Improved :doc:`performance/unnecessary-value-param +- Improved :doc:`performance-unnecessary-value-param ` check performance by tolerating fix-it breaking compilation when functions is used as pointers to avoid matching usage of functions within the current compilation unit. diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp index 81c980e0217e6..ff8c80b682bdb 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp @@ -518,3 +518,29 @@ class ArrayBraceInitMultipleValues { }; } // namespace PR63285 + +namespace PR122480 { + +#define ARITHMETIC_MACRO (44 - 2) + +class DefaultMemberInitWithArithmetic { + DefaultMemberInitWithArithmetic() : a{1 + 1}, b{1 + 11 + 123 + 1234}, c{2 + (4 / 2) + 3 + (7 / 11)}, d{ARITHMETIC_MACRO * 2}, e{1.2 + 3.4} {} + // CHECK-MESSAGES: :[[@LINE-1]]:39: warning: member initializer for 'a' is redundant [modernize-use-default-member-init] + // CHECK-FIXES: DefaultMemberInitWithArithmetic() {} + + int a{1 + 1}; + int b; + // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use default member initializer for 'b' [modernize-use-default-member-init] + // CHECK-FIXES: int b{1
[clang-tools-extra] [clang-tidy] Add check on constexpr & static values in modernize-use-default-member-init (PR #129425)
RiverDave wrote: Ping https://github.com/llvm/llvm-project/pull/129425 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] detect arithmetic operations within member list initialization in modernize-use-default-member-init check (PR #129370)
https://github.com/RiverDave edited https://github.com/llvm/llvm-project/pull/129370 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Add check on constexpr & static values in modernize-use-default-member-init (PR #129425)
https://github.com/RiverDave updated https://github.com/llvm/llvm-project/pull/129425 >From 4f25319a076bddd11de886dbb35693cbfd2a3a73 Mon Sep 17 00:00:00 2001 From: David Rivera Date: Sun, 2 Mar 2025 01:12:05 -0500 Subject: [PATCH] [clang-tidy] Add check on constexpr & static values on member initialization in modernize-use-default-member-init --- .../modernize/UseDefaultMemberInitCheck.cpp | 4 +++- clang-tools-extra/docs/ReleaseNotes.rst | 4 .../modernize/use-default-member-init.cpp | 19 +++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp index 6c06b0af342f6..5b0b9b59d4e3b 100644 --- a/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp @@ -194,6 +194,8 @@ void UseDefaultMemberInitCheck::storeOptions( } void UseDefaultMemberInitCheck::registerMatchers(MatchFinder *Finder) { + auto ConstExpRef = varDecl(anyOf(isConstexpr(), isStaticStorageClass())); + auto InitBase = anyOf(stringLiteral(), characterLiteral(), integerLiteral(), unaryOperator(hasAnyOperatorName("+", "-"), @@ -202,7 +204,7 @@ void UseDefaultMemberInitCheck::registerMatchers(MatchFinder *Finder) { unaryOperator(hasAnyOperatorName("+", "-"), hasUnaryOperand(floatLiteral())), cxxBoolLiteral(), cxxNullPtrLiteralExpr(), implicitValueInitExpr(), -declRefExpr(to(enumConstantDecl(; +declRefExpr(to(anyOf(enumConstantDecl(), ConstExpRef; auto Init = anyOf(initListExpr(anyOf(allOf(initCountIs(1), hasInit(0, InitBase)), diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 951b7f20af4c8..ccd0856c905e0 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -136,6 +136,10 @@ Changes in existing checks ` check by fix false positives for function or variable in header file which contains macro expansion. +- Improved :doc:`modernize-use-default-member-init + ` check by matching + ``constexpr`` and ``static`` values on member initialization. + - Improved :doc:`performance/unnecessary-value-param ` check performance by tolerating fix-it breaking compilation when functions is used as pointers diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp index 81c980e0217e6..e97c521832e81 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp @@ -518,3 +518,22 @@ class ArrayBraceInitMultipleValues { }; } // namespace PR63285 + +namespace PR122480 { + + static int STATIC_VAL = 23; + static constexpr const char* CONSTEXPR_REF = "Static"; + + class StaticConstExprInit { + +StaticConstExprInit() : a{CONSTEXPR_REF}, b{STATIC_VAL}{} +// CHECK-FIXES: StaticConstExprInit() {} +const char* a; +// CHECK-MESSAGES: :[[@LINE-1]]:17: warning: use default member initializer for 'a' [modernize-use-default-member-init] +// CHECK-FIXES: const char* a{CONSTEXPR_REF}; +int b; +// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use default member initializer for 'b' [modernize-use-default-member-init] +// CHECK-FIXES: int b{STATIC_VAL}; + }; + +} //namespace PR122480 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] detect arithmetic operations within member list initialization in modernize-use-default-member-init check (PR #129370)
RiverDave wrote: Ping https://github.com/llvm/llvm-project/pull/129370 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] detect explicit casting within modernize-use-default-member-init (PR #129408)
RiverDave wrote: ping https://github.com/llvm/llvm-project/pull/129408 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] detect arithmetic operations within member list initialization in modernize-use-default-member-init check (PR #129370)
@@ -132,11 +132,15 @@ Changes in existing checks ` check by providing additional examples and fixing some macro related false positives. +- Improved :doc:`modernize-use-default-member-init RiverDave wrote: Done https://github.com/llvm/llvm-project/pull/129370 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] detect arithmetic operations within member list initialization in modernize-use-default-member-init check (PR #129370)
https://github.com/RiverDave updated https://github.com/llvm/llvm-project/pull/129370 >From 08049760b837d026fe9a69b8b6019c6d2bed06b1 Mon Sep 17 00:00:00 2001 From: David Rivera Date: Sat, 1 Mar 2025 02:09:02 -0500 Subject: [PATCH] [clang-tidy] detect arithmetic operations within member list initialization in modernize-use-default-member-init --- .../modernize/UseDefaultMemberInitCheck.cpp | 27 +-- clang-tools-extra/docs/ReleaseNotes.rst | 6 - .../modernize/use-default-member-init.cpp | 26 ++ 3 files changed, 50 insertions(+), 9 deletions(-) diff --git a/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp index 6c06b0af342f6..6ce6c6af3fbb2 100644 --- a/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp @@ -159,6 +159,13 @@ static bool sameValue(const Expr *E1, const Expr *E2) { case Stmt::UnaryOperatorClass: return sameValue(cast(E1)->getSubExpr(), cast(E2)->getSubExpr()); + case Stmt::BinaryOperatorClass: { +const auto *BinOp1 = cast(E1); +const auto *BinOp2 = cast(E2); +return BinOp1->getOpcode() == BinOp2->getOpcode() && + sameValue(BinOp1->getLHS(), BinOp2->getLHS()) && + sameValue(BinOp1->getRHS(), BinOp2->getRHS()); + } case Stmt::CharacterLiteralClass: return cast(E1)->getValue() == cast(E2)->getValue(); @@ -194,15 +201,19 @@ void UseDefaultMemberInitCheck::storeOptions( } void UseDefaultMemberInitCheck::registerMatchers(MatchFinder *Finder) { + auto NumericLiteral = anyOf(integerLiteral(), floatLiteral()); + auto UnaryNumericLiteral = unaryOperator(hasAnyOperatorName("+", "-"), + hasUnaryOperand(NumericLiteral)); + auto EnumRef = declRefExpr(to(enumConstantDecl())); + + auto BinaryNumericExpr = binaryOperator( + hasOperands(anyOf(NumericLiteral, EnumRef, binaryOperator()), + anyOf(NumericLiteral, EnumRef, binaryOperator(; + auto InitBase = - anyOf(stringLiteral(), characterLiteral(), integerLiteral(), -unaryOperator(hasAnyOperatorName("+", "-"), - hasUnaryOperand(integerLiteral())), -floatLiteral(), -unaryOperator(hasAnyOperatorName("+", "-"), - hasUnaryOperand(floatLiteral())), -cxxBoolLiteral(), cxxNullPtrLiteralExpr(), implicitValueInitExpr(), -declRefExpr(to(enumConstantDecl(; + anyOf(stringLiteral(), characterLiteral(), NumericLiteral, +UnaryNumericLiteral, cxxBoolLiteral(), cxxNullPtrLiteralExpr(), +implicitValueInitExpr(), EnumRef, BinaryNumericExpr); auto Init = anyOf(initListExpr(anyOf(allOf(initCountIs(1), hasInit(0, InitBase)), diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 951b7f20af4c8..66d2d63eb079b 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -136,7 +136,11 @@ Changes in existing checks ` check by fix false positives for function or variable in header file which contains macro expansion. -- Improved :doc:`performance/unnecessary-value-param +- Improved :doc:`modernize-use-default-member-init + ` check by matching + arithmetic operations within member list initialization. + +- Improved :doc:`performance-unnecessary-value-param ` check performance by tolerating fix-it breaking compilation when functions is used as pointers to avoid matching usage of functions within the current compilation unit. diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp index 81c980e0217e6..ff8c80b682bdb 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp @@ -518,3 +518,29 @@ class ArrayBraceInitMultipleValues { }; } // namespace PR63285 + +namespace PR122480 { + +#define ARITHMETIC_MACRO (44 - 2) + +class DefaultMemberInitWithArithmetic { + DefaultMemberInitWithArithmetic() : a{1 + 1}, b{1 + 11 + 123 + 1234}, c{2 + (4 / 2) + 3 + (7 / 11)}, d{ARITHMETIC_MACRO * 2}, e{1.2 + 3.4} {} + // CHECK-MESSAGES: :[[@LINE-1]]:39: warning: member initializer for 'a' is redundant [modernize-use-default-member-init] + // CHECK-FIXES: DefaultMemberInitWithArithmetic() {} + + int a{1 + 1}; + int b; + // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use default member initializer for 'b' [modernize-use-default-member-init] + // CHECK-FIXES: int b{1 + 11 + 123 + 1234}; + int c; + // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use default member initializer for 'c' [modernize-us
[clang-tools-extra] [clang-tidy] detect arithmetic operations within member list initialization in modernize-use-default-member-init check (PR #129370)
RiverDave wrote: > LGTM, but please address a nit Should be done now, thanks! https://github.com/llvm/llvm-project/pull/129370 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Add check on constexpr & static values in modernize-use-default-member-init (PR #129425)
RiverDave wrote: > @RiverDave Unless you want this to be merged using github generated private > email, change your email privacy settings. My email should be public now https://github.com/llvm/llvm-project/pull/129425 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] detect explicit casting within modernize-use-default-member-init (PR #129408)
RiverDave wrote: > Change looks correct, but looks like this check got one more limitation in > case of code like this: > > ``` > class CastInit { > CastInit() : m(static_cast(9)) {} > int m = 9; > }; > ``` > > And this: > > ``` > class CastInit { > CastInit() : m(static_cast(0)) {} > int m {}; > }; > ``` > Change looks correct, but looks like this check got one more limitation in > case of code like this: > > ``` > class CastInit { > CastInit() : m(static_cast(9)) {} > int m = 9; > }; > ``` > > And this: > > ``` > class CastInit { > CastInit() : m(static_cast(0)) {} > int m {}; > }; > ``` Hadn't thought of this, but your idea makes sense, I'll try to implement it. https://github.com/llvm/llvm-project/pull/129408 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] detect explicit casting within modernize-use-default-member-init (PR #129408)
https://github.com/RiverDave edited https://github.com/llvm/llvm-project/pull/129408 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] detect explicit casting within modernize-use-default-member-init (PR #129408)
https://github.com/RiverDave edited https://github.com/llvm/llvm-project/pull/129408 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Fix performance-move-const-arg false negative in ternary… (PR #128402)
RiverDave wrote: > ⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️ > > You can test this locally with the following command: > ```shell > git-clang-format --diff 5d501c6137976ff1f14f3b0e2e593fb9740d0146 > 848be2ccd25fd68b6a2d2037198184b08ff5d6e2 --extensions cpp -- > clang-tools-extra/clang-tidy/performance/MoveConstArgCheck.cpp > clang-tools-extra/test/clang-tidy/checkers/performance/move-const-arg.cpp > ``` > > View the diff from clang-format here. > ```diff > diff --git a/clang-tools-extra/clang-tidy/performance/MoveConstArgCheck.cpp > b/clang-tools-extra/clang-tidy/performance/MoveConstArgCheck.cpp > index 553c1d20cb..703ad162f9 100644 > --- a/clang-tools-extra/clang-tidy/performance/MoveConstArgCheck.cpp > +++ b/clang-tools-extra/clang-tidy/performance/MoveConstArgCheck.cpp > @@ -63,16 +63,15 @@ void MoveConstArgCheck::registerMatchers(MatchFinder > *Finder) { >// Matches respective ParmVarDecl for a CallExpr or CXXConstructExpr. >auto ArgumentWithParamMatcher = forEachArgumentWithParam( >anyOf(MoveCallMatcher, TernaryWithMoveMatcher), > - parmVarDecl(anyOf(hasType(ConstTypeParmMatcher), > - hasType(RValueTypeParmMatcher))) > - .bind("invocation-parm")); > + parmVarDecl( > + anyOf(hasType(ConstTypeParmMatcher), > hasType(RValueTypeParmMatcher))) > + .bind("invocation-parm")); >// Matches respective types of arguments for a CallExpr or CXXConstructExpr >// and it works on calls through function pointers as well. >auto ArgumentWithParamTypeMatcher = forEachArgumentWithParamType( >anyOf(MoveCallMatcher, TernaryWithMoveMatcher), >anyOf(ConstTypeParmMatcher, RValueTypeParmMatcher)); > > - >Finder->addMatcher( >invocation(anyOf(ArgumentWithParamMatcher, > ArgumentWithParamTypeMatcher)) >.bind("receiving-expr"), > ``` This should be fixed now https://github.com/llvm/llvm-project/pull/128402 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] detect arithmetic operations within member list initialization in modernize-use-default-member-init check (PR #129370)
https://github.com/RiverDave created https://github.com/llvm/llvm-project/pull/129370 This aims to address a portion of #122480 by adding matchers on binary operators. **This allows explicit arithmetic operations within initializers.** ### Note I'm aware of the other false-negatives presented in the issue above, specifically not matching explicit castings & constexpr values. These will be addressed separately on a different PR which is in the process and shouldn't take long (let me know if that's the right approach). Feedback is much appreciated. >From 60856b908e2b52cc512445d7d31e7fc72cd124cb Mon Sep 17 00:00:00 2001 From: David Rivera Date: Sat, 1 Mar 2025 02:09:02 -0500 Subject: [PATCH] [clang-tidy] detect arithmetic operations within member list initialization in modernize-use-default-member-init --- .../modernize/UseDefaultMemberInitCheck.cpp | 13 +- clang-tools-extra/docs/ReleaseNotes.rst | 4 +++ .../modernize/use-default-member-init.cpp | 26 +++ 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp index 6c06b0af342f6..a9d9e8b66ea7c 100644 --- a/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp @@ -159,6 +159,12 @@ static bool sameValue(const Expr *E1, const Expr *E2) { case Stmt::UnaryOperatorClass: return sameValue(cast(E1)->getSubExpr(), cast(E2)->getSubExpr()); + case Stmt::BinaryOperatorClass: { +const auto *BinOp1 = cast(E1); +const auto *BinOp2 = cast(E2); +return sameValue(BinOp1->getLHS(), BinOp2->getLHS()) && +sameValue(BinOp1->getRHS(), BinOp2->getRHS()); + } case Stmt::CharacterLiteralClass: return cast(E1)->getValue() == cast(E2)->getValue(); @@ -202,7 +208,12 @@ void UseDefaultMemberInitCheck::registerMatchers(MatchFinder *Finder) { unaryOperator(hasAnyOperatorName("+", "-"), hasUnaryOperand(floatLiteral())), cxxBoolLiteral(), cxxNullPtrLiteralExpr(), implicitValueInitExpr(), -declRefExpr(to(enumConstantDecl(; +declRefExpr(to(enumConstantDecl())), +binaryOperator( +hasLHS(anyOf(integerLiteral(), floatLiteral(), + declRefExpr(to(enumConstantDecl())), binaryOperator())), +hasRHS(anyOf(integerLiteral(), floatLiteral(), + declRefExpr(to(enumConstantDecl())), binaryOperator(); auto Init = anyOf(initListExpr(anyOf(allOf(initCountIs(1), hasInit(0, InitBase)), diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 07a79d6bbe807..6d909f9d7c6bd 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -137,6 +137,10 @@ Changes in existing checks ` check by fixing false negatives on ternary operators calling ``std::move``. +- Improved :doc:`modernize-use-default-member-init + ` check by matching arithmetic + operations within member list initialization. + Removed checks ^^ diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp index 81c980e0217e6..ff8c80b682bdb 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp @@ -518,3 +518,29 @@ class ArrayBraceInitMultipleValues { }; } // namespace PR63285 + +namespace PR122480 { + +#define ARITHMETIC_MACRO (44 - 2) + +class DefaultMemberInitWithArithmetic { + DefaultMemberInitWithArithmetic() : a{1 + 1}, b{1 + 11 + 123 + 1234}, c{2 + (4 / 2) + 3 + (7 / 11)}, d{ARITHMETIC_MACRO * 2}, e{1.2 + 3.4} {} + // CHECK-MESSAGES: :[[@LINE-1]]:39: warning: member initializer for 'a' is redundant [modernize-use-default-member-init] + // CHECK-FIXES: DefaultMemberInitWithArithmetic() {} + + int a{1 + 1}; + int b; + // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use default member initializer for 'b' [modernize-use-default-member-init] + // CHECK-FIXES: int b{1 + 11 + 123 + 1234}; + int c; + // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use default member initializer for 'c' [modernize-use-default-member-init] + // CHECK-FIXES: int c{2 + (4 / 2) + 3 + (7 / 11)} + int d; + // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use default member initializer for 'd' [modernize-use-default-member-init] + // CHECK-FIXES: int d{ARITHMETIC_MACRO * 2}; + double e; + // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use default member initializer for 'e' [modernize-use-default-member-init] + // CHECK-FIXES: double e{1.2 + 3.4}; + +}; +} // n
[clang-tools-extra] [clang-tidy] detect arithmetic operations within member list initialization in modernize-use-default-member-init check (PR #129370)
https://github.com/RiverDave edited https://github.com/llvm/llvm-project/pull/129370 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] detect arithmetic operations within member list initialization in modernize-use-default-member-init check (PR #129370)
https://github.com/RiverDave edited https://github.com/llvm/llvm-project/pull/129370 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] detect arithmetic operations within member list initialization in modernize-use-default-member-init check (PR #129370)
https://github.com/RiverDave updated https://github.com/llvm/llvm-project/pull/129370 >From 68c371939323c9be13dba2e8e80ac4138859d845 Mon Sep 17 00:00:00 2001 From: David Rivera Date: Sat, 1 Mar 2025 02:09:02 -0500 Subject: [PATCH] [clang-tidy] detect arithmetic operations within member list initialization in modernize-use-default-member-init --- .../modernize/UseDefaultMemberInitCheck.cpp | 14 +- clang-tools-extra/docs/ReleaseNotes.rst | 4 +++ .../modernize/use-default-member-init.cpp | 26 +++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp index 6c06b0af342f6..d155207d2a3ea 100644 --- a/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp @@ -159,6 +159,12 @@ static bool sameValue(const Expr *E1, const Expr *E2) { case Stmt::UnaryOperatorClass: return sameValue(cast(E1)->getSubExpr(), cast(E2)->getSubExpr()); + case Stmt::BinaryOperatorClass: { +const auto *BinOp1 = cast(E1); +const auto *BinOp2 = cast(E2); +return sameValue(BinOp1->getLHS(), BinOp2->getLHS()) && + sameValue(BinOp1->getRHS(), BinOp2->getRHS()); + } case Stmt::CharacterLiteralClass: return cast(E1)->getValue() == cast(E2)->getValue(); @@ -202,7 +208,13 @@ void UseDefaultMemberInitCheck::registerMatchers(MatchFinder *Finder) { unaryOperator(hasAnyOperatorName("+", "-"), hasUnaryOperand(floatLiteral())), cxxBoolLiteral(), cxxNullPtrLiteralExpr(), implicitValueInitExpr(), -declRefExpr(to(enumConstantDecl(; +declRefExpr(to(enumConstantDecl())), +binaryOperator(hasLHS(anyOf(integerLiteral(), floatLiteral(), +declRefExpr(to(enumConstantDecl())), +binaryOperator())), + hasRHS(anyOf(integerLiteral(), floatLiteral(), +declRefExpr(to(enumConstantDecl())), +binaryOperator(); auto Init = anyOf(initListExpr(anyOf(allOf(initCountIs(1), hasInit(0, InitBase)), diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 07a79d6bbe807..6d909f9d7c6bd 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -137,6 +137,10 @@ Changes in existing checks ` check by fixing false negatives on ternary operators calling ``std::move``. +- Improved :doc:`modernize-use-default-member-init + ` check by matching arithmetic + operations within member list initialization. + Removed checks ^^ diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp index 81c980e0217e6..ff8c80b682bdb 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp @@ -518,3 +518,29 @@ class ArrayBraceInitMultipleValues { }; } // namespace PR63285 + +namespace PR122480 { + +#define ARITHMETIC_MACRO (44 - 2) + +class DefaultMemberInitWithArithmetic { + DefaultMemberInitWithArithmetic() : a{1 + 1}, b{1 + 11 + 123 + 1234}, c{2 + (4 / 2) + 3 + (7 / 11)}, d{ARITHMETIC_MACRO * 2}, e{1.2 + 3.4} {} + // CHECK-MESSAGES: :[[@LINE-1]]:39: warning: member initializer for 'a' is redundant [modernize-use-default-member-init] + // CHECK-FIXES: DefaultMemberInitWithArithmetic() {} + + int a{1 + 1}; + int b; + // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use default member initializer for 'b' [modernize-use-default-member-init] + // CHECK-FIXES: int b{1 + 11 + 123 + 1234}; + int c; + // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use default member initializer for 'c' [modernize-use-default-member-init] + // CHECK-FIXES: int c{2 + (4 / 2) + 3 + (7 / 11)} + int d; + // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use default member initializer for 'd' [modernize-use-default-member-init] + // CHECK-FIXES: int d{ARITHMETIC_MACRO * 2}; + double e; + // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use default member initializer for 'e' [modernize-use-default-member-init] + // CHECK-FIXES: double e{1.2 + 3.4}; + +}; +} // namespace PR122480 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] detect arithmetic operations within member list initialization in modernize-use-default-member-init check (PR #129370)
https://github.com/RiverDave updated https://github.com/llvm/llvm-project/pull/129370 >From 6dff411516cb4c0e807307fd36d15955e805d5c4 Mon Sep 17 00:00:00 2001 From: David Rivera Date: Sat, 1 Mar 2025 02:09:02 -0500 Subject: [PATCH] [clang-tidy] detect arithmetic operations within member list initialization in modernize-use-default-member-init --- .../modernize/UseDefaultMemberInitCheck.cpp | 23 ++-- clang-tools-extra/docs/ReleaseNotes.rst | 4 +++ .../modernize/use-default-member-init.cpp | 26 +++ 3 files changed, 45 insertions(+), 8 deletions(-) diff --git a/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp index 6c06b0af342f6..d01f440fe7e16 100644 --- a/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp @@ -159,6 +159,12 @@ static bool sameValue(const Expr *E1, const Expr *E2) { case Stmt::UnaryOperatorClass: return sameValue(cast(E1)->getSubExpr(), cast(E2)->getSubExpr()); + case Stmt::BinaryOperatorClass: { +const auto *BinOp1 = cast(E1); +const auto *BinOp2 = cast(E2); +return sameValue(BinOp1->getLHS(), BinOp2->getLHS()) && + sameValue(BinOp1->getRHS(), BinOp2->getRHS()); + } case Stmt::CharacterLiteralClass: return cast(E1)->getValue() == cast(E2)->getValue(); @@ -194,15 +200,16 @@ void UseDefaultMemberInitCheck::storeOptions( } void UseDefaultMemberInitCheck::registerMatchers(MatchFinder *Finder) { + auto NumericLiteral = anyOf(integerLiteral(), floatLiteral()); + auto UnaryNumericLiteral = unaryOperator(hasAnyOperatorName("+", "-"), hasUnaryOperand(NumericLiteral)); + auto EnumRef = declRefExpr(to(enumConstantDecl())); + auto InitBase = - anyOf(stringLiteral(), characterLiteral(), integerLiteral(), -unaryOperator(hasAnyOperatorName("+", "-"), - hasUnaryOperand(integerLiteral())), -floatLiteral(), -unaryOperator(hasAnyOperatorName("+", "-"), - hasUnaryOperand(floatLiteral())), -cxxBoolLiteral(), cxxNullPtrLiteralExpr(), implicitValueInitExpr(), -declRefExpr(to(enumConstantDecl(; + anyOf(stringLiteral(), characterLiteral(), NumericLiteral, UnaryNumericLiteral, +cxxBoolLiteral(), cxxNullPtrLiteralExpr(), implicitValueInitExpr(), EnumRef, +binaryOperator(hasLHS(anyOf(NumericLiteral, EnumRef, binaryOperator())), + hasRHS(anyOf(NumericLiteral, EnumRef, binaryOperator(); + auto Init = anyOf(initListExpr(anyOf(allOf(initCountIs(1), hasInit(0, InitBase)), diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 07a79d6bbe807..01892f4cde648 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -128,6 +128,10 @@ Changes in existing checks ` check by providing additional examples and fixing some macro related false positives. +- Improved :doc:`modernize-use-default-member-init + ` check by matching arithmetic + operations within member list initialization. + - Improved :doc:`performance/unnecessary-value-param ` check performance by tolerating fix-it breaking compilation when functions is used as pointers diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp index 81c980e0217e6..ff8c80b682bdb 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp @@ -518,3 +518,29 @@ class ArrayBraceInitMultipleValues { }; } // namespace PR63285 + +namespace PR122480 { + +#define ARITHMETIC_MACRO (44 - 2) + +class DefaultMemberInitWithArithmetic { + DefaultMemberInitWithArithmetic() : a{1 + 1}, b{1 + 11 + 123 + 1234}, c{2 + (4 / 2) + 3 + (7 / 11)}, d{ARITHMETIC_MACRO * 2}, e{1.2 + 3.4} {} + // CHECK-MESSAGES: :[[@LINE-1]]:39: warning: member initializer for 'a' is redundant [modernize-use-default-member-init] + // CHECK-FIXES: DefaultMemberInitWithArithmetic() {} + + int a{1 + 1}; + int b; + // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use default member initializer for 'b' [modernize-use-default-member-init] + // CHECK-FIXES: int b{1 + 11 + 123 + 1234}; + int c; + // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use default member initializer for 'c' [modernize-use-default-member-init] + // CHECK-FIXES: int c{2 + (4 / 2) + 3 + (7 / 11)} + int d; + // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use default member initializer for 'd' [modernize-use-default-member-init] + // CHECK-FIXES: int d{ARITHMETIC_MACRO * 2}; + double e; + // CHECK-MESSAG
[clang-tools-extra] [clang-tidy] detect arithmetic operations within member list initialization in modernize-use-default-member-init check (PR #129370)
@@ -137,6 +137,10 @@ Changes in existing checks ` check by fixing false negatives on ternary operators calling ``std::move``. +- Improved :doc:`modernize-use-default-member-init RiverDave wrote: Thx for pointing out, It's fixed now. https://github.com/llvm/llvm-project/pull/129370 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] detect arithmetic operations within member list initialization in modernize-use-default-member-init check (PR #129370)
https://github.com/RiverDave updated https://github.com/llvm/llvm-project/pull/129370 >From be9f035f738c8ea2771c9e79d4c22ff0fa82542e Mon Sep 17 00:00:00 2001 From: David Rivera Date: Sat, 1 Mar 2025 02:09:02 -0500 Subject: [PATCH] [clang-tidy] detect arithmetic operations within member list initialization in modernize-use-default-member-init --- .../modernize/UseDefaultMemberInitCheck.cpp | 26 --- clang-tools-extra/docs/ReleaseNotes.rst | 4 +++ .../modernize/use-default-member-init.cpp | 26 +++ 3 files changed, 47 insertions(+), 9 deletions(-) diff --git a/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp index 6c06b0af342f6..639b594abaf30 100644 --- a/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp @@ -159,6 +159,12 @@ static bool sameValue(const Expr *E1, const Expr *E2) { case Stmt::UnaryOperatorClass: return sameValue(cast(E1)->getSubExpr(), cast(E2)->getSubExpr()); + case Stmt::BinaryOperatorClass: { +const auto *BinOp1 = cast(E1); +const auto *BinOp2 = cast(E2); +return sameValue(BinOp1->getLHS(), BinOp2->getLHS()) && + sameValue(BinOp1->getRHS(), BinOp2->getRHS()); + } case Stmt::CharacterLiteralClass: return cast(E1)->getValue() == cast(E2)->getValue(); @@ -194,15 +200,17 @@ void UseDefaultMemberInitCheck::storeOptions( } void UseDefaultMemberInitCheck::registerMatchers(MatchFinder *Finder) { - auto InitBase = - anyOf(stringLiteral(), characterLiteral(), integerLiteral(), -unaryOperator(hasAnyOperatorName("+", "-"), - hasUnaryOperand(integerLiteral())), -floatLiteral(), -unaryOperator(hasAnyOperatorName("+", "-"), - hasUnaryOperand(floatLiteral())), -cxxBoolLiteral(), cxxNullPtrLiteralExpr(), implicitValueInitExpr(), -declRefExpr(to(enumConstantDecl(; + auto NumericLiteral = anyOf(integerLiteral(), floatLiteral()); + auto UnaryNumericLiteral = unaryOperator(hasAnyOperatorName("+", "-"), hasUnaryOperand(NumericLiteral)); + auto EnumRef = declRefExpr(to(enumConstantDecl())); + + auto InitBase = anyOf( + stringLiteral(), characterLiteral(), NumericLiteral, UnaryNumericLiteral, + cxxBoolLiteral(), cxxNullPtrLiteralExpr(), implicitValueInitExpr(), + EnumRef, + binaryOperator(hasLHS(anyOf(NumericLiteral, EnumRef, binaryOperator())), + hasRHS(anyOf(NumericLiteral, EnumRef, binaryOperator(); + auto Init = anyOf(initListExpr(anyOf(allOf(initCountIs(1), hasInit(0, InitBase)), diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 07a79d6bbe807..01892f4cde648 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -128,6 +128,10 @@ Changes in existing checks ` check by providing additional examples and fixing some macro related false positives. +- Improved :doc:`modernize-use-default-member-init + ` check by matching arithmetic + operations within member list initialization. + - Improved :doc:`performance/unnecessary-value-param ` check performance by tolerating fix-it breaking compilation when functions is used as pointers diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp index 81c980e0217e6..ff8c80b682bdb 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp @@ -518,3 +518,29 @@ class ArrayBraceInitMultipleValues { }; } // namespace PR63285 + +namespace PR122480 { + +#define ARITHMETIC_MACRO (44 - 2) + +class DefaultMemberInitWithArithmetic { + DefaultMemberInitWithArithmetic() : a{1 + 1}, b{1 + 11 + 123 + 1234}, c{2 + (4 / 2) + 3 + (7 / 11)}, d{ARITHMETIC_MACRO * 2}, e{1.2 + 3.4} {} + // CHECK-MESSAGES: :[[@LINE-1]]:39: warning: member initializer for 'a' is redundant [modernize-use-default-member-init] + // CHECK-FIXES: DefaultMemberInitWithArithmetic() {} + + int a{1 + 1}; + int b; + // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use default member initializer for 'b' [modernize-use-default-member-init] + // CHECK-FIXES: int b{1 + 11 + 123 + 1234}; + int c; + // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use default member initializer for 'c' [modernize-use-default-member-init] + // CHECK-FIXES: int c{2 + (4 / 2) + 3 + (7 / 11)} + int d; + // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use default member initializer for 'd' [modernize-use-default-member-init] + // CHECK-FIXES: int d{ARITHMETIC_MACRO * 2}; + double e; + // CHE
[clang-tools-extra] [clang-tidy] detect arithmetic operations within member list initialization in modernize-use-default-member-init check (PR #129370)
@@ -202,7 +208,13 @@ void UseDefaultMemberInitCheck::registerMatchers(MatchFinder *Finder) { unaryOperator(hasAnyOperatorName("+", "-"), hasUnaryOperand(floatLiteral())), cxxBoolLiteral(), cxxNullPtrLiteralExpr(), implicitValueInitExpr(), -declRefExpr(to(enumConstantDecl(; +declRefExpr(to(enumConstantDecl())), +binaryOperator(hasLHS(anyOf(integerLiteral(), floatLiteral(), RiverDave wrote: I've improved it a bit, not only my changes but the rest as well as I think is way more readable now, there's still some duplicate code like: ```cpp binaryOperator(hasLHS(anyOf(NumericLiteral, EnumRef, binaryOperator())), hasRHS(anyOf(NumericLiteral, EnumRef, binaryOperator(); ``` But I still didn't wanted to change it as I think It's better for readability. let me know if this is the right call. https://github.com/llvm/llvm-project/pull/129370 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] detect arithmetic operations within member list initialization in modernize-use-default-member-init check (PR #129370)
https://github.com/RiverDave edited https://github.com/llvm/llvm-project/pull/129370 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] detect arithmetic operations within member list initialization in modernize-use-default-member-init check (PR #129370)
https://github.com/RiverDave updated https://github.com/llvm/llvm-project/pull/129370 >From b46375a39104f967bdb3b6a9bd3214545a5965cf Mon Sep 17 00:00:00 2001 From: David Rivera Date: Sat, 1 Mar 2025 02:09:02 -0500 Subject: [PATCH] [clang-tidy] detect arithmetic operations within member list initialization in modernize-use-default-member-init --- .../modernize/UseDefaultMemberInitCheck.cpp | 26 --- clang-tools-extra/docs/ReleaseNotes.rst | 4 +++ .../modernize/use-default-member-init.cpp | 26 +++ 3 files changed, 47 insertions(+), 9 deletions(-) diff --git a/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp index 6c06b0af342f6..16f04b5f555e1 100644 --- a/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp @@ -159,6 +159,12 @@ static bool sameValue(const Expr *E1, const Expr *E2) { case Stmt::UnaryOperatorClass: return sameValue(cast(E1)->getSubExpr(), cast(E2)->getSubExpr()); + case Stmt::BinaryOperatorClass: { +const auto *BinOp1 = cast(E1); +const auto *BinOp2 = cast(E2); +return sameValue(BinOp1->getLHS(), BinOp2->getLHS()) && + sameValue(BinOp1->getRHS(), BinOp2->getRHS()); + } case Stmt::CharacterLiteralClass: return cast(E1)->getValue() == cast(E2)->getValue(); @@ -194,15 +200,17 @@ void UseDefaultMemberInitCheck::storeOptions( } void UseDefaultMemberInitCheck::registerMatchers(MatchFinder *Finder) { - auto InitBase = - anyOf(stringLiteral(), characterLiteral(), integerLiteral(), -unaryOperator(hasAnyOperatorName("+", "-"), - hasUnaryOperand(integerLiteral())), -floatLiteral(), -unaryOperator(hasAnyOperatorName("+", "-"), - hasUnaryOperand(floatLiteral())), -cxxBoolLiteral(), cxxNullPtrLiteralExpr(), implicitValueInitExpr(), -declRefExpr(to(enumConstantDecl(; + auto NumericLiteral = anyOf(integerLiteral(), floatLiteral()); + auto UnaryNumericLiteral = unaryOperator(hasAnyOperatorName("+", "-"), + hasUnaryOperand(NumericLiteral)); + auto EnumRef = declRefExpr(to(enumConstantDecl())); + + auto InitBase = anyOf( + stringLiteral(), characterLiteral(), NumericLiteral, UnaryNumericLiteral, + cxxBoolLiteral(), cxxNullPtrLiteralExpr(), implicitValueInitExpr(), + EnumRef, + binaryOperator(hasLHS(anyOf(NumericLiteral, EnumRef, binaryOperator())), + hasRHS(anyOf(NumericLiteral, EnumRef, binaryOperator(); auto Init = anyOf(initListExpr(anyOf(allOf(initCountIs(1), hasInit(0, InitBase)), diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 07a79d6bbe807..01892f4cde648 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -128,6 +128,10 @@ Changes in existing checks ` check by providing additional examples and fixing some macro related false positives. +- Improved :doc:`modernize-use-default-member-init + ` check by matching arithmetic + operations within member list initialization. + - Improved :doc:`performance/unnecessary-value-param ` check performance by tolerating fix-it breaking compilation when functions is used as pointers diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp index 81c980e0217e6..ff8c80b682bdb 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp @@ -518,3 +518,29 @@ class ArrayBraceInitMultipleValues { }; } // namespace PR63285 + +namespace PR122480 { + +#define ARITHMETIC_MACRO (44 - 2) + +class DefaultMemberInitWithArithmetic { + DefaultMemberInitWithArithmetic() : a{1 + 1}, b{1 + 11 + 123 + 1234}, c{2 + (4 / 2) + 3 + (7 / 11)}, d{ARITHMETIC_MACRO * 2}, e{1.2 + 3.4} {} + // CHECK-MESSAGES: :[[@LINE-1]]:39: warning: member initializer for 'a' is redundant [modernize-use-default-member-init] + // CHECK-FIXES: DefaultMemberInitWithArithmetic() {} + + int a{1 + 1}; + int b; + // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use default member initializer for 'b' [modernize-use-default-member-init] + // CHECK-FIXES: int b{1 + 11 + 123 + 1234}; + int c; + // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use default member initializer for 'c' [modernize-use-default-member-init] + // CHECK-FIXES: int c{2 + (4 / 2) + 3 + (7 / 11)} + int d; + // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use default member initializer for 'd' [modernize-use-default-member-init] + // CHECK-FIXES: int d{ARIT
[clang-tools-extra] [clang-tidy] detect arithmetic operations within member list initialization in modernize-use-default-member-init check (PR #129370)
https://github.com/RiverDave edited https://github.com/llvm/llvm-project/pull/129370 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] detect arithmetic operations within member list initialization in modernize-use-default-member-init check (PR #129370)
https://github.com/RiverDave edited https://github.com/llvm/llvm-project/pull/129370 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] detect explicit casting within modernize-use-default-member-init (PR #129408)
@@ -194,15 +199,21 @@ void UseDefaultMemberInitCheck::storeOptions( } void UseDefaultMemberInitCheck::registerMatchers(MatchFinder *Finder) { - auto InitBase = - anyOf(stringLiteral(), characterLiteral(), integerLiteral(), -unaryOperator(hasAnyOperatorName("+", "-"), - hasUnaryOperand(integerLiteral())), -floatLiteral(), -unaryOperator(hasAnyOperatorName("+", "-"), - hasUnaryOperand(floatLiteral())), -cxxBoolLiteral(), cxxNullPtrLiteralExpr(), implicitValueInitExpr(), -declRefExpr(to(enumConstantDecl(; + + auto ExplicitCastExpr = castExpr(hasSourceExpression(anyOf( + unaryOperator(hasAnyOperatorName("+", "-"), +hasUnaryOperand(anyOf(integerLiteral(), floatLiteral(, + integerLiteral(), floatLiteral(), characterLiteral(; RiverDave wrote: Amazing, will apply these suggestions soon https://github.com/llvm/llvm-project/pull/129408 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] detect explicit casting within modernize-use-default-member-init (PR #129408)
https://github.com/RiverDave updated https://github.com/llvm/llvm-project/pull/129408 >From 8995f2517be1235e01cd68d0d68199505b8ffaad Mon Sep 17 00:00:00 2001 From: David Rivera Date: Sat, 1 Mar 2025 19:22:25 -0500 Subject: [PATCH] [clang-tidy] detect explicit casting within modernize-use-default-member-init --- .../modernize/UseDefaultMemberInitCheck.cpp | 12 +-- clang-tools-extra/docs/ReleaseNotes.rst | 3 +- .../modernize/use-default-member-init.cpp | 36 +++ 3 files changed, 48 insertions(+), 3 deletions(-) diff --git a/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp index 5b0b9b59d4e3b..bf99c738da1a3 100644 --- a/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp @@ -176,6 +176,11 @@ static bool sameValue(const Expr *E1, const Expr *E2) { cast(E2)->getString(); case Stmt::DeclRefExprClass: return cast(E1)->getDecl() == cast(E2)->getDecl(); + case Stmt::CStyleCastExprClass: + case Stmt::CXXStaticCastExprClass: + case Stmt::CXXFunctionalCastExprClass: +return sameValue(cast(E1)->getSubExpr(), + cast(E2)->getSubExpr()); default: return false; } @@ -206,10 +211,13 @@ void UseDefaultMemberInitCheck::registerMatchers(MatchFinder *Finder) { cxxBoolLiteral(), cxxNullPtrLiteralExpr(), implicitValueInitExpr(), declRefExpr(to(anyOf(enumConstantDecl(), ConstExpRef; + auto ExplicitCastExpr = castExpr(hasSourceExpression(InitBase)); + auto InitMatcher = anyOf(InitBase, ExplicitCastExpr); + auto Init = - anyOf(initListExpr(anyOf(allOf(initCountIs(1), hasInit(0, InitBase)), + anyOf(initListExpr(anyOf(allOf(initCountIs(1), hasInit(0, InitMatcher)), initCountIs(0), hasType(arrayType(, -InitBase); +InitBase, ExplicitCastExpr); Finder->addMatcher( cxxConstructorDecl(forEachConstructorInitializer( diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 110f949741c3f..3a5361e23b5b6 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -156,7 +156,8 @@ Changes in existing checks - Improved :doc:`modernize-use-default-member-init ` check by matching - ``constexpr`` and ``static`` values on member initialization. + ``constexpr`` and ``static``` values on member initialization and by detecting explicit + casting of built-in types within member list initialization. - Improved :doc:`performance/unnecessary-value-param ` check performance by diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp index 8b9bfaf0df34f..cac50be9e4368 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp @@ -536,4 +536,40 @@ namespace PR122480 { // CHECK-FIXES: int b{STATIC_VAL}; }; +class CStyleCastInit { + CStyleCastInit() : a{(int)1.23}, b{(float)42}, c{(double)'C'} {} + // CHECK-MESSAGES: :[[@LINE-1]]:50: warning: member initializer for 'c' is redundant [modernize-use-default-member-init] + // CHECK-FIXES: CStyleCastInit() {} + + int a; + // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use default member initializer for 'a' [modernize-use-default-member-init] + // CHECK-FIXES: int a{(int)1.23}; + float b; + // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use default member initializer for 'b' [modernize-use-default-member-init] + // CHECK-FIXES: float b{(float)42}; + double c{(double)'C'}; +}; + +class StaticCastInit { + StaticCastInit() : m(static_cast(9.99)), n(static_cast(65)) {} + // CHECK-MESSAGES: :[[@LINE-1]]:49: warning: member initializer for 'n' is redundant [modernize-use-default-member-init] + // CHECK-FIXES: StaticCastInit() {} + int m; + // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use default member initializer for 'm' [modernize-use-default-member-init] + // CHECK-FIXES: int m{static_cast(9.99)}; + char n{static_cast(65)}; +}; + +class FunctionalCastInit { + FunctionalCastInit() : a(int(5.67)), b(float(2)), c(double('C')) {} + // CHECK-MESSAGES: :[[@LINE-1]]:40: warning: member initializer for 'b' is redundant [modernize-use-default-member-init] + int a; + // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use default member initializer for 'a' [modernize-use-default-member-init] + // CHECK-FIXES: int a{int(5.67)}; + float b{float(2)}; + double c; + // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use default member initializer for 'c' [modernize-use-default-member-init] + // CHECK-FIXES: double c{double('C')}; +}; + } //namespace PR122480
[clang-tools-extra] [clang-tidy] detect explicit casting within modernize-use-default-member-init (PR #129408)
@@ -158,6 +158,10 @@ Changes in existing checks ` check by matching ``constexpr`` and ``static`` values on member initialization. +- Improved :doc:`modernize-use-default-member-init + ` check by detecting + explicit casting of built-in types within member list initialization. RiverDave wrote: Should be done now https://github.com/llvm/llvm-project/pull/129408 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] detect explicit casting within modernize-use-default-member-init (PR #129408)
https://github.com/RiverDave updated https://github.com/llvm/llvm-project/pull/129408 >From a06de0100d34d3f18b8eca690161b3720620f4af Mon Sep 17 00:00:00 2001 From: David Rivera Date: Sat, 1 Mar 2025 19:22:25 -0500 Subject: [PATCH] [clang-tidy] detect explicit casting within modernize-use-default-member-init --- .../modernize/UseDefaultMemberInitCheck.cpp | 12 +-- clang-tools-extra/docs/ReleaseNotes.rst | 3 +- .../modernize/use-default-member-init.cpp | 36 +++ 3 files changed, 48 insertions(+), 3 deletions(-) diff --git a/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp index 5b0b9b59d4e3b..bf99c738da1a3 100644 --- a/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp @@ -176,6 +176,11 @@ static bool sameValue(const Expr *E1, const Expr *E2) { cast(E2)->getString(); case Stmt::DeclRefExprClass: return cast(E1)->getDecl() == cast(E2)->getDecl(); + case Stmt::CStyleCastExprClass: + case Stmt::CXXStaticCastExprClass: + case Stmt::CXXFunctionalCastExprClass: +return sameValue(cast(E1)->getSubExpr(), + cast(E2)->getSubExpr()); default: return false; } @@ -206,10 +211,13 @@ void UseDefaultMemberInitCheck::registerMatchers(MatchFinder *Finder) { cxxBoolLiteral(), cxxNullPtrLiteralExpr(), implicitValueInitExpr(), declRefExpr(to(anyOf(enumConstantDecl(), ConstExpRef; + auto ExplicitCastExpr = castExpr(hasSourceExpression(InitBase)); + auto InitMatcher = anyOf(InitBase, ExplicitCastExpr); + auto Init = - anyOf(initListExpr(anyOf(allOf(initCountIs(1), hasInit(0, InitBase)), + anyOf(initListExpr(anyOf(allOf(initCountIs(1), hasInit(0, InitMatcher)), initCountIs(0), hasType(arrayType(, -InitBase); +InitBase, ExplicitCastExpr); Finder->addMatcher( cxxConstructorDecl(forEachConstructorInitializer( diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 110f949741c3f..58d43ab43dbcc 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -156,7 +156,8 @@ Changes in existing checks - Improved :doc:`modernize-use-default-member-init ` check by matching - ``constexpr`` and ``static`` values on member initialization. + ``constexpr`` and ``static``` values on member initialization and by detecting + explicit casting of built-in types within member list initialization. - Improved :doc:`performance/unnecessary-value-param ` check performance by diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp index 8b9bfaf0df34f..cac50be9e4368 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp @@ -536,4 +536,40 @@ namespace PR122480 { // CHECK-FIXES: int b{STATIC_VAL}; }; +class CStyleCastInit { + CStyleCastInit() : a{(int)1.23}, b{(float)42}, c{(double)'C'} {} + // CHECK-MESSAGES: :[[@LINE-1]]:50: warning: member initializer for 'c' is redundant [modernize-use-default-member-init] + // CHECK-FIXES: CStyleCastInit() {} + + int a; + // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use default member initializer for 'a' [modernize-use-default-member-init] + // CHECK-FIXES: int a{(int)1.23}; + float b; + // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use default member initializer for 'b' [modernize-use-default-member-init] + // CHECK-FIXES: float b{(float)42}; + double c{(double)'C'}; +}; + +class StaticCastInit { + StaticCastInit() : m(static_cast(9.99)), n(static_cast(65)) {} + // CHECK-MESSAGES: :[[@LINE-1]]:49: warning: member initializer for 'n' is redundant [modernize-use-default-member-init] + // CHECK-FIXES: StaticCastInit() {} + int m; + // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use default member initializer for 'm' [modernize-use-default-member-init] + // CHECK-FIXES: int m{static_cast(9.99)}; + char n{static_cast(65)}; +}; + +class FunctionalCastInit { + FunctionalCastInit() : a(int(5.67)), b(float(2)), c(double('C')) {} + // CHECK-MESSAGES: :[[@LINE-1]]:40: warning: member initializer for 'b' is redundant [modernize-use-default-member-init] + int a; + // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use default member initializer for 'a' [modernize-use-default-member-init] + // CHECK-FIXES: int a{int(5.67)}; + float b{float(2)}; + double c; + // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use default member initializer for 'c' [modernize-use-default-member-init] + // CHECK-FIXES: double c{double('C')}; +}; + } //namespace PR122480
[clang-tools-extra] [clang-tidy] detect explicit casting within modernize-use-default-member-init (PR #129408)
RiverDave wrote: Ping https://github.com/llvm/llvm-project/pull/129408 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy][C++20] Add support for aggregate types within modernize-use-emplace (PR #131969)
https://github.com/RiverDave edited https://github.com/llvm/llvm-project/pull/131969 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy][C++20] Add support for aggregate types within modernize-use-emplace (PR #131969)
https://github.com/RiverDave edited https://github.com/llvm/llvm-project/pull/131969 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] detect arithmetic operations within member list initialization in modernize-use-default-member-init check (PR #129370)
https://github.com/RiverDave updated https://github.com/llvm/llvm-project/pull/129370 >From c81ff04def9d2198c84b96fb5cfa1de90ca57a11 Mon Sep 17 00:00:00 2001 From: David Rivera Date: Sat, 1 Mar 2025 02:09:02 -0500 Subject: [PATCH] [clang-tidy] detect arithmetic operations within member list initialization in modernize-use-default-member-init --- .../modernize/UseDefaultMemberInitCheck.cpp | 30 +-- clang-tools-extra/docs/ReleaseNotes.rst | 4 +-- .../modernize/use-default-member-init.cpp | 23 ++ 3 files changed, 46 insertions(+), 11 deletions(-) diff --git a/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp index bf99c738da1a3..9ad6dcefbc1bb 100644 --- a/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp @@ -159,6 +159,13 @@ static bool sameValue(const Expr *E1, const Expr *E2) { case Stmt::UnaryOperatorClass: return sameValue(cast(E1)->getSubExpr(), cast(E2)->getSubExpr()); + case Stmt::BinaryOperatorClass: { +const auto *BinOp1 = cast(E1); +const auto *BinOp2 = cast(E2); +return BinOp1->getOpcode() == BinOp2->getOpcode() && + sameValue(BinOp1->getLHS(), BinOp2->getLHS()) && + sameValue(BinOp1->getRHS(), BinOp2->getRHS()); + } case Stmt::CharacterLiteralClass: return cast(E1)->getValue() == cast(E2)->getValue(); @@ -199,17 +206,22 @@ void UseDefaultMemberInitCheck::storeOptions( } void UseDefaultMemberInitCheck::registerMatchers(MatchFinder *Finder) { - auto ConstExpRef = varDecl(anyOf(isConstexpr(), isStaticStorageClass())); + auto NumericLiteral = anyOf(integerLiteral(), floatLiteral()); + auto UnaryNumericLiteral = unaryOperator(hasAnyOperatorName("+", "-"), + hasUnaryOperand(NumericLiteral)); + + auto ConstExprRef = varDecl(anyOf(isConstexpr(), isStaticStorageClass())); + auto ImmutableRef = + declRefExpr(to(decl(anyOf(enumConstantDecl(), ConstExprRef; + + auto BinaryNumericExpr = binaryOperator( + hasOperands(anyOf(NumericLiteral, ImmutableRef, binaryOperator()), + anyOf(NumericLiteral, ImmutableRef, binaryOperator(; auto InitBase = - anyOf(stringLiteral(), characterLiteral(), integerLiteral(), -unaryOperator(hasAnyOperatorName("+", "-"), - hasUnaryOperand(integerLiteral())), -floatLiteral(), -unaryOperator(hasAnyOperatorName("+", "-"), - hasUnaryOperand(floatLiteral())), -cxxBoolLiteral(), cxxNullPtrLiteralExpr(), implicitValueInitExpr(), -declRefExpr(to(anyOf(enumConstantDecl(), ConstExpRef; + anyOf(stringLiteral(), characterLiteral(), NumericLiteral, +UnaryNumericLiteral, cxxBoolLiteral(), cxxNullPtrLiteralExpr(), +implicitValueInitExpr(), ImmutableRef, BinaryNumericExpr); auto ExplicitCastExpr = castExpr(hasSourceExpression(InitBase)); auto InitMatcher = anyOf(InitBase, ExplicitCastExpr); diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index ed7da975f3de7..27748e52f1707 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -168,8 +168,8 @@ Changes in existing checks - Improved :doc:`modernize-use-default-member-init ` check by matching - ``constexpr`` and ``static``` values on member initialization and by detecting - explicit casting of built-in types within member list initialization. + arithmetic operations, ``constexpr`` and ``static`` values, and detecting explicit + casting of built-in types within member list initialization. - Improved :doc:`modernize-use-ranges ` check by updating suppress diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp index cac50be9e4368..bb2a7388e75f1 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp @@ -572,4 +572,27 @@ class FunctionalCastInit { // CHECK-FIXES: double c{double('C')}; }; +#define ARITHMETIC_MACRO (44 - 2) + +class DefaultMemberInitWithArithmetic { + DefaultMemberInitWithArithmetic() : a{1 + 1}, b{1 + 11 + 123 + 1234}, c{2 + (4 / 2) + 3 + (7 / 11)}, d{ARITHMETIC_MACRO * 2}, e{1.2 + 3.4} {} + // CHECK-MESSAGES: :[[@LINE-1]]:39: warning: member initializer for 'a' is redundant [modernize-use-default-member-init] + // CHECK-FIXES: DefaultMemberInitWithArithmetic() {} + + int a{1 + 1}; + int b; + // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use default member initializer for 'b' [modernize-use-default-member-in
[clang-tools-extra] [clang-tidy] detect arithmetic operations within member list initialization in modernize-use-default-member-init check (PR #129370)
https://github.com/RiverDave updated https://github.com/llvm/llvm-project/pull/129370 >From 4e5e44c721e1867b9263d0a0ea4669d0bd3bdd4a Mon Sep 17 00:00:00 2001 From: David Rivera Date: Sat, 1 Mar 2025 02:09:02 -0500 Subject: [PATCH] [clang-tidy] detect arithmetic operations within member list initialization in modernize-use-default-member-init --- .../modernize/UseDefaultMemberInitCheck.cpp | 30 +-- clang-tools-extra/docs/ReleaseNotes.rst | 2 +- .../modernize/use-default-member-init.cpp | 23 ++ 3 files changed, 45 insertions(+), 10 deletions(-) diff --git a/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp index bf99c738da1a3..9ad6dcefbc1bb 100644 --- a/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp @@ -159,6 +159,13 @@ static bool sameValue(const Expr *E1, const Expr *E2) { case Stmt::UnaryOperatorClass: return sameValue(cast(E1)->getSubExpr(), cast(E2)->getSubExpr()); + case Stmt::BinaryOperatorClass: { +const auto *BinOp1 = cast(E1); +const auto *BinOp2 = cast(E2); +return BinOp1->getOpcode() == BinOp2->getOpcode() && + sameValue(BinOp1->getLHS(), BinOp2->getLHS()) && + sameValue(BinOp1->getRHS(), BinOp2->getRHS()); + } case Stmt::CharacterLiteralClass: return cast(E1)->getValue() == cast(E2)->getValue(); @@ -199,17 +206,22 @@ void UseDefaultMemberInitCheck::storeOptions( } void UseDefaultMemberInitCheck::registerMatchers(MatchFinder *Finder) { - auto ConstExpRef = varDecl(anyOf(isConstexpr(), isStaticStorageClass())); + auto NumericLiteral = anyOf(integerLiteral(), floatLiteral()); + auto UnaryNumericLiteral = unaryOperator(hasAnyOperatorName("+", "-"), + hasUnaryOperand(NumericLiteral)); + + auto ConstExprRef = varDecl(anyOf(isConstexpr(), isStaticStorageClass())); + auto ImmutableRef = + declRefExpr(to(decl(anyOf(enumConstantDecl(), ConstExprRef; + + auto BinaryNumericExpr = binaryOperator( + hasOperands(anyOf(NumericLiteral, ImmutableRef, binaryOperator()), + anyOf(NumericLiteral, ImmutableRef, binaryOperator(; auto InitBase = - anyOf(stringLiteral(), characterLiteral(), integerLiteral(), -unaryOperator(hasAnyOperatorName("+", "-"), - hasUnaryOperand(integerLiteral())), -floatLiteral(), -unaryOperator(hasAnyOperatorName("+", "-"), - hasUnaryOperand(floatLiteral())), -cxxBoolLiteral(), cxxNullPtrLiteralExpr(), implicitValueInitExpr(), -declRefExpr(to(anyOf(enumConstantDecl(), ConstExpRef; + anyOf(stringLiteral(), characterLiteral(), NumericLiteral, +UnaryNumericLiteral, cxxBoolLiteral(), cxxNullPtrLiteralExpr(), +implicitValueInitExpr(), ImmutableRef, BinaryNumericExpr); auto ExplicitCastExpr = castExpr(hasSourceExpression(InitBase)); auto InitMatcher = anyOf(InitBase, ExplicitCastExpr); diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index ed7da975f3de7..7d9a6039ea5cb 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -168,7 +168,7 @@ Changes in existing checks - Improved :doc:`modernize-use-default-member-init ` check by matching - ``constexpr`` and ``static``` values on member initialization and by detecting + arithmetic operations, ``constexpr`` and ``static`` values, and detecting explicit casting of built-in types within member list initialization. - Improved :doc:`modernize-use-ranges diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp index cac50be9e4368..bb2a7388e75f1 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp @@ -572,4 +572,27 @@ class FunctionalCastInit { // CHECK-FIXES: double c{double('C')}; }; +#define ARITHMETIC_MACRO (44 - 2) + +class DefaultMemberInitWithArithmetic { + DefaultMemberInitWithArithmetic() : a{1 + 1}, b{1 + 11 + 123 + 1234}, c{2 + (4 / 2) + 3 + (7 / 11)}, d{ARITHMETIC_MACRO * 2}, e{1.2 + 3.4} {} + // CHECK-MESSAGES: :[[@LINE-1]]:39: warning: member initializer for 'a' is redundant [modernize-use-default-member-init] + // CHECK-FIXES: DefaultMemberInitWithArithmetic() {} + + int a{1 + 1}; + int b; + // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use default member initializer for 'b' [modernize-use-default-member-init] + // CHECK-FIXES: int b{1 + 11 + 123 + 1234}; + int c; + // CHECK-MESSAGES: :[[@LINE-1]]:7: warning:
[clang-tools-extra] [clang-tidy] detect arithmetic operations within member list initialization in modernize-use-default-member-init check (PR #129370)
@@ -159,8 +159,8 @@ Changes in existing checks - Improved :doc:`modernize-use-default-member-init ` check by matching - ``constexpr`` and ``static``` values on member initialization and by detecting - explicit casting of built-in types within member list initialization. + arithmetic operations, constexpr and static values, and detecting explicit RiverDave wrote: Thx, should be done now. https://github.com/llvm/llvm-project/pull/129370 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] detect arithmetic operations within member list initialization in modernize-use-default-member-init check (PR #129370)
RiverDave wrote: @HerrCai0907 could we merge this? https://github.com/llvm/llvm-project/pull/129370 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy][C++20] Add support for aggregate types within modernize-use-emplace (PR #131969)
@@ -1,4 +1,4 @@ -// RUN: %check_clang_tidy %s modernize-use-emplace %t -- \ +// RUN: %check_clang_tidy %s -std=c++17 modernize-use-emplace %t -- \ RiverDave wrote: On second thought would this be sufficient for the cases shown above?: I've noticed that `use-ranges.cpp` uses a custom suffix bound to a standard ```cpp // RUN: %check_clang_tidy -std=c++23 %s modernize-use-ranges %t -check-suffixes=,CPP23 -- -I %S/Inputs/ // CHECK-FIXES-CPP23: #include ``` I could implement cases with designated initializer in a separate c++20 file. https://github.com/llvm/llvm-project/pull/131969 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy][C++20] Add support for aggregate types within modernize-use-emplace (PR #131969)
https://github.com/RiverDave edited https://github.com/llvm/llvm-project/pull/131969 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy][C++20] Add support for aggregate types within modernize-use-emplace (PR #131969)
https://github.com/RiverDave edited https://github.com/llvm/llvm-project/pull/131969 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy][C++20] Add support for aggregate types within modernize-use-emplace (PR #131969)
https://github.com/RiverDave edited https://github.com/llvm/llvm-project/pull/131969 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Add support for aggregate types within modernize-use-emplace (PR #131969)
https://github.com/RiverDave created https://github.com/llvm/llvm-project/pull/131969 Aims to address: #115841 C++20 introduced in-place initialization for aggregate types within `emplace_back` calls. I ensured to match any braced init expressions with temporals (and applied similar rules as they were used in constructor expressions) when called with c++20. I'd love to get some feedback related to my test coverage and more possible edge cases I might've missed. >From d72ff20640c734169c2d1ae4b8c92dcc7bc09288 Mon Sep 17 00:00:00 2001 From: David Rivera Date: Sun, 16 Mar 2025 16:20:16 -0400 Subject: [PATCH] [clang-tidy] Add support for aggregate types within modernize-use-emplace --- .../clang-tidy/modernize/UseEmplaceCheck.cpp | 70 ++- clang-tools-extra/docs/ReleaseNotes.rst | 4 + .../checkers/modernize/use-emplace-cxx20.cpp | 86 +++ .../checkers/modernize/use-emplace.cpp| 2 +- 4 files changed, 140 insertions(+), 22 deletions(-) create mode 100644 clang-tools-extra/test/clang-tidy/checkers/modernize/use-emplace-cxx20.cpp diff --git a/clang-tools-extra/clang-tidy/modernize/UseEmplaceCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseEmplaceCheck.cpp index 430455a38f395..8c42b3a8a6e3f 100644 --- a/clang-tools-extra/clang-tidy/modernize/UseEmplaceCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/UseEmplaceCheck.cpp @@ -98,8 +98,8 @@ auto hasWantedType(llvm::ArrayRef TypeNames) { // Matches member call expressions of the named method on the listed container // types. -auto cxxMemberCallExprOnContainer( -StringRef MethodName, llvm::ArrayRef ContainerNames) { +auto cxxMemberCallExprOnContainer(StringRef MethodName, + llvm::ArrayRef ContainerNames) { return cxxMemberCallExpr( hasDeclaration(functionDecl(hasName(MethodName))), on(hasTypeOrPointeeType(hasWantedType(ContainerNames; @@ -174,19 +174,19 @@ void UseEmplaceCheck::registerMatchers(MatchFinder *Finder) { // passed pointer because smart pointer won't be constructed // (and destructed) as in push_back case. auto IsCtorOfSmartPtr = - hasDeclaration(cxxConstructorDecl(ofClass(hasAnyName(SmartPointers; + cxxConstructorDecl(ofClass(hasAnyName(SmartPointers))); // Bitfields binds only to consts and emplace_back take it by universal ref. - auto BitFieldAsArgument = hasAnyArgument( - ignoringImplicit(memberExpr(hasDeclaration(fieldDecl(isBitField()); + auto BitFieldAsArgument = + ignoringImplicit(memberExpr(hasDeclaration(fieldDecl(isBitField(); // Initializer list can't be passed to universal reference. - auto InitializerListAsArgument = hasAnyArgument( + auto InitializerListAsArgument = ignoringImplicit(allOf(cxxConstructExpr(isListInitialization()), - unless(cxxTemporaryObjectExpr(); + unless(cxxTemporaryObjectExpr(; // We could have leak of resource. - auto NewExprAsArgument = hasAnyArgument(ignoringImplicit(cxxNewExpr())); + auto NewExprAsArgument = ignoringImplicit(cxxNewExpr()); // We would call another constructor. auto ConstructingDerived = hasParent(implicitCastExpr(hasCastKind(CastKind::CK_DerivedToBase))); @@ -202,19 +202,36 @@ void UseEmplaceCheck::registerMatchers(MatchFinder *Finder) { // overloaded functions and template names. auto SoughtConstructExpr = cxxConstructExpr( - unless(anyOf(IsCtorOfSmartPtr, HasInitList, BitFieldAsArgument, - InitializerListAsArgument, NewExprAsArgument, - ConstructingDerived, IsPrivateOrProtectedCtor))) + unless(anyOf(hasDeclaration(IsCtorOfSmartPtr), HasInitList, + hasAnyArgument(BitFieldAsArgument), + hasAnyArgument(InitializerListAsArgument), + hasAnyArgument(NewExprAsArgument), ConstructingDerived, + IsPrivateOrProtectedCtor))) .bind("ctor"); - auto HasConstructExpr = has(ignoringImplicit(SoughtConstructExpr)); + + auto IsPrimitiveType = hasType(builtinType()); + + auto AggregateInitExpr = + getLangOpts().CPlusPlus20 + ? initListExpr(unless(anyOf(HasInitList, has(IsCtorOfSmartPtr), + has(BitFieldAsArgument), + has(InitializerListAsArgument), + has(NewExprAsArgument), IsPrimitiveType))) +.bind("agg_init") + : unless(anything()); + + auto HasConstructExpr = + has(ignoringImplicit(anyOf(SoughtConstructExpr, AggregateInitExpr))); // allow for T{} to be replaced, even if no CTOR is declared auto HasConstructInitListExpr = has(initListExpr( - initCountLeq(1), anyOf(allOf(has(SoughtConstructExpr), - has(cxxConstructExpr(argumentCountIs(0, -
[clang-tools-extra] [clang-tidy] Add support for aggregate types within modernize-use-emplace (PR #131969)
https://github.com/RiverDave edited https://github.com/llvm/llvm-project/pull/131969 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] detect arithmetic operations within member list initialization in modernize-use-default-member-init check (PR #129370)
RiverDave wrote: Ping @HerrCai0907 https://github.com/llvm/llvm-project/pull/129370 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Improve integer comparison by matching valid expressions outside implicitCastExpr (PR #134188)
https://github.com/RiverDave updated https://github.com/llvm/llvm-project/pull/134188 >From b37d2b18e12cce53137eb78af5507ebf13ee45a1 Mon Sep 17 00:00:00 2001 From: David Rivera Date: Wed, 2 Apr 2025 21:02:00 -0400 Subject: [PATCH] [clang-tidy] Improve integer comparison by matching valid expressions outside implicitCastExpr --- .../UseIntegerSignComparisonCheck.cpp | 17 +--- clang-tools-extra/docs/ReleaseNotes.rst | 4 +++ .../modernize/use-integer-sign-comparison.cpp | 26 +++ 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/clang-tools-extra/clang-tidy/modernize/UseIntegerSignComparisonCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseIntegerSignComparisonCheck.cpp index eeba5cce80da5..8f2bb4c4ba8f2 100644 --- a/clang-tools-extra/clang-tidy/modernize/UseIntegerSignComparisonCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/UseIntegerSignComparisonCheck.cpp @@ -39,9 +39,11 @@ intCastExpression(bool IsSigned, // std::cmp_{} functions trigger a compile-time error if either LHS or RHS // is a non-integer type, char, enum or bool // (unsigned char/ signed char are Ok and can be used). - auto IntTypeExpr = expr(hasType(hasCanonicalType(qualType( + const auto HasIntegerType = hasType(hasCanonicalType(qualType( isInteger(), IsSigned ? isSignedInteger() : isUnsignedInteger(), - unless(isActualChar()), unless(booleanType()), unless(enumType()); + unless(isActualChar()), unless(booleanType()), unless(enumType(); + + auto IntTypeExpr = expr(HasIntegerType); const auto ImplicitCastExpr = CastBindName.empty() ? implicitCastExpr(hasSourceExpression(IntTypeExpr)) @@ -52,8 +54,17 @@ intCastExpression(bool IsSigned, const auto StaticCastExpr = cxxStaticCastExpr(has(ImplicitCastExpr)); const auto FunctionalCastExpr = cxxFunctionalCastExpr(has(ImplicitCastExpr)); + // Match function calls or variable references not directly wrapped by an + // implicit cast + const auto CallIntExpr = CastBindName.empty() + ? callExpr(HasIntegerType) + : callExpr(HasIntegerType).bind(CastBindName); + const auto DeclRefIntExpr = + CastBindName.empty() ? declRefExpr(HasIntegerType) + : declRefExpr(HasIntegerType).bind(CastBindName); + return expr(anyOf(ImplicitCastExpr, CStyleCastExpr, StaticCastExpr, -FunctionalCastExpr)); +FunctionalCastExpr, CallIntExpr)); } static StringRef parseOpCode(BinaryOperator::Opcode Code) { diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 6c1f05009df98..a341cbc273f31 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -166,6 +166,10 @@ Changes in existing checks excluding variables with ``thread_local`` storage class specifier from being matched. +- Improved :doc:`modernize-use-integer-sign-comparison + ` check by matching + valid integer expressions not directly wrapped around an implicit cast. + - Improved :doc:`modernize-use-default-member-init ` check by matching ``constexpr`` and ``static``` values on member initialization and by detecting diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-integer-sign-comparison.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-integer-sign-comparison.cpp index 99f00444c2d3f..1d2f64a359a2c 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-integer-sign-comparison.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-integer-sign-comparison.cpp @@ -120,3 +120,29 @@ int AllComparisons() { return 0; } + +namespace PR127471 { +int getSignedValue(); +unsigned int getUnsignedValue(); + +void callExprTest() { + +if (getSignedValue() < getUnsignedValue()) +return; +// CHECK-MESSAGES: :[[@LINE-2]]:13: warning: comparison between 'signed' and 'unsigned' integers [modernize-use-integer-sign-comparison] +// CHECK-FIXES: if (std::cmp_less(getSignedValue() , getUnsignedValue())) + +int sVar = 0; +if (getUnsignedValue() > sVar) +return; +// CHECK-MESSAGES: :[[@LINE-2]]:13: warning: comparison between 'signed' and 'unsigned' integers [modernize-use-integer-sign-comparison] +// CHECK-FIXES: if (std::cmp_greater(getUnsignedValue() , sVar)) + +unsigned int uVar = 0; +if (getSignedValue() > uVar) +return; +// CHECK-MESSAGES: :[[@LINE-2]]:13: warning: comparison between 'signed' and 'unsigned' integers [modernize-use-integer-sign-comparison] +// CHECK-FIXES: if (std::cmp_greater(getSignedValue() , uVar)) + +} +} // namespace PR127471 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Improve integer comparison by matching valid expressions outside implicitCastExpr (PR #134188)
@@ -177,6 +177,10 @@ Changes in existing checks matched scenarios of ``find`` and ``rfind`` methods and fixing false positives when those methods were called with 3 arguments. +- Improved :doc:`modernize-use-integer-sign-comparison RiverDave wrote: Thanks, should be addressed now https://github.com/llvm/llvm-project/pull/134188 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Improve integer comparison by matching valid expressions outside implicitCastExpr (PR #134188)
https://github.com/RiverDave updated https://github.com/llvm/llvm-project/pull/134188 >From 3a2d0c5cc8de153e6d0139154c2c6cd674936a40 Mon Sep 17 00:00:00 2001 From: David Rivera Date: Wed, 2 Apr 2025 21:02:00 -0400 Subject: [PATCH] [clang-tidy] Improve integer comparison by matching valid expressions outside implicitCastExpr --- .../UseIntegerSignComparisonCheck.cpp | 17 +--- clang-tools-extra/docs/ReleaseNotes.rst | 4 +++ .../modernize/use-integer-sign-comparison.cpp | 26 +++ 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/clang-tools-extra/clang-tidy/modernize/UseIntegerSignComparisonCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseIntegerSignComparisonCheck.cpp index eeba5cce80da5..8f2bb4c4ba8f2 100644 --- a/clang-tools-extra/clang-tidy/modernize/UseIntegerSignComparisonCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/UseIntegerSignComparisonCheck.cpp @@ -39,9 +39,11 @@ intCastExpression(bool IsSigned, // std::cmp_{} functions trigger a compile-time error if either LHS or RHS // is a non-integer type, char, enum or bool // (unsigned char/ signed char are Ok and can be used). - auto IntTypeExpr = expr(hasType(hasCanonicalType(qualType( + const auto HasIntegerType = hasType(hasCanonicalType(qualType( isInteger(), IsSigned ? isSignedInteger() : isUnsignedInteger(), - unless(isActualChar()), unless(booleanType()), unless(enumType()); + unless(isActualChar()), unless(booleanType()), unless(enumType(); + + auto IntTypeExpr = expr(HasIntegerType); const auto ImplicitCastExpr = CastBindName.empty() ? implicitCastExpr(hasSourceExpression(IntTypeExpr)) @@ -52,8 +54,17 @@ intCastExpression(bool IsSigned, const auto StaticCastExpr = cxxStaticCastExpr(has(ImplicitCastExpr)); const auto FunctionalCastExpr = cxxFunctionalCastExpr(has(ImplicitCastExpr)); + // Match function calls or variable references not directly wrapped by an + // implicit cast + const auto CallIntExpr = CastBindName.empty() + ? callExpr(HasIntegerType) + : callExpr(HasIntegerType).bind(CastBindName); + const auto DeclRefIntExpr = + CastBindName.empty() ? declRefExpr(HasIntegerType) + : declRefExpr(HasIntegerType).bind(CastBindName); + return expr(anyOf(ImplicitCastExpr, CStyleCastExpr, StaticCastExpr, -FunctionalCastExpr)); +FunctionalCastExpr, CallIntExpr)); } static StringRef parseOpCode(BinaryOperator::Opcode Code) { diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 6c1f05009df98..31f0870b6173e 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -171,6 +171,10 @@ Changes in existing checks ``constexpr`` and ``static``` values on member initialization and by detecting explicit casting of built-in types within member list initialization. +- Improved :doc:`modernize-use-integer-sign-comparison + ` check by matching + valid integer expressions not directly wrapped around an implicit cast. + - Improved :doc:`modernize-use-ranges ` check by updating suppress warnings logic for ``nullptr`` in ``std::find``. diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-integer-sign-comparison.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-integer-sign-comparison.cpp index 99f00444c2d3f..1d2f64a359a2c 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-integer-sign-comparison.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-integer-sign-comparison.cpp @@ -120,3 +120,29 @@ int AllComparisons() { return 0; } + +namespace PR127471 { +int getSignedValue(); +unsigned int getUnsignedValue(); + +void callExprTest() { + +if (getSignedValue() < getUnsignedValue()) +return; +// CHECK-MESSAGES: :[[@LINE-2]]:13: warning: comparison between 'signed' and 'unsigned' integers [modernize-use-integer-sign-comparison] +// CHECK-FIXES: if (std::cmp_less(getSignedValue() , getUnsignedValue())) + +int sVar = 0; +if (getUnsignedValue() > sVar) +return; +// CHECK-MESSAGES: :[[@LINE-2]]:13: warning: comparison between 'signed' and 'unsigned' integers [modernize-use-integer-sign-comparison] +// CHECK-FIXES: if (std::cmp_greater(getUnsignedValue() , sVar)) + +unsigned int uVar = 0; +if (getSignedValue() > uVar) +return; +// CHECK-MESSAGES: :[[@LINE-2]]:13: warning: comparison between 'signed' and 'unsigned' integers [modernize-use-integer-sign-comparison] +// CHECK-FIXES: if (std::cmp_greater(getSignedValue() , uVar)) + +} +} // namespace PR127471 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Improve integer comparison by matching valid expressions outside implicitCastExpr (PR #134188)
@@ -166,6 +166,10 @@ Changes in existing checks excluding variables with ``thread_local`` storage class specifier from being matched. +- Improved :doc:`modernize-use-integer-sign-comparison RiverDave wrote: Figured it out the instant I pushed my changes 😂 https://github.com/llvm/llvm-project/pull/134188 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Improve integer comparison by matching valid expressions outside implicitCastExpr (PR #134188)
https://github.com/RiverDave updated https://github.com/llvm/llvm-project/pull/134188 >From 357f8d3b2fc424968a9e17c3dd2a13fe046f6cf9 Mon Sep 17 00:00:00 2001 From: David Rivera Date: Wed, 2 Apr 2025 21:02:00 -0400 Subject: [PATCH] [clang-tidy] Improve integer comparison by matching valid expressions outside implicitCastExpr --- .../UseIntegerSignComparisonCheck.cpp | 17 +--- clang-tools-extra/docs/ReleaseNotes.rst | 4 +++ .../modernize/use-integer-sign-comparison.cpp | 26 +++ 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/clang-tools-extra/clang-tidy/modernize/UseIntegerSignComparisonCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseIntegerSignComparisonCheck.cpp index eeba5cce80da5..8f2bb4c4ba8f2 100644 --- a/clang-tools-extra/clang-tidy/modernize/UseIntegerSignComparisonCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/UseIntegerSignComparisonCheck.cpp @@ -39,9 +39,11 @@ intCastExpression(bool IsSigned, // std::cmp_{} functions trigger a compile-time error if either LHS or RHS // is a non-integer type, char, enum or bool // (unsigned char/ signed char are Ok and can be used). - auto IntTypeExpr = expr(hasType(hasCanonicalType(qualType( + const auto HasIntegerType = hasType(hasCanonicalType(qualType( isInteger(), IsSigned ? isSignedInteger() : isUnsignedInteger(), - unless(isActualChar()), unless(booleanType()), unless(enumType()); + unless(isActualChar()), unless(booleanType()), unless(enumType(); + + auto IntTypeExpr = expr(HasIntegerType); const auto ImplicitCastExpr = CastBindName.empty() ? implicitCastExpr(hasSourceExpression(IntTypeExpr)) @@ -52,8 +54,17 @@ intCastExpression(bool IsSigned, const auto StaticCastExpr = cxxStaticCastExpr(has(ImplicitCastExpr)); const auto FunctionalCastExpr = cxxFunctionalCastExpr(has(ImplicitCastExpr)); + // Match function calls or variable references not directly wrapped by an + // implicit cast + const auto CallIntExpr = CastBindName.empty() + ? callExpr(HasIntegerType) + : callExpr(HasIntegerType).bind(CastBindName); + const auto DeclRefIntExpr = + CastBindName.empty() ? declRefExpr(HasIntegerType) + : declRefExpr(HasIntegerType).bind(CastBindName); + return expr(anyOf(ImplicitCastExpr, CStyleCastExpr, StaticCastExpr, -FunctionalCastExpr)); +FunctionalCastExpr, CallIntExpr)); } static StringRef parseOpCode(BinaryOperator::Opcode Code) { diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 6cb8d572d3a78..d90f277045003 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -177,6 +177,10 @@ Changes in existing checks matched scenarios of ``find`` and ``rfind`` methods and fixing false positives when those methods were called with 3 arguments. +- Improved :doc:`modernize-use-integer-sign-comparison + ` check by matching + valid integer expressions not directly wrapped around an Implicit Cast. + - Improved :doc:`modernize-use-std-numbers ` check to support math functions of different precisions. diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-integer-sign-comparison.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-integer-sign-comparison.cpp index 99f00444c2d3f..1d2f64a359a2c 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-integer-sign-comparison.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-integer-sign-comparison.cpp @@ -120,3 +120,29 @@ int AllComparisons() { return 0; } + +namespace PR127471 { +int getSignedValue(); +unsigned int getUnsignedValue(); + +void callExprTest() { + +if (getSignedValue() < getUnsignedValue()) +return; +// CHECK-MESSAGES: :[[@LINE-2]]:13: warning: comparison between 'signed' and 'unsigned' integers [modernize-use-integer-sign-comparison] +// CHECK-FIXES: if (std::cmp_less(getSignedValue() , getUnsignedValue())) + +int sVar = 0; +if (getUnsignedValue() > sVar) +return; +// CHECK-MESSAGES: :[[@LINE-2]]:13: warning: comparison between 'signed' and 'unsigned' integers [modernize-use-integer-sign-comparison] +// CHECK-FIXES: if (std::cmp_greater(getUnsignedValue() , sVar)) + +unsigned int uVar = 0; +if (getSignedValue() > uVar) +return; +// CHECK-MESSAGES: :[[@LINE-2]]:13: warning: comparison between 'signed' and 'unsigned' integers [modernize-use-integer-sign-comparison] +// CHECK-FIXES: if (std::cmp_greater(getSignedValue() , uVar)) + +} +} // namespace PR127471 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] detect arithmetic operations within member list initialization in modernize-use-default-member-init check (PR #129370)
RiverDave wrote: Ping @5chmidti @PiotrZSL @carlosgalvezp https://github.com/llvm/llvm-project/pull/129370 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy][C++20] Add support for Initialization Forwarding in structs and Nested Objects within modernize-use-emplace (PR #131969)
https://github.com/RiverDave updated https://github.com/llvm/llvm-project/pull/131969 >From 760916be170bc3a3e4188f9b399050cc92ab334f Mon Sep 17 00:00:00 2001 From: David Rivera Date: Sun, 16 Mar 2025 16:20:16 -0400 Subject: [PATCH] [clang-tidy] Add support for Initialization Forwarding in Nested Objects within modernize-use-emplace --- .../clang-tidy/modernize/UseEmplaceCheck.cpp | 105 +++--- clang-tools-extra/docs/ReleaseNotes.rst | 4 + .../checkers/modernize/use-emplace.cpp| 48 ++-- 3 files changed, 132 insertions(+), 25 deletions(-) diff --git a/clang-tools-extra/clang-tidy/modernize/UseEmplaceCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseEmplaceCheck.cpp index 430455a38f395..ccc939e4b89fa 100644 --- a/clang-tools-extra/clang-tidy/modernize/UseEmplaceCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/UseEmplaceCheck.cpp @@ -98,8 +98,8 @@ auto hasWantedType(llvm::ArrayRef TypeNames) { // Matches member call expressions of the named method on the listed container // types. -auto cxxMemberCallExprOnContainer( -StringRef MethodName, llvm::ArrayRef ContainerNames) { +auto cxxMemberCallExprOnContainer(StringRef MethodName, + llvm::ArrayRef ContainerNames) { return cxxMemberCallExpr( hasDeclaration(functionDecl(hasName(MethodName))), on(hasTypeOrPointeeType(hasWantedType(ContainerNames; @@ -174,19 +174,19 @@ void UseEmplaceCheck::registerMatchers(MatchFinder *Finder) { // passed pointer because smart pointer won't be constructed // (and destructed) as in push_back case. auto IsCtorOfSmartPtr = - hasDeclaration(cxxConstructorDecl(ofClass(hasAnyName(SmartPointers; + cxxConstructorDecl(ofClass(hasAnyName(SmartPointers))); // Bitfields binds only to consts and emplace_back take it by universal ref. - auto BitFieldAsArgument = hasAnyArgument( - ignoringImplicit(memberExpr(hasDeclaration(fieldDecl(isBitField()); + auto BitFieldAsArgument = + ignoringImplicit(memberExpr(hasDeclaration(fieldDecl(isBitField(); // Initializer list can't be passed to universal reference. - auto InitializerListAsArgument = hasAnyArgument( + auto InitializerListAsArgument = ignoringImplicit(allOf(cxxConstructExpr(isListInitialization()), - unless(cxxTemporaryObjectExpr(); + unless(cxxTemporaryObjectExpr(; // We could have leak of resource. - auto NewExprAsArgument = hasAnyArgument(ignoringImplicit(cxxNewExpr())); + auto NewExprAsArgument = ignoringImplicit(cxxNewExpr()); // We would call another constructor. auto ConstructingDerived = hasParent(implicitCastExpr(hasCastKind(CastKind::CK_DerivedToBase))); @@ -202,11 +202,26 @@ void UseEmplaceCheck::registerMatchers(MatchFinder *Finder) { // overloaded functions and template names. auto SoughtConstructExpr = cxxConstructExpr( - unless(anyOf(IsCtorOfSmartPtr, HasInitList, BitFieldAsArgument, - InitializerListAsArgument, NewExprAsArgument, - ConstructingDerived, IsPrivateOrProtectedCtor))) + unless(anyOf(hasDeclaration(IsCtorOfSmartPtr), HasInitList, + hasAnyArgument(BitFieldAsArgument), + hasAnyArgument(InitializerListAsArgument), + hasAnyArgument(NewExprAsArgument), ConstructingDerived, + IsPrivateOrProtectedCtor))) .bind("ctor"); - auto HasConstructExpr = has(ignoringImplicit(SoughtConstructExpr)); + + auto IsPrimitiveType = hasType(builtinType()); + + auto AggregateInitExpr = + getLangOpts().CPlusPlus20 + ? initListExpr(unless(anyOf(HasInitList, has(IsCtorOfSmartPtr), + has(BitFieldAsArgument), + has(InitializerListAsArgument), + has(NewExprAsArgument), IsPrimitiveType))) +.bind("agg_init") + : unless(anything()); + + auto HasConstructExpr = + has(ignoringImplicit(anyOf(SoughtConstructExpr, AggregateInitExpr))); // allow for T{} to be replaced, even if no CTOR is declared auto HasConstructInitListExpr = has(initListExpr( @@ -305,6 +320,36 @@ void UseEmplaceCheck::registerMatchers(MatchFinder *Finder) { this); } +static const CXXConstructExpr *unwrapToConstructorExpr(const Expr *E) { + + while (E) { + +if (const auto *ConstructorExpr = llvm::dyn_cast(E)) { + return ConstructorExpr; +} + +if (const auto *BindTemp = llvm::dyn_cast(E)) { + E = BindTemp->getSubExpr(); + continue; +} + +if (const auto *MaterialTemp = +llvm::dyn_cast(E)) { + E = MaterialTemp->getSubExpr(); + continue; +} + +if (const auto *Cast = llvm::dyn_cast(E)) { + E = Cast->getSubExpr(); + continue; +} + +break; + } + + return nullp
[clang-tools-extra] [clang-tidy][C++20] Add support for Initialization Forwarding in structs and Nested Objects within modernize-use-emplace (PR #131969)
@@ -332,19 +380,44 @@ void UseEmplaceCheck::check(const MatchFinder::MatchResult &Result) { }(); assert(Call && "No call matched"); - assert((CtorCall || MakeCall) && "No push_back parameter matched"); + assert((CtorCall || MakeCall || AggInitCall) && + "No push_back parameter matched"); if (IgnoreImplicitConstructors && CtorCall && CtorCall->getNumArgs() >= 1 && CtorCall->getArg(0)->getSourceRange() == CtorCall->getSourceRange()) return; + if (IgnoreImplicitConstructors && AggInitCall && + AggInitCall->getNumInits() >= 1 && + AggInitCall->getInit(0)->getSourceRange() == + AggInitCall->getSourceRange()) +return; + + if (getLangOpts().LangStd >= LangStandard::lang_cxx20 && AggInitCall) { +for (const auto *Init : AggInitCall->inits()) { + if (const auto *InnermostInit = unwrapInnerExpression(Init)) { RiverDave wrote: Good catch, thx for the feedback, should be fixed now. https://github.com/llvm/llvm-project/pull/131969 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy][C++20] Add support for Initialization Forwarding in structs and Nested Objects within modernize-use-emplace (PR #131969)
RiverDave wrote: Ping https://github.com/llvm/llvm-project/pull/131969 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Avoid diagnosing std::array initializations for modernize-use-designated-initializers (PR #134774)
RiverDave wrote: > We should fix `IgnoreSingleElementAggregates` instead. great observation, will fix https://github.com/llvm/llvm-project/pull/134774 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy][C++20] Add support for Initialization Forwarding in structs and Nested Objects within modernize-use-emplace (PR #131969)
@@ -1285,11 +1289,9 @@ void testBracedInitTemporaries() { // These should not be noticed or fixed; after the correction, the code won't - // compile. + // compile in version previous to C++20. RiverDave wrote: Indeed, forgot to delete that, thx https://github.com/llvm/llvm-project/pull/131969 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy][C++20] Add support for Initialization Forwarding in structs and Nested Objects within modernize-use-emplace (PR #131969)
https://github.com/RiverDave edited https://github.com/llvm/llvm-project/pull/131969 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy][C++20] Add support for Initialization Forwarding in structs and Nested Objects within modernize-use-emplace (PR #131969)
https://github.com/RiverDave updated https://github.com/llvm/llvm-project/pull/131969 >From 89412d91ff6cd38a8060c84f66bbab3caead2478 Mon Sep 17 00:00:00 2001 From: David Rivera Date: Sun, 16 Mar 2025 16:20:16 -0400 Subject: [PATCH] [clang-tidy] Add support for Initialization Forwarding in Nested Objects within modernize-use-emplace --- .../clang-tidy/modernize/UseEmplaceCheck.cpp | 105 +++--- clang-tools-extra/docs/ReleaseNotes.rst | 4 + .../checkers/modernize/use-emplace.cpp| 48 ++-- 3 files changed, 132 insertions(+), 25 deletions(-) diff --git a/clang-tools-extra/clang-tidy/modernize/UseEmplaceCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseEmplaceCheck.cpp index 430455a38f395..6f9875674448d 100644 --- a/clang-tools-extra/clang-tidy/modernize/UseEmplaceCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/UseEmplaceCheck.cpp @@ -98,8 +98,8 @@ auto hasWantedType(llvm::ArrayRef TypeNames) { // Matches member call expressions of the named method on the listed container // types. -auto cxxMemberCallExprOnContainer( -StringRef MethodName, llvm::ArrayRef ContainerNames) { +auto cxxMemberCallExprOnContainer(StringRef MethodName, + llvm::ArrayRef ContainerNames) { return cxxMemberCallExpr( hasDeclaration(functionDecl(hasName(MethodName))), on(hasTypeOrPointeeType(hasWantedType(ContainerNames; @@ -174,19 +174,19 @@ void UseEmplaceCheck::registerMatchers(MatchFinder *Finder) { // passed pointer because smart pointer won't be constructed // (and destructed) as in push_back case. auto IsCtorOfSmartPtr = - hasDeclaration(cxxConstructorDecl(ofClass(hasAnyName(SmartPointers; + cxxConstructorDecl(ofClass(hasAnyName(SmartPointers))); // Bitfields binds only to consts and emplace_back take it by universal ref. - auto BitFieldAsArgument = hasAnyArgument( - ignoringImplicit(memberExpr(hasDeclaration(fieldDecl(isBitField()); + auto BitFieldAsArgument = + ignoringImplicit(memberExpr(hasDeclaration(fieldDecl(isBitField(); // Initializer list can't be passed to universal reference. - auto InitializerListAsArgument = hasAnyArgument( + auto InitializerListAsArgument = ignoringImplicit(allOf(cxxConstructExpr(isListInitialization()), - unless(cxxTemporaryObjectExpr(); + unless(cxxTemporaryObjectExpr(; // We could have leak of resource. - auto NewExprAsArgument = hasAnyArgument(ignoringImplicit(cxxNewExpr())); + auto NewExprAsArgument = ignoringImplicit(cxxNewExpr()); // We would call another constructor. auto ConstructingDerived = hasParent(implicitCastExpr(hasCastKind(CastKind::CK_DerivedToBase))); @@ -202,11 +202,26 @@ void UseEmplaceCheck::registerMatchers(MatchFinder *Finder) { // overloaded functions and template names. auto SoughtConstructExpr = cxxConstructExpr( - unless(anyOf(IsCtorOfSmartPtr, HasInitList, BitFieldAsArgument, - InitializerListAsArgument, NewExprAsArgument, - ConstructingDerived, IsPrivateOrProtectedCtor))) + unless(anyOf(hasDeclaration(IsCtorOfSmartPtr), HasInitList, + hasAnyArgument(BitFieldAsArgument), + hasAnyArgument(InitializerListAsArgument), + hasAnyArgument(NewExprAsArgument), ConstructingDerived, + IsPrivateOrProtectedCtor))) .bind("ctor"); - auto HasConstructExpr = has(ignoringImplicit(SoughtConstructExpr)); + + auto IsPrimitiveType = hasType(builtinType()); + + auto AggregateInitExpr = + getLangOpts().CPlusPlus20 + ? initListExpr(unless(anyOf(HasInitList, has(IsCtorOfSmartPtr), + has(BitFieldAsArgument), + has(InitializerListAsArgument), + has(NewExprAsArgument), IsPrimitiveType))) +.bind("agg_init") + : unless(anything()); + + auto HasConstructExpr = + has(ignoringImplicit(anyOf(SoughtConstructExpr, AggregateInitExpr))); // allow for T{} to be replaced, even if no CTOR is declared auto HasConstructInitListExpr = has(initListExpr( @@ -305,6 +320,36 @@ void UseEmplaceCheck::registerMatchers(MatchFinder *Finder) { this); } +static const CXXConstructExpr *unwrapToConstructorExpr(const Expr *E) { + + while (E) { + +if (const auto *ConstructorExpr = llvm::dyn_cast(E)) { + return ConstructorExpr; +} + +if (const auto *BindTemp = llvm::dyn_cast(E)) { + E = BindTemp->getSubExpr(); + continue; +} + +if (const auto *MaterialTemp = +llvm::dyn_cast(E)) { + E = MaterialTemp->getSubExpr(); + continue; +} + +if (const auto *Cast = llvm::dyn_cast(E)) { + E = Cast->getSubExpr(); + continue; +} + +break; + } + + return nullp
[clang-tools-extra] [clang-tidy][C++20] Add support for Initialization Forwarding in structs and Nested Objects within modernize-use-emplace (PR #131969)
@@ -1285,11 +1289,9 @@ void testBracedInitTemporaries() { // These should not be noticed or fixed; after the correction, the code won't - // compile. + // compile in version previous to C++20. RiverDave wrote: All should be addressed now https://github.com/llvm/llvm-project/pull/131969 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy][C++20] Add support for Initialization Forwarding in structs and Nested Objects within modernize-use-emplace (PR #131969)
https://github.com/RiverDave updated https://github.com/llvm/llvm-project/pull/131969 >From 27f7da4bee6ea2f5c1c5dcd899bfe980df30f0ce Mon Sep 17 00:00:00 2001 From: David Rivera Date: Sun, 16 Mar 2025 16:20:16 -0400 Subject: [PATCH] [clang-tidy] Add support for Initialization Forwarding in Nested Objects within modernize-use-emplace --- .../clang-tidy/modernize/UseEmplaceCheck.cpp | 103 +++--- clang-tools-extra/docs/ReleaseNotes.rst | 4 + .../checkers/modernize/use-emplace.cpp| 46 ++-- 3 files changed, 129 insertions(+), 24 deletions(-) diff --git a/clang-tools-extra/clang-tidy/modernize/UseEmplaceCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseEmplaceCheck.cpp index 430455a38f395..1feb5593ade38 100644 --- a/clang-tools-extra/clang-tidy/modernize/UseEmplaceCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/UseEmplaceCheck.cpp @@ -98,8 +98,8 @@ auto hasWantedType(llvm::ArrayRef TypeNames) { // Matches member call expressions of the named method on the listed container // types. -auto cxxMemberCallExprOnContainer( -StringRef MethodName, llvm::ArrayRef ContainerNames) { +auto cxxMemberCallExprOnContainer(StringRef MethodName, + llvm::ArrayRef ContainerNames) { return cxxMemberCallExpr( hasDeclaration(functionDecl(hasName(MethodName))), on(hasTypeOrPointeeType(hasWantedType(ContainerNames; @@ -174,19 +174,19 @@ void UseEmplaceCheck::registerMatchers(MatchFinder *Finder) { // passed pointer because smart pointer won't be constructed // (and destructed) as in push_back case. auto IsCtorOfSmartPtr = - hasDeclaration(cxxConstructorDecl(ofClass(hasAnyName(SmartPointers; + cxxConstructorDecl(ofClass(hasAnyName(SmartPointers))); // Bitfields binds only to consts and emplace_back take it by universal ref. - auto BitFieldAsArgument = hasAnyArgument( - ignoringImplicit(memberExpr(hasDeclaration(fieldDecl(isBitField()); + auto BitFieldAsArgument = + ignoringImplicit(memberExpr(hasDeclaration(fieldDecl(isBitField(); // Initializer list can't be passed to universal reference. - auto InitializerListAsArgument = hasAnyArgument( + auto InitializerListAsArgument = ignoringImplicit(allOf(cxxConstructExpr(isListInitialization()), - unless(cxxTemporaryObjectExpr(); + unless(cxxTemporaryObjectExpr(; // We could have leak of resource. - auto NewExprAsArgument = hasAnyArgument(ignoringImplicit(cxxNewExpr())); + auto NewExprAsArgument = ignoringImplicit(cxxNewExpr()); // We would call another constructor. auto ConstructingDerived = hasParent(implicitCastExpr(hasCastKind(CastKind::CK_DerivedToBase))); @@ -202,11 +202,26 @@ void UseEmplaceCheck::registerMatchers(MatchFinder *Finder) { // overloaded functions and template names. auto SoughtConstructExpr = cxxConstructExpr( - unless(anyOf(IsCtorOfSmartPtr, HasInitList, BitFieldAsArgument, - InitializerListAsArgument, NewExprAsArgument, - ConstructingDerived, IsPrivateOrProtectedCtor))) + unless(anyOf(hasDeclaration(IsCtorOfSmartPtr), HasInitList, + hasAnyArgument(BitFieldAsArgument), + hasAnyArgument(InitializerListAsArgument), + hasAnyArgument(NewExprAsArgument), ConstructingDerived, + IsPrivateOrProtectedCtor))) .bind("ctor"); - auto HasConstructExpr = has(ignoringImplicit(SoughtConstructExpr)); + + auto IsPrimitiveType = hasType(builtinType()); + + auto AggregateInitExpr = + getLangOpts().CPlusPlus20 + ? initListExpr(unless(anyOf(HasInitList, has(IsCtorOfSmartPtr), + has(BitFieldAsArgument), + has(InitializerListAsArgument), + has(NewExprAsArgument), IsPrimitiveType))) +.bind("agg_init") + : unless(anything()); + + auto HasConstructExpr = + has(ignoringImplicit(anyOf(SoughtConstructExpr, AggregateInitExpr))); // allow for T{} to be replaced, even if no CTOR is declared auto HasConstructInitListExpr = has(initListExpr( @@ -305,6 +320,34 @@ void UseEmplaceCheck::registerMatchers(MatchFinder *Finder) { this); } +static const CXXConstructExpr *unwrapToConstructorExpr(const Expr *E) { + while (E) { +if (const auto *ConstructorExpr = llvm::dyn_cast(E)) { + return ConstructorExpr; +} + +if (const auto *BindTemp = llvm::dyn_cast(E)) { + E = BindTemp->getSubExpr(); + continue; +} + +if (const auto *MaterialTemp = +llvm::dyn_cast(E)) { + E = MaterialTemp->getSubExpr(); + continue; +} + +if (const auto *Cast = llvm::dyn_cast(E)) { + E = Cast->getSubExpr(); + continue; +} + +break; + } + + return nullptr;
[clang-tools-extra] [clang-tidy] detect arithmetic operations within member list initialization in modernize-use-default-member-init check (PR #129370)
RiverDave wrote: Ping https://github.com/llvm/llvm-project/pull/129370 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] detect arithmetic operations within member list initialization in modernize-use-default-member-init check (PR #129370)
https://github.com/RiverDave updated https://github.com/llvm/llvm-project/pull/129370 >From 85796af53e845110824b11b0f35c7ab24827c7d5 Mon Sep 17 00:00:00 2001 From: David Rivera Date: Sat, 1 Mar 2025 02:09:02 -0500 Subject: [PATCH] [clang-tidy] detect arithmetic operations within member list initialization in modernize-use-default-member-init --- .../modernize/UseDefaultMemberInitCheck.cpp | 30 +-- clang-tools-extra/docs/ReleaseNotes.rst | 4 +-- .../modernize/use-default-member-init.cpp | 23 ++ 3 files changed, 46 insertions(+), 11 deletions(-) diff --git a/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp index bf99c738da1a3..9ad6dcefbc1bb 100644 --- a/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp @@ -159,6 +159,13 @@ static bool sameValue(const Expr *E1, const Expr *E2) { case Stmt::UnaryOperatorClass: return sameValue(cast(E1)->getSubExpr(), cast(E2)->getSubExpr()); + case Stmt::BinaryOperatorClass: { +const auto *BinOp1 = cast(E1); +const auto *BinOp2 = cast(E2); +return BinOp1->getOpcode() == BinOp2->getOpcode() && + sameValue(BinOp1->getLHS(), BinOp2->getLHS()) && + sameValue(BinOp1->getRHS(), BinOp2->getRHS()); + } case Stmt::CharacterLiteralClass: return cast(E1)->getValue() == cast(E2)->getValue(); @@ -199,17 +206,22 @@ void UseDefaultMemberInitCheck::storeOptions( } void UseDefaultMemberInitCheck::registerMatchers(MatchFinder *Finder) { - auto ConstExpRef = varDecl(anyOf(isConstexpr(), isStaticStorageClass())); + auto NumericLiteral = anyOf(integerLiteral(), floatLiteral()); + auto UnaryNumericLiteral = unaryOperator(hasAnyOperatorName("+", "-"), + hasUnaryOperand(NumericLiteral)); + + auto ConstExprRef = varDecl(anyOf(isConstexpr(), isStaticStorageClass())); + auto ImmutableRef = + declRefExpr(to(decl(anyOf(enumConstantDecl(), ConstExprRef; + + auto BinaryNumericExpr = binaryOperator( + hasOperands(anyOf(NumericLiteral, ImmutableRef, binaryOperator()), + anyOf(NumericLiteral, ImmutableRef, binaryOperator(; auto InitBase = - anyOf(stringLiteral(), characterLiteral(), integerLiteral(), -unaryOperator(hasAnyOperatorName("+", "-"), - hasUnaryOperand(integerLiteral())), -floatLiteral(), -unaryOperator(hasAnyOperatorName("+", "-"), - hasUnaryOperand(floatLiteral())), -cxxBoolLiteral(), cxxNullPtrLiteralExpr(), implicitValueInitExpr(), -declRefExpr(to(anyOf(enumConstantDecl(), ConstExpRef; + anyOf(stringLiteral(), characterLiteral(), NumericLiteral, +UnaryNumericLiteral, cxxBoolLiteral(), cxxNullPtrLiteralExpr(), +implicitValueInitExpr(), ImmutableRef, BinaryNumericExpr); auto ExplicitCastExpr = castExpr(hasSourceExpression(InitBase)); auto InitMatcher = anyOf(InitBase, ExplicitCastExpr); diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index eaf37e746050e..da2fe319b9b87 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -159,8 +159,8 @@ Changes in existing checks - Improved :doc:`modernize-use-default-member-init ` check by matching - ``constexpr`` and ``static``` values on member initialization and by detecting - explicit casting of built-in types within member list initialization. + arithmetic operations, constexpr and static values, and detecting explicit + casting of built-in types within member list initialization. - Improved :doc:`modernize-use-ranges ` check by updating suppress diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp index cac50be9e4368..bb2a7388e75f1 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp @@ -572,4 +572,27 @@ class FunctionalCastInit { // CHECK-FIXES: double c{double('C')}; }; +#define ARITHMETIC_MACRO (44 - 2) + +class DefaultMemberInitWithArithmetic { + DefaultMemberInitWithArithmetic() : a{1 + 1}, b{1 + 11 + 123 + 1234}, c{2 + (4 / 2) + 3 + (7 / 11)}, d{ARITHMETIC_MACRO * 2}, e{1.2 + 3.4} {} + // CHECK-MESSAGES: :[[@LINE-1]]:39: warning: member initializer for 'a' is redundant [modernize-use-default-member-init] + // CHECK-FIXES: DefaultMemberInitWithArithmetic() {} + + int a{1 + 1}; + int b; + // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use default member initializer for 'b' [modernize-use-default-member-init] + //
[clang-tools-extra] [clang-tidy] Avoid diagnosing std::array initializations for modernize-use-designated-initializers (PR #134774)
https://github.com/RiverDave edited https://github.com/llvm/llvm-project/pull/134774 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Avoid diagnosing std::array initializations for modernize-use-designated-initializers (PR #134774)
RiverDave wrote: > We should fix `IgnoreSingleElementAggregates` instead. Very good observation I didn't get this detail when investigating this bug. indeed, we shouldn't warn when the 3 conditions you previously mentioned: > 1) the class is an aggregate and 2) it contains only one member and 3) that > member is an array type? are true and `IgnoreSingleElementAggregates` is toggled off https://github.com/llvm/llvm-project/pull/134774 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Avoid diagnosing std::array initializations for modernize-use-designated-initializers (PR #134774)
RiverDave wrote: > We should fix `IgnoreSingleElementAggregates` instead. Just to be clear, I suppose we're looking to fix `std::array` + `IgnoreSingleElementAggregates = false` as It's the only problematic combination I could analyze. The fixit this check provides on aggregate types with a single field of array type, seem to be correct to me: https://godbolt.org/z/Tahfa7onP https://github.com/llvm/llvm-project/pull/134774 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Avoid diagnosing std::array initializations for modernize-use-designated-initializers (PR #134774)
https://github.com/RiverDave edited https://github.com/llvm/llvm-project/pull/134774 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Improve integer comparison by matching valid expressions outside implicitCastExpr (PR #134188)
https://github.com/RiverDave created https://github.com/llvm/llvm-project/pull/134188 Covered the edge case where an int expression is not necessarily directly wrapped around an ImplicitCastExpr which seemed to be a requirement in this check to trigger. **For instance**: ```cpp #include bool f() { std::vector v; unsigned int i = 0; return i >= v.size(); } ``` **See AST:**  >From ebce879a943f5817aca4b0d3d637a17a626c9683 Mon Sep 17 00:00:00 2001 From: David Rivera Date: Wed, 2 Apr 2025 21:02:00 -0400 Subject: [PATCH] [clang-tidy] Improve integer comparison by matching valid expressions outside implicitCastExpr --- .../UseIntegerSignComparisonCheck.cpp | 16 +--- clang-tools-extra/docs/ReleaseNotes.rst | 4 +++ .../modernize/use-integer-sign-comparison.cpp | 26 +++ 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/clang-tools-extra/clang-tidy/modernize/UseIntegerSignComparisonCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseIntegerSignComparisonCheck.cpp index eeba5cce80da5..089df7ece3f82 100644 --- a/clang-tools-extra/clang-tidy/modernize/UseIntegerSignComparisonCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/UseIntegerSignComparisonCheck.cpp @@ -39,9 +39,11 @@ intCastExpression(bool IsSigned, // std::cmp_{} functions trigger a compile-time error if either LHS or RHS // is a non-integer type, char, enum or bool // (unsigned char/ signed char are Ok and can be used). - auto IntTypeExpr = expr(hasType(hasCanonicalType(qualType( + const auto HasIntegerType = hasType(hasCanonicalType(qualType( isInteger(), IsSigned ? isSignedInteger() : isUnsignedInteger(), - unless(isActualChar()), unless(booleanType()), unless(enumType()); + unless(isActualChar()), unless(booleanType()), unless(enumType(); + + auto IntTypeExpr = expr(HasIntegerType); const auto ImplicitCastExpr = CastBindName.empty() ? implicitCastExpr(hasSourceExpression(IntTypeExpr)) @@ -52,8 +54,16 @@ intCastExpression(bool IsSigned, const auto StaticCastExpr = cxxStaticCastExpr(has(ImplicitCastExpr)); const auto FunctionalCastExpr = cxxFunctionalCastExpr(has(ImplicitCastExpr)); + // Match function calls or variable references not wrapped by an implicit cast + const auto CallIntExpr = CastBindName.empty() + ? callExpr(HasIntegerType) + : callExpr(HasIntegerType).bind(CastBindName); + const auto DeclRefIntExpr = + CastBindName.empty() ? declRefExpr(HasIntegerType) + : declRefExpr(HasIntegerType).bind(CastBindName); + return expr(anyOf(ImplicitCastExpr, CStyleCastExpr, StaticCastExpr, -FunctionalCastExpr)); +FunctionalCastExpr, CallIntExpr)); } static StringRef parseOpCode(BinaryOperator::Opcode Code) { diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 6cb8d572d3a78..b5f2d8e8fcbd7 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -177,6 +177,10 @@ Changes in existing checks matched scenarios of ``find`` and ``rfind`` methods and fixing false positives when those methods were called with 3 arguments. +- Improved :doc:`modernize-use-integer-sign-comparison + ` check by matching + valid integer expressions not wrapped around an Implicit Cast. + - Improved :doc:`modernize-use-std-numbers ` check to support math functions of different precisions. diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-integer-sign-comparison.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-integer-sign-comparison.cpp index 99f00444c2d3f..1d2f64a359a2c 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-integer-sign-comparison.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-integer-sign-comparison.cpp @@ -120,3 +120,29 @@ int AllComparisons() { return 0; } + +namespace PR127471 { +int getSignedValue(); +unsigned int getUnsignedValue(); + +void callExprTest() { + +if (getSignedValue() < getUnsignedValue()) +return; +// CHECK-MESSAGES: :[[@LINE-2]]:13: warning: comparison between 'signed' and 'unsigned' integers [modernize-use-integer-sign-comparison] +// CHECK-FIXES: if (std::cmp_less(getSignedValue() , getUnsignedValue())) + +int sVar = 0; +if (getUnsignedValue() > sVar) +return; +// CHECK-MESSAGES: :[[@LINE-2]]:13: warning: comparison between 'signed' and 'unsigned' integers [modernize-use-integer-sign-comparison] +// CHECK-FIXES: if (std::cmp_greater(getUnsignedValue() , sVar)) + +unsigned int uVar = 0; +if (getSignedValue() > uVar) +return; +// CHECK-MESSAGES: :[[@LINE-2]]:13: warning: comparison between 'signed' and 'unsig
[clang-tools-extra] [clang-tidy] Improve integer comparison by matching valid expressions outside implicitCastExpr (PR #134188)
https://github.com/RiverDave updated https://github.com/llvm/llvm-project/pull/134188 >From ac9833cd5abdaf4a91c785d5fa9d51190219851a Mon Sep 17 00:00:00 2001 From: David Rivera Date: Wed, 2 Apr 2025 21:02:00 -0400 Subject: [PATCH] [clang-tidy] Improve integer comparison by matching valid expressions outside implicitCastExpr --- .../UseIntegerSignComparisonCheck.cpp | 16 +--- clang-tools-extra/docs/ReleaseNotes.rst | 4 +++ .../modernize/use-integer-sign-comparison.cpp | 26 +++ 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/clang-tools-extra/clang-tidy/modernize/UseIntegerSignComparisonCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseIntegerSignComparisonCheck.cpp index eeba5cce80da5..63e044f8ca993 100644 --- a/clang-tools-extra/clang-tidy/modernize/UseIntegerSignComparisonCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/UseIntegerSignComparisonCheck.cpp @@ -39,9 +39,11 @@ intCastExpression(bool IsSigned, // std::cmp_{} functions trigger a compile-time error if either LHS or RHS // is a non-integer type, char, enum or bool // (unsigned char/ signed char are Ok and can be used). - auto IntTypeExpr = expr(hasType(hasCanonicalType(qualType( + const auto HasIntegerType = hasType(hasCanonicalType(qualType( isInteger(), IsSigned ? isSignedInteger() : isUnsignedInteger(), - unless(isActualChar()), unless(booleanType()), unless(enumType()); + unless(isActualChar()), unless(booleanType()), unless(enumType(); + + auto IntTypeExpr = expr(HasIntegerType); const auto ImplicitCastExpr = CastBindName.empty() ? implicitCastExpr(hasSourceExpression(IntTypeExpr)) @@ -52,8 +54,16 @@ intCastExpression(bool IsSigned, const auto StaticCastExpr = cxxStaticCastExpr(has(ImplicitCastExpr)); const auto FunctionalCastExpr = cxxFunctionalCastExpr(has(ImplicitCastExpr)); + // Match function calls or variable references not directly wrapped by an implicit cast + const auto CallIntExpr = CastBindName.empty() + ? callExpr(HasIntegerType) + : callExpr(HasIntegerType).bind(CastBindName); + const auto DeclRefIntExpr = + CastBindName.empty() ? declRefExpr(HasIntegerType) + : declRefExpr(HasIntegerType).bind(CastBindName); + return expr(anyOf(ImplicitCastExpr, CStyleCastExpr, StaticCastExpr, -FunctionalCastExpr)); +FunctionalCastExpr, CallIntExpr)); } static StringRef parseOpCode(BinaryOperator::Opcode Code) { diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 6cb8d572d3a78..d90f277045003 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -177,6 +177,10 @@ Changes in existing checks matched scenarios of ``find`` and ``rfind`` methods and fixing false positives when those methods were called with 3 arguments. +- Improved :doc:`modernize-use-integer-sign-comparison + ` check by matching + valid integer expressions not directly wrapped around an Implicit Cast. + - Improved :doc:`modernize-use-std-numbers ` check to support math functions of different precisions. diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-integer-sign-comparison.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-integer-sign-comparison.cpp index 99f00444c2d3f..1d2f64a359a2c 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-integer-sign-comparison.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-integer-sign-comparison.cpp @@ -120,3 +120,29 @@ int AllComparisons() { return 0; } + +namespace PR127471 { +int getSignedValue(); +unsigned int getUnsignedValue(); + +void callExprTest() { + +if (getSignedValue() < getUnsignedValue()) +return; +// CHECK-MESSAGES: :[[@LINE-2]]:13: warning: comparison between 'signed' and 'unsigned' integers [modernize-use-integer-sign-comparison] +// CHECK-FIXES: if (std::cmp_less(getSignedValue() , getUnsignedValue())) + +int sVar = 0; +if (getUnsignedValue() > sVar) +return; +// CHECK-MESSAGES: :[[@LINE-2]]:13: warning: comparison between 'signed' and 'unsigned' integers [modernize-use-integer-sign-comparison] +// CHECK-FIXES: if (std::cmp_greater(getUnsignedValue() , sVar)) + +unsigned int uVar = 0; +if (getSignedValue() > uVar) +return; +// CHECK-MESSAGES: :[[@LINE-2]]:13: warning: comparison between 'signed' and 'unsigned' integers [modernize-use-integer-sign-comparison] +// CHECK-FIXES: if (std::cmp_greater(getSignedValue() , uVar)) + +} +} // namespace PR127471 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy][C++20] Add support for Initialization Forwarding in Nested Objects (PR #131969)
https://github.com/RiverDave edited https://github.com/llvm/llvm-project/pull/131969 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy][C++20] Add support for Initialization Forwarding in Nested Object Construction (PR #131969)
https://github.com/RiverDave edited https://github.com/llvm/llvm-project/pull/131969 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy][C++20] Add support for Initialization Forwarding in Nested Object Construction (PR #131969)
https://github.com/RiverDave updated https://github.com/llvm/llvm-project/pull/131969 >From c966cef850ed1350264110e622866dae66f99a07 Mon Sep 17 00:00:00 2001 From: David Rivera Date: Sun, 16 Mar 2025 16:20:16 -0400 Subject: [PATCH] [clang-tidy] Add support for Initialization Forwarding in Nested Objects within modernize-use-emplace --- .../clang-tidy/modernize/UseEmplaceCheck.cpp | 121 ++ clang-tools-extra/docs/ReleaseNotes.rst | 4 + .../checkers/modernize/use-emplace.cpp| 26 +++- 3 files changed, 126 insertions(+), 25 deletions(-) diff --git a/clang-tools-extra/clang-tidy/modernize/UseEmplaceCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseEmplaceCheck.cpp index 430455a38f395..4c601d2677257 100644 --- a/clang-tools-extra/clang-tidy/modernize/UseEmplaceCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/UseEmplaceCheck.cpp @@ -98,8 +98,8 @@ auto hasWantedType(llvm::ArrayRef TypeNames) { // Matches member call expressions of the named method on the listed container // types. -auto cxxMemberCallExprOnContainer( -StringRef MethodName, llvm::ArrayRef ContainerNames) { +auto cxxMemberCallExprOnContainer(StringRef MethodName, + llvm::ArrayRef ContainerNames) { return cxxMemberCallExpr( hasDeclaration(functionDecl(hasName(MethodName))), on(hasTypeOrPointeeType(hasWantedType(ContainerNames; @@ -174,19 +174,19 @@ void UseEmplaceCheck::registerMatchers(MatchFinder *Finder) { // passed pointer because smart pointer won't be constructed // (and destructed) as in push_back case. auto IsCtorOfSmartPtr = - hasDeclaration(cxxConstructorDecl(ofClass(hasAnyName(SmartPointers; + cxxConstructorDecl(ofClass(hasAnyName(SmartPointers))); // Bitfields binds only to consts and emplace_back take it by universal ref. - auto BitFieldAsArgument = hasAnyArgument( - ignoringImplicit(memberExpr(hasDeclaration(fieldDecl(isBitField()); + auto BitFieldAsArgument = + ignoringImplicit(memberExpr(hasDeclaration(fieldDecl(isBitField(); // Initializer list can't be passed to universal reference. - auto InitializerListAsArgument = hasAnyArgument( + auto InitializerListAsArgument = ignoringImplicit(allOf(cxxConstructExpr(isListInitialization()), - unless(cxxTemporaryObjectExpr(); + unless(cxxTemporaryObjectExpr(; // We could have leak of resource. - auto NewExprAsArgument = hasAnyArgument(ignoringImplicit(cxxNewExpr())); + auto NewExprAsArgument = ignoringImplicit(cxxNewExpr()); // We would call another constructor. auto ConstructingDerived = hasParent(implicitCastExpr(hasCastKind(CastKind::CK_DerivedToBase))); @@ -202,19 +202,36 @@ void UseEmplaceCheck::registerMatchers(MatchFinder *Finder) { // overloaded functions and template names. auto SoughtConstructExpr = cxxConstructExpr( - unless(anyOf(IsCtorOfSmartPtr, HasInitList, BitFieldAsArgument, - InitializerListAsArgument, NewExprAsArgument, - ConstructingDerived, IsPrivateOrProtectedCtor))) + unless(anyOf(hasDeclaration(IsCtorOfSmartPtr), HasInitList, + hasAnyArgument(BitFieldAsArgument), + hasAnyArgument(InitializerListAsArgument), + hasAnyArgument(NewExprAsArgument), ConstructingDerived, + IsPrivateOrProtectedCtor))) .bind("ctor"); - auto HasConstructExpr = has(ignoringImplicit(SoughtConstructExpr)); + + auto IsPrimitiveType = hasType(builtinType()); + + auto AggregateInitExpr = + getLangOpts().CPlusPlus20 + ? initListExpr(unless(anyOf(HasInitList, has(IsCtorOfSmartPtr), + has(BitFieldAsArgument), + has(InitializerListAsArgument), + has(NewExprAsArgument), IsPrimitiveType))) +.bind("agg_init") + : unless(anything()); + + auto HasConstructExpr = + has(ignoringImplicit(anyOf(SoughtConstructExpr, AggregateInitExpr))); // allow for T{} to be replaced, even if no CTOR is declared auto HasConstructInitListExpr = has(initListExpr( - initCountLeq(1), anyOf(allOf(has(SoughtConstructExpr), - has(cxxConstructExpr(argumentCountIs(0, - has(cxxBindTemporaryExpr( - has(SoughtConstructExpr), - has(cxxConstructExpr(argumentCountIs(0; + initCountLeq(1), + anyOf(allOf(has(SoughtConstructExpr), + has(cxxConstructExpr(argumentCountIs(0, +has(cxxBindTemporaryExpr(has(SoughtConstructExpr), + has(cxxConstructExpr(argumentCountIs(0))) + + ); auto HasBracedIni
[clang-tools-extra] [clang-tidy][C++20] Add support for aggregate types within modernize-use-emplace (PR #131969)
https://github.com/RiverDave edited https://github.com/llvm/llvm-project/pull/131969 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy][C++20] Add support for Initialization Forwarding in Nested Object Construction (PR #131969)
https://github.com/RiverDave edited https://github.com/llvm/llvm-project/pull/131969 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy][C++20] Add support for Initialization Forwarding in Nested Object Construction (PR #131969)
https://github.com/RiverDave edited https://github.com/llvm/llvm-project/pull/131969 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy][C++20] Add support for Initialization Forwarding in Nested Object Construction (PR #131969)
https://github.com/RiverDave updated https://github.com/llvm/llvm-project/pull/131969 >From fa0f1079587e6e26279c293fe4467ff0388f6a43 Mon Sep 17 00:00:00 2001 From: David Rivera Date: Sun, 16 Mar 2025 16:20:16 -0400 Subject: [PATCH] [clang-tidy] Add support for Initialization Forwarding in Nested Objects within modernize-use-emplace --- .../clang-tidy/modernize/UseEmplaceCheck.cpp | 120 ++ clang-tools-extra/docs/ReleaseNotes.rst | 4 + .../checkers/modernize/use-emplace.cpp| 26 +++- 3 files changed, 125 insertions(+), 25 deletions(-) diff --git a/clang-tools-extra/clang-tidy/modernize/UseEmplaceCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseEmplaceCheck.cpp index 430455a38f395..277994bbb6082 100644 --- a/clang-tools-extra/clang-tidy/modernize/UseEmplaceCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/UseEmplaceCheck.cpp @@ -98,8 +98,8 @@ auto hasWantedType(llvm::ArrayRef TypeNames) { // Matches member call expressions of the named method on the listed container // types. -auto cxxMemberCallExprOnContainer( -StringRef MethodName, llvm::ArrayRef ContainerNames) { +auto cxxMemberCallExprOnContainer(StringRef MethodName, + llvm::ArrayRef ContainerNames) { return cxxMemberCallExpr( hasDeclaration(functionDecl(hasName(MethodName))), on(hasTypeOrPointeeType(hasWantedType(ContainerNames; @@ -174,19 +174,19 @@ void UseEmplaceCheck::registerMatchers(MatchFinder *Finder) { // passed pointer because smart pointer won't be constructed // (and destructed) as in push_back case. auto IsCtorOfSmartPtr = - hasDeclaration(cxxConstructorDecl(ofClass(hasAnyName(SmartPointers; + cxxConstructorDecl(ofClass(hasAnyName(SmartPointers))); // Bitfields binds only to consts and emplace_back take it by universal ref. - auto BitFieldAsArgument = hasAnyArgument( - ignoringImplicit(memberExpr(hasDeclaration(fieldDecl(isBitField()); + auto BitFieldAsArgument = + ignoringImplicit(memberExpr(hasDeclaration(fieldDecl(isBitField(); // Initializer list can't be passed to universal reference. - auto InitializerListAsArgument = hasAnyArgument( + auto InitializerListAsArgument = ignoringImplicit(allOf(cxxConstructExpr(isListInitialization()), - unless(cxxTemporaryObjectExpr(); + unless(cxxTemporaryObjectExpr(; // We could have leak of resource. - auto NewExprAsArgument = hasAnyArgument(ignoringImplicit(cxxNewExpr())); + auto NewExprAsArgument = ignoringImplicit(cxxNewExpr()); // We would call another constructor. auto ConstructingDerived = hasParent(implicitCastExpr(hasCastKind(CastKind::CK_DerivedToBase))); @@ -202,19 +202,35 @@ void UseEmplaceCheck::registerMatchers(MatchFinder *Finder) { // overloaded functions and template names. auto SoughtConstructExpr = cxxConstructExpr( - unless(anyOf(IsCtorOfSmartPtr, HasInitList, BitFieldAsArgument, - InitializerListAsArgument, NewExprAsArgument, - ConstructingDerived, IsPrivateOrProtectedCtor))) + unless(anyOf(hasDeclaration(IsCtorOfSmartPtr), HasInitList, + hasAnyArgument(BitFieldAsArgument), + hasAnyArgument(InitializerListAsArgument), + hasAnyArgument(NewExprAsArgument), ConstructingDerived, + IsPrivateOrProtectedCtor))) .bind("ctor"); - auto HasConstructExpr = has(ignoringImplicit(SoughtConstructExpr)); + + auto IsPrimitiveType = hasType(builtinType()); + + auto AggregateInitExpr = + getLangOpts().CPlusPlus20 + ? initListExpr(unless(anyOf(HasInitList, has(IsCtorOfSmartPtr), + has(BitFieldAsArgument), + has(InitializerListAsArgument), + has(NewExprAsArgument), IsPrimitiveType))) +.bind("agg_init") + : unless(anything()); + + auto HasConstructExpr = + has(ignoringImplicit(anyOf(SoughtConstructExpr, AggregateInitExpr))); // allow for T{} to be replaced, even if no CTOR is declared auto HasConstructInitListExpr = has(initListExpr( - initCountLeq(1), anyOf(allOf(has(SoughtConstructExpr), - has(cxxConstructExpr(argumentCountIs(0, - has(cxxBindTemporaryExpr( - has(SoughtConstructExpr), - has(cxxConstructExpr(argumentCountIs(0; + initCountLeq(1), + anyOf(allOf(has(SoughtConstructExpr), + has(cxxConstructExpr(argumentCountIs(0, +has(cxxBindTemporaryExpr(has(SoughtConstructExpr), + has(cxxConstructExpr(argumentCountIs(0))) + ); auto HasBracedInitL
[clang-tools-extra] [clang-tidy][C++20] Add support for Initialization Forwarding in Nested Object Construction (PR #131969)
https://github.com/RiverDave updated https://github.com/llvm/llvm-project/pull/131969 >From 30678d79b5f7cd91da8eef5251eabe96875c0397 Mon Sep 17 00:00:00 2001 From: David Rivera Date: Sun, 16 Mar 2025 16:20:16 -0400 Subject: [PATCH] [clang-tidy] Add support for Initialization Forwarding in Nested Objects within modernize-use-emplace --- .../clang-tidy/modernize/UseEmplaceCheck.cpp | 109 +++--- clang-tools-extra/docs/ReleaseNotes.rst | 4 + .../checkers/modernize/use-emplace.cpp| 26 - 3 files changed, 119 insertions(+), 20 deletions(-) diff --git a/clang-tools-extra/clang-tidy/modernize/UseEmplaceCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseEmplaceCheck.cpp index 430455a38f395..ea449944bb199 100644 --- a/clang-tools-extra/clang-tidy/modernize/UseEmplaceCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/UseEmplaceCheck.cpp @@ -98,8 +98,8 @@ auto hasWantedType(llvm::ArrayRef TypeNames) { // Matches member call expressions of the named method on the listed container // types. -auto cxxMemberCallExprOnContainer( -StringRef MethodName, llvm::ArrayRef ContainerNames) { +auto cxxMemberCallExprOnContainer(StringRef MethodName, + llvm::ArrayRef ContainerNames) { return cxxMemberCallExpr( hasDeclaration(functionDecl(hasName(MethodName))), on(hasTypeOrPointeeType(hasWantedType(ContainerNames; @@ -174,19 +174,19 @@ void UseEmplaceCheck::registerMatchers(MatchFinder *Finder) { // passed pointer because smart pointer won't be constructed // (and destructed) as in push_back case. auto IsCtorOfSmartPtr = - hasDeclaration(cxxConstructorDecl(ofClass(hasAnyName(SmartPointers; + cxxConstructorDecl(ofClass(hasAnyName(SmartPointers))); // Bitfields binds only to consts and emplace_back take it by universal ref. - auto BitFieldAsArgument = hasAnyArgument( - ignoringImplicit(memberExpr(hasDeclaration(fieldDecl(isBitField()); + auto BitFieldAsArgument = + ignoringImplicit(memberExpr(hasDeclaration(fieldDecl(isBitField(); // Initializer list can't be passed to universal reference. - auto InitializerListAsArgument = hasAnyArgument( + auto InitializerListAsArgument = ignoringImplicit(allOf(cxxConstructExpr(isListInitialization()), - unless(cxxTemporaryObjectExpr(); + unless(cxxTemporaryObjectExpr(; // We could have leak of resource. - auto NewExprAsArgument = hasAnyArgument(ignoringImplicit(cxxNewExpr())); + auto NewExprAsArgument = ignoringImplicit(cxxNewExpr()); // We would call another constructor. auto ConstructingDerived = hasParent(implicitCastExpr(hasCastKind(CastKind::CK_DerivedToBase))); @@ -202,11 +202,26 @@ void UseEmplaceCheck::registerMatchers(MatchFinder *Finder) { // overloaded functions and template names. auto SoughtConstructExpr = cxxConstructExpr( - unless(anyOf(IsCtorOfSmartPtr, HasInitList, BitFieldAsArgument, - InitializerListAsArgument, NewExprAsArgument, - ConstructingDerived, IsPrivateOrProtectedCtor))) + unless(anyOf(hasDeclaration(IsCtorOfSmartPtr), HasInitList, + hasAnyArgument(BitFieldAsArgument), + hasAnyArgument(InitializerListAsArgument), + hasAnyArgument(NewExprAsArgument), ConstructingDerived, + IsPrivateOrProtectedCtor))) .bind("ctor"); - auto HasConstructExpr = has(ignoringImplicit(SoughtConstructExpr)); + + auto IsPrimitiveType = hasType(builtinType()); + + auto AggregateInitExpr = + getLangOpts().CPlusPlus20 + ? initListExpr(unless(anyOf(HasInitList, has(IsCtorOfSmartPtr), + has(BitFieldAsArgument), + has(InitializerListAsArgument), + has(NewExprAsArgument), IsPrimitiveType))) +.bind("agg_init") + : unless(anything()); + + auto HasConstructExpr = + has(ignoringImplicit(anyOf(SoughtConstructExpr, AggregateInitExpr))); // allow for T{} to be replaced, even if no CTOR is declared auto HasConstructInitListExpr = has(initListExpr( @@ -305,6 +320,38 @@ void UseEmplaceCheck::registerMatchers(MatchFinder *Finder) { this); } +const Expr *unwrapInnerExpression(const Expr *E) { + + while (true) { +if (!E) + break; + +if (llvm::isa(E)) { + return E; +} + +if (const auto *BindTemp = llvm::dyn_cast(E)) { + E = BindTemp->getSubExpr(); + continue; +} + +if (const auto *MaterialTemp = +llvm::dyn_cast(E)) { + E = MaterialTemp->getSubExpr(); + continue; +} + +if (const auto *Cast = llvm::dyn_cast(E)) { + E = Cast->getSubExpr(); + continue; +} + +break; + } + + return nullptr; // No relevant sub-expression found +}
[clang-tools-extra] [clang-tidy][C++20] Add support for aggregate types within modernize-use-emplace (PR #131969)
@@ -0,0 +1,86 @@ +// RUN: %check_clang_tidy %s -std=c++20 modernize-use-emplace %t -- \ +// RUN: -config="{CheckOptions: \ +// RUN: {modernize-use-emplace.ContainersWithPushBack: \ +// RUN:'::std::vector; ::std::list; ::std::deque; llvm::LikeASmallVector', \ +// RUN: modernize-use-emplace.TupleTypes: \ +// RUN:'::std::pair; std::tuple; ::test::Single', \ +// RUN: modernize-use-emplace.TupleMakeFunctions: \ +// RUN:'::std::make_pair; ::std::make_tuple; ::test::MakeSingle'}}" + +namespace std { +template +class initializer_list { +public: + const E *a, *b; + initializer_list() noexcept {} +}; + +template +class vector { +public: + using value_type = T; + + class iterator {}; + class const_iterator {}; + const_iterator begin() { return const_iterator{}; } + + vector() = default; + vector(initializer_list) {} + + void push_back(const T &) {} + void push_back(T &&) {} + + template + void emplace_back(Args &&... args){}; + template + iterator emplace(const_iterator pos, Args &&...args); + ~vector(); +}; + +} // namespace std + +struct InnerType { + InnerType() {} + InnerType(char const*) {} +}; + +//Not aggregate but we should still be able to directly initialize it with emplace_back +struct NonTrivialNoCtor { + InnerType it; +}; + +struct Aggregate { + int a; + int b; +}; + +void testCXX20Cases() { + std::vector v1; + + v1.push_back(Aggregate{1, 2}); RiverDave wrote: Been thinking about this while I've spent some time refining what has been already done. I think It'd be better to target this feature in a different PR since the scope would increase significantly. (Specially in the `check` logic). https://github.com/llvm/llvm-project/pull/131969 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy][C++20] Add support for Initialization Forwarding in Nested Object Construction (PR #131969)
@@ -1,4 +1,4 @@ -// RUN: %check_clang_tidy %s modernize-use-emplace %t -- \ +// RUN: %check_clang_tidy %s -std=c++17 modernize-use-emplace %t -- \ RiverDave wrote: Thx for your suggestions, this is really great, this should be addressed now. https://github.com/llvm/llvm-project/pull/131969 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy][C++20] Add support for Initialization Forwarding in Nested Object Construction (PR #131969)
RiverDave wrote: Removed **Aggregates** from both title and release notes considering my changes can potentially affect non aggregate structs types as well. https://github.com/llvm/llvm-project/pull/131969 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy][C++20] Add support for Initialization Forwarding in Nested Object Construction (PR #131969)
https://github.com/RiverDave edited https://github.com/llvm/llvm-project/pull/131969 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy][C++20] Add support for Initialization Forwarding in Nested Object Construction (PR #131969)
@@ -0,0 +1,86 @@ +// RUN: %check_clang_tidy %s -std=c++20 modernize-use-emplace %t -- \ RiverDave wrote: File was removed as suggested, will create a new file for future work on designated inits https://github.com/llvm/llvm-project/pull/131969 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits