-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[clang-tidy] Add check on constexpr & static values in modernize-use-default-member-init #129425
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[clang-tidy] Add check on constexpr & static values in modernize-use-default-member-init #129425
Conversation
|
@llvm/pr-subscribers-clang-tools-extra @llvm/pr-subscribers-clang-tidy Author: David Rivera (RiverDave) ChangesThis aims to fix a portion of #122480. Simply added a check to look for either compile time initialized variables or static stored variables. Full diff: https://github.com/llvm/llvm-project/pull/129425.diff 3 Files Affected:
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 07a79d6bbe807..3a9da3fda86a5 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -128,6 +128,10 @@ Changes in existing checks
<clang-tidy/checks/misc/redundant-expression>` check by providing additional
examples and fixing some macro related false positives.
+- Improved :doc:`modernize-use-default-member-init
+ <clang-tidy/checks/modernize/use-default-member-init>` check by Adding check on
+ constexpr & 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..c1a35516e001e 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
\ No newline at end of file
|
clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp
Outdated
Show resolved
Hide resolved
f3e63b3 to
ab06aa4
Compare
ab06aa4 to
2b4d70b
Compare
|
Ping |
2b4d70b to
4f25319
Compare
PiotrZSL
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Except small nit, in tests, looks fine.
clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp
Outdated
Show resolved
Hide resolved
4f25319 to
d0f3af2
Compare
…ization in modernize-use-default-member-init
d0f3af2 to
ec6b41f
Compare
Amazing, your comments have been addressed |
|
@RiverDave Unless you want this to be merged using github generated private email, change your email privacy settings. |
My email should be public now. thx for pointing out |
This aims to fix a portion of #122480. Simply added a check to look for either compile time initialized variables or static stored variables.