llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
27.85k stars 11.47k forks source link

[Clang-Tidy] Create readability-unambiguous-optional-assigment check #80862

Open PiotrZSL opened 7 months ago

PiotrZSL commented 7 months ago

I'm adding this here so, i wouldn't forget. Check name to negotiation.

std::optional<int> opt;
...
opt = {};
opt = std::optional<int>();

Problem with above, is that it's not so straightforward. I run into issue when developer initialized optional with = std::optional<...>() and were thinking that optional is actually set.

In std::optional we got very nice methods called reset and emplace. I would argue that they should be used instead of above examples. Even std::nullopt is better.

llvmbot commented 7 months ago

@llvm/issue-subscribers-clang-tidy

Author: Piotr Zegar (PiotrZSL)

I'm adding this here so, i wouldn't forget. Check name to negotiation. ``` std::optional<int> opt; ... opt = {}; opt = std::optional<int>(); ``` Problem with above, is that it's not so straightforward. I run into issue when developer initialized optional with `= std::optional<...>()` and were thinking that optional is actually set. In std::optional we got very nice methods called `reset` and `emplace`. I would argue that they should be used instead of above examples. Even `std::nullopt` is better.