◐ Shell
clean mode source ↗

enabled and fixed `bugprone-narrowing-conversions` clang-tidy warnings by firewave · Pull Request #611 · cppcheck-opensource/simplecpp

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, not if the narrowing is intentional. The point is to make the code explicit.

I feel that casts are dangerous because they hide all compiler warnings about all conversion errors. It's the sloppy approach to silencing warnings. Similar to writing --suppress=*. And there is no mechanism to detect redundant casts.

It does not say specifically that it tries to prevent warnings about the narrowing conversion. You could suggest a new cppcheck checker that will warn about this which provides a more explicit mechanism to hide the warnings. And I could approve that.

A similar code example where there is no warning:

void foo(unsigned int x) {
    char y = static_cast<char>(x);  // <- we have loss of precision here
}

Your cast hides warning about the sign conversion and it would also hide future warnings about loss of precision if we will have that bug. Real bugs can be hidden.