diff --git a/etc/codeTemplates/source/_Template.C b/etc/codeTemplates/source/_Template.C index c8f231a79e..8708db0503 100644 --- a/etc/codeTemplates/source/_Template.C +++ b/etc/codeTemplates/source/_Template.C @@ -89,19 +89,16 @@ Foam::CLASSNAME::~CLASSNAME() void Foam::CLASSNAME::operator=(const CLASSNAME& rhs) { - // Check for assignment to self if (this == &rhs) { - FatalErrorInFunction - << "Attempted assignment to self" - << abort(FatalError); + return; // Self-assignment is a no-op } } + // * * * * * * * * * * * * * * Friend Functions * * * * * * * * * * * * * * // - -// * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * * // +// * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * // // ************************************************************************* // diff --git a/etc/codeTemplates/template/_TemplateTemplate.C b/etc/codeTemplates/template/_TemplateTemplate.C index 9c5c3caa97..8adec14c39 100644 --- a/etc/codeTemplates/template/_TemplateTemplate.C +++ b/etc/codeTemplates/template/_TemplateTemplate.C @@ -102,12 +102,9 @@ void Foam::CLASSNAME::operator= const CLASSNAME& rhs ) { - // Check for assignment to self if (this == &rhs) { - FatalErrorInFunction - << "Attempted assignment to self" - << abort(FatalError); + return; // Self-assignment is a no-op } } diff --git a/src/OSspecific/POSIX/regExp/regExpPosixI.H b/src/OSspecific/POSIX/regExp/regExpPosixI.H index 60cf6d9e5c..3d8df849af 100644 --- a/src/OSspecific/POSIX/regExp/regExpPosixI.H +++ b/src/OSspecific/POSIX/regExp/regExpPosixI.H @@ -126,7 +126,11 @@ inline bool Foam::regExpPosix::search(const std::string& text) const inline void Foam::regExpPosix::swap(regExpPosix& rgx) { - std::swap(preg_, rgx.preg_); + if (this != &rgx) + { + // Self-swap is a no-op + std::swap(preg_, rgx.preg_); + } } diff --git a/src/OpenFOAM/containers/Bits/PackedList/PackedListI.H b/src/OpenFOAM/containers/Bits/PackedList/PackedListI.H index 4919d62568..4fe51d14e8 100644 --- a/src/OpenFOAM/containers/Bits/PackedList/PackedListI.H +++ b/src/OpenFOAM/containers/Bits/PackedList/PackedListI.H @@ -320,6 +320,7 @@ inline void Foam::PackedList::reference::operator= const reference& other ) { + // Accepts self-assignment this->set(other.get()); } @@ -556,6 +557,11 @@ inline std::streamsize Foam::PackedList::byteSize() const template inline void Foam::PackedList::swap(PackedList& rhs) { + if (this == &rhs) + { + return; // Self-swap is a no-op + } + blocks_.swap(rhs.blocks_); Foam::Swap(size_, rhs.size_); } @@ -564,8 +570,12 @@ inline void Foam::PackedList::swap(PackedList& rhs) template inline void Foam::PackedList::transfer(PackedList& rhs) { - blocks_.transfer(rhs.blocks_); + if (this == &rhs) + { + return; // Self-assignment is a no-op + } + blocks_.transfer(rhs.blocks_); size_ = rhs.size_; rhs.size_ = 0; } diff --git a/src/OpenFOAM/containers/Bits/bitSet/bitSetI.H b/src/OpenFOAM/containers/Bits/bitSet/bitSetI.H index 161f68c60b..c3332a4a2f 100644 --- a/src/OpenFOAM/containers/Bits/bitSet/bitSetI.H +++ b/src/OpenFOAM/containers/Bits/bitSet/bitSetI.H @@ -197,6 +197,7 @@ inline void Foam::bitSet::reference::operator= const reference& other ) { + // Accepts self-assignment set(other.get()); } diff --git a/src/OpenFOAM/containers/Circulators/Circulator/CirculatorI.H b/src/OpenFOAM/containers/Circulators/Circulator/CirculatorI.H index 9da7624c22..88aa28fe42 100644 --- a/src/OpenFOAM/containers/Circulators/Circulator/CirculatorI.H +++ b/src/OpenFOAM/containers/Circulators/Circulator/CirculatorI.H @@ -6,6 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2012-2015 OpenFOAM Foundation + Copyright (C) 2019 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -170,12 +171,9 @@ void Foam::Circulator::operator= const Circulator& rhs ) { - // Check for assignment to self if (this == &rhs) { - FatalErrorInFunction - << "Attempted assignment to self" - << abort(FatalError); + return; // Self-assignment is a no-op } begin_ = rhs.begin_; diff --git a/src/OpenFOAM/containers/Circulators/ConstCirculator/ConstCirculatorI.H b/src/OpenFOAM/containers/Circulators/ConstCirculator/ConstCirculatorI.H index 7c8ed24f3f..fded93cc55 100644 --- a/src/OpenFOAM/containers/Circulators/ConstCirculator/ConstCirculatorI.H +++ b/src/OpenFOAM/containers/Circulators/ConstCirculator/ConstCirculatorI.H @@ -6,6 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2012-2015 OpenFOAM Foundation + Copyright (C) 2019 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -175,12 +176,9 @@ void Foam::ConstCirculator::operator= const ConstCirculator& rhs ) { - // Check for assignment to self if (this == &rhs) { - FatalErrorInFunction - << "Attempted assignment to self" - << abort(FatalError); + return; // Self-assignment is a no-op } begin_ = rhs.begin_; diff --git a/src/OpenFOAM/containers/Dictionaries/DictionaryBase/DictionaryBase.C b/src/OpenFOAM/containers/Dictionaries/DictionaryBase/DictionaryBase.C index a2f73e1828..e2df5eb424 100644 --- a/src/OpenFOAM/containers/Dictionaries/DictionaryBase/DictionaryBase.C +++ b/src/OpenFOAM/containers/Dictionaries/DictionaryBase/DictionaryBase.C @@ -232,6 +232,11 @@ void Foam::DictionaryBase::transfer DictionaryBase& dict ) { + if (this == &dict) + { + return; // Self-assignment is a no-op + } + IDLListType::transfer(dict); hashedTs_.transfer(dict.hashedTs_); } @@ -247,9 +252,7 @@ void Foam::DictionaryBase::operator= { if (this == &dict) { - FatalErrorInFunction - << "attempted assignment to self" - << abort(FatalError); + return; // Self-assignment is a no-op } IDLListType::operator=(dict); diff --git a/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTable.C b/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTable.C index 8f93c5f3d7..c8a8fffed5 100644 --- a/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTable.C +++ b/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTable.C @@ -149,9 +149,7 @@ void Foam::HashPtrTable::operator= { if (this == &rhs) { - FatalErrorInFunction - << "attempted copy assignment to self" - << abort(FatalError); + return; // Self-assignment is a no-op } this->clear(); @@ -179,9 +177,7 @@ void Foam::HashPtrTable::operator= { if (this == &rhs) { - FatalErrorInFunction - << "attempted move assignment to self" - << abort(FatalError); + return; // Self-assignment is a no-op } this->clear(); diff --git a/src/OpenFOAM/containers/HashTables/HashTable/HashTable.C b/src/OpenFOAM/containers/HashTables/HashTable/HashTable.C index d2b0c45f6f..17d0121bfc 100644 --- a/src/OpenFOAM/containers/HashTables/HashTable/HashTable.C +++ b/src/OpenFOAM/containers/HashTables/HashTable/HashTable.C @@ -656,6 +656,11 @@ void Foam::HashTable::clearStorage() template void Foam::HashTable::swap(HashTable& rhs) { + if (this == &rhs) + { + return; // Self-swap is a no-op + } + Foam::Swap(size_, rhs.size_); Foam::Swap(capacity_, rhs.capacity_); Foam::Swap(table_, rhs.table_); @@ -665,6 +670,11 @@ void Foam::HashTable::swap(HashTable& rhs) template void Foam::HashTable::transfer(HashTable& rhs) { + if (this == &rhs) + { + return; // Self-assignment is a no-op + } + clear(); swap(rhs); } @@ -759,12 +769,9 @@ void Foam::HashTable::operator= const HashTable& rhs ) { - // Check for assignment to self if (this == &rhs) { - FatalErrorInFunction - << "attempted assignment to self" - << abort(FatalError); + return; // Self-assignment is a no-op } if (!capacity_) @@ -813,12 +820,9 @@ void Foam::HashTable::operator= HashTable&& rhs ) { - // Check for assignment to self if (this == &rhs) { - FatalErrorInFunction - << "attempted assignment to self" - << abort(FatalError); + return; // Self-assignment is a no-op } transfer(rhs); diff --git a/src/OpenFOAM/containers/LinkedLists/linkTypes/DLListBase/DLListBaseI.H b/src/OpenFOAM/containers/LinkedLists/linkTypes/DLListBase/DLListBaseI.H index 13d1d7b4d7..02cafc82b0 100644 --- a/src/OpenFOAM/containers/LinkedLists/linkTypes/DLListBase/DLListBaseI.H +++ b/src/OpenFOAM/containers/LinkedLists/linkTypes/DLListBase/DLListBaseI.H @@ -185,6 +185,11 @@ inline void Foam::DLListBase::clear() inline void Foam::DLListBase::swap(DLListBase& lst) { + if (this == &lst) + { + return; // Self-swap is a no-op + } + std::swap(first_, lst.first_); std::swap(last_, lst.last_); std::swap(size_, lst.size_); @@ -193,6 +198,11 @@ inline void Foam::DLListBase::swap(DLListBase& lst) inline void Foam::DLListBase::transfer(DLListBase& lst) { + if (this == &lst) + { + return; // Self-assignment is a no-op + } + first_ = lst.first_; last_ = lst.last_; size_ = lst.size_; diff --git a/src/OpenFOAM/containers/LinkedLists/linkTypes/SLListBase/SLListBaseI.H b/src/OpenFOAM/containers/LinkedLists/linkTypes/SLListBase/SLListBaseI.H index 42c08708c6..d9a4456521 100644 --- a/src/OpenFOAM/containers/LinkedLists/linkTypes/SLListBase/SLListBaseI.H +++ b/src/OpenFOAM/containers/LinkedLists/linkTypes/SLListBase/SLListBaseI.H @@ -143,6 +143,11 @@ inline void Foam::SLListBase::clear() inline void Foam::SLListBase::swap(SLListBase& lst) { + if (this == &lst) + { + return; // Self-swap is a no-op + } + std::swap(last_, lst.last_); std::swap(size_, lst.size_); } @@ -150,6 +155,11 @@ inline void Foam::SLListBase::swap(SLListBase& lst) inline void Foam::SLListBase::transfer(SLListBase& lst) { + if (this == &lst) + { + return; // Self-assignment is a no-op + } + last_ = lst.last_; size_ = lst.size_; diff --git a/src/OpenFOAM/containers/Lists/CompactListList/CompactListList.C b/src/OpenFOAM/containers/Lists/CompactListList/CompactListList.C index d837f41b9f..f2fbd29724 100644 --- a/src/OpenFOAM/containers/Lists/CompactListList/CompactListList.C +++ b/src/OpenFOAM/containers/Lists/CompactListList/CompactListList.C @@ -6,6 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation + Copyright (C) 2019 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -203,6 +204,11 @@ void Foam::CompactListList::swap CompactListList& lst ) { + if (this == &lst) + { + return; // Self-swap is a no-op + } + Foam::Swap(size_, lst.size_); offsets_.swap(lst.offsets_); m_.swap(lst.m_); @@ -215,6 +221,11 @@ void Foam::CompactListList::transfer CompactListList& lst ) { + if (this == &lst) + { + return; // Self-assignment is a no-op + } + size_ = lst.size_; offsets_.transfer(lst.offsets_); m_.transfer(lst.m_); diff --git a/src/OpenFOAM/containers/Lists/CompactListList/CompactListListI.H b/src/OpenFOAM/containers/Lists/CompactListList/CompactListListI.H index ff79c865be..1a3c2613ab 100644 --- a/src/OpenFOAM/containers/Lists/CompactListList/CompactListListI.H +++ b/src/OpenFOAM/containers/Lists/CompactListList/CompactListListI.H @@ -303,6 +303,11 @@ inline void Foam::CompactListList::operator= const CompactListList& lst ) { + if (this == &lst) + { + return; // Self-assignment is a no-op + } + size_ = lst.size_; offsets_ = lst.offsets_, m_ = lst.m_; @@ -315,6 +320,11 @@ inline void Foam::CompactListList::operator= CompactListList&& lst ) { + if (this == &lst) + { + return; // Self-assignment is a no-op + } + transfer(lst); } diff --git a/src/OpenFOAM/containers/Lists/Distribution/Distribution.C b/src/OpenFOAM/containers/Lists/Distribution/Distribution.C index 19b29270f9..1e5d2e541b 100644 --- a/src/OpenFOAM/containers/Lists/Distribution/Distribution.C +++ b/src/OpenFOAM/containers/Lists/Distribution/Distribution.C @@ -6,6 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation + Copyright (C) 2019 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -557,12 +558,9 @@ void Foam::Distribution::operator= const Distribution& rhs ) { - // Check for assignment to self if (this == &rhs) { - FatalErrorInFunction - << "Attempted assignment to self" - << abort(FatalError); + return; // Self-assignment is a no-op } List>::operator=(rhs); diff --git a/src/OpenFOAM/containers/Lists/DynamicList/DynamicListI.H b/src/OpenFOAM/containers/Lists/DynamicList/DynamicListI.H index d74bc281db..6b71e10211 100644 --- a/src/OpenFOAM/containers/Lists/DynamicList/DynamicListI.H +++ b/src/OpenFOAM/containers/Lists/DynamicList/DynamicListI.H @@ -397,6 +397,11 @@ inline void Foam::DynamicList::swap DynamicList& lst ) { + if (this == &lst) + { + return; // Self-swap is a no-op + } + DynamicList& cur = *this; // Make addressable size identical to the allocated capacity @@ -434,6 +439,11 @@ Foam::DynamicList::transfer DynamicList& lst ) { + if (this == &lst) + { + return; // Self-assignment is a no-op + } + // Take over storage as-is (without shrink, without using SizeMin) // clear addressing and storage for old lst. capacity_ = lst.capacity(); @@ -793,8 +803,7 @@ inline void Foam::DynamicList::operator= { if (this == &lst) { - FatalErrorInFunction - << "Attempted assignment to self" << abort(FatalError); + return; // Self-assignment is a no-op } assignDynList(lst); @@ -808,6 +817,11 @@ inline void Foam::DynamicList::operator= const DynamicList& lst ) { + if (this == &lst) + { + return; // Self-assignment is a no-op + } + assignDynList(lst); } @@ -852,8 +866,7 @@ inline void Foam::DynamicList::operator= { if (this == &lst) { - FatalErrorInFunction - << "Attempted assignment to self" << abort(FatalError); + return; // Self-assignment is a no-op } clear(); @@ -868,6 +881,11 @@ inline void Foam::DynamicList::operator= DynamicList&& lst ) { + if (this == &lst) + { + return; // Self-assignment is a no-op + } + clear(); transfer(lst); } @@ -887,7 +905,11 @@ inline void Foam::DynamicList::operator= // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * // template -inline void Foam::Swap(DynamicList& a, DynamicList& b) +inline void Foam::Swap +( + DynamicList& a, + DynamicList& b +) { a.swap(b); } diff --git a/src/OpenFOAM/containers/Lists/FixedList/FixedListI.H b/src/OpenFOAM/containers/Lists/FixedList/FixedListI.H index d6e5f62bc6..47057ec995 100644 --- a/src/OpenFOAM/containers/Lists/FixedList/FixedListI.H +++ b/src/OpenFOAM/containers/Lists/FixedList/FixedListI.H @@ -331,6 +331,11 @@ inline void Foam::FixedList::setSize(const label n) template inline void Foam::FixedList::swap(FixedList& list) { + if (this == &list) + { + return; // Self-swap is a no-op + } + for (unsigned i=0; i::swap(FixedList& list) template inline void Foam::FixedList::transfer(FixedList& list) { + if (this == &list) + { + return; // Self-assignment is a no-op + } + for (unsigned i=0; i::operator=(const T& val) template inline void Foam::FixedList::operator=(const FixedList& list) { + if (this == &list) + { + return; // Self-assignment is a no-op + } + for (unsigned i=0; i::operator=(const FixedList& list) template inline void Foam::FixedList::operator=(FixedList&& list) { + if (this == &list) + { + return; // Self-assignment is a no-op + } + // No significant speedup observed for copy assignment on simple types, // use move assignment for generality with more complex types for (unsigned i=0; i::resize(const label newSize, const T& val) template void Foam::List::transfer(List& list) { + if (this == &list) + { + return; // Self-assignment is a no-op + } + // Clear and swap - could also check for self assignment clear(); this->size_ = list.size_; @@ -472,6 +477,11 @@ void Foam::List::transfer(SortableList& list) template void Foam::List::operator=(const UList& a) { + if (this == &a) + { + return; // Self-assignment is a no-op + } + reAlloc(a.size_); const label len = this->size_; @@ -505,9 +515,7 @@ void Foam::List::operator=(const List& list) { if (this == &list) { - FatalErrorInFunction - << "attempted assignment to self" - << abort(FatalError); + return; // Self-assignment is a no-op } operator=(static_cast&>(list)); @@ -581,9 +589,7 @@ void Foam::List::operator=(List&& list) { if (this == &list) { - FatalErrorInFunction - << "attempted assignment to self" - << abort(FatalError); + return; // Self-assignment is a no-op } transfer(list); diff --git a/src/OpenFOAM/containers/Lists/List/ListI.H b/src/OpenFOAM/containers/Lists/List/ListI.H index b6e2d2f990..c32eef5394 100644 --- a/src/OpenFOAM/containers/Lists/List/ListI.H +++ b/src/OpenFOAM/containers/Lists/List/ListI.H @@ -193,7 +193,7 @@ inline void Foam::List::append(const UList& list) if (this == &list) { FatalErrorInFunction - << "attempted appending to self" << abort(FatalError); + << "Attempted appending to self" << abort(FatalError); } label idx = this->size(); diff --git a/src/OpenFOAM/containers/Lists/SortableList/SortableList.C b/src/OpenFOAM/containers/Lists/SortableList/SortableList.C index 817e9c00d0..4d3d755a2f 100644 --- a/src/OpenFOAM/containers/Lists/SortableList/SortableList.C +++ b/src/OpenFOAM/containers/Lists/SortableList/SortableList.C @@ -197,6 +197,11 @@ void Foam::SortableList::partialReverseSort(label n, label start) template void Foam::SortableList::swap(SortableList& lst) { + if (this == &lst) + { + return; // Self-swap is a no-op + } + List::swap(lst); indices_.swap(lst.indices_); } @@ -223,6 +228,11 @@ inline void Foam::SortableList::operator=(const UList& lst) template inline void Foam::SortableList::operator=(const SortableList& lst) { + if (this == &lst) + { + return; // Self-assigment is a no-op + } + List::operator=(lst); indices_ = lst.indices(); } @@ -239,6 +249,11 @@ inline void Foam::SortableList::operator=(List&& lst) template inline void Foam::SortableList::operator=(SortableList&& lst) { + if (this == &lst) + { + return; // Self-assigment is a no-op + } + clear(); this->swap(lst); } diff --git a/src/OpenFOAM/containers/Lists/UList/UListI.H b/src/OpenFOAM/containers/Lists/UList/UListI.H index 8066626e65..4a8452487f 100644 --- a/src/OpenFOAM/containers/Lists/UList/UListI.H +++ b/src/OpenFOAM/containers/Lists/UList/UListI.H @@ -380,6 +380,11 @@ inline bool Foam::UList::empty() const noexcept template inline void Foam::UList::swap(UList& list) { + if (&list == this) + { + return; // Self-swap is a no-op + } + Foam::Swap(size_, list.size_); Foam::Swap(v_, list.v_); } diff --git a/src/OpenFOAM/containers/PtrLists/PtrDynList/PtrDynListI.H b/src/OpenFOAM/containers/PtrLists/PtrDynList/PtrDynListI.H index b3045ad253..d6845719d8 100644 --- a/src/OpenFOAM/containers/PtrLists/PtrDynList/PtrDynListI.H +++ b/src/OpenFOAM/containers/PtrLists/PtrDynList/PtrDynListI.H @@ -369,6 +369,11 @@ inline void Foam::PtrDynList::operator= const PtrList& list ) { + if (this == &list) + { + return; // Self-assignment is a no-op + } + PtrList::operator=(list); capacity_ = PtrList::size(); } @@ -380,6 +385,11 @@ inline void Foam::PtrDynList::operator= const PtrDynList& list ) { + if (this == &list) + { + return; // Self-assignment is a no-op + } + PtrList::operator=(list); capacity_ = PtrList::size(); } @@ -392,6 +402,11 @@ inline void Foam::PtrDynList::operator= const PtrDynList& list ) { + if (this == &list) + { + return; // Self-assignment is a no-op + } + PtrList::operator=(list); capacity_ = PtrList::size(); } @@ -403,6 +418,11 @@ inline void Foam::PtrDynList::operator= PtrList&& list ) { + if (this == &list) + { + return; // Self-assignment is a no-op + } + PtrList::transfer(list); capacity_ = PtrList::size(); list.clearStorage(); @@ -415,6 +435,11 @@ inline void Foam::PtrDynList::operator= PtrDynList&& list ) { + if (this == &list) + { + return; // Self-assignment is a no-op + } + PtrList::transfer(list); capacity_ = list.capacity(); list.clearStorage(); @@ -428,6 +453,11 @@ inline void Foam::PtrDynList::operator= PtrDynList&& list ) { + if (this == &list) + { + return; // Self-assignment is a no-op + } + PtrList::transfer(list); capacity_ = list.capacity(); list.clearStorage(); diff --git a/src/OpenFOAM/containers/PtrLists/PtrList/PtrList.C b/src/OpenFOAM/containers/PtrLists/PtrList/PtrList.C index b75abd5366..7df86c8da3 100644 --- a/src/OpenFOAM/containers/PtrLists/PtrList/PtrList.C +++ b/src/OpenFOAM/containers/PtrLists/PtrList/PtrList.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation - Copyright (C) 2018 OpenCFD Ltd. + Copyright (C) 2018-2019 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -134,9 +134,7 @@ void Foam::PtrList::operator=(const PtrList& list) { if (this == &list) { - FatalErrorInFunction - << "attempted assignment to self for type " << typeid(T).name() - << abort(FatalError); + return; // Self-assignment is a no-op } const label oldLen = this->size(); diff --git a/src/OpenFOAM/containers/PtrLists/UPtrList/UPtrListI.H b/src/OpenFOAM/containers/PtrLists/UPtrList/UPtrListI.H index 2d56f368bb..6c29a834a7 100644 --- a/src/OpenFOAM/containers/PtrLists/UPtrList/UPtrListI.H +++ b/src/OpenFOAM/containers/PtrLists/UPtrList/UPtrListI.H @@ -203,7 +203,7 @@ inline const T& Foam::UPtrList::operator[](const label i) const { FatalErrorInFunction << "Cannot dereference nullptr at index " << i - << " in range [0," << size() << ")" + << " in range [0," << size() << ")\n" << abort(FatalError); } @@ -220,7 +220,7 @@ inline T& Foam::UPtrList::operator[](const label i) { FatalErrorInFunction << "Cannot dereference nullptr at index " << i - << " in range [0," << size() << ")" + << " in range [0," << size() << ")\n" << abort(FatalError); } diff --git a/src/OpenFOAM/db/IOobjects/CompactIOField/CompactIOField.C b/src/OpenFOAM/db/IOobjects/CompactIOField/CompactIOField.C index 2763bcdfb1..c7ab9b7ac9 100644 --- a/src/OpenFOAM/db/IOobjects/CompactIOField/CompactIOField.C +++ b/src/OpenFOAM/db/IOobjects/CompactIOField/CompactIOField.C @@ -215,6 +215,11 @@ void Foam::CompactIOField::operator= const CompactIOField& rhs ) { + if (this == &rhs) + { + return; // Self-assigment is a no-op + } + Field::operator=(rhs); } diff --git a/src/OpenFOAM/db/IOobjects/CompactIOList/CompactIOList.C b/src/OpenFOAM/db/IOobjects/CompactIOList/CompactIOList.C index c13cf8ccd7..cfd3507fca 100644 --- a/src/OpenFOAM/db/IOobjects/CompactIOList/CompactIOList.C +++ b/src/OpenFOAM/db/IOobjects/CompactIOList/CompactIOList.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2017 OpenFOAM Foundation - Copyright (C) 2015-2018 OpenCFD Ltd. + Copyright (C) 2015-2019 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. diff --git a/src/OpenFOAM/db/IOstreams/token/tokenI.H b/src/OpenFOAM/db/IOstreams/token/tokenI.H index 33885121a6..6b63aed010 100644 --- a/src/OpenFOAM/db/IOstreams/token/tokenI.H +++ b/src/OpenFOAM/db/IOstreams/token/tokenI.H @@ -281,6 +281,11 @@ inline void Foam::token::reset() inline void Foam::token::swap(token& tok) { + if (this == &tok) + { + return; // Self-swap is a no-op + } + std::swap(data_, tok.data_); std::swap(type_, tok.type_); std::swap(lineNumber_, tok.lineNumber_); @@ -642,6 +647,11 @@ inline void Foam::token::setBad() inline void Foam::token::operator=(const token& tok) { + if (this == &tok) + { + return; // Self-assignment is a no-op + } + reset(); type_ = tok.type_; @@ -683,6 +693,11 @@ inline void Foam::token::operator=(const token& tok) inline void Foam::token::operator=(token&& tok) { + if (this == &tok) + { + return; // Self-assignment is a no-op + } + reset(); lineNumber_ = 0; swap(tok); diff --git a/src/OpenFOAM/db/dictionary/dictionary.C b/src/OpenFOAM/db/dictionary/dictionary.C index 10382ee816..4b07aca7fc 100644 --- a/src/OpenFOAM/db/dictionary/dictionary.C +++ b/src/OpenFOAM/db/dictionary/dictionary.C @@ -876,7 +876,7 @@ bool Foam::dictionary::merge(const dictionary& dict) if (this == &dict) { FatalIOErrorInFunction(*this) - << "Attempted merge to self for dictionary " + << "Attempted merge to self, for dictionary " << name() << nl << abort(FatalIOError); } @@ -944,10 +944,7 @@ void Foam::dictionary::operator=(const dictionary& rhs) { if (this == &rhs) { - FatalIOErrorInFunction(*this) - << "Attempted assignment to self for dictionary " - << name() << nl - << abort(FatalIOError); + return; // Self-assignment is a no-op } name() = rhs.name(); @@ -968,7 +965,7 @@ void Foam::dictionary::operator+=(const dictionary& rhs) if (this == &rhs) { FatalIOErrorInFunction(*this) - << "Attempted addition assignment to self for dictionary " + << "Attempted addition to self, for dictionary " << name() << nl << abort(FatalIOError); } @@ -985,7 +982,7 @@ void Foam::dictionary::operator|=(const dictionary& rhs) if (this == &rhs) { FatalIOErrorInFunction(*this) - << "Attempted assignment to self for dictionary " + << "Attempted |= merging to self, for dictionary " << name() << nl << abort(FatalIOError); } @@ -1005,7 +1002,7 @@ void Foam::dictionary::operator<<=(const dictionary& rhs) if (this == &rhs) { FatalIOErrorInFunction(*this) - << "Attempted assignment to self for dictionary " + << "Attempted addition to self, for dictionary " << name() << nl << abort(FatalIOError); } diff --git a/src/OpenFOAM/db/dictionary/entry/entry.C b/src/OpenFOAM/db/dictionary/entry/entry.C index cb065eec76..3e5fe33877 100644 --- a/src/OpenFOAM/db/dictionary/entry/entry.C +++ b/src/OpenFOAM/db/dictionary/entry/entry.C @@ -179,12 +179,9 @@ void Foam::entry::checkITstream(const ITstream& is) const void Foam::entry::operator=(const entry& e) { - // check for assignment to self if (this == &e) { - FatalErrorInFunction - << "attempted assignment to self" - << abort(FatalError); + return; // Self-assignment is a no-op } keyword_ = e.keyword_; @@ -193,20 +190,24 @@ void Foam::entry::operator=(const entry& e) bool Foam::entry::operator==(const entry& e) const { + if (this == &e) + { + return true; + } if (keyword_ != e.keyword_) { return false; } - else - { - OStringStream oss1; - oss1 << *this; - OStringStream oss2; - oss2 << e; + // Compare contents (as strings) - return oss1.str() == oss2.str(); - } + OStringStream oss1; + oss1 << *this; + + OStringStream oss2; + oss2 << e; + + return oss1.str() == oss2.str(); } diff --git a/src/OpenFOAM/db/objectRegistry/objectRegistry.C b/src/OpenFOAM/db/objectRegistry/objectRegistry.C index ed32f81a8b..f6832dc47b 100644 --- a/src/OpenFOAM/db/objectRegistry/objectRegistry.C +++ b/src/OpenFOAM/db/objectRegistry/objectRegistry.C @@ -259,7 +259,7 @@ bool Foam::objectRegistry::checkIn(regIOobject* io) const if (!ok && objectRegistry::debug) { WarningInFunction - << name() << " : attempted to checkIn object with name " + << name() << " : Attempt to checkIn object with name " << io->name() << " which was already checked in" << endl; } @@ -291,7 +291,7 @@ bool Foam::objectRegistry::checkOut(regIOobject* io) const if (objectRegistry::debug) { WarningInFunction - << name() << " : attempt to checkOut copy of " + << name() << " : Attempt to checkOut copy of " << iter.key() << endl; } diff --git a/src/OpenFOAM/fields/DimensionedFields/DimensionedField/DimensionedField.C b/src/OpenFOAM/fields/DimensionedFields/DimensionedField/DimensionedField.C index 19f3542e67..17b50d374b 100644 --- a/src/OpenFOAM/fields/DimensionedFields/DimensionedField/DimensionedField.C +++ b/src/OpenFOAM/fields/DimensionedFields/DimensionedField/DimensionedField.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation - Copyright (C) 2015-2017 OpenCFD Ltd. + Copyright (C) 2015-2019 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -492,12 +492,9 @@ void Foam::DimensionedField::operator= const DimensionedField& df ) { - // Check for assignment to self if (this == &df) { - FatalErrorInFunction - << "attempted assignment to self" - << abort(FatalError); + return; // Self-assignment is a no-op } checkField(*this, df, "="); @@ -516,12 +513,9 @@ void Foam::DimensionedField::operator= { auto& df = tdf.constCast(); - // Check for assignment to self if (this == &df) { - FatalErrorInFunction - << "attempted assignment to self" - << abort(FatalError); + return; // Self-assignment is a no-op } checkField(*this, df, "="); diff --git a/src/OpenFOAM/fields/FieldFields/FieldField/FieldField.C b/src/OpenFOAM/fields/FieldFields/FieldField/FieldField.C index 17e38a80c9..29f4fe8c21 100644 --- a/src/OpenFOAM/fields/FieldFields/FieldField/FieldField.C +++ b/src/OpenFOAM/fields/FieldFields/FieldField/FieldField.C @@ -292,10 +292,9 @@ void FieldField::operator=(const FieldField& ff) { if (this == &ff) { - FatalErrorInFunction - << "attempted assignment to self" - << abort(FatalError); + return; // Self-assignment is a no-op } + // No size checking done forAll(*this, i) @@ -310,9 +309,7 @@ void FieldField::operator=(FieldField&& ff) { if (this == &ff) { - FatalErrorInFunction - << "attempted assignment to self" - << abort(FatalError); + return; // Self-assignment is a no-op } PtrList>::transfer(ff); @@ -325,9 +322,7 @@ void FieldField::operator=(const tmp& tf) // The cref() method also checks that tmp is not nullptr. if (this == &(tf.cref())) { - FatalErrorInFunction - << "attempted assignment to self" - << abort(FatalError); + return; // Self-assignment is a no-op } PtrList>::clear(); @@ -414,6 +409,6 @@ Ostream& operator<<(Ostream& os, const tmp>& tf) // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // - #include "FieldFieldFunctions.C" +#include "FieldFieldFunctions.C" // ************************************************************************* // diff --git a/src/OpenFOAM/fields/Fields/DynamicField/DynamicFieldI.H b/src/OpenFOAM/fields/Fields/DynamicField/DynamicFieldI.H index 53bc5b4944..114480ba1b 100644 --- a/src/OpenFOAM/fields/Fields/DynamicField/DynamicFieldI.H +++ b/src/OpenFOAM/fields/Fields/DynamicField/DynamicFieldI.H @@ -399,6 +399,11 @@ inline void Foam::DynamicField::swap DynamicField& lst ) { + if (this == &lst) + { + return; // Self-swap is a no-op + } + DynamicList& cur = *this; // Make addressable size identical to the allocated capacity @@ -450,6 +455,11 @@ inline void Foam::DynamicField::transfer DynamicField& list ) { + if (this == &list) + { + return; // Self-assignment is a no-op + } + // Take over storage as-is (without shrink, without using SizeMin) // clear addressing and storage for old list. capacity_ = list.capacity(); @@ -484,7 +494,7 @@ Foam::DynamicField::append if (this == &list) { FatalErrorInFunction - << "attempted appending to self" << abort(FatalError); + << "Attempted appending to self" << abort(FatalError); } label idx = List::size(); @@ -563,8 +573,7 @@ inline void Foam::DynamicField::operator= { if (this == &list) { - FatalErrorInFunction - << "Attempted assignment to self" << abort(FatalError); + return; // Self-assignment is a no-op } assignDynField(list); diff --git a/src/OpenFOAM/fields/Fields/Field/Field.C b/src/OpenFOAM/fields/Fields/Field/Field.C index 3409578237..549b6d850f 100644 --- a/src/OpenFOAM/fields/Fields/Field/Field.C +++ b/src/OpenFOAM/fields/Fields/Field/Field.C @@ -656,9 +656,7 @@ void Foam::Field::operator=(const Field& rhs) { if (this == &rhs) { - FatalErrorInFunction - << "attempted assignment to self" - << abort(FatalError); + return; // Self-assignment is a no-op } List::operator=(rhs); @@ -670,9 +668,7 @@ void Foam::Field::operator=(const tmp& rhs) { if (this == &(rhs())) { - FatalErrorInFunction - << "attempted assignment to self" - << abort(FatalError); + return; // Self-assignment is a no-op } List::operator=(rhs()); diff --git a/src/OpenFOAM/fields/GeometricFields/GeometricField/GeometricField.C b/src/OpenFOAM/fields/GeometricFields/GeometricField/GeometricField.C index df340d238f..1b1c486680 100644 --- a/src/OpenFOAM/fields/GeometricFields/GeometricField/GeometricField.C +++ b/src/OpenFOAM/fields/GeometricFields/GeometricField/GeometricField.C @@ -1327,9 +1327,7 @@ void Foam::GeometricField::operator= { if (this == &gf) { - FatalErrorInFunction - << "attempted assignment to self" - << abort(FatalError); + return; // Self-assignment is a no-op } checkField(*this, gf, "="); @@ -1351,9 +1349,7 @@ void Foam::GeometricField::operator= if (this == &gf) { - FatalErrorInFunction - << "attempted assignment to self" - << abort(FatalError); + return; // Self-assignment is a no-op } checkField(*this, gf, "="); diff --git a/src/OpenFOAM/matrices/LduMatrix/LduMatrix/LduMatrixOperations.C b/src/OpenFOAM/matrices/LduMatrix/LduMatrix/LduMatrixOperations.C index 5521b4f03e..7524e15f06 100644 --- a/src/OpenFOAM/matrices/LduMatrix/LduMatrix/LduMatrixOperations.C +++ b/src/OpenFOAM/matrices/LduMatrix/LduMatrix/LduMatrixOperations.C @@ -6,6 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation + Copyright (C) 2019 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -170,9 +171,7 @@ void Foam::LduMatrix::operator=(const LduMatrix& A) { if (this == &A) { - FatalErrorInFunction - << "attempted assignment to self" - << abort(FatalError); + return; // Self-assignment is a no-op } if (A.diagPtr_) diff --git a/src/OpenFOAM/matrices/Matrix/Matrix.C b/src/OpenFOAM/matrices/Matrix/Matrix.C index 94fc3ed55d..b5ea2e4519 100644 --- a/src/OpenFOAM/matrices/Matrix/Matrix.C +++ b/src/OpenFOAM/matrices/Matrix/Matrix.C @@ -289,6 +289,11 @@ Foam::List Foam::Matrix::release() template void Foam::Matrix::swap(Matrix& mat) { + if (this == &mat) + { + return; // Self-swap is a no-op + } + Foam::Swap(mRows_, mat.mRows_); Foam::Swap(nCols_, mat.nCols_); Foam::Swap(v_, mat.v_); @@ -298,6 +303,11 @@ void Foam::Matrix::swap(Matrix& mat) template void Foam::Matrix::transfer(Matrix& mat) { + if (this == &mat) + { + return; // Self-assignment is a no-op + } + clear(); mRows_ = mat.mRows_; @@ -457,9 +467,7 @@ void Foam::Matrix::operator=(const Matrix& mat) { if (this == &mat) { - FatalErrorInFunction - << "Attempted assignment to self" - << abort(FatalError); + return; // Self-assignment is a no-op } if (mRows_ != mat.mRows_ || nCols_ != mat.nCols_) @@ -480,14 +488,11 @@ void Foam::Matrix::operator=(const Matrix& mat) template void Foam::Matrix::operator=(Matrix&& mat) { - if (this == &mat) + if (this != &mat) { - FatalErrorInFunction - << "Attempted assignment to self" - << abort(FatalError); + // Self-assignment is a no-op + this->transfer(mat); } - - this->transfer(mat); } diff --git a/src/OpenFOAM/matrices/lduMatrix/lduMatrix/lduMatrixOperations.C b/src/OpenFOAM/matrices/lduMatrix/lduMatrix/lduMatrixOperations.C index 915f682b53..4d307f5710 100644 --- a/src/OpenFOAM/matrices/lduMatrix/lduMatrix/lduMatrixOperations.C +++ b/src/OpenFOAM/matrices/lduMatrix/lduMatrix/lduMatrixOperations.C @@ -6,6 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation + Copyright (C) 2019 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -91,10 +92,7 @@ void Foam::lduMatrix::operator=(const lduMatrix& A) { if (this == &A) { - FatalError - << "lduMatrix::operator=(const lduMatrix&) : " - << "attempted assignment to self" - << abort(FatalError); + return; // Self-assignment is a no-op } if (A.lowerPtr_) diff --git a/src/OpenFOAM/matrices/simpleMatrix/simpleMatrix.C b/src/OpenFOAM/matrices/simpleMatrix/simpleMatrix.C index d9d531d639..693a33ff01 100644 --- a/src/OpenFOAM/matrices/simpleMatrix/simpleMatrix.C +++ b/src/OpenFOAM/matrices/simpleMatrix/simpleMatrix.C @@ -6,6 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation + Copyright (C) 2019 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -103,9 +104,7 @@ void Foam::simpleMatrix::operator=(const simpleMatrix& m) { if (this == &m) { - FatalErrorInFunction - << "Attempted assignment to self" - << abort(FatalError); + return; // Self-assignment is a no-op } if (m() != m.m()) diff --git a/src/OpenFOAM/memory/autoPtr/autoPtrI.H b/src/OpenFOAM/memory/autoPtr/autoPtrI.H index 7b536a0fc9..ba1eb5b93e 100644 --- a/src/OpenFOAM/memory/autoPtr/autoPtrI.H +++ b/src/OpenFOAM/memory/autoPtr/autoPtrI.H @@ -172,6 +172,7 @@ inline void Foam::autoPtr::reset(autoPtr&& ap) noexcept template inline void Foam::autoPtr::swap(autoPtr& other) noexcept { + // Self-swap is effectively ignored T* p = ptr_; ptr_ = other.ptr_; other.ptr_ = p; @@ -259,6 +260,7 @@ inline void Foam::autoPtr::operator=(autoPtr&& ap) noexcept { if (this != &ap) { + // Ignore self-assignment reset(ap.release()); } } @@ -270,6 +272,7 @@ inline void Foam::autoPtr::operator=(autoPtr&& ap) noexcept { if (this != &ap) { + // Ignore self-assignment reset(ap.release()); } } diff --git a/src/OpenFOAM/memory/tmp/tmpI.H b/src/OpenFOAM/memory/tmp/tmpI.H index 261472c6a0..21e30caf6f 100644 --- a/src/OpenFOAM/memory/tmp/tmpI.H +++ b/src/OpenFOAM/memory/tmp/tmpI.H @@ -369,6 +369,11 @@ inline void Foam::tmp::cref(const T& obj) noexcept template inline void Foam::tmp::swap(tmp& other) noexcept { + if (&other == this) + { + return; // Self-swap is a no-op + } + // Copy/assign for pointer types T* p = ptr_; ptr_ = other.ptr_; @@ -460,6 +465,11 @@ inline void Foam::tmp::operator=(T* p) template inline void Foam::tmp::operator=(const tmp& t) { + if (&t == this) + { + return; // Self-assignment is a no-op + } + clear(); if (t.isTmp()) diff --git a/src/OpenFOAM/memory/tmp/tmpNrcI.H b/src/OpenFOAM/memory/tmp/tmpNrcI.H index b6a1e7a1a1..fd35c741ef 100644 --- a/src/OpenFOAM/memory/tmp/tmpNrcI.H +++ b/src/OpenFOAM/memory/tmp/tmpNrcI.H @@ -319,6 +319,11 @@ inline void Foam::tmpNrc::cref(const T& obj) noexcept template inline void Foam::tmpNrc::swap(tmpNrc& other) noexcept { + if (&other == this) + { + return; // Self-swap is a no-op + } + // Copy/assign for pointer types T* p = ptr_; ptr_ = other.ptr_; @@ -403,6 +408,11 @@ inline void Foam::tmpNrc::operator=(T* p) template inline void Foam::tmpNrc::operator=(const tmpNrc& t) { + if (&t == this) + { + return; // Self-assignment is a no-op + } + clear(); if (t.isTmp()) diff --git a/src/OpenFOAM/meshes/polyMesh/mapPolyMesh/mapDistribute/mapDistribute.C b/src/OpenFOAM/meshes/polyMesh/mapPolyMesh/mapDistribute/mapDistribute.C index 6e4eee7fcb..5551bcc0b4 100644 --- a/src/OpenFOAM/meshes/polyMesh/mapPolyMesh/mapDistribute/mapDistribute.C +++ b/src/OpenFOAM/meshes/polyMesh/mapPolyMesh/mapDistribute/mapDistribute.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation - Copyright (C) 2015-2018 OpenCFD Ltd. + Copyright (C) 2015-2019 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -516,6 +516,11 @@ Foam::label Foam::mapDistribute::whichTransform(const label index) const void Foam::mapDistribute::transfer(mapDistribute& rhs) { + if (this == &rhs) + { + // Self-assignment is a no-op + } + mapDistributeBase::transfer(rhs); transformElements_.transfer(rhs.transformElements_); transformStart_.transfer(rhs.transformStart_); @@ -528,11 +533,9 @@ void Foam::mapDistribute::operator=(const mapDistribute& rhs) { if (this == &rhs) { - // Avoid self-assignment - FatalErrorInFunction - << "Attempted assignment to self" - << abort(FatalError); + return; // Self-assignment is a no-op } + mapDistributeBase::operator=(rhs); transformElements_ = rhs.transformElements_; transformStart_ = rhs.transformStart_; diff --git a/src/OpenFOAM/meshes/polyMesh/mapPolyMesh/mapDistribute/mapDistributeBase.C b/src/OpenFOAM/meshes/polyMesh/mapPolyMesh/mapDistribute/mapDistributeBase.C index d5bbcab8a7..a5af2224a4 100644 --- a/src/OpenFOAM/meshes/polyMesh/mapPolyMesh/mapDistribute/mapDistributeBase.C +++ b/src/OpenFOAM/meshes/polyMesh/mapPolyMesh/mapDistribute/mapDistributeBase.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2015-2017 OpenFOAM Foundation - Copyright (C) 2015-2016, OpenCFD Ltd. + Copyright (C) 2015-2019 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -839,6 +839,12 @@ Foam::mapDistributeBase::mapDistributeBase(Istream& is) void Foam::mapDistributeBase::transfer(mapDistributeBase& rhs) { + if (this == &rhs) + { + // Self-assignment is a no-op + return; + } + constructSize_ = rhs.constructSize_; subMap_.transfer(rhs.subMap_); constructMap_.transfer(rhs.constructMap_); @@ -1264,11 +1270,9 @@ void Foam::mapDistributeBase::operator=(const mapDistributeBase& rhs) { if (this == &rhs) { - // Avoid self assignment - FatalErrorInFunction - << "Attempted assignment to self" - << abort(FatalError); + return; // Self-assignment is a no-op } + constructSize_ = rhs.constructSize_; subMap_ = rhs.subMap_; constructMap_ = rhs.constructMap_; diff --git a/src/OpenFOAM/primitives/strings/keyType/keyTypeI.H b/src/OpenFOAM/primitives/strings/keyType/keyTypeI.H index 8c1d8fa680..cbf710e517 100644 --- a/src/OpenFOAM/primitives/strings/keyType/keyTypeI.H +++ b/src/OpenFOAM/primitives/strings/keyType/keyTypeI.H @@ -170,10 +170,9 @@ inline void Foam::keyType::clear() inline void Foam::keyType::swap(keyType& s) { - // Self-swapping is a no-op if (this == &s) { - return; + return; // Self-swap is a no-op } word::swap(static_cast(s)); @@ -191,10 +190,9 @@ inline bool Foam::keyType::operator()(const std::string& text) const inline void Foam::keyType::operator=(const keyType& s) { - // Self-assignment is a no-op if (this == &s) { - return; + return; // Self-assignment is a no-op } assign(s); // Bypasses char checking @@ -204,10 +202,9 @@ inline void Foam::keyType::operator=(const keyType& s) inline void Foam::keyType::operator=(keyType&& s) { - // Self-assignment is a no-op if (this == &s) { - return; + return; // Self-assignment is a no-op } clear(); diff --git a/src/OpenFOAM/primitives/strings/lists/hashedWordListI.H b/src/OpenFOAM/primitives/strings/lists/hashedWordListI.H index bb20a17639..17ab7f3238 100644 --- a/src/OpenFOAM/primitives/strings/lists/hashedWordListI.H +++ b/src/OpenFOAM/primitives/strings/lists/hashedWordListI.H @@ -146,6 +146,11 @@ inline bool Foam::hashedWordList::found(const word& name) const inline void Foam::hashedWordList::swap(hashedWordList& list) { + if (this == &list) + { + return; // Self-swap is a no-op + } + wordList::swap(static_cast(list)); lookup_.swap(list.lookup_); } diff --git a/src/OpenFOAM/primitives/strings/regex/regExpCxxI.H b/src/OpenFOAM/primitives/strings/regex/regExpCxxI.H index 4e2b3b8149..1c68adb20f 100644 --- a/src/OpenFOAM/primitives/strings/regex/regExpCxxI.H +++ b/src/OpenFOAM/primitives/strings/regex/regExpCxxI.H @@ -154,8 +154,12 @@ inline bool Foam::regExpCxx::clear() inline void Foam::regExpCxx::swap(regExpCxx& rgx) { - re_.swap(rgx.re_); - std::swap(ok_, rgx.ok_); + if (this != &rgx) + { + // Self-swap is a no-op + re_.swap(rgx.re_); + std::swap(ok_, rgx.ok_); + } } diff --git a/src/OpenFOAM/primitives/strings/string/stringI.H b/src/OpenFOAM/primitives/strings/string/stringI.H index 19ce1f9818..a5e890749e 100644 --- a/src/OpenFOAM/primitives/strings/string/stringI.H +++ b/src/OpenFOAM/primitives/strings/string/stringI.H @@ -265,9 +265,9 @@ inline bool Foam::string::match(const std::string& text) const inline void Foam::string::swap(std::string& str) { - // Self-swapping is a no-op if (this != &str) { + // Self-swap is a no-op std::string::swap(str); } } diff --git a/src/OpenFOAM/primitives/strings/wordRe/wordReI.H b/src/OpenFOAM/primitives/strings/wordRe/wordReI.H index f008f4794f..9d09b771d0 100644 --- a/src/OpenFOAM/primitives/strings/wordRe/wordReI.H +++ b/src/OpenFOAM/primitives/strings/wordRe/wordReI.H @@ -259,10 +259,9 @@ inline void Foam::wordRe::set(const char* str, const compOption opt) inline void Foam::wordRe::swap(wordRe& str) { - // Self-swapping is a no-op if (this == &str) { - return; + return; // Self-swap is a no-op } word::swap(static_cast(str)); @@ -280,10 +279,9 @@ inline bool Foam::wordRe::operator()(const std::string& text) const inline void Foam::wordRe::operator=(const wordRe& str) { - // Self-assignment is a no-op if (this == &str) { - return; + return; // Self-assignment is a no-op } assign(str); @@ -342,10 +340,9 @@ inline void Foam::wordRe::operator=(const char* str) inline void Foam::wordRe::operator=(wordRe&& str) { - // Self-assignment is a no-op if (this == &str) { - return; + return; // Self-assignment is a no-op } clear(); diff --git a/src/dynamicMesh/polyTopoChange/polyTopoChange/hexRef8/refinementHistory.C b/src/dynamicMesh/polyTopoChange/polyTopoChange/hexRef8/refinementHistory.C index 10cea017c2..11b98c08c1 100644 --- a/src/dynamicMesh/polyTopoChange/polyTopoChange/hexRef8/refinementHistory.C +++ b/src/dynamicMesh/polyTopoChange/polyTopoChange/hexRef8/refinementHistory.C @@ -147,12 +147,9 @@ void Foam::refinementHistory::splitCell8::operator=(const splitCell8& s) { // Assignment operator since autoPtr otherwise 'steals' storage. - // Check for assignment to self if (this == &s) { - FatalErrorInFunction - << "Attempted assignment to self" - << abort(FatalError); + return; // Self-assignment is a no-op } parent_ = s.parent_; diff --git a/src/finiteArea/faMatrices/faMatrix/faMatrix.C b/src/finiteArea/faMatrices/faMatrix/faMatrix.C index 5a80f21dd6..eef14bd5b0 100644 --- a/src/finiteArea/faMatrices/faMatrix/faMatrix.C +++ b/src/finiteArea/faMatrices/faMatrix/faMatrix.C @@ -6,6 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2016-2017 Wikki Ltd + Copyright (C) 2019 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -712,9 +713,7 @@ void Foam::faMatrix::operator=(const faMatrix& famv) { if (this == &famv) { - FatalErrorInFunction - << "attempted to assignment to self" - << abort(FatalError); + return; // Self-assignment is a no-op } if (&psi_ != &(famv.psi_)) diff --git a/src/finiteVolume/finiteVolume/convectionSchemes/convectionScheme/convectionScheme.C b/src/finiteVolume/finiteVolume/convectionSchemes/convectionScheme/convectionScheme.C index 4b3f41b6fe..f21e3254db 100644 --- a/src/finiteVolume/finiteVolume/convectionSchemes/convectionScheme/convectionScheme.C +++ b/src/finiteVolume/finiteVolume/convectionSchemes/convectionScheme/convectionScheme.C @@ -159,9 +159,7 @@ void convectionScheme::operator=(const convectionScheme& cs) { if (this == &cs) { - FatalErrorInFunction - << "attempted assignment to self" - << abort(FatalError); + return; // Self-assignment is a no-op } } diff --git a/src/finiteVolume/fvMatrices/fvMatrix/fvMatrix.C b/src/finiteVolume/fvMatrices/fvMatrix/fvMatrix.C index e08e91f6e4..1aeeb961e0 100644 --- a/src/finiteVolume/fvMatrices/fvMatrix/fvMatrix.C +++ b/src/finiteVolume/fvMatrices/fvMatrix/fvMatrix.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2017 OpenFOAM Foundation - Copyright (C) 2016 OpenCFD Ltd. + Copyright (C) 2016-2019 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -1031,9 +1031,7 @@ void Foam::fvMatrix::operator=(const fvMatrix& fvmv) { if (this == &fvmv) { - FatalErrorInFunction - << "attempted assignment to self" - << abort(FatalError); + return; // Self-assignment is a no-op } if (&psi_ != &(fvmv.psi_)) diff --git a/src/functionObjects/field/fieldAverage/fieldAverageItem/fieldAverageItem.C b/src/functionObjects/field/fieldAverage/fieldAverageItem/fieldAverageItem.C index 850ed93d07..6396c13547 100644 --- a/src/functionObjects/field/fieldAverage/fieldAverageItem/fieldAverageItem.C +++ b/src/functionObjects/field/fieldAverage/fieldAverageItem/fieldAverageItem.C @@ -228,12 +228,9 @@ void Foam::functionObjects::fieldAverageItem::operator= const fieldAverageItem& rhs ) { - // Check for assignment to self if (this == &rhs) { - FatalErrorInFunction - << "Attempted assignment to self" << nl - << abort(FatalError); + return; // Self-assignment is a no-op } // Set updated values diff --git a/src/lagrangian/intermediate/parcels/Templates/CollidingParcel/CollisionRecordList/CollisionRecordList.C b/src/lagrangian/intermediate/parcels/Templates/CollidingParcel/CollisionRecordList/CollisionRecordList.C index bbc7a9dcef..fecdcb9177 100644 --- a/src/lagrangian/intermediate/parcels/Templates/CollidingParcel/CollisionRecordList/CollisionRecordList.C +++ b/src/lagrangian/intermediate/parcels/Templates/CollidingParcel/CollisionRecordList/CollisionRecordList.C @@ -6,6 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation + Copyright (C) 2019 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -393,12 +394,9 @@ void Foam::CollisionRecordList::operator= const CollisionRecordList& rhs ) { - // Check for assignment to self if (this == &rhs) { - FatalErrorInFunction - << "Attempted assignment to self" - << abort(FatalError); + return; // Self-assignment is a no-op } pairRecords_ = rhs.pairRecords_; diff --git a/src/lagrangian/intermediate/parcels/Templates/CollidingParcel/CollisionRecordList/PairCollisionRecord/PairCollisionRecord.C b/src/lagrangian/intermediate/parcels/Templates/CollidingParcel/CollisionRecordList/PairCollisionRecord/PairCollisionRecord.C index e96f8f87cb..e6d921c7f1 100644 --- a/src/lagrangian/intermediate/parcels/Templates/CollidingParcel/CollisionRecordList/PairCollisionRecord/PairCollisionRecord.C +++ b/src/lagrangian/intermediate/parcels/Templates/CollidingParcel/CollisionRecordList/PairCollisionRecord/PairCollisionRecord.C @@ -6,6 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation + Copyright (C) 2019 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -87,12 +88,9 @@ void Foam::PairCollisionRecord::operator= const PairCollisionRecord& rhs ) { - // Check for assignment to self if (this == &rhs) { - FatalErrorInFunction - << "Attempted assignment to self" - << abort(FatalError); + return; // Self-assignment is a no-op } origProcOfOther_ = rhs.origProcOfOther_; diff --git a/src/lagrangian/intermediate/parcels/Templates/CollidingParcel/CollisionRecordList/WallCollisionRecord/WallCollisionRecord.C b/src/lagrangian/intermediate/parcels/Templates/CollidingParcel/CollisionRecordList/WallCollisionRecord/WallCollisionRecord.C index a6073dfb8f..581828a593 100644 --- a/src/lagrangian/intermediate/parcels/Templates/CollidingParcel/CollisionRecordList/WallCollisionRecord/WallCollisionRecord.C +++ b/src/lagrangian/intermediate/parcels/Templates/CollidingParcel/CollisionRecordList/WallCollisionRecord/WallCollisionRecord.C @@ -6,6 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation + Copyright (C) 2019 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -85,12 +86,9 @@ void Foam::WallCollisionRecord::operator= const WallCollisionRecord& rhs ) { - // Check for assignment to self if (this == &rhs) { - FatalErrorInFunction - << "Attempted assignment to self" - << abort(FatalError); + return; // Self-assignment is a no-op } accessed_ = rhs.accessed_; diff --git a/src/lagrangian/molecularDynamics/molecularMeasurements/bufferedAccumulator/bufferedAccumulator.C b/src/lagrangian/molecularDynamics/molecularMeasurements/bufferedAccumulator/bufferedAccumulator.C index a2954a60ae..42b88a90ba 100644 --- a/src/lagrangian/molecularDynamics/molecularMeasurements/bufferedAccumulator/bufferedAccumulator.C +++ b/src/lagrangian/molecularDynamics/molecularMeasurements/bufferedAccumulator/bufferedAccumulator.C @@ -6,6 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation + Copyright (C) 2019 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -211,12 +212,9 @@ void Foam::bufferedAccumulator::operator= const bufferedAccumulator& rhs ) { - // Check for assignment to self if (this == &rhs) { - FatalErrorInFunction - << "Attempted assignment to self" - << abort(FatalError); + return; // Self-assignment is a no-op } List>::operator=(rhs); @@ -229,6 +227,6 @@ void Foam::bufferedAccumulator::operator= // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // - #include "bufferedAccumulatorIO.C" +#include "bufferedAccumulatorIO.C" // ************************************************************************* // diff --git a/src/lagrangian/molecularDynamics/molecularMeasurements/distribution/distribution.C b/src/lagrangian/molecularDynamics/molecularMeasurements/distribution/distribution.C index 26078ed741..1cf639b836 100644 --- a/src/lagrangian/molecularDynamics/molecularMeasurements/distribution/distribution.C +++ b/src/lagrangian/molecularDynamics/molecularMeasurements/distribution/distribution.C @@ -432,12 +432,9 @@ Foam::List> Foam::distribution::raw() void Foam::distribution::operator=(const distribution& rhs) { - // Check for assignment to self if (this == &rhs) { - FatalErrorInFunction - << "Attempted assignment to self" - << abort(FatalError); + return; // Self-assignment is a no-op } Map