Tested on llvm codebase.
It have found many places like:
- returning true/false in function returning int,
- assigning true/false to integer inside some classes.
Differential D18821
Add bugprone-bool-to-integer-conversion Prazek on Apr 6 2016, 3:28 AM. Authored by
Details
Diff Detail Event TimelineComment Actions Maybe we should merge it with http://reviews.llvm.org/D18745 and name it 'modernize-wrong-literal-cast'. The other question is, will it be better to move it to readability?
Comment Actions This check throws a warning also on the conversion to floats (probably very rare ones): double number = true; Even though this behavior is correct, the code warns about the implicit conversion to integers.
Comment Actions Actually, did you think about adding this as a clang diagnostic? Richard, what do you think about complaining in Clang about int i = true; kind of code?
Comment Actions BTW, why is the check in the 'modernize' module? It doesn't seem to make anything more modern. I would guess, the pattern it detects is most likely to result from a programming error. Also, the fix, though it retains the behavior, has a high chance to be incorrect. Can you share the results of running this check on LLVM? At least, how many problems it found and how many times the suggested fix was correct. I'd suggest to move the check to misc or maybe it's time to create a separate directory for checks targeting various bug-prone patterns.
Comment Actions There were many places. Most of them where when assigning true/false to some member which was int. Comment Actions Would be nice, if you could tell the number and provide a list of locations. Have any of these been fixed since then? Comment Actions No, I didn't know that I can do that - I always heard that I can't format code that I didn't change, so I though the same thing is here. I will try do post review of changes in clang/llvm soon. Comment Actions http://reviews.llvm.org/D19105 Comment Actions I will think name for new module that would have all the checks like this. Comment Actions Do you have any thought about the name for such a module? I belive that misc is overloaded. So for this we are looking for something that is probably not a bug, but it makes code a little bit inaccurate
Comment Actions I would like to see a new version of http://reviews.llvm.org/D19105 with all the "1-bit-bitfield" diffs removed. Do you have an example of a large codebase where this check runs with few false positives and a non-zero number of true positives? Comment Actions I will post fixex tomorrow. I don't think there will be any false or true positives in warnings, but the main problems with fixes are that many times developer wants to use bools, but it decalres field/return type as int. Comment Actions after a long though I think that "accuracy" is the best name here - we want to look for a code that is valid, but not accurate Comment Actions There are many possible reasons this pattern can appear in the code. Sometimes it's a bug, sometimes, it's an attempt to make the code look better than it is ;) It seems to me though, that having a type mismatch of this kind is a bug-prone pattern, so I would start a more generic "bugprone" category of checks (and move some misc- checks there later on). Also, please re-upload D19105 after disabling matches on 1-bit bitfields in the check. Comment Actions It seems that it works right now. bool b; I will update the llvm/clang changes today.
Comment Actions +Fixed bug with operators I will post changes on clang tomorrow
Comment Actions Sorry for the delay. Your patch was lost in my inbox. Feel free to ping me earlier.
|
with int literals