From 7228592bedb75492db88018da230c1a852408eeb Mon Sep 17 00:00:00 2001 From: Jonathan Stone Date: Mon, 13 Mar 2023 08:55:45 -0700 Subject: [PATCH] Static analysis optimizations This changelist addresses a handful of static analysis optimizations flagged by PVS-Studio: - Pass immutable std::string arguments by const reference. - Remove unnecessary conversions between std::string and char*. - Prefer std::string::empty to comparison against an empty string. - Remove an unused local variable. --- source/MaterialXGraphEditor/Graph.cpp | 16 +++++++--------- source/MaterialXGraphEditor/Graph.h | 6 +++--- source/MaterialXGraphEditor/UiNode.cpp | 2 +- 3 files changed, 11 insertions(+), 13 deletions(-) diff --git a/source/MaterialXGraphEditor/Graph.cpp b/source/MaterialXGraphEditor/Graph.cpp index 2f64f8a486..f84cdd1ab3 100644 --- a/source/MaterialXGraphEditor/Graph.cpp +++ b/source/MaterialXGraphEditor/Graph.cpp @@ -190,8 +190,6 @@ void Graph::addExtraNodes() return; } - const std::vector groups{ "Input Nodes", "Output Nodes", "Group Nodes", "Node Graph" }; - // clear any old nodes, if we previously used tab with another graph doc _extraNodes.clear(); @@ -468,7 +466,7 @@ ImVec2 Graph::layoutPosition(UiNodePtr layoutNode, ImVec2 startingPos, bool init else { // don't set position of group nodes - if (node->getMessage() == "") + if (node->getMessage().empty()) { float x = std::stof(node->getMxElement()->getAttribute("xpos")); float y = std::stof(node->getMxElement()->getAttribute("ypos")); @@ -1075,7 +1073,7 @@ void Graph::setConstant(UiNodePtr node, mx::InputPtr& input, const mx::UIPropert } } // build the initial graph of a loaded mtlx document including shader, material and nodegraph node -void Graph::setUiNodeInfo(UiNodePtr node, std::string type, std::string category) +void Graph::setUiNodeInfo(UiNodePtr node, const std::string& type, const std::string& category) { node->setType(type); node->setCategory(category); @@ -1273,7 +1271,7 @@ void Graph::buildUiBaseGraph(mx::DocumentPtr doc) mx::OutputPtr connectedOutput = input->getConnectedOutput(); int upNum = -1; int downNum = -1; - if (nodeGraphName != "") + if (!nodeGraphName.empty()) { upNum = findNode(nodeGraphName, "nodegraph"); @@ -1289,7 +1287,7 @@ void Graph::buildUiBaseGraph(mx::DocumentPtr doc) upNum = findNode(connectedOutput->getName(), "output"); downNum = findNode(node->getName(), "node"); } - else if (input->getInterfaceName() != "") + else if (!input->getInterfaceName().empty()) { upNum = findNode(input->getInterfaceName(), "input"); downNum = findNode(node->getName(), "node"); @@ -1549,7 +1547,7 @@ void Graph::buildUiNodeGraph(const mx::NodeGraphPtr& nodeGraphs) } // return node position in _graphNodes based off node name and type to account for input/output UiNodes with same names as mx Nodes -int Graph::findNode(std::string name, std::string type) +int Graph::findNode(const std::string& name, const std::string& type) { int count = 0; for (size_t i = 0; i < _graphNodes.size(); i++) @@ -1787,7 +1785,7 @@ void Graph::copyInputs() } } // add node to graphNodes based off of node def information -void Graph::addNode(std::string category, std::string name, std::string type) +void Graph::addNode(const std::string& category, const std::string& name, const std::string& type) { mx::NodePtr node = nullptr; std::vector matchingNodeDefs; @@ -3145,7 +3143,7 @@ void Graph::propertyEditor() if (_currUiNode->getNode()) { ImGui::Text("%s", _currUiNode->getNode()->getCategory().c_str()); - docString += _currUiNode->getNode()->getCategory().c_str(); + docString += _currUiNode->getNode()->getCategory(); docString += ":"; docString += _currUiNode->getNode()->getNodeDef()->getDocString() + "\n"; if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled)) diff --git a/source/MaterialXGraphEditor/Graph.h b/source/MaterialXGraphEditor/Graph.h index 9044eb27a6..296af93f5f 100644 --- a/source/MaterialXGraphEditor/Graph.h +++ b/source/MaterialXGraphEditor/Graph.h @@ -96,10 +96,10 @@ class Graph std::vector createNodes(bool nodegraph); int getNodeId(ed::PinId pinId); int findNode(int nodeId); - int findNode(std::string name, std::string type); - void addNode(std::string category, std::string name, std::string type); + int findNode(const std::string& name, const std::string& type); + void addNode(const std::string& category, const std::string& name, const std::string& type); void deleteNode(UiNodePtr node); - void setUiNodeInfo(UiNodePtr node, std::string type, std::string category); + void setUiNodeInfo(UiNodePtr node, const std::string& type, const std::string& category); // UiEdge functions bool edgeExists(UiEdge edge); diff --git a/source/MaterialXGraphEditor/UiNode.cpp b/source/MaterialXGraphEditor/UiNode.cpp index 8c562a154d..b2c04c5a53 100644 --- a/source/MaterialXGraphEditor/UiNode.cpp +++ b/source/MaterialXGraphEditor/UiNode.cpp @@ -47,7 +47,7 @@ UiNodePtr UiNode::getConnectedNode(const std::string& name) } for (UiEdge edge : edges) { - if (edge.getInputName() == "") + if (edge.getInputName().empty()) { return edge.getDown(); }