[clang-tools-extra] [clang-tidy] Add new `construct-reusable-objects-once` check (PR #131455)
Baranov Victor via cfe-commits
cfe-commits at lists.llvm.org
Sat Mar 15 12:27:19 PDT 2025
vbvictor wrote:
Thank you for the fast feedback.
As for general approach, I will limit the usage to classes that are marked `const` and provide reference to `misc-const-correctness` in docs. Also, I will add support for initializer-list constructors.
However, I think we should not give warning for container-classes that are constructed from a couple of elements, e.g. `std::vector<int> v{1, 2, 3}`.
So my next actions will be:
- Add `const` as a must
- Add support for init-list ctors
- Add option `ContainerClasses` and `ContainerElementThreshold` - this will match only containers which has more than `ContainerElementThreshold` elements in their init-list.
- Rename option `CheckedClasses` to `ReusableClasses` - those will be classes with same semantics as `std::regex` - their constructors are expensive no matter what.
However, if I make such improvements we would still NOT match many cases, e.g.
```cpp
std::vector<int> v = {1, 2, 3} // match
std::vector<std::string> = {"foo", "bar"} // not match
std::map<int, int> m = {{1, 1}, {2, 2}}; // not match
```
Because here we have `std::initializer_list<std::string>` and `std::initializer_list<std::pair<int, int>>` as ctor-parameters and the check may not mark `std::pair` and `std::string` as "literal-expressions".
With this in mind, should I even try to work with containers or leave It only to `regex`-like objects?
As for now, I see little benefits from matching a very small portion of container-usages.
What about separating this check in two: `construct-reusable-objects-once` and `initialize-big-containers-once`?
https://github.com/llvm/llvm-project/pull/131455
More information about the cfe-commits
mailing list