Skip to content

Fix cppcheck errors: final follow-up#11584

Draft
dareumnam wants to merge 10 commits into
developfrom
Cppcheck_final_followup
Draft

Fix cppcheck errors: final follow-up#11584
dareumnam wants to merge 10 commits into
developfrom
Cppcheck_final_followup

Conversation

@dareumnam
Copy link
Copy Markdown
Collaborator

@dareumnam dareumnam commented May 8, 2026

Pull request overview

After the cppcheck_final branch 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.

  • 60 checkLevelNormal errors: cppcheck reports that certain functions are too complex for full analysis. OK to skip.
  • 122 useStlAlgorithm errors: cppcheck suggests replacing raw loops with std::all_of or std::none_of. OK to skip.
  • 96 constParameterReference errors: Confirmed all remaining warnings are false positives. cppcheck tends to flag cases involving state pointer dereferencing and Array1D types where mutations are not properly detected.
  • 1 duplicateExpression error: Addressed in this PR. The 0.1/0.1 assumption is correct per IndoorGreen.cc: Possible bug in aerodynamic resistance calculation (ETBaseFunction) #11560, so a cppcheck-suppress comment was added.
  • 2 unreadVariable errors: Addressed in this PR.
  • 17 variableScope errors: Addressed in this PR.
  • 3 shadowVariable errors: Addressed in this PR.
  • 10 unpreciseMathCall errors: Skipped, as fixes caused diffs on Mac.
  • 1 duplicateCondition error: Addressed in this PR.
  • 1 constParameterPointer error: Addressed in this PR.
  • 1 uselessAssignmentArg error: Addressed in this PR.
  • 1 constVariablePointer error: Addressed in this PR.
  • 4 constVariable errors: Addressed in this PR.
  • 1 invalidFunctionArg error: Addressed in this PR.
  • Review of remaining uselessCallsSubstr, knownConditionTrueFalse, unassignedVariable, passedByValueCallback, stlIfStrFind, redundantAssignment, and constVariableReference errors is in progress.

Description of the purpose of this PR

Pull Request Author

  • Title of PR should be user-synopsis style (clearly understandable in a standalone changelog context)
  • Label the PR with at least one of: Defect, Refactoring, NewFeature, Performance, and/or DoNoPublish
  • Pull requests that impact EnergyPlus code must also include unit tests to cover enhancement or defect repair
  • Author should provide a "walkthrough" of relevant code changes using a GitHub code review comment process
  • If any diffs are expected, author must demonstrate they are justified using plots and descriptions
  • If changes fix a defect, the fix should be demonstrated in plots and descriptions
  • If any defect files are updated to a more recent version, upload new versions here or on DevSupport
  • If IDD requires transition, transition source, rules, ExpandObjects, and IDFs must be updated, and add IDDChange label
  • If structural output changes, add to output rules file and add OutputChange label
  • If adding/removing any LaTeX docs or figures, update that document's CMakeLists file dependencies
  • If adding/removing any output files (e.g., eplustbl.*)
    • Update ..\scripts\Epl-run.bat
    • Update ..\scripts\RunEPlus.bat
    • Update ..\src\EPLaunch\ MainModule.bas, epl-ui.frm, and epl.vbp (VersionComments)
    • Update ...github\workflows\energyplus.py

Reviewer

  • Perform a Code Review on GitHub
  • If branch is behind develop, merge develop and build locally to check for side effects of the merge
  • If defect, verify by running develop branch and reproducing defect, then running PR and reproducing fix
  • If feature, test running new feature, try creative ways to break it
  • CI status: all green or justified
  • Check that performance is not impacted (CI Linux results include performance check)
  • Run Unit Test(s) locally
  • Check any new function arguments for performance impacts
  • Verify IDF naming conventions and styles, memos and notes and defaults
  • If new idf included, locally check the err file and other outputs

@dareumnam dareumnam added the DoNotPublish Includes changes that shouldn't be reported in the changelog label May 8, 2026
#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;
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;
Copy link
Copy Markdown
Collaborator Author

@dareumnam dareumnam May 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.

if (TotalLoad > this->MaxLoad) {
DeRate = true;
}
bool DeRate = (TotalLoad > this->MaxLoad); // If true, need to derate aircoils because don't carry over unmet energy
Copy link
Copy Markdown
Collaborator Author

@dareumnam dareumnam May 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)) {
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;
}
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

DoNotPublish Includes changes that shouldn't be reported in the changelog

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants