The following code is safe and should not trigger the warning
constexpr std::size_t k1Mb = 1024 * 1024;
Fixes #64732
Differential D158338
[clang-tidy] [bugprone-implicit-widening-of-multiplication-result] Improved check to ignore false positives with integer literals. felix642 on Aug 19 2023, 10:10 AM. Authored by
Details
The following code is safe and should not trigger the warning Fixes #64732
Diff Detail
Event TimelineComment Actions I'm not sure if this is right fix, example: const std::size_t k1Tb = 1024 * 1024 * 1024 * 1024; This is detected by -Winteger-overflow, but this: const std::size_t k1Pb = 1024U * 1024U * 1024U * 1024U * 1024U; is not detect by anything except this check, even that it overflow to 0. For me we shoudn't silent those issues, if someone want they they can always put nolint or explicit cast, we could consider adding some basic calculations just to verify if there will be no overflow, but still proper way would be to provide warning that would say, hey, write this as: "1024LU * 1024LU" Comment Actions In short I'm against simply ignoring literal calculations because this check is only thing that currently detect this issue: https://github.com/llvm/llvm-project/issues/64828 Comment Actions Hi @PiotrZSL thank you for taking the time to look at this revision. I agree with you we should not silence a warning if no other tool can diagnose the issue. I'm guessing that -Winteger-overflow does no trigger any warning on unsigned "overflow" since behavior is well defined in the standard :
But on the other hand I have to agree with DenisYaroshevskiy that it is tedious to add a cast to every multiplication of integer literals on a large codebase. Especially when we know that those values do not overflow. Maybe we should add some basic calculations when the operation is composed of integer literals, like you previously mentioned, to check if the operation actually overflows and print this warning if it does? Comment Actions Yes doing some basic calculations would be probably the best, but for that we would need to implement some "calculator" or find some exist implementation some're in clang. |