[Libreoffice] [PATCH] Simple fix for fontconfig.cxx to review (from cppcheck report)

Mat M matm at gmx.fr
Sun Jan 15 05:18:14 PST 2012


Le Sun, 15 Jan 2012 06:48:28 +0100, Ivan Timofeev <timofeev.i.s at gmail.com>  
a écrit:

> Hi Mat,
>
>>> cppcheck reported this :
>>> core/vcl/generic/fontmanager/fontconfig.cxx
>>> 196 duplicateExpression style Same expression on both sides of '-'
>>>
>>> class SortFont : public ::std::binary_function< const FcPattern*, const
>>> FcPattern*, bool >
>>> {
>>> public:
>>> bool operator()(const FcPattern *a, const FcPattern *b)
>>> {
>>> int comp = compareFontNames(a, b);
>>> if (comp != 0)
>>> return comp < 0;
>>>
>>> int nVersionA=0, nVersionB=0;
>>>
>>> bool bHaveA = FcPatternGetInteger(a, FC_FONTVERSION, 0,
>>> &nVersionA) == FcResultMatch;
>>> bool bHaveB = FcPatternGetInteger(b, FC_FONTVERSION, 0,
>>> &nVersionB) == FcResultMatch;
>>>
>>> if (bHaveA && bHaveB)
>>> return nVersionA > nVersionB;
>>>
>>> 196: return bHaveA - bHaveA;<--- Same expression on both sides of
>>> '-'.
>>> }
>>> };
>>>
>>> it seems it should be :
>>> return bHaveA - bHaveB;
>>>
>>> Any idea ?
>>>
>> Subtracting booleans ??? Is there some magic here ?
>> Moreover, if we state that boolean difference returns a boolean, is
>> there an automatic cast, as the function should return an int ?
>
> AFAIK in arithmetic bools are concerted to int so that true->1 and  
> false->0.
> Then, the result is converted back to bool (because the function returns  
> bool) so that 0->false and 'any other value'->true.
>
> So "return bool1 - bool2" can be replaced with not-so-magic "return  
> bool1 != bool2", right?
>

For operator(), absolutely. I was actually talking about compareFontNames.  
But knowing that C++ does "on-the-fly" cast explains all.

> By the way, the author of the code is Caolan, let's ask him. :)
>
> Ivan
>
Mat


More information about the LibreOffice mailing list