Fix cppcheck errors: final follow-up#11584
Conversation
…verified to be correct
…into Cppcheck_final_followup
| #else | ||
| if (number_of_threads > 1) { | ||
| displayMessage("ConvertInputFormat is not compiled with OpenMP. Only running on 1 thread, not requested {} threads.", number_of_threads); | ||
| number_of_threads = 1; |
There was a problem hiding this comment.
In the non-OpenMP path, number_of_threads = 1 is assigned inside the if block but never read afterward. Removed the dead assignment.
| if (errorFound) { | ||
| ShowSevereError(state, EnergyPlus::format("The index of \"{}\" is not found", thisFurnace.SuppHeatCoilName)); | ||
| ShowContinueError(state, EnergyPlus::format("...occurs for {}", thisFurnace.Name)); | ||
| errorFound = false; |
There was a problem hiding this comment.
The last errorFound = false at the end of the suppHeatCoil error block is a dead assignment since the function returns right after and errorFound is never read again. Safe to just remove that line.
|
|
||
| int BoilerNum = 1; | ||
| if (boilerObjects != inputProcessor->epJSON.end()) { | ||
| int BoilerNum = 1; |
There was a problem hiding this comment.
Same issue as the ConvHWBaseboardNum case. BoilerNum is declared outside the if block but only used inside the for loop within it. Just move the declaration inside the if block to tighten the scope. Easy fix, no logic change needed.
|
|
||
| int ConvHWBaseboardNum = 0; | ||
| if (baseboardObjects != inputProcessor->epJSON.end()) { | ||
| int ConvHWBaseboardNum = 0; |
There was a problem hiding this comment.
ConvHWBaseboardNum is declared outside the if block but only used inside the for loop. Just move the declaration inside the if block to tighten the scope. No logic change needed.
|
|
||
| int BaseboardNum = 0; | ||
| if (elecBaseboardObjects != inputProcessor->epJSON.end()) { | ||
| int BaseboardNum = 0; |
| if (TotalLoad > this->MaxLoad) { | ||
| DeRate = true; | ||
| } | ||
| bool DeRate = (TotalLoad > this->MaxLoad); // If true, need to derate aircoils because don't carry over unmet energy |
There was a problem hiding this comment.
DeRate is declared way up at the top of the function(line 15207) but only used in the else block at the very bottom, and it's not even initialized at declaration. Move it into the else block and initialize inline works good and avoids the potential uninitialized read.
| } | ||
|
|
||
| AlphaOffset = 3; | ||
| constexpr int AlphaOffset = 3; // local temp var |
There was a problem hiding this comment.
AlphaOffset is only used inside the for loop, so let's move the declaration there to tighten the scope. No overhead concern here inside the for loop since it's a primitive int (and a constexpr at that, so the compiler just substitutes 3 at compile time anyway).
| state.dataHeatBal->ExtVentedCavity(Item).OSCMPtr = Found; | ||
|
|
||
| Roughness = s_ipsc->cAlphaArgs(3); | ||
| std::string Roughness = s_ipsc->cAlphaArgs(3); |
There was a problem hiding this comment.
Roughness is only used inside the for loop, so move the declaration there and initialize it directly.
| IShadedConst = windowShadingControl.getInputShadedConstruction; | ||
| IShadingDevice = windowShadingControl.ShadingDevice; | ||
| int IShadedConst = windowShadingControl.getInputShadedConstruction; // Construction number of shaded construction | ||
| int IShadingDevice = windowShadingControl.ShadingDevice; // Material number of shading device |
There was a problem hiding this comment.
Both are only used inside the for loop, so declare them there where they're first assigned. This is safe because both are re-initialized at the start of every iteration anyway, so no state leaks between iterations.
|
|
||
| // Check for illegal shading type name | ||
| Found = Util::FindItemInList(s_ipsc->cAlphaArgs(3), cValidShadingTypes, NumValidShadingTypes); | ||
| int Found = Util::FindItemInList(s_ipsc->cAlphaArgs(3), cValidShadingTypes, NumValidShadingTypes); |
There was a problem hiding this comment.
Found is only used in one spot inside the loop, so declare and initialize it there directly.
| Aface = {{0.0}}; | ||
| Bface = {0.0}; | ||
| std::array<std::array<Real64, maxArraySize>, maxArraySize> Aface = {{0.0}}; // Coefficient in equation Aface*thetas = Bface | ||
| std::array<Real64, maxArraySize> Bface = {0.0}; // Coefficient in equation Aface*thetas = Bface |
There was a problem hiding this comment.
Aface and Bface are only used inside the while loop and get reset to zero at the start of each iteration anyway. Move the declarations into the loop and combine them with the reset. No real performance concern here either since both arrays are fully overwritten every iteration and the compiler will optimize the stack allocation away.
| if (Util::SameString(AirflowNetworkLinkageData(i).NodeNames[0], DisSysNodeData(k).Name)) { | ||
| AirflowNetworkLinkageData(i).NodeNums[0] = k; | ||
| for (int l = 1; l <= DisSysNumOfNodes; ++l) { | ||
| if (Util::SameString(AirflowNetworkLinkageData(i).NodeNames[0], DisSysNodeData(l).Name)) { |
There was a problem hiding this comment.
This local k declared inside a nested block that shadows the outer function-level k. Rename the inner one to something more descriptive to avoid the shadow.
| auto &vrfTU = state.dataHVACVarRefFlow->VRFTU(TUIndex); | ||
| if (vrfTU.CoolCoilIndex > 0) { | ||
| DXCoilCap = DXCoils::GetCoilCapacityByIndexType(state, vrfTU.CoolCoilIndex, vrfTU.coolCoilType, errFlag); | ||
| auto &vrfTUobj = state.dataHVACVarRefFlow->VRFTU(TUIndex); |
There was a problem hiding this comment.
vrfTU is declared at function scope and then shadowed inside the for loop by another vrfTU pointing to a different TU. Rename the inner one to vrfTUobj to make it clear these are different objects.
|
|
||
| // Set current outside flux: | ||
| if (construct.SourceSinkPresent) { | ||
| // Set current outside flux: |
There was a problem hiding this comment.
The second and third if (construct.SourceSinkPresent) checks are consecutive with no intervening changes to construct, so they can be merged into one.
| inline int FindItemInList(std::string_view const String, | ||
| Container const &ListOfItems, | ||
| const std::string Container::value_type::*const name_p, | ||
| int const NumItems) |
There was a problem hiding this comment.
name_p is a pointer-to-member that's only used for reading (ListOfItems[i].*name_p), but the member it points to isn't declared const. Change to const to correctly express that the pointed-to member is read-only.
| } | ||
| } else { | ||
| nRunPeriods = 1; | ||
| } |
There was a problem hiding this comment.
nRunPeriods is passed by value, so nRunPeriods = 1 in the else branch is useless. The assignment has no effect after the function returns. Remove the else block entirely.
Pull request overview
After the
cppcheck_finalbranch got merged, there are still a number of cppcheck issues remaining. More than half of them are false positives, but this PR aims to double-check and make sure all actionable ones are addressed.checkLevelNormalerrors: cppcheck reports that certain functions are too complex for full analysis. OK to skip.useStlAlgorithmerrors: cppcheck suggests replacing raw loops withstd::all_oforstd::none_of. OK to skip.constParameterReferenceerrors: Confirmed all remaining warnings are false positives. cppcheck tends to flag cases involvingstatepointer dereferencing andArray1Dtypes where mutations are not properly detected.duplicateExpression error: Addressed in this PR. The 0.1/0.1 assumption is correct perIndoorGreen.cc: Possible bug in aerodynamic resistance calculation (ETBaseFunction) #11560, so a cppcheck-suppress comment was added.unreadVariableerrors: Addressed in this PR.variableScopeerrors: Addressed in this PR.shadowVariableerrors: Addressed in this PR.unpreciseMathCallerrors: Skipped, as fixes caused diffs on Mac.duplicateConditionerror: Addressed in this PR.constParameterPointererror: Addressed in this PR.uselessAssignmentArgerror: Addressed in this PR.constVariablePointererror: Addressed in this PR.constVariableerrors: Addressed in this PR.invalidFunctionArgerror: Addressed in this PR.uselessCallsSubstr,knownConditionTrueFalse,unassignedVariable,passedByValueCallback,stlIfStrFind,redundantAssignment, andconstVariableReferenceerrors is in progress.Description of the purpose of this PR
Pull Request Author
Reviewer