llvm / llvm/llvm-project

[clang-tidy] misc-const-correctness fix-it inserts invalid `const` after `decltype(auto)`, breaking compilation

Open
#223,940 1 comment 0 reactions 0 assignees View on GitHub
clang-tidy invalid-code-generation
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

**Disclaimer:** the body of this issue was *partly* written by Claude Sonnet 5. I found the hard regression when running `clang-tidy`with my usual diagnostics activated with the brand-new version, and then interacted with it in the browser to check whether the issue was already known.

### Description

`misc-const-correctness` now adds a harmful `const` to `decltype(auto)` declarations.

### Reproducer

```cpp
int global = 0;
int& get_ref() { return global; }

void use_int(int v) {
decltype(auto) x = get_ref();
use_int(x);
}
```

```
$ clang-tidy --checks='-*,misc-const-correctness' repro.cpp -- -std=c++17
repro.cpp:5:5: warning: variable 'x' of type 'int &' can be declared 'const' [misc-const-correctness]
5 | decltype(auto) x = get_ref();
| ^
| const
```

Applying the suggested fix:

```cpp
decltype(auto) const x = get_ref(); // <-- inserted here
```

Compiling the result:

```
repro.cpp:5:5: error: 'decltype(auto)' cannot be combined with other type specifiers
5 | decltype(auto) const x = get_ref();
| ^ ~~~~~
1 error generated.
```

The same happens for a value-deduced case:

```cpp
void f() {
int value = 42;
decltype(auto) ref_value = value; // deduces plain int, not a reference
int result = ref_value * 3;
}
```
fixes to:
```cpp
decltype(auto) const ref_value = value; // still a hard error
```

### Environment

- `clang-tidy --version`: LLVM version 23.1.0 (Homebrew build; reproduced identically against the official `llvmorg-23.1.0` Linux x86_64 release binary) - Linux attempt was from Claude, and original issue was found on macOS / Homebrew.
- Reproduced with both `clang-tidy -fix` and by hand-verifying the resulting
file with `clang++` from the same 23.1.0 release.
- Does **not** reproduce on clang-tidy 18.1.3 / 19.1.1 / 20.1.2 — those
versions simply never analyze `decltype(auto)`-declared variables at all
(no warning is emitted), so this is a regression introduced alongside
whatever change made the check start analyzing `decltype(auto)` variables
(likely part of the misc-const-correctness rework around
llvm/llvm-project#171215, Dec 2025 — worth checking if that's the
responsible change, or a related later patch).

### Expected behavior

Do not add `const` qualifier to `decltype(auto)`.

### Impact

This is a correctness regression for consumers of `misc-const-correctness`
`--fix`/`-fix-errors`: running the check with autofix on a codebase using
`decltype(auto)` locals will silently break the build at every flagged
occurrence, requiring manual review of every hunk in the diff rather than a
trusted automated pass.

Contributor guide

Open the contributing guide

Research direction

Start with the misc-const-correctness check and reproduce the issue using the provided repro.cpp example and clang-tidy command. Check the fix-it logic for decltype(auto) declarations, then verify that the check no longer suggests an invalid const and that the resulting C++17 code compiles.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
72/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.