This revision was automatically updated to reflect the committed changes. Closed by commit rCTE353554: [clang-tidy] Don't use assignment for value-initialized enums (authored by malcolm.parsons, committed by ).
Changed prior to commit: https://reviews.llvm.org/D57852?vs=185649&id=186014#toc Repository: rCTE Clang Tools Extra CHANGES SINCE LAST ACTION https://reviews.llvm.org/D57852/new/ https://reviews.llvm.org/D57852 Files: clang-tidy/modernize/UseDefaultMemberInitCheck.cpp test/clang-tidy/modernize-use-default-member-init-assignment.cpp test/clang-tidy/modernize-use-default-member-init.cpp Index: clang-tidy/modernize/UseDefaultMemberInitCheck.cpp =================================================================== --- clang-tidy/modernize/UseDefaultMemberInitCheck.cpp +++ 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()); Index: test/clang-tidy/modernize-use-default-member-init.cpp =================================================================== --- test/clang-tidy/modernize-use-default-member-init.cpp +++ 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: test/clang-tidy/modernize-use-default-member-init-assignment.cpp =================================================================== --- test/clang-tidy/modernize-use-default-member-init-assignment.cpp +++ 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-tidy/modernize/UseDefaultMemberInitCheck.cpp =================================================================== --- clang-tidy/modernize/UseDefaultMemberInitCheck.cpp +++ 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()); Index: test/clang-tidy/modernize-use-default-member-init.cpp =================================================================== --- test/clang-tidy/modernize-use-default-member-init.cpp +++ 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: test/clang-tidy/modernize-use-default-member-init-assignment.cpp =================================================================== --- test/clang-tidy/modernize-use-default-member-init-assignment.cpp +++ 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() {}
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits