llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

clang-tidy bugprone-unchecked-optional-access assumes unstable function results when it's impossible

Open tsdgeos opened this issue 4 years ago • 2 comments

It will complain in code like

#include <optional>
#include <string>

class FontInfo
{
public:
    FontInfo() = default;

    const std::optional<std::string> &getName() const { return name; };

private:
    std::optional<std::string> name;

};

int main(int argc, char **)
{
    FontInfo fi;
    printf("HAS NAME %s\n", fi.getName() ? fi.getName()->c_str() : "[none]");
}

because it has the rule that consecutive calls to the same function might return different values, but that's simply not possible given how the two getName calls are one after the other and they are just returning a const & to a member in a const method, no?

tsdgeos avatar Oct 20 '22 22:10 tsdgeos

@llvm/issue-subscribers-clang-tidy

llvmbot avatar Oct 20 '22 22:10 llvmbot

ClangTidy trunk should no longer be warning on this https://godbolt.org/z/rMGnM3eh8

jvoung avatar Nov 05 '24 16:11 jvoung