llvm / llvm/llvm-project

[clang-tidy] Add option to modernize-use-auto for declarations where the type is known

Open
#160,272 6 comments 0 reactions 0 assignees View on GitHub
clang-tidy enhancement
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

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.

Contributor guide

Open the contributing guide

Research direction

Start by reading the modernize-use-auto entry point and its existing behavior for declarations. Define the option's supported transformations and edge cases from the examples, then verify that the opt-in behavior produces the requested explicit initializer forms without changing unrelated declarations.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.