<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/102945>102945</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
clang-tidy: [cppcoreguidelines-pro-type-union-access] sometimes does not report location
</td>
</tr>
<tr>
<th>Labels</th>
<td>
clang-tidy
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
ksromanov
</td>
</tr>
</table>
<pre>
[cppcoreguidelines-pro-type-union-access] gets triggered on any union member access. It uses `getMemberLoc()` function to retrieve the location of the union member (i.e. `unionMemberA` in `auto i = Z.unionMemberA`)
here — https://github.com/llvm/llvm-project/blob/11aa31f595325d6b2dede3364e4b86d78fffe635/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeUnionAccessCheck.cpp#L26
Sometimes this location is unavailable. For example
```c++
union Z {
int a;
template <class TP> operator TP *() const;
};
Z foo();
int main() {
void *ret = foo(); // Triggers warning
return 0;
}
```
This results in the confusing message, _note the absence_ of `file:line:col` triple:
```console
warning: do not access members of unions; use (boost::)variant instead [cppcoreguidelines-pro-type-union-access]
```
_____________________
There are two options to fix the issue:
1. Disable report if the member location is invalid:
```c++
if (auto MemberLoc = Matched->getMemberLoc(); MemberLoc.isValid())
diag(MemberLoc,
"do not access members of unions; use (boost::)variant instead");
```
2. Use `Matched` location instead:
```c++
if (auto MemberLoc = Matched->getMemberLoc(); MemberLoc.isValid())
diag(MemberLoc,
"do not access members of unions; use (boost::)variant instead");
else
diag(Matched->getExprLoc(),
"do not access members of unions; use (boost::)variant instead");
```
or similar code.
Please confirm and advise.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzkVlFr4zgQ_jXKyxBjS47jPPghbRo42IXCde9hX4osjR3dypKR5Gz77w_J6cYpvYPCwT2cMQnWaD5pvvlmJO696g1iQzZ3ZHNY8SmcrGt-eGcHbux51Vr5Go1iHIV12E9KolYG_Xp0dh1eR1xPR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