Skip to content

Commit 0fbf91a

Browse files
authored
[clang-tidy] Fix cppcoreguidelines-pro-type-union-access if memLoc is invalid (llvm#104540)
Fixes llvm#102945.
1 parent 8234f8a commit 0fbf91a

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@ 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+
SourceLocation Loc = Matched->getMemberLoc();
27+
if (Loc.isInvalid())
28+
Loc = Matched->getBeginLoc();
29+
diag(Loc, "do not access members of unions; consider using (boost::)variant "
30+
"instead");
2831
}
2932

3033
} // namespace clang::tidy::cppcoreguidelines

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,10 @@ Changes in existing checks
180180
avoid false positive when member initialization depends on a structured
181181
binding variable.
182182

183+
- Fixed :doc:`cppcoreguidelines-pro-type-union-access
184+
<clang-tidy/checks/cppcoreguidelines/pro-type-union-access>` check to
185+
report a location even when the member location is not valid.
186+
183187
- Improved :doc:`misc-definitions-in-headers
184188
<clang-tidy/checks/misc/definitions-in-headers>` check by rewording the
185189
diagnostic note that suggests adding ``inline``.

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

Lines changed: 11 additions & 4 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,22 +24,25 @@ void f(char);
2024
void f2(U);
2125
void f3(U&);
2226
void f4(U*);
27+
W f5();
2328

2429
void check()
2530
{
2631
u.union_member1 = true;
27-
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: do not access members of unions; use (boost::)variant instead [cppcoreguidelines-pro-type-union-access]
32+
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: do not access members of unions; consider using (boost::)variant instead [cppcoreguidelines-pro-type-union-access]
2833
auto b = u.union_member2;
29-
// CHECK-MESSAGES: :[[@LINE-1]]:14: warning: do not access members of unions; use (boost::)variant instead
34+
// CHECK-MESSAGES: :[[@LINE-1]]:14: warning: do not access members of unions; consider using (boost::)variant instead
3035
auto a = &s.union_member;
31-
// CHECK-MESSAGES: :[[@LINE-1]]:15: warning: do not access members of unions; use (boost::)variant instead
36+
// CHECK-MESSAGES: :[[@LINE-1]]:15: warning: do not access members of unions; consider using (boost::)variant instead
3237
f(s.u.union_member2);
33-
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: do not access members of unions; use (boost::)variant instead
38+
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: do not access members of unions; consider using (boost::)variant instead
3439

3540
s.non_union_member = 2; // OK
3641

3742
U u2 = u; // OK
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; consider using (boost::)variant instead
4148
}

0 commit comments

Comments
 (0)