File tree Expand file tree Collapse file tree 2 files changed +13
-2
lines changed Expand file tree Collapse file tree 2 files changed +13
-2
lines changed Original file line number Diff line number Diff line change @@ -23,8 +23,12 @@ void ProTypeUnionAccessCheck::registerMatchers(MatchFinder *Finder) {
2323
2424void ProTypeUnionAccessCheck::check (const MatchFinder::MatchResult &Result) {
2525 const auto *Matched = Result.Nodes .getNodeAs <MemberExpr>(" expr" );
26- diag (Matched->getMemberLoc (),
27- " do not access members of unions; use (boost::)variant instead" );
26+ if (auto MemberLoc = Matched->getMemberLoc (); MemberLoc.isValid ())
27+ diag (MemberLoc,
28+ " do not access members of unions; use (boost::)variant instead" );
29+ else
30+ diag (Matched->getBeginLoc (),
31+ " do not access members of unions; use (boost::)variant instead" );
2832}
2933
3034} // namespace clang::tidy::cppcoreguidelines
Original file line number Diff line number Diff line change @@ -5,6 +5,10 @@ union U {
55 char union_member2;
66} u;
77
8+ union W {
9+ template <class TP > operator TP *() const ;
10+ };
11+
812struct S {
913 int non_union_member;
1014 union {
@@ -20,6 +24,7 @@ void f(char);
2024void f2 (U);
2125void f3 (U&);
2226void f4 (U*);
27+ W f5 ();
2328
2429void check ()
2530{
@@ -38,4 +43,6 @@ void check()
3843 f2 (u); // OK
3944 f3 (u); // OK
4045 f4 (&u); // OK
46+ void *ret = f5 ();
47+ // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: do not access members of unions; use (boost::)variant instead
4148}
You can’t perform that action at this time.
0 commit comments