llvm-project
llvm-project copied to clipboard
clang-tidy bugprone-unchecked-optional-access assumes unstable function results when it's impossible
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?
@llvm/issue-subscribers-clang-tidy
ClangTidy trunk should no longer be warning on this https://godbolt.org/z/rMGnM3eh8