Checks for narrowing conversions, e.g.
int i = 0;
i += 0.1;
Differential D38455
[clang-tidy] new cppcoreguidelines-narrowing-conversions check. courbet on Oct 2 2017, 5:48 AM. Authored by
Details Checks for narrowing conversions, e.g. int i = 0;
Diff Detail
Event TimelineComment Actions Before going deeper, I'd like to understand what's the difference between this check and -Wconversion group of clang warnings? Comment Actions This check is a more useful subset of -Wfloat-conversion that targets cases that are more likely to be bugs than the rest of -Wfloat-conversion. Since "more useful" is a somewhat subjective notion, let me point out a typical class of bugs that this uncovers: // Paying 0.5 less dollars per day. int to_be_payed = 0; for (const auto& v : us_dollars_per_day) { to_be_payed += v[i]; } This is not necessarily more dangerous than int my_int = my_float;, but the effects will add up dramatically. Why can't this be done in clang ? It could, but we would need to add more fine-grained control of what to enable or not enable. It feels more inline with clang-tidy's options approach, but I don;t have strong feelings on this. Comment Actions One more reason for this check being in clang-tidy would be automatically inserting narrow_cast<T>(v), which is the utility of the gsl to make narrowing casts explicitly visible. clang-tidy implements bugprone-integer-division already, which does a somewhat related thing: Comment Actions Do we already have other cppcoreguidelines checks that provides fixes with symbols from the the gsl ? Comment Actions
AFAIK not yet. I am working on fixits for gsl::owner<> and the bounds-* stuff could suggest gsl::at() as well. I think you should add std::floor and std::ceil recognition here. This would be inline with hicpp, too. Comment Actions Now I finally got around to this. Since the "Enforcement" part of the relevant C++ Core Guidelines rule is not very helpful (kind of a "hey, we know enforcing this rule will lead to tons of false positive, so we don't know what exactly makes sense to enforce"), this may well be a good first step. I personally have no concerns. Maybe someone more interested in this module will have something to say though. Could you rebase the patch against current head?
Comment Actions
Removing from my dashboard for now. Comment Actions OK, here's an analysis of 100 instances of a check that would warn on all fully implicit (no C/static/reinterpret/...) casts from float/double to integral: link
Comment Actions Hi, my 2 cents:
Comment Actions Hi Jonas, A large repository of open-source code, plus internal code at google. External code includes e.g. code from ffmpeg, Eigen, R, Chromium, gnuplot, lua ,...
Yes, that's the version for which I have provided analysis. I'll update the diff with that version.
The "normal" assignments are covered by the implicitCastExpr() above. Comment Actions That sounds good.
@aaron.ballman seems to be busy now, maybe you should add alexfh again and discuss the results. Comment Actions Is there anything I need to do for the diff to change state ? I thought updating the code would automatically mark it "ready for review" again. Comment Actions
thought updating the code would automatically mark it "ready for review" I think all comments must be marked as done to be ready for review But aaron seems busy right now and i think alexfh did disable the Am 09.04.2018 um 12:55 schrieb Clement Courbet via Phabricator:
Comment Actions
I see, thanks.
Comment Actions Thanks.
Comment Actions Could you please add some tests that include user defined literals and there interaction with other literals. We should catch narrowing conversions from them, too. Comment Actions Sorry for the long review due to the holidays. Generally, I would also like Aaron to take a look when he's back, since he had some concerns. While we're waiting, one minor comment from me.
Comment Actions User defined literals do not have this issue: the only accepted signatures are with long double or unsigned long long parameters. Narrowing cannot happen because XYZ_ud actually means operator""(XYZull) (same for floats). I've added a test with a narrowing on the return value. Comment Actions Sure. Is it OK to add a dependency from clang-tidy/bugprone/BugproneTidyModule.cpp to stuff in clang-tidy/cpp-coreguidelines ? Is there an existing example of this ? Comment Actions
Take a look in the hicpp module, almost everything there is aliased :) Comment Actions We currently don't have any strict rules about aliases, but we should remember that aliases have certain costs (in terms of maintenance and possible user-facing issues like duplicate warnings from different checks). I think that in general it would be better to avoid excessive aliases. There are cases when aliases are inevitable (or at least feel like a proper solution), like when multiple style guides have the same rule or very similar rules that make sense to be implemented in a single check with a bit of configurability to easily accommodate the differences. But in this case there's so far one specific rule of the C++ Core Guidelines that the check is enforcing, and adding an alias under bugprone- for it seems to be unnecessary. I also think that if a user wants to enable this check, they'd better know that it backs up a rule of the C++ Core Guidelines rather than being just an invention of clang-tidy contributors (there's nothing wrong with the latter, it's just nice to give credits where appropriate ;). I suggest to try applying the following decision process to find proper place(s) for a check:
Comment Actions
Comment Actions Thanks.
|