From 82e87a4602a5b7c502674560bc93dd097c59fd02 Mon Sep 17 00:00:00 2001 From: Archit Gupta Date: Mon, 12 Sep 2022 15:59:47 -0700 Subject: [PATCH] Remove unnecessary ternaries --- source/core_mqtt.c | 27 +++++++++++----------- source/core_mqtt_serializer.c | 4 ++-- source/core_mqtt_state.c | 42 +++++++++++++++++------------------ 3 files changed, 36 insertions(+), 37 deletions(-) diff --git a/source/core_mqtt.c b/source/core_mqtt.c index ef1290a4b..2f7fa04f6 100644 --- a/source/core_mqtt.c +++ b/source/core_mqtt.c @@ -565,8 +565,8 @@ static bool matchEndWildcardsSpecialCases( const char * pTopicFilter, ( pTopicFilter[ filterIndex ] == '/' ) ) { /* Check that the last character is a wildcard. */ - matchFound = ( ( pTopicFilter[ filterIndex + 1U ] == '+' ) || - ( pTopicFilter[ filterIndex + 1U ] == '#' ) ) ? true : false; + matchFound = ( pTopicFilter[ filterIndex + 1U ] == '+' ) || + ( pTopicFilter[ filterIndex + 1U ] == '#' ); } return matchFound; @@ -595,9 +595,8 @@ static bool matchWildcards( const char * pTopicName, /* Wild card in a topic filter is only valid either at the starting position * or when it is preceded by a '/'.*/ - locationIsValidForWildcard = ( ( *pFilterIndex == 0u ) || - ( pTopicFilter[ *pFilterIndex - 1U ] == '/' ) - ) ? true : false; + locationIsValidForWildcard = ( *pFilterIndex == 0u ) || + ( pTopicFilter[ *pFilterIndex - 1U ] == '/' ); if( ( pTopicFilter[ *pFilterIndex ] == '+' ) && ( locationIsValidForWildcard == true ) ) { @@ -736,8 +735,8 @@ static bool matchTopicFilter( const char * pTopicName, * case when the topic filter contains the '+' wildcard at a non-starting position. * For example, when matching either of "sport/+/player" OR "sport/hockey/+" topic * filters with "sport/hockey/player" topic name. */ - matchFound = ( ( nameIndex == topicNameLength ) && - ( filterIndex == topicFilterLength ) ) ? true : false; + matchFound = ( nameIndex == topicNameLength ) && + ( filterIndex == topicFilterLength ); } return matchFound; @@ -1581,7 +1580,7 @@ static MQTTStatus_t handleIncomingAck( MQTTContext_t * pContext, case MQTT_PACKET_TYPE_PINGRESP: status = MQTT_DeserializeAck( pIncomingPacket, &packetIdentifier, NULL ); - invokeAppCallback = ( ( status == MQTTSuccess ) && ( manageKeepAlive == false ) ) ? true : false; + invokeAppCallback = ( status == MQTTSuccess ) && !manageKeepAlive; if( ( status == MQTTSuccess ) && ( manageKeepAlive == true ) ) { @@ -1594,7 +1593,7 @@ static MQTTStatus_t handleIncomingAck( MQTTContext_t * pContext, case MQTT_PACKET_TYPE_UNSUBACK: /* Deserialize and give these to the app provided callback. */ status = MQTT_DeserializeAck( pIncomingPacket, &packetIdentifier, NULL ); - invokeAppCallback = ( ( status == MQTTSuccess ) || ( status == MQTTServerRefused ) ) ? true : false; + invokeAppCallback = ( status == MQTTSuccess ) || ( status == MQTTServerRefused ); break; default: @@ -2202,11 +2201,11 @@ static MQTTStatus_t receiveConnack( const MQTTContext_t * pContext, * A value of 0 for the config will try once to read CONNACK. */ if( timeoutMs > 0U ) { - breakFromLoop = ( calculateElapsedTime( getTimeStamp(), entryTimeMs ) >= timeoutMs ) ? true : false; + breakFromLoop = calculateElapsedTime( getTimeStamp(), entryTimeMs ) >= timeoutMs; } else { - breakFromLoop = ( loopCount >= MQTT_MAX_CONNACK_RECEIVE_RETRY_COUNT ) ? true : false; + breakFromLoop = loopCount >= MQTT_MAX_CONNACK_RECEIVE_RETRY_COUNT; loopCount++; } @@ -3044,7 +3043,7 @@ MQTTStatus_t MQTT_MatchTopic( const char * pTopicName, * topic filter length match. */ if( topicNameLength == topicFilterLength ) { - matchStatus = ( strncmp( pTopicName, pTopicFilter, topicNameLength ) == 0 ) ? true : false; + matchStatus = strncmp( pTopicName, pTopicFilter, topicNameLength ) == 0; } if( matchStatus == false ) @@ -3053,8 +3052,8 @@ MQTTStatus_t MQTT_MatchTopic( const char * pTopicName, * topic filter.*/ /* Determine if topic filter starts with a wildcard. */ - topicFilterStartsWithWildcard = ( ( pTopicFilter[ 0 ] == '+' ) || - ( pTopicFilter[ 0 ] == '#' ) ) ? true : false; + topicFilterStartsWithWildcard = ( pTopicFilter[ 0 ] == '+' ) || + ( pTopicFilter[ 0 ] == '#' ); /* Note: According to the MQTT 3.1.1 specification, incoming PUBLISH topic names * starting with "$" character cannot be matched against topic filter starting with diff --git a/source/core_mqtt_serializer.c b/source/core_mqtt_serializer.c index af2ef09ab..83b554ee3 100644 --- a/source/core_mqtt_serializer.c +++ b/source/core_mqtt_serializer.c @@ -1038,12 +1038,12 @@ static MQTTStatus_t processPublishFlags( uint8_t publishFlags, LogDebug( ( "QoS is %d.", ( int ) pPublishInfo->qos ) ); /* Parse the Retain bit. */ - pPublishInfo->retain = ( UINT8_CHECK_BIT( publishFlags, MQTT_PUBLISH_FLAG_RETAIN ) ) ? true : false; + pPublishInfo->retain = UINT8_CHECK_BIT( publishFlags, MQTT_PUBLISH_FLAG_RETAIN ); LogDebug( ( "Retain bit is %d.", ( int ) pPublishInfo->retain ) ); /* Parse the DUP bit. */ - pPublishInfo->dup = ( UINT8_CHECK_BIT( publishFlags, MQTT_PUBLISH_FLAG_DUP ) ) ? true : false; + pPublishInfo->dup = UINT8_CHECK_BIT( publishFlags, MQTT_PUBLISH_FLAG_DUP ); LogDebug( ( "DUP bit is %d.", ( int ) pPublishInfo->dup ) ); } diff --git a/source/core_mqtt_state.c b/source/core_mqtt_state.c index feb38daaf..355a2534a 100644 --- a/source/core_mqtt_state.c +++ b/source/core_mqtt_state.c @@ -239,7 +239,7 @@ static bool validateTransitionPublish( MQTTPublishState_t currentState, /* Transitions from null occur when storing a new entry into the record. */ if( opType == MQTT_RECEIVE ) { - isValid = ( ( newState == MQTTPubAckSend ) || ( newState == MQTTPubRecSend ) ) ? true : false; + isValid = ( newState == MQTTPubAckSend ) || ( newState == MQTTPubRecSend ); } break; @@ -251,11 +251,11 @@ static bool validateTransitionPublish( MQTTPublishState_t currentState, switch( qos ) { case MQTTQoS1: - isValid = ( newState == MQTTPubAckPending ) ? true : false; + isValid = newState == MQTTPubAckPending; break; case MQTTQoS2: - isValid = ( newState == MQTTPubRecPending ) ? true : false; + isValid = newState == MQTTPubRecPending; break; case MQTTQoS0: @@ -272,7 +272,7 @@ static bool validateTransitionPublish( MQTTPublishState_t currentState, /* When a session is reestablished, outgoing QoS1 publishes in state * #MQTTPubAckPending can be resent. The state remains the same. */ - isValid = ( newState == MQTTPubAckPending ) ? true : false; + isValid = newState == MQTTPubAckPending; break; @@ -280,7 +280,7 @@ static bool validateTransitionPublish( MQTTPublishState_t currentState, /* When a session is reestablished, outgoing QoS2 publishes in state * #MQTTPubRecPending can be resent. The state remains the same. */ - isValid = ( newState == MQTTPubRecPending ) ? true : false; + isValid = newState == MQTTPubRecPending; break; @@ -312,12 +312,12 @@ static bool validateTransitionAck( MQTTPublishState_t currentState, /* Incoming publish, QoS 1. */ case MQTTPubAckPending: /* Outgoing publish, QoS 1. */ - isValid = ( newState == MQTTPublishDone ) ? true : false; + isValid = newState == MQTTPublishDone; break; case MQTTPubRecSend: /* Incoming publish, QoS 2. */ - isValid = ( newState == MQTTPubRelPending ) ? true : false; + isValid = newState == MQTTPubRelPending; break; case MQTTPubRelPending: @@ -339,8 +339,8 @@ static bool validateTransitionAck( MQTTPublishState_t currentState, * MQTTPubRelPending. * 7. Sending out a PUBREC will result in this transition * to the same state. */ - isValid = ( ( newState == MQTTPubCompSend ) || - ( newState == MQTTPubRelPending ) ) ? true : false; + isValid = ( newState == MQTTPubCompSend ) || + ( newState == MQTTPubRelPending ); break; case MQTTPubCompSend: @@ -359,18 +359,18 @@ static bool validateTransitionAck( MQTTPublishState_t currentState, * 3. MQTT broker resent the un-acked PUBREL. * 4. Receiving the PUBREL again will result in this transition * to the same state. */ - isValid = ( ( newState == MQTTPublishDone ) || - ( newState == MQTTPubCompSend ) ) ? true : false; + isValid = ( newState == MQTTPublishDone ) || + ( newState == MQTTPubCompSend ); break; case MQTTPubRecPending: /* Outgoing publish, Qos 2. */ - isValid = ( newState == MQTTPubRelSend ) ? true : false; + isValid = newState == MQTTPubRelSend; break; case MQTTPubRelSend: /* Outgoing publish, Qos 2. */ - isValid = ( newState == MQTTPubCompPending ) ? true : false; + isValid = newState == MQTTPubCompPending; break; case MQTTPubCompPending: @@ -390,8 +390,8 @@ static bool validateTransitionAck( MQTTPublishState_t currentState, * 2. An MQTT session is reestablished. * 3. Resending the un-acked PUBREL results in this transition * to the same state. */ - isValid = ( ( newState == MQTTPublishDone ) || - ( newState == MQTTPubCompPending ) ) ? true : false; + isValid = ( newState == MQTTPublishDone ) || + ( newState == MQTTPubCompPending ); break; case MQTTPublishDone: @@ -420,11 +420,11 @@ static bool isPublishOutgoing( MQTTPubAckType_t packetType, case MQTTPuback: case MQTTPubrec: case MQTTPubcomp: - isOutgoing = ( opType == MQTT_RECEIVE ) ? true : false; + isOutgoing = opType == MQTT_RECEIVE; break; case MQTTPubrel: - isOutgoing = ( opType == MQTT_SEND ) ? true : false; + isOutgoing = opType == MQTT_SEND; break; default: @@ -626,7 +626,7 @@ static uint16_t stateSelect( const MQTTContext_t * pMqttContext, while( *pCursor < maxCount ) { /* Check if any of the search states are present. */ - stateCheck = UINT16_CHECK_BIT( searchStates, records[ *pCursor ].publishState ) ? true : false; + stateCheck = UINT16_CHECK_BIT( searchStates, records[ *pCursor ].publishState ); if( stateCheck == true ) { @@ -649,12 +649,12 @@ MQTTPublishState_t MQTT_CalculateStateAck( MQTTPubAckType_t packetType, { MQTTPublishState_t calculatedState = MQTTStateNull; /* There are more QoS2 cases than QoS1, so initialize to that. */ - bool qosValid = ( qos == MQTTQoS2 ) ? true : false; + bool qosValid = qos == MQTTQoS2; switch( packetType ) { case MQTTPuback: - qosValid = ( qos == MQTTQoS1 ) ? true : false; + qosValid = qos == MQTTQoS1; calculatedState = MQTTPublishDone; break; @@ -709,7 +709,7 @@ static MQTTStatus_t updateStateAck( MQTTPubAckInfo_t * records, * is received for an outgoing QoS2 publish. When a PUBREC is received, * record is deleted and added back to the end of the records to maintain * ordering for PUBRELs. */ - shouldDeleteRecord = ( ( newState == MQTTPublishDone ) || ( newState == MQTTPubRelSend ) ) ? true : false; + shouldDeleteRecord = ( newState == MQTTPublishDone ) || ( newState == MQTTPubRelSend ); isTransitionValid = validateTransitionAck( currentState, newState ); if( isTransitionValid == true )