Page MenuHomePhabricator

[clang-tidy] Extension of checker misc-misplaced-widening-cast

Authored by baloghadamsoftware on Mar 9 2016, 4:44 AM.



Existing checker misc-misplaced-widening-cast was extended:

  • New use cases: casted expression as lhs or rhs of a logical comparison or function argument
  • New types: beside int, long and long long various char types, short and int128 added
  • New option to check implicit casts: forgetting a cast is at least as common and as dangerous as misplacing it. This option can be disabled.

This patch depends on AST Matcher patches D17986 and D18243 and also contains fix for checker misc-bool-pointer-implicit-conversion needed because of the fix in the AST Matcher patch.

Diff Detail

Event Timeline

baloghadamsoftware retitled this revision from to Extension of checker misc-misplaced-widening-cast.
baloghadamsoftware updated this object.
baloghadamsoftware added a reviewer: alexfh.
baloghadamsoftware added a subscriber: cfe-commits.
baloghadamsoftware retitled this revision from Extension of checker misc-misplaced-widening-cast to [clang-tidy] Extension of checker misc-misplaced-widening-cast.Mar 9 2016, 4:46 AM

Prerequisites (matchers) are accepted now.

alexfh added inline comments.Mar 22 2016, 4:29 AM

The line exceeds 80 columns limit. Please clang-format the change.


isa<T> should be used instead of dyn_cast<T>, when you don't need the resulting pointer.


isSpecificBuiltinType is not the best tool here. Instead, we could get the builtin type kinds and work with them (CalcType->getAs<BuiltinType>()->getKind() and CastType->getAs<BuiltinType>()->getKind() after checking that both are BuiltinTypes).


Please truncate "is ineffective, or there is loss of precision before the conversion [misc-misplaced-widening-cast]" in all CHECK-MESSAGES lines but the first one.


It makes sense to leave the to '<target type' part in all messages.


I do not like it either, just reused the original code and copy-pasted it. I also think that getKind() should be used and instead of the long branches we could do the comparison using relative size table(s).

Required fixes done.

alexfh added inline comments.Mar 24 2016, 7:59 AM

This code shouldn't repeat the lookups. You can, for example, use iterators to keep the results of .find().

It also seems more appropriate to use SmallDenseMap.


Looks much better now, thanks!


Please use const auto * to make it obvious it's a pointer.


No need for the braces around single-line if bodies.


We should also have a test with this option turned off.

alexfh requested changes to this revision.Mar 24 2016, 8:00 AM
alexfh edited edge metadata.
This revision now requires changes to proceed.Mar 24 2016, 8:00 AM
baloghadamsoftware edited edge metadata.

Requested revision done.

baloghadamsoftware marked 9 inline comments as done.Mar 25 2016, 6:32 AM
baloghadamsoftware added inline comments.

I changed to SmallDenseMap and its lookup() member function is nice. However it unfortunately lacks an initializer_list constructor.


Is this an LLVM style rule? I always learned that it is the safest to use braces even for single-line if bodies.

alexfh accepted this revision.Apr 1 2016, 5:54 PM
alexfh edited edge metadata.

Looks good! Thank you for addressing the comments.


It has a constructor from two iterators, but I'm not sure it'll result in a significantly better code.

188 is surprisingly inconsistent in this regard, but "no braces around single-line if/for/... bodies" is a more common style in LLVM and, in particular, in clang-tidy code. The problems this style might lead to (statements indented as-if they were under if/for/..., but they actually aren't) are mitigated by the wide use of clang-format.

This revision is now accepted and ready to land.Apr 1 2016, 5:54 PM
xazax.hun closed this revision.Apr 6 2016, 5:10 AM