Skip to content

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

Closed
@tsdgeos

Description

@tsdgeos

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?

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions