[clang-tools-extra] [clang-tidy] Workaround for cppcoreguidelines-pro-type-union-access if memLoc is invalid (PR #104540)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Aug 15 19:24:22 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-tools-extra
Author: Konstantin Romanov (ksromanov)
<details>
<summary>Changes</summary>
Fixes #<!-- -->102945
---
Full diff: https://github.com/llvm/llvm-project/pull/104540.diff
2 Files Affected:
- (modified) clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeUnionAccessCheck.cpp (+6-2)
- (modified) clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-type-union-access.cpp (+7)
``````````diff
diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeUnionAccessCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeUnionAccessCheck.cpp
index 1ed444e630ec25..0e9185956b7aa8 100644
--- a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeUnionAccessCheck.cpp
+++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeUnionAccessCheck.cpp
@@ -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");
}
} // namespace clang::tidy::cppcoreguidelines
diff --git a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-type-union-access.cpp b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-type-union-access.cpp
index 6abc22b9e4345e..46bb06ba2c8fbe 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-type-union-access.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-type-union-access.cpp
@@ -5,6 +5,10 @@ union U {
char union_member2;
} u;
+union W {
+ template <class TP> operator TP *() const;
+};
+
struct S {
int non_union_member;
union {
@@ -20,6 +24,7 @@ void f(char);
void f2(U);
void f3(U&);
void f4(U*);
+W f5();
void check()
{
@@ -38,4 +43,6 @@ void check()
f2(u); // OK
f3(u); // OK
f4(&u); // OK
+ void *ret = f5();
+ // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: do not access members of unions; use (boost::)variant instead
}
``````````
</details>
https://github.com/llvm/llvm-project/pull/104540
More information about the cfe-commits
mailing list