Skip to content

[Clang-Tidy] readability-use-std-min-max should consider std::clampΒ #97835

@SunBlack

Description

@SunBlack

Tried the new check readability-use-std-min-max of the upcoming Clang-Tidy 19.

In one case we had following code:

if (ratio < 0.0F)
{
	ratio = 0.0F;
}
if (ratio > 1.0F)
{
	ratio = 1.0F;
}

Running run-clang-tidy-19 -header-filter='.*' -checks='-*,readability-use-std-min-max' -fix results in:

ratio = std::max(ratio, 0.0F);
ratio = std::min(ratio, 1.0F);

But in this case it would be better

ratio = std::clamp(ratio, 0.0F, 1.0F);

I don't know how complex it would be to recognize this pattern, i.e. whether it is better to introduce a readability-use-std-clamp, even if this may require a second pass, or whether this can be combined by changing the check to readability-use-std-min-max-clamp.

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang-tidyenhancementImproving things as opposed to bug fixing, e.g. new or missing feature

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions