Skip to content

clang-tidy: bugprone-dangling-handle works with foo{"a"} but not foo("a") #68637

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
sean-mcmanus opened this issue Oct 9, 2023 · 0 comments · Fixed by #69067
Closed

clang-tidy: bugprone-dangling-handle works with foo{"a"} but not foo("a") #68637

sean-mcmanus opened this issue Oct 9, 2023 · 0 comments · Fixed by #69067
Assignees

Comments

@sean-mcmanus
Copy link

sean-mcmanus commented Oct 9, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment