From 32b1d1379258aa57c2a7e278f60d1117fcf17953 Mon Sep 17 00:00:00 2001 From: Cory Fields Date: Wed, 5 Jun 2024 17:42:27 +0000 Subject: [PATCH 1/2] refactor: add self-assign checks to classes which violate the clang-tidy check Both of these cases appear to be harmless, but adding the tests allows us to turn on the aggressive clang-tidy checks. --- src/arith_uint256.h | 6 ++++-- src/key.h | 14 ++++++++------ src/test/util_tests.cpp | 6 ++++-- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/arith_uint256.h b/src/arith_uint256.h index ba36cebbdcc..955fd655632 100644 --- a/src/arith_uint256.h +++ b/src/arith_uint256.h @@ -43,8 +43,10 @@ public: base_uint& operator=(const base_uint& b) { - for (int i = 0; i < WIDTH; i++) - pn[i] = b.pn[i]; + if (this != &b) { + for (int i = 0; i < WIDTH; i++) + pn[i] = b.pn[i]; + } return *this; } diff --git a/src/key.h b/src/key.h index c802e1ebb8c..bbca2c4deef 100644 --- a/src/key.h +++ b/src/key.h @@ -75,13 +75,15 @@ public: CKey& operator=(const CKey& other) { - if (other.keydata) { - MakeKeyData(); - *keydata = *other.keydata; - } else { - ClearKeyData(); + if (this != &other) { + if (other.keydata) { + MakeKeyData(); + *keydata = *other.keydata; + } else { + ClearKeyData(); + } + fCompressed = other.fCompressed; } - fCompressed = other.fCompressed; return *this; } diff --git a/src/test/util_tests.cpp b/src/test/util_tests.cpp index a371753adf0..1b66496505b 100644 --- a/src/test/util_tests.cpp +++ b/src/test/util_tests.cpp @@ -1508,8 +1508,10 @@ struct Tracker Tracker(Tracker&& t) noexcept : origin(t.origin), copies(t.copies) {} Tracker& operator=(const Tracker& t) noexcept { - origin = t.origin; - copies = t.copies + 1; + if (this != &t) { + origin = t.origin; + copies = t.copies + 1; + } return *this; } }; From 26a7f70b5d2b1cbfbf03e0b57b9b5ea92dafbb19 Mon Sep 17 00:00:00 2001 From: Cory Fields Date: Wed, 5 Jun 2024 17:47:21 +0000 Subject: [PATCH 2/2] ci: enable self-assignment clang-tidy check --- src/.clang-tidy | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/.clang-tidy b/src/.clang-tidy index 61adce1d506..8e511480eff 100644 --- a/src/.clang-tidy +++ b/src/.clang-tidy @@ -6,6 +6,7 @@ bugprone-move-forwarding-reference, bugprone-string-constructor, bugprone-use-after-move, bugprone-lambda-function-name, +bugprone-unhandled-self-assignment, misc-unused-using-decls, misc-no-recursion, modernize-use-default-member-init, @@ -23,8 +24,10 @@ readability-const-return-type, readability-redundant-declaration, readability-redundant-string-init, ' +HeaderFilterRegex: '.' WarningsAsErrors: '*' CheckOptions: - key: performance-move-const-arg.CheckTriviallyCopyableMove value: false -HeaderFilterRegex: '.' + - key: bugprone-unhandled-self-assignment.WarnOnlyIfThisHasSuspiciousField + value: false