From 95b37fa80641fe3b9d9aba09082c3740532eeb22 Mon Sep 17 00:00:00 2001 From: Ladislav Michl Date: Wed, 15 Nov 2023 10:12:42 +0100 Subject: [PATCH] Base: Do not use else before return --- src/Base/Interpreter.cpp | 5 +--- src/Base/MatrixPyImp.cpp | 8 ++--- src/Base/Parameter.cpp | 6 +--- src/Base/PlacementPyImp.cpp | 18 +++++------- src/Base/PyObjectBase.cpp | 43 ++++++++++++--------------- src/Base/PyTools.c | 58 +++++++++++++++++-------------------- src/Base/QuantityPyImp.cpp | 10 +++---- src/Base/Reader.cpp | 44 ++++++++++++---------------- src/Base/RotationPyImp.cpp | 18 +++++------- src/Base/StackWalker.cpp | 5 +--- src/Base/UnitPyImp.cpp | 24 ++++++--------- src/Base/UnitsApi.cpp | 1 + src/Base/VectorPyImp.cpp | 51 ++++++++++++-------------------- 13 files changed, 115 insertions(+), 176 deletions(-) diff --git a/src/Base/Interpreter.cpp b/src/Base/Interpreter.cpp index 9c5b7be311..b1e9870f27 100644 --- a/src/Base/Interpreter.cpp +++ b/src/Base/Interpreter.cpp @@ -475,14 +475,11 @@ void InterpreterSingleton::runFile(const char* pxFileName, bool local) if (PyErr_ExceptionMatches(PyExc_SystemExit)) { throw SystemExitException(); } - throw PyException(); } Py_DECREF(result); } - else { - throw FileException("Unknown file", pxFileName); - } + throw FileException("Unknown file", pxFileName); } bool InterpreterSingleton::loadModule(const char* psModName) diff --git a/src/Base/MatrixPyImp.cpp b/src/Base/MatrixPyImp.cpp index 8d0229b0ec..0fdf71b054 100644 --- a/src/Base/MatrixPyImp.cpp +++ b/src/Base/MatrixPyImp.cpp @@ -256,11 +256,9 @@ PyObject* MatrixPy::richCompare(PyObject* v, PyObject* w, int op) Py_INCREF(res); return res; } - else { - // This always returns False - Py_INCREF(Py_NotImplemented); - return Py_NotImplemented; - } + // This always returns False + Py_INCREF(Py_NotImplemented); + return Py_NotImplemented; } PyObject* MatrixPy::move(PyObject* args) diff --git a/src/Base/Parameter.cpp b/src/Base/Parameter.cpp index 47163d7259..54a33bf907 100644 --- a/src/Base/Parameter.cpp +++ b/src/Base/Parameter.cpp @@ -501,11 +501,7 @@ std::vector> ParameterGrp::GetGroups() /// test if this group is empty bool ParameterGrp::IsEmpty() const { - if (_pGroupNode && _pGroupNode->getFirstChild()) { - return false; - } - - return true; + return !(_pGroupNode && _pGroupNode->getFirstChild()); } /// test if a special sub group is in this group diff --git a/src/Base/PlacementPyImp.cpp b/src/Base/PlacementPyImp.cpp index fb5367b591..d57791d69f 100644 --- a/src/Base/PlacementPyImp.cpp +++ b/src/Base/PlacementPyImp.cpp @@ -148,22 +148,18 @@ PyObject* PlacementPy::richCompare(PyObject* v, PyObject* w, int op) PyErr_SetString(PyExc_TypeError, "no ordering relation is defined for Placement"); return nullptr; } - else if (op == Py_EQ) { + if (op == Py_EQ) { res = (p1 == p2) ? Py_True : Py_False; Py_INCREF(res); return res; } - else { - res = (p1 != p2) ? Py_True : Py_False; - Py_INCREF(res); - return res; - } - } - else { - // This always returns False - Py_INCREF(Py_NotImplemented); - return Py_NotImplemented; + res = (p1 != p2) ? Py_True : Py_False; + Py_INCREF(res); + return res; } + // This always returns False + Py_INCREF(Py_NotImplemented); + return Py_NotImplemented; } PyObject* PlacementPy::move(PyObject* args) diff --git a/src/Base/PyObjectBase.cpp b/src/Base/PyObjectBase.cpp index 4f00c0b4ae..0c4000ddfa 100644 --- a/src/Base/PyObjectBase.cpp +++ b/src/Base/PyObjectBase.cpp @@ -365,7 +365,7 @@ int PyObjectBase::__setattro(PyObject *obj, PyObject *attro, PyObject *value) PyErr_Format(PyExc_AttributeError, "Cannot delete attribute: '%s'", attr); return -1; } - else if (!static_cast(obj)->isValid()){ + if (!static_cast(obj)->isValid()){ PyErr_Format(PyExc_ReferenceError, "Cannot access attribute '%s' of deleted object", attr); return -1; } @@ -404,36 +404,32 @@ PyObject *PyObjectBase::_getattr(const char *attr) // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) return reinterpret_cast(Py_TYPE(this)); } - else if (streq(attr, "__members__")) { + if (streq(attr, "__members__")) { // Use __dict__ instead as __members__ is deprecated return nullptr; } - else if (streq(attr,"__dict__")) { + if (streq(attr,"__dict__")) { // Return the default dict PyTypeObject *tp = Py_TYPE(this); Py_XINCREF(tp->tp_dict); return tp->tp_dict; } - else if (streq(attr,"softspace")) { + if (streq(attr,"softspace")) { // Internal Python stuff return nullptr; } - else { - // As fallback solution use Python's default method to get generic attributes - PyObject *w{}; - PyObject *res{}; - w = PyUnicode_InternFromString(attr); - if (w) { - res = PyObject_GenericGetAttr(this, w); - Py_XDECREF(w); - return res; - } else { - // Throw an exception for unknown attributes - PyTypeObject *tp = Py_TYPE(this); - PyErr_Format(PyExc_AttributeError, "%.50s instance has no attribute '%.400s'", tp->tp_name, attr); - return nullptr; - } + // As fallback solution use Python's default method to get generic attributes + PyObject *w{}, *res{}; + w = PyUnicode_InternFromString(attr); + if (w) { + res = PyObject_GenericGetAttr(this, w); + Py_XDECREF(w); + return res; } + // Throw an exception for unknown attributes + PyTypeObject *tp = Py_TYPE(this); + PyErr_Format(PyExc_AttributeError, "%.50s instance has no attribute '%.400s'", tp->tp_name, attr); + return nullptr; } int PyObjectBase::_setattr(const char *attr, PyObject *value) @@ -449,12 +445,11 @@ int PyObjectBase::_setattr(const char *attr, PyObject *value) int res = PyObject_GenericSetAttr(this, w, value); Py_DECREF(w); return res; - } else { - // Throw an exception for unknown attributes - PyTypeObject *tp = Py_TYPE(this); - PyErr_Format(PyExc_AttributeError, "%.50s instance has no attribute '%.400s'", tp->tp_name, attr); - return -1; } + // Throw an exception for unknown attributes + PyTypeObject *tp = Py_TYPE(this); + PyErr_Format(PyExc_AttributeError, "%.50s instance has no attribute '%.400s'", tp->tp_name, attr); + return -1; } /*------------------------------ diff --git a/src/Base/PyTools.c b/src/Base/PyTools.c index 4a90a3becd..e71216a224 100644 --- a/src/Base/PyTools.c +++ b/src/Base/PyTools.c @@ -359,27 +359,25 @@ PP_Convert_Result(PyObject *presult, const char *resFormat, void *resTarget) Py_DECREF(presult); /* procedures and stmts return None */ return 0; } - else if (! PyArg_Parse(presult, resFormat, resTarget)) { /* convert Python->C */ Py_DECREF(presult); /* need not be tuple */ return -1; /* error in convert */ } - else { - if (strcmp(resFormat, "O") != 0) { /* free object unless exported */ - if (strcmp(resFormat, "s") == 0) { /* copy string: caller owns it */ - char **target = (char**) resTarget; + if (strcmp(resFormat, "O") != 0) { /* free object unless exported */ + if (strcmp(resFormat, "s") == 0) { /* copy string: caller owns it */ + char **target = (char**) resTarget; #if defined (__GNUC__) - *target = strdup(*target); + *target = strdup(*target); #else - *target = _strdup(*target); + *target = _strdup(*target); #endif - } - Py_DECREF(presult); } - return 0; /* returns 0=success, -1=failure */ - } /* if 0: C result in *resTarget */ -} /* caller must decref if fmt="O" */ - /* caller must free() if fmt="s" */ + Py_DECREF(presult); + } + return 0; /* returns 0=success, -1=failure */ + /* if 0: C result in *resTarget */ +} /* caller must decref if fmt="O" */ + /* caller must free() if fmt="s" */ int PP_Get_Global(const char *modname, const char *varname, const char *resfmt, void *cresult) @@ -430,27 +428,26 @@ int PP_DEBUG = 0; /* debug embedded code with pdb? */ const char *PP_Init(const char *modname) { Py_Initialize(); /* init python if needed */ - if (modname!=NULL) return modname; - { /* we assume here that the caller frees allocated memory */ - return "__main__"; - } + if (modname) + return modname; + /* we assume here that the caller frees allocated memory */ + return "__main__"; } int -PP_Make_Dummy_Module(const char *modname) /* namespace for strings, if no file */ -{ /* instead of sharing __main__ for all */ - PyObject *module = NULL, *dict = NULL; /* note: __main__ is created in py_init */ +PP_Make_Dummy_Module(const char *modname) /* namespace for strings, if no file */ +{ /* instead of sharing __main__ for all */ + PyObject *module = NULL, *dict = NULL; /* note: __main__ is created in py_init */ Py_Initialize(); module = PyImport_AddModule(modname); /* fetch or make, no load */ if (module == NULL) /* module not incref'd */ return -1; - else { /* module.__dict__ */ - dict = PyModule_GetDict(module); /* ['__dummy__'] = None */ - PyDict_SetItemString(dict, "__dummy__", Py_None); - PyDict_SetItemString(dict, "__builtins__", PyEval_GetBuiltins()); - return 0; - } + /* module.__dict__ */ + dict = PyModule_GetDict(module); /* ['__dummy__'] = None */ + PyDict_SetItemString(dict, "__dummy__", Py_None); + PyDict_SetItemString(dict, "__builtins__", PyEval_GetBuiltins()); + return 0; } @@ -479,17 +476,14 @@ PP_Load_Module(const char *modname) /* modname can be "package.module" for PyDict_GetItemString(PyModule_GetDict(module), "__dummy__")) { return module; /* not increfd */ } - else if (PP_RELOAD && module != NULL && PyModule_Check(module)) { module = PyImport_ReloadModule(module); /* reload file,run code */ Py_XDECREF(module); /* still on sys.modules */ return module; /* not increfd */ } - else { - module = PyImport_ImportModule(modname); /* fetch or load module */ - Py_XDECREF(module); /* still on sys.modules */ - return module; /* not increfd */ - } + module = PyImport_ImportModule(modname); /* fetch or load module */ + Py_XDECREF(module); /* still on sys.modules */ + return module; /* not increfd */ } diff --git a/src/Base/QuantityPyImp.cpp b/src/Base/QuantityPyImp.cpp index 6069342ebf..527c01e2d4 100644 --- a/src/Base/QuantityPyImp.cpp +++ b/src/Base/QuantityPyImp.cpp @@ -508,20 +508,18 @@ PyObject* QuantityPy::number_power_handler(PyObject* self, PyObject* other, PyOb return new QuantityPy(new Quantity(q)); } - else if (PyFloat_Check(other)) { + if (PyFloat_Check(other)) { Base::Quantity* a = static_cast(self)->getQuantityPtr(); double b = PyFloat_AsDouble(other); return new QuantityPy(new Quantity(a->pow(b))); } - else if (PyLong_Check(other)) { + if (PyLong_Check(other)) { Base::Quantity* a = static_cast(self)->getQuantityPtr(); double b = (double)PyLong_AsLong(other); return new QuantityPy(new Quantity(a->pow(b))); } - else { - PyErr_SetString(PyExc_TypeError, "Expected quantity or number"); - return nullptr; - } + PyErr_SetString(PyExc_TypeError, "Expected quantity or number"); + return nullptr; } PY_CATCH } diff --git a/src/Base/Reader.cpp b/src/Base/Reader.cpp index 6b6068bf12..3fb14a803c 100644 --- a/src/Base/Reader.cpp +++ b/src/Base/Reader.cpp @@ -116,12 +116,10 @@ long Base::XMLReader::getAttributeAsInteger(const char* AttrName) const if (pos != AttrMap.end()) { return atol(pos->second.c_str()); } - else { - // wrong name, use hasAttribute if not sure! - std::ostringstream msg; - msg << "XML Attribute: \"" << AttrName << "\" not found"; - throw Base::XMLAttributeError(msg.str()); - } + // wrong name, use hasAttribute if not sure! + std::ostringstream msg; + msg << "XML Attribute: \"" << AttrName << "\" not found"; + throw Base::XMLAttributeError(msg.str()); } unsigned long Base::XMLReader::getAttributeAsUnsigned(const char* AttrName) const @@ -131,12 +129,10 @@ unsigned long Base::XMLReader::getAttributeAsUnsigned(const char* AttrName) cons if (pos != AttrMap.end()) { return strtoul(pos->second.c_str(), nullptr, 10); } - else { - // wrong name, use hasAttribute if not sure! - std::ostringstream msg; - msg << "XML Attribute: \"" << AttrName << "\" not found"; - throw Base::XMLAttributeError(msg.str()); - } + // wrong name, use hasAttribute if not sure! + std::ostringstream msg; + msg << "XML Attribute: \"" << AttrName << "\" not found"; + throw Base::XMLAttributeError(msg.str()); } double Base::XMLReader::getAttributeAsFloat(const char* AttrName) const @@ -146,12 +142,10 @@ double Base::XMLReader::getAttributeAsFloat(const char* AttrName) const if (pos != AttrMap.end()) { return atof(pos->second.c_str()); } - else { - // wrong name, use hasAttribute if not sure! - std::ostringstream msg; - msg << "XML Attribute: \"" << AttrName << "\" not found"; - throw Base::XMLAttributeError(msg.str()); - } + // wrong name, use hasAttribute if not sure! + std::ostringstream msg; + msg << "XML Attribute: \"" << AttrName << "\" not found"; + throw Base::XMLAttributeError(msg.str()); } const char* Base::XMLReader::getAttribute(const char* AttrName) const @@ -161,12 +155,10 @@ const char* Base::XMLReader::getAttribute(const char* AttrName) const if (pos != AttrMap.end()) { return pos->second.c_str(); } - else { - // wrong name, use hasAttribute if not sure! - std::ostringstream msg; - msg << "XML Attribute: \"" << AttrName << "\" not found"; - throw Base::XMLAttributeError(msg.str()); - } + // wrong name, use hasAttribute if not sure! + std::ostringstream msg; + msg << "XML Attribute: \"" << AttrName << "\" not found"; + throw Base::XMLAttributeError(msg.str()); } bool Base::XMLReader::hasAttribute(const char* AttrName) const @@ -216,7 +208,7 @@ void Base::XMLReader::readElement(const char* ElementName) // thus we must stop reading on. break; } - else if (ReadType == EndDocument) { + if (ReadType == EndDocument) { // the end of the document has been reached but we still try to continue on reading throw Base::XMLParseException("End of document reached"); } @@ -276,7 +268,7 @@ void Base::XMLReader::readEndElement(const char* ElementName, int level) && (level < 0 || level == Level)) { return; } - else if (ReadType == EndDocument) { + if (ReadType == EndDocument) { // the end of the document has been reached but we still try to continue on reading throw Base::XMLParseException("End of document reached"); } diff --git a/src/Base/RotationPyImp.cpp b/src/Base/RotationPyImp.cpp index 8d0b019a1c..a4747ac6f1 100644 --- a/src/Base/RotationPyImp.cpp +++ b/src/Base/RotationPyImp.cpp @@ -271,22 +271,18 @@ PyObject* RotationPy::richCompare(PyObject* v, PyObject* w, int op) PyErr_SetString(PyExc_TypeError, "no ordering relation is defined for Rotation"); return nullptr; } - else if (op == Py_EQ) { + if (op == Py_EQ) { res = (r1 == r2) ? Py_True : Py_False; Py_INCREF(res); return res; } - else { - res = (r1 != r2) ? Py_True : Py_False; - Py_INCREF(res); - return res; - } - } - else { - // This always returns False - Py_INCREF(Py_NotImplemented); - return Py_NotImplemented; + res = (r1 != r2) ? Py_True : Py_False; + Py_INCREF(res); + return res; } + // This always returns False + Py_INCREF(Py_NotImplemented); + return Py_NotImplemented; } PyObject* RotationPy::invert(PyObject* args) diff --git a/src/Base/StackWalker.cpp b/src/Base/StackWalker.cpp index c3c4a7159c..fcc7fa4dc7 100644 --- a/src/Base/StackWalker.cpp +++ b/src/Base/StackWalker.cpp @@ -1118,10 +1118,7 @@ BOOL __stdcall StackWalker::myReadProcMem( //printf("ReadMemory: hProcess: %p, baseAddr: %p, buffer: %p, size: %d, read: %d, result: %d\n", hProcess, (LPVOID) qwBaseAddress, lpBuffer, nSize, (DWORD) st, (DWORD) bRet); return bRet; } - else - { - return s_readMemoryFunction(hProcess, qwBaseAddress, lpBuffer, nSize, lpNumberOfBytesRead, s_readMemoryFunction_UserData); - } + return s_readMemoryFunction(hProcess, qwBaseAddress, lpBuffer, nSize, lpNumberOfBytesRead, s_readMemoryFunction_UserData); } void StackWalker::OnLoadModule(LPCSTR img, LPCSTR mod, DWORD64 baseAddr, DWORD size, DWORD result, LPCSTR symType, LPCSTR pdbName, ULONGLONG fileVersion) diff --git a/src/Base/UnitPyImp.cpp b/src/Base/UnitPyImp.cpp index e8db65865a..364ffb6641 100644 --- a/src/Base/UnitPyImp.cpp +++ b/src/Base/UnitPyImp.cpp @@ -175,10 +175,8 @@ PyObject* UnitPy::number_multiply_handler(PyObject* self, PyObject* other) return new UnitPy(new Unit((*a) * (*b))); } - else { - PyErr_SetString(PyExc_TypeError, "A Unit can only be multiplied by a Unit"); - return nullptr; - } + PyErr_SetString(PyExc_TypeError, "A Unit can only be multiplied by a Unit"); + return nullptr; } PyObject* UnitPy::richCompare(PyObject* v, PyObject* w, int op) @@ -192,22 +190,18 @@ PyObject* UnitPy::richCompare(PyObject* v, PyObject* w, int op) PyErr_SetString(PyExc_TypeError, "no ordering relation is defined for Units"); return nullptr; } - else if (op == Py_EQ) { + if (op == Py_EQ) { res = (*u1 == *u2) ? Py_True : Py_False; // NOLINT Py_INCREF(res); return res; } - else { - res = (*u1 != *u2) ? Py_True : Py_False; // NOLINT - Py_INCREF(res); - return res; - } - } - else { - // This always returns False - Py_INCREF(Py_NotImplemented); - return Py_NotImplemented; + res = (*u1 != *u2) ? Py_True : Py_False; // NOLINT + Py_INCREF(res); + return res; } + // This always returns False + Py_INCREF(Py_NotImplemented); + return Py_NotImplemented; } Py::String UnitPy::getType() const diff --git a/src/Base/UnitsApi.cpp b/src/Base/UnitsApi.cpp index f0c118bd20..9984a4dee8 100644 --- a/src/Base/UnitsApi.cpp +++ b/src/Base/UnitsApi.cpp @@ -193,6 +193,7 @@ double UnitsApi::toDouble(PyObject* args, const Base::Unit& u) } throw Base::UnitsMismatchError("Wrong unit type!"); } + if (PyFloat_Check(args)) { return PyFloat_AsDouble(args); } diff --git a/src/Base/VectorPyImp.cpp b/src/Base/VectorPyImp.cpp index 8e0ea738e9..bf2b66181b 100644 --- a/src/Base/VectorPyImp.cpp +++ b/src/Base/VectorPyImp.cpp @@ -150,30 +150,24 @@ PyObject* VectorPy::number_multiply_handler(PyObject* self, PyObject* other) Py::Float mult(a * b); return Py::new_reference_to(mult); } - else if (PyNumber_Check(other)) { + if (PyNumber_Check(other)) { double b = PyFloat_AsDouble(other); return new VectorPy(a * b); } - else { - PyErr_SetString(PyExc_TypeError, "A Vector can only be multiplied by Vector or number"); - return nullptr; - } + PyErr_SetString(PyExc_TypeError, "A Vector can only be multiplied by Vector or number"); + return nullptr; } - else if (PyObject_TypeCheck(other, &(VectorPy::Type))) { + if (PyObject_TypeCheck(other, &(VectorPy::Type))) { Base::Vector3d a = static_cast(other)->value(); if (PyNumber_Check(self)) { double b = PyFloat_AsDouble(self); return new VectorPy(a * b); } - else { - PyErr_SetString(PyExc_TypeError, "A Vector can only be multiplied by Vector or number"); - return nullptr; - } - } - else { - PyErr_SetString(PyExc_TypeError, "First or second arg must be Vector"); + PyErr_SetString(PyExc_TypeError, "A Vector can only be multiplied by Vector or number"); return nullptr; } + PyErr_SetString(PyExc_TypeError, "First or second arg must be Vector"); + return nullptr; } Py_ssize_t VectorPy::sequence_length(PyObject* /*unused*/) @@ -243,13 +237,8 @@ PyObject* VectorPy::mapping_subscript(PyObject* self, PyObject* item) } return sequence_item(self, i); } - else if (PySlice_Check(item)) { - Py_ssize_t start = 0; - Py_ssize_t stop = 0; - Py_ssize_t step = 0; - Py_ssize_t slicelength = 0; - Py_ssize_t cur = 0; - Py_ssize_t i = 0; + if (PySlice_Check(item)) { + Py_ssize_t start = 0, stop = 0, step = 0, slicelength = 0, cur = 0, i = 0; PyObject* slice = item; if (PySlice_GetIndicesEx(slice, sequence_length(self), &start, &stop, &step, &slicelength) @@ -260,7 +249,7 @@ PyObject* VectorPy::mapping_subscript(PyObject* self, PyObject* item) if (slicelength <= 0) { return PyTuple_New(0); } - else if (start == 0 && step == 1 && slicelength == sequence_length(self) + if (start == 0 && step == 1 && slicelength == sequence_length(self) && PyObject_TypeCheck(self, &(VectorPy::Type))) { Base::Vector3d v = static_cast(self)->value(); Py::Tuple xyz(3); @@ -269,7 +258,7 @@ PyObject* VectorPy::mapping_subscript(PyObject* self, PyObject* item) xyz.setItem(2, Py::Float(v.z)); return Py::new_reference_to(xyz); } - else if (PyObject_TypeCheck(self, &(VectorPy::Type))) { + if (PyObject_TypeCheck(self, &(VectorPy::Type))) { Base::Vector3d v = static_cast(self)->value(); Py::Tuple xyz(static_cast(slicelength)); @@ -342,22 +331,18 @@ PyObject* VectorPy::richCompare(PyObject* v, PyObject* w, int op) PyErr_SetString(PyExc_TypeError, "no ordering relation is defined for Vector"); return nullptr; } - else if (op == Py_EQ) { + if (op == Py_EQ) { res = (v1 == v2) ? Py_True : Py_False; // NOLINT Py_INCREF(res); return res; } - else { - res = (v1 != v2) ? Py_True : Py_False; // NOLINT - Py_INCREF(res); - return res; - } - } - else { - // This always returns False - Py_INCREF(Py_NotImplemented); - return Py_NotImplemented; + res = (v1 != v2) ? Py_True : Py_False; // NOLINT + Py_INCREF(res); + return res; } + // This always returns False + Py_INCREF(Py_NotImplemented); + return Py_NotImplemented; } PyObject* VectorPy::isEqual(PyObject* args)