if
is identical to the body of else
175 // Check which frame we should copy.
176 switch (stContainer.eFrameType)
177 {
178 case PIXEL_FORMATS::eDepthDetection: *(stContainer.pFrame) = m_cvProcFrame; break;179 case PIXEL_FORMATS::eTensorflowDetection: *(stContainer.pFrame) = m_cvProcFrame; break;
180 default: *(stContainer.pFrame) = m_cvProcFrame;
181 }
if
is identical to the body of else
157 // Determine which frame should be copied.
158 switch (stContainer.eFrameType)
159 {
160 case PIXEL_FORMATS::eBGRA: *(stContainer.pFrame) = m_cvFrame; break;161 case PIXEL_FORMATS::eDepthMeasure: *(stContainer.pFrame) = m_cvDepthMeasure; break;
162 case PIXEL_FORMATS::eDepthImage: *(stContainer.pFrame) = m_cvDepthImage; break;
163 case PIXEL_FORMATS::eXYZ: *(stContainer.pFrame) = m_cvPointCloud; break;
if
is identical to the body of else
2186 }
2187
2188 // Check if the spatial map was exported successfully.
2189 if (m_slCamera.getSpatialMapRequestStatusAsync() == sl::ERROR_CODE::SUCCESS)2190 {
2191 // Get and store the spatial map.
2192 m_slCamera.retrieveSpatialMapAsync(slSpatialMap);
if
is identical to the body of else
1480 {
1481 // Covariance error.
1482 if (slReturnCode == sl::FUSION_ERROR_CODE::GNSS_DATA_COVARIANCE_MUST_VARY || slReturnCode == sl::FUSION_ERROR_CODE::INVALID_COVARIANCE)
1483 {1484 // Submit logger message.
1485 LOG_WARNING(logging::g_qSharedLogger,
1486 "Unable to ingest fusion GNSS data for camera {} ({})! sl::Fusion positional tracking may be inaccurate! sl::FUSION_ERROR_CODE "
if
is identical to the body of else
1420 // Get the GNSS fix type status from the given GPS coordinate.
1421 switch (stNewGPSLocation.eCoordinateAccuracyFixType)
1422 {
1423 case geoops::PositionFixType::eNoFix:1424 {
1425 slGNSSData.gnss_status = sl::GNSS_STATUS::SINGLE;
1426 slGNSSData.gnss_mode = sl::GNSS_MODE::NO_FIX;
Found repeated branches in if/else statements, consecutive repeated branches in switch statements, and identical true and false branches in conditional operators.
In the bad practice example, the then
and else
branches are identical, which can be considered redundant.
The code can be simplified by removing the conditional statement and executing the shared code unconditionally.
However, if this is the intended behavior, there is no reason to use a conditional statement in the first place.
The check also detects repeated branches in longer if/else if/else chains, where it can be harder to notice the problem. In switch statements, the check reports repeated branches only when they are consecutive. This is because the case labels often have a natural ordering, and rearranging them would decrease code readability.
For example:
switch (ch) {
case 'a':
return 10;
case 'A':
return 10;
case 'b':
return 11;
case 'B':
return 11;
default:
return 10;
}
In this case, the check reports that the 'a'
and 'A'
branches are identical (and that the 'b'
and 'B'
branches are also identical), but it does not report that the default
branch is also identical to the first two branches.
If preserving the order of branches is important, the repeated branches can be combined using multiple case labels:
switch (ch) {
case 'a':
case 'A':
return 10;
case 'b':
case 'B':
return 11;
default:
return 10;
}
In this case, the check does not warn for the repeated return 10;
, as it helps preserve the order of branches.
The check also examines conditional operators and reports code where the true and false branches are identical:
return test_value(x) ? x : x;
Unlike if statements, the check does not detect chains of conditional operators.
Note: This check also reports situations where branches become identical only after preprocessing.
if (test_value(x)) {
y++;
do_something(x, y);
} else {
y++;
do_something(x, y);
}
test_value(x); // can be omitted unless it has side effects
y++;
do_something(x, y);