llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

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

Open unterumarmung opened this issue 8 months ago • 4 comments

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:

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:

std::midpoint(a, b)

Similarly, detect lerp-style expressions such as:

auto x = a + (b - a) * t;

and suggest:

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.

unterumarmung avatar Mar 30 '26 21:03 unterumarmung

@llvm/issue-subscribers-clang-tidy

Author: Daniil Dudkin (unterumarmung)

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:

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:

std::midpoint(a, b)

Similarly, detect lerp-style expressions such as:

auto x = a + (b - a) * t;

and suggest:

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.

llvmbot avatar Mar 30 '26 21:03 llvmbot

@llvm/issue-subscribers-c-20

Author: Daniil Dudkin (unterumarmung)

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:

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:

std::midpoint(a, b)

Similarly, detect lerp-style expressions such as:

auto x = a + (b - a) * t;

and suggest:

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.

llvmbot avatar Apr 03 '26 19:04 llvmbot

Hi @unterumarmung can you assign this issue to me

sipher-01 avatar Jul 31 '26 17:07 sipher-01

Hi @unterumarmung can you assign this issue to me

Just create pull request and mention it on this page.

EugeneZelenko avatar Jul 31 '26 17:07 EugeneZelenko