Closed
Description
Repro is using clang-tidy 17.0.1 on the code below with -std=c++17 and https://releases.llvm.org/17.0.1/tools/clang/tools/extra/docs/clang-tidy/checks/bugprone/dangling-handle.html . See #54984 for an example command line (this issue seems different from that bug).
#include <cstddef>
#include <string>
#include <string_view>
struct foo {
std::string sss;
explicit foo(std::string_view sss): sss{sss}{}
operator std::string_view()
const noexcept
{
return { sss.data(), sss.size() };
}
};
int main()
{
const std::string_view test1 = foo("a"); // no bugprone-dangling-handle
const std::string_view test2 = foo{"a"}; // bugprone-dangling-handle
return static_cast<int>(test1.length() + test2.length());
}
Bug: The foo{"a"}
gets a bugprone-dangling-handle warning but the foo("a")
line does not. I have other examples with class("str")
not getting a warning, so it seems like the foo{"a"}
is a special case in which it works, but usually it doesn't.