This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] Fix cppcoreguidelines-narrowing-conversions false positive
ClosedPublic

Authored by njames93 on Mar 11 2021, 5:03 AM.

Details

Summary

Fix https://llvm.org/PR49498.
The check notices 2 sides of a conditional operator have types with a different constness and so tries to examine the implicit cast.
As one side is infinity, the float narrowing detection sees when its casted to a double(which it already was) it thinks the result is out of range.

I've fixed this by just disregarding expressions where the builtin type(without quals) match as no conversion would take place.

However this has opened a can of worms. Currenty float a = std::numeric_limits<double>::infinity(); is marked as narrowing.
Whats more suspicious is double a = std::numeric_limits<float>::infinity(); is also marked as narrowing.
It could be argued double inf -> float inf is narrowing, but float inf -> double inf definitely isnt.

Diff Detail

Event Timeline

njames93 created this revision.Mar 11 2021, 5:03 AM
njames93 requested review of this revision.Mar 11 2021, 5:03 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 11 2021, 5:03 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript

However this has opened a can of worms. Currenty float a = std::numeric_limits<double>::infinity(); is marked as narrowing.

I think this is technically not narrowing, per http://eel.is/c++draft/dcl.init.list#7.2, because the source is a constant expression (at least, after C++11) and the value can be represented after conversion. However, I wonder if the same is true for the various NaNs as it is for infinity?

Whats more suspicious is double a = std::numeric_limits<float>::infinity(); is also marked as narrowing.

This is definitely not narrowing.

Herald added a project: Restricted Project. · View Herald TranscriptMar 15 2021, 6:05 AM
carlosgalvezp accepted this revision.Apr 6 2023, 7:20 AM
carlosgalvezp added a subscriber: carlosgalvezp.

LGTM, is there anything that should be fixed before merging?

This revision is now accepted and ready to land.Apr 6 2023, 7:20 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 6 2023, 7:20 AM
Herald added a subscriber: PiotrZSL. · View Herald Transcript

Took the liberty to land it for you, hope it's ok! :)