Std::optional always has value

I want a rule that finds cases where a std::optional is initialized with a value and is never changed to std::nullopt. Because in that case, it it tautologically true that the optional has a value, and it it is either a mistake to use an optional in the first place, since the variable could simply be the underlying type, or the variable was meant to be initialized from something that is a std::optional in the first place.

std::optional<std::reference_wrapper<const Thing>> maybeThing();
const Thing &thing();

const std::optional maybeThing1 = thing(); // Should be flagged

const std::optional maybeThing2 = maybeThing(); // Compliant, candidate resolution
const Thing &actualThing = thing(); // Another candidate resolution


Hi @torgeir.skogen,

Thanks for your suggestion. I feel this makes sense so I’ve raised [CPP-4025] - Jira to track this request.