[PATCH] D57852: [clang-tidy] Don't use assignment for value-initialized enums
Malcolm Parsons via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Wed Feb 6 15:00:05 PST 2019
malcolm.parsons created this revision.
malcolm.parsons added reviewers: aaron.ballman, alexfh, JonasToth.
Herald added subscribers: cfe-commits, xazax.hun.
Herald added a project: clang.
The modernize-use-default-member-init check crashes when trying to
create an assignment value for a value-initialized enum because it isn't a
BuiltinType.
An enum cannot be initialized by assigning 0 to it unless a cast is added.
It could be initialized with an enumerator with the value 0, but there might not
be one.
Avoid these issues by ignoring the UseAssignment setting for value-initialized
enums.
Fixes PR35050.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D57852
Files:
clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp
clang-tools-extra/test/clang-tidy/modernize-use-default-member-init-assignment.cpp
clang-tools-extra/test/clang-tidy/modernize-use-default-member-init.cpp
Index: clang-tools-extra/test/clang-tidy/modernize-use-default-member-init.cpp
===================================================================
--- clang-tools-extra/test/clang-tidy/modernize-use-default-member-init.cpp
+++ clang-tools-extra/test/clang-tidy/modernize-use-default-member-init.cpp
@@ -165,6 +165,14 @@
// CHECK-FIXES: Enum e{Foo};
};
+struct PositiveValueEnum {
+ PositiveValueEnum() : e() {}
+ // CHECK-FIXES: PositiveValueEnum() {}
+ Enum e;
+ // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use default member initializer for 'e'
+ // CHECK-FIXES: Enum e{};
+};
+
struct PositiveString {
PositiveString() : s("foo") {}
// CHECK-FIXES: PositiveString() {}
Index: clang-tools-extra/test/clang-tidy/modernize-use-default-member-init-assignment.cpp
===================================================================
--- clang-tools-extra/test/clang-tidy/modernize-use-default-member-init-assignment.cpp
+++ clang-tools-extra/test/clang-tidy/modernize-use-default-member-init-assignment.cpp
@@ -166,6 +166,14 @@
// CHECK-FIXES: Enum e = Foo;
};
+struct PositiveValueEnum {
+ PositiveValueEnum() : e() {}
+ // CHECK-FIXES: PositiveValueEnum() {}
+ Enum e;
+ // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use default member initializer for 'e'
+ // CHECK-FIXES: Enum e{};
+};
+
struct PositiveString {
PositiveString() : s("foo") {}
// CHECK-FIXES: PositiveString() {}
Index: clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp
+++ clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp
@@ -255,17 +255,20 @@
CharSourceRange InitRange =
CharSourceRange::getCharRange(LParenEnd, Init->getRParenLoc());
+ bool ValueInit = isa<ImplicitValueInitExpr>(Init->getInit());
+ bool CanAssign = UseAssignment && (!ValueInit || !Init->getInit()->getType()->isEnumeralType());
+
auto Diag =
diag(Field->getLocation(), "use default member initializer for %0")
<< Field
- << FixItHint::CreateInsertion(FieldEnd, UseAssignment ? " = " : "{")
+ << FixItHint::CreateInsertion(FieldEnd, CanAssign ? " = " : "{")
<< FixItHint::CreateInsertionFromRange(FieldEnd, InitRange);
- if (UseAssignment && isa<ImplicitValueInitExpr>(Init->getInit()))
+ if (CanAssign && ValueInit)
Diag << FixItHint::CreateInsertion(
FieldEnd, getValueOfValueInit(Init->getInit()->getType()));
- if (!UseAssignment)
+ if (!CanAssign)
Diag << FixItHint::CreateInsertion(FieldEnd, "}");
Diag << FixItHint::CreateRemoval(Init->getSourceRange());
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D57852.185649.patch
Type: text/x-patch
Size: 2698 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190206/25415ecb/attachment.bin>
More information about the cfe-commits
mailing list