llvm / llvm/llvm-project

[clang-tidy] Add `modernize-use-std-interpolation` check for `std::midpoint` / `std::lerp`

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

Description

Add a new clang-tidy check, `modernize-use-std-interpolation`, that detects arithmetic patterns expressing a midpoint or linear interpolation and suggests the corresponding standard library facility.

Examples:
```cpp
auto m = (a + b) / 2;
auto m = a + (b - a) / 2;
```
These are commonly intended to compute the midpoint between two values. The first form is especially problematic for signed integers because `(a + b)` may overflow, which is undefined behavior. Both forms should be diagnosed and suggested to be replaced with:
```cpp
std::midpoint(a, b)
```
Similarly, detect lerp-style expressions such as:
```cpp
auto x = a + (b - a) * t;
```
and suggest:
```cpp
std::lerp(a, b, t)
```
The motivation is correctness and intent clarity: `std::midpoint` avoids overflow pitfalls and directly expresses the operation, while `std::lerp` makes interpolation intent explicit.

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.