[clang-tidy] Add `modernize-use-std-interpolation` check for `std::midpoint` / `std::lerp`
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.
@llvm/issue-subscribers-clang-tidy
Author: Daniil Dudkin (unterumarmung)
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.
@llvm/issue-subscribers-c-20
Author: Daniil Dudkin (unterumarmung)
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.
Hi @unterumarmung can you assign this issue to me
Hi @unterumarmung can you assign this issue to me
Just create pull request and mention it on this page.