Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is the cast in this code?

I have some lines of my code which returns an error from our static code analyzer.

This analyzer is programmed with clang, and the source code of the rule that is violated is quite simple :

// For each cast expression in the code 
bool VisitCastExpr(CastExpr *castExpr){
    string errorMsg;
    string CastName = castExpr->getCastKindName();
    // If cast is from pointer to everything different than an integer, add violation 
    if((castExpr->getCastKind() == CK_MemberPointerToBoolean)||(castExpr->getCastKind() == CK_PointerToBoolean)||(castExpr->getCastKind() == CK_CPointerToObjCPointerCast)||(castExpr->getCastKind() == CK_BlockPointerToObjCPointerCast)||(castExpr->getCastKind() == CK_AnyPointerToBlockPointerCast)){
        errorMsg = "Forbidden cast "+CastName+" from pointer to non-integer type";
        addViolation(castExpr,this,errorMsg);
    }
    return true; 
}

So basically, it just adds a violation when some cast (implicit or explicit) is done from pointer to something different than integer.

Here is one of the expressions that returns an error :

if(st_parametre_embarque.qs_nom.contains("PAR")){.

st_parametre_embarque is just a structure, and the field qs_nom is a QString.

Method Qstring::contains() does return a boolean.

Here is the violation message yielded by the code analyzer :

Forbidden cast PointerToBoolean from pointer to non-integer type

So I really don't see where there could be any castExpr, moreover from pointer to boolean.

like image 247
Marc-O Avatar asked Dec 02 '25 17:12

Marc-O


1 Answers

Before Qt5, QString::contains returned a QBool, not a bool. That value has to be converted to a bool somehow, and the static analyzer has decided it's an implicit cast. Try making an explicit comparison with a boolean constant (i.e. invoking operator==(QBool, bool)) and see if the static analyzer follows it.

like image 177
aecolley Avatar answered Dec 05 '25 06:12

aecolley



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!