llvm-project/clang/test/SemaCXX/range-for-lifetime-cxx23.cpp
Marco Vitale c86c815fc5
[Sema] Fix lifetime extension for temporaries in range-based for loops in C++23 (#145164)
C++23 mandates that temporaries used in range-based for loops are
lifetime-extended
to cover the full loop. This patch adds a check for loop variables and
compiler-
generated `__range` bindings to apply the correct extension.

Includes test cases based on examples from CWG900/P2644R1.

Fixes https://github.com/llvm/llvm-project/issues/109793
2025-07-10 09:57:07 +08:00

28 lines
507 B
C++

// RUN: %clang_cc1 -std=c++23 -fsyntax-only -verify %s
using size_t = decltype(sizeof(void *));
namespace std {
template <typename T> struct vector {
T &operator[](size_t I);
};
struct string {
const char *begin();
const char *end();
};
} // namespace std
std::vector<std::string> getData();
void foo() {
// Verifies we don't trigger a diagnostic from -Wdangling-gsl
// when iterating over a temporary in C++23.
for (auto c : getData()[0]) {
(void)c;
}
}
// expected-no-diagnostics