Skip to content

clang should warn when tautologically comparing a smaller-type numeric value against a larger-typed constant outside the smaller type's range #13059

@llvmbot

Description

@llvmbot
Bugzilla Link 12687
Version unspecified
OS All
Reporter LLVM Bugzilla Contributor
CC @gribozavr

Extended Description

I recently wrote code that looked roughly like this (the isfinite and copysign expressions were spelled in other terms that didn't make the code look so obviously wrong):

#include <math.h>
#include <stdlib.h>

inline double TimeClip(double time)
{
if (!isfinite(time) || abs(time) > 8.64e15)
return NAN;
return copysign(floor(fabs(time)), time + 0.0);
}

int main()
{
TimeClip(17.0);
return 0;
}

This code's buggy in that it uses abs (int -> int) when it should have used fabs (double -> double). The compiler can't be expected to figure that out, of course. But it should be able to figure out that no |int| value will ever be greater than 8.64e15, and it should emit a tautological-compare warning just as it would if it saw any other always-true or always-false comparison.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugzillaIssues migrated from bugzillaclang:diagnosticsNew/improved warning or error message in Clang, but not in clang-tidy or static analyzer

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions