Skip to content

Commit bf69788

Browse files
committed
[clang] Don't warn if the capturing object is also temporary.
1 parent 1b2c8f1 commit bf69788

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

clang/lib/Sema/SemaChecking.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3248,9 +3248,13 @@ void Sema::checkLifetimeCaptureBy(FunctionDecl *FD, bool IsMemberFunction,
32483248
checkCaptureByLifetime(*this, CE, Captured);
32493249
}
32503250
};
3251-
for (unsigned I = 0; I < FD->getNumParams(); ++I)
3252-
HandleCaptureByAttr(FD->getParamDecl(I)->getAttr<LifetimeCaptureByAttr>(),
3253-
I + IsMemberFunction);
3251+
// Suppress the warning if the capturing object is also a temporary to reduce
3252+
// noise, e.g `vector<string_view>().push_back(std::string());`.
3253+
if (!isa_and_present<MaterializeTemporaryExpr>(ThisArg)) {
3254+
for (unsigned I = 0; I < FD->getNumParams(); ++I)
3255+
HandleCaptureByAttr(FD->getParamDecl(I)->getAttr<LifetimeCaptureByAttr>(),
3256+
I + IsMemberFunction);
3257+
}
32543258
// Check when the implicit object param is captured.
32553259
if (IsMemberFunction) {
32563260
TypeSourceInfo *TSI = FD->getTypeSourceInfo();

clang/test/Sema/warn-lifetime-analysis-capture-by.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,17 @@ void use() {
143143
}
144144
} // namespace this_is_captured
145145

146+
namespace ignore_temporary_class_object {
147+
struct S {
148+
void add(const int& x [[clang::lifetime_capture_by(this)]]);
149+
};
150+
151+
void test() {
152+
S().add(1);
153+
S{}.add(1);
154+
}
155+
} // namespace ignore_temporary_class_object
156+
146157
// ****************************************************************************
147158
// Capture by Global and Unknown.
148159
// ****************************************************************************

0 commit comments

Comments
 (0)