Author: David Rivera Date: 2025-03-11T18:00:43+01:00 New Revision: 517c6778ead6e7c476e0f4482763060c679ee80b
URL: https://github.com/llvm/llvm-project/commit/517c6778ead6e7c476e0f4482763060c679ee80b DIFF: https://github.com/llvm/llvm-project/commit/517c6778ead6e7c476e0f4482763060c679ee80b.diff LOG: [clang-tidy] Add check on constexpr & static values in modernize-use-default-member-init (#129425) This aims to fix a portion of #122480. Simply added a check to look for either compile time initialized variables or static stored variables. Added: Modified: clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp clang-tools-extra/docs/ReleaseNotes.rst clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp Removed: ################################################################################ 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 fa68b6fabd549..0f9da5ec5ee83 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -146,6 +146,10 @@ Changes in existing checks <clang-tidy/checks/misc/use-internal-linkage>` check by fix false positives for function or variable in header file which contains macro expansion. +- Improved :doc:`modernize-use-default-member-init + <clang-tidy/checks/modernize/use-default-member-init>` check by matching + ``constexpr`` and ``static`` values on member initialization. + - Improved :doc:`performance/unnecessary-value-param <clang-tidy/checks/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..8b9bfaf0df34f 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; + constexpr const char* CONSTEXPR_REF = "Const"; + + 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