Issue 160272
Summary [clang-tidy] Add option to modernize-use-auto for declarations where the type is known
Labels enhancement, clang-tidy
Assignees
Reporter carlosgalvezp
    I recently came across [this blog](https://abuehl.github.io/2025/09/17/even-more-auto.html), based on an old CppCon talk by Herb Sutter. There, they suggest applying the following transformation:

```cpp
SomeType x{...};
SomeOtherLongerType y{...};
int z{123};
```

to:

```cpp
auto x = SomeType{...};
auto y = SomeOtherLongerType{...};
auto z = 123;
```

Which I think is quite neat and provides improved readability while still being explicit about the type. As a bonus, this pattern forces variables to be defined at the declaration, reducing the risk for uninitialiezd variables.

It would be nice to have support for this in `modernize-use-auto`, with an option that is possibly off-by-default. 

Caveat: if `SomeType` is expensive to copy this may lead to lower performance. In C++17 the copy is guaranteed to be elided, though.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to