llvm / llvm/llvm-project

[clang-tidy] Add `modernize-use-as-const` check

Open
#189,665 2 comments 0 reactions 0 assignees View on GitHub
check-request clang-tidy
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

It would be useful to have a clang-tidy check that recognizes common ways of treating an lvalue as `const` and suggests `std::as_const` in C++17 and later.

Today code like this is fairly common:

```cpp
use(static_cast(obj));

for (const auto &x : static_cast(c)) {
...
}
```

but the clearer modern spelling is:

```cpp
use(std::as_const(obj));

for (const auto &x : std::as_const(c)) {
...
}
```

This makes the intent explicit: the code is not performing an arbitrary cast, it is simply viewing an existing object as const.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.