[clang-tools-extra] [clang-tidy] Workaround for cppcoreguidelines-pro-type-union-access if memLoc is invalid (PR #104540)
    Piotr Zegar via cfe-commits 
    cfe-commits at lists.llvm.org
       
    Fri Aug 16 08:32:21 PDT 2024
    
    
  
================
@@ -23,8 +23,12 @@ void ProTypeUnionAccessCheck::registerMatchers(MatchFinder *Finder) {
 
 void ProTypeUnionAccessCheck::check(const MatchFinder::MatchResult &Result) {
   const auto *Matched = Result.Nodes.getNodeAs<MemberExpr>("expr");
-  diag(Matched->getMemberLoc(),
-       "do not access members of unions; use (boost::)variant instead");
+  if (auto MemberLoc = Matched->getMemberLoc(); MemberLoc.isValid())
+    diag(MemberLoc,
+         "do not access members of unions; use (boost::)variant instead");
+  else
+    diag(Matched->getBeginLoc(),
+         "do not access members of unions; use (boost::)variant instead");
----------------
PiotrZSL wrote:
```suggestion
  SourceLocation Loc = Matched->getMemberLoc();
  if (Loc.isInvalid())
     Loc = Matched->getBeginLoc();
  diag(Loc, "do not access members of unions; use (boost::)variant instead");
```
re-format may be needed
https://github.com/llvm/llvm-project/pull/104540
    
    
More information about the cfe-commits
mailing list