Skip to content

Commit 729657d

Browse files
authored
[misc-coroutine-hostile-raii] Use getOperand instead of getCommonExpr. (#79206)
We were previously allowlisting awaitable types returned by `await_transform` instead of the type of the operand of the `co_await` expression. This previously used to give false positives and not respect the `AllowedAwaitablesList` flag when `await_transform` is used. See added test cases for such examples.
1 parent 9d476e1 commit 729657d

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

clang-tools-extra/clang-tidy/misc/CoroutineHostileRAIICheck.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ AST_MATCHER_P(Stmt, forEachPrevStmt, ast_matchers::internal::Matcher<Stmt>,
5656
// Matches the expression awaited by the `co_await`.
5757
AST_MATCHER_P(CoawaitExpr, awaitable, ast_matchers::internal::Matcher<Expr>,
5858
InnerMatcher) {
59-
if (Expr *E = Node.getCommonExpr())
59+
if (Expr *E = Node.getOperand())
6060
return InnerMatcher.matches(*E, Finder, Builder);
6161
return false;
6262
}

clang-tools-extra/test/clang-tidy/checkers/misc/coroutine-hostile-raii.cpp

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// RUN: %check_clang_tidy -std=c++20 %s misc-coroutine-hostile-raii %t \
22
// RUN: -config="{CheckOptions: {\
33
// RUN: misc-coroutine-hostile-raii.RAIITypesList: 'my::Mutex; ::my::other::Mutex', \
4-
// RUN: misc-coroutine-hostile-raii.AllowedAwaitablesList: 'safe::awaitable; ::my::other::awaitable' \
4+
// RUN: misc-coroutine-hostile-raii.AllowedAwaitablesList: 'safe::awaitable; ::transformable::awaitable' \
55
// RUN: }}"
66

77
namespace std {
@@ -136,6 +136,9 @@ ReturnObject scopedLockableTest() {
136136
absl::Mutex no_warning_5;
137137
}
138138

139+
// ================================================================================
140+
// Safe awaitable
141+
// ================================================================================
139142
namespace safe {
140143
struct awaitable {
141144
bool await_ready() noexcept { return false; }
@@ -150,6 +153,32 @@ ReturnObject RAIISafeSuspendTest() {
150153
co_await other{};
151154
}
152155

156+
// ================================================================================
157+
// Safe transformable awaitable
158+
// ================================================================================
159+
struct transformable { struct awaitable{}; };
160+
using alias_transformable_awaitable = transformable::awaitable;
161+
struct UseTransformAwaitable {
162+
struct promise_type {
163+
UseTransformAwaitable get_return_object() { return {}; }
164+
std::suspend_always initial_suspend() { return {}; }
165+
std::suspend_always final_suspend() noexcept { return {}; }
166+
void unhandled_exception() {}
167+
std::suspend_always await_transform(transformable::awaitable) { return {}; }
168+
};
169+
};
170+
171+
auto retAwaitable() { return transformable::awaitable{}; }
172+
UseTransformAwaitable RAIISafeSuspendTest2() {
173+
absl::Mutex a;
174+
co_await retAwaitable();
175+
co_await transformable::awaitable{};
176+
co_await alias_transformable_awaitable{};
177+
}
178+
179+
// ================================================================================
180+
// Lambdas
181+
// ================================================================================
153182
void lambda() {
154183
absl::Mutex no_warning;
155184
auto lambda = []() -> ReturnObject {
@@ -164,6 +193,9 @@ void lambda() {
164193
absl::Mutex no_warning_2;
165194
}
166195

196+
// ================================================================================
197+
// Denylisted RAII
198+
// ================================================================================
167199
template<class T>
168200
ReturnObject raii_in_template(){
169201
T a;

0 commit comments

Comments
 (0)