Skip to content

Commit 31ed1cf

Browse files
committed
Workaround for cppcoreguidelines-pro-type-union-access if member location is invalid.
1 parent 08acc3f commit 31ed1cf

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeUnionAccessCheck.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,12 @@ void ProTypeUnionAccessCheck::registerMatchers(MatchFinder *Finder) {
2323

2424
void 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

clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-type-union-access.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
812
struct S {
913
int non_union_member;
1014
union {
@@ -20,6 +24,7 @@ void f(char);
2024
void f2(U);
2125
void f3(U&);
2226
void f4(U*);
27+
W f5();
2328

2429
void 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
}

0 commit comments

Comments
 (0)