Skip to content

Commit 00feabf

Browse files
committed
Merge bitcoin/bitcoin#30234: Enable clang-tidy checks for self-assignment
26a7f70 ci: enable self-assignment clang-tidy check (Cory Fields) 32b1d13 refactor: add self-assign checks to classes which violate the clang-tidy check (Cory Fields) Pull request description: See comment here: bitcoin/bitcoin#30161 (comment) Our code failed these checks in three places, which have been fixed up here. Though these appear to have been harmless, adding the check avoids the copy in the self-assignment case so there should be no downside. ~Additionally, minisketch failed the check as well. See bitcoin-core/minisketch#87 Edit: Done After fixing up the violations, turn on the aggressive clang-tidy check. Note for reviewers: `git diff -w` makes this trivial to review. ACKs for top commit: hebasto: ACK 26a7f70, I have reviewed the code and it looks OK. TheCharlatan: ACK 26a7f70 Tree-SHA512: 74d8236a1b5a698f2f61c4740c4fc77788b7f882c4b395acc4e6bfef1ec8a4554ea8821a26b14d70cfa6c8e2e9ea305deeea3fbf323967fa19343c007a53c5ba
2 parents 01dc38b + 26a7f70 commit 00feabf

File tree

4 files changed

+20
-11
lines changed

4 files changed

+20
-11
lines changed

src/.clang-tidy

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ bugprone-move-forwarding-reference,
66
bugprone-string-constructor,
77
bugprone-use-after-move,
88
bugprone-lambda-function-name,
9+
bugprone-unhandled-self-assignment,
910
misc-unused-using-decls,
1011
misc-no-recursion,
1112
modernize-use-default-member-init,
@@ -24,8 +25,10 @@ readability-const-return-type,
2425
readability-redundant-declaration,
2526
readability-redundant-string-init,
2627
'
28+
HeaderFilterRegex: '.'
2729
WarningsAsErrors: '*'
2830
CheckOptions:
2931
- key: performance-move-const-arg.CheckTriviallyCopyableMove
3032
value: false
31-
HeaderFilterRegex: '.'
33+
- key: bugprone-unhandled-self-assignment.WarnOnlyIfThisHasSuspiciousField
34+
value: false

src/arith_uint256.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,10 @@ class base_uint
4343

4444
base_uint& operator=(const base_uint& b)
4545
{
46-
for (int i = 0; i < WIDTH; i++)
47-
pn[i] = b.pn[i];
46+
if (this != &b) {
47+
for (int i = 0; i < WIDTH; i++)
48+
pn[i] = b.pn[i];
49+
}
4850
return *this;
4951
}
5052

src/key.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,15 @@ class CKey
7575

7676
CKey& operator=(const CKey& other)
7777
{
78-
if (other.keydata) {
79-
MakeKeyData();
80-
*keydata = *other.keydata;
81-
} else {
82-
ClearKeyData();
78+
if (this != &other) {
79+
if (other.keydata) {
80+
MakeKeyData();
81+
*keydata = *other.keydata;
82+
} else {
83+
ClearKeyData();
84+
}
85+
fCompressed = other.fCompressed;
8386
}
84-
fCompressed = other.fCompressed;
8587
return *this;
8688
}
8789

src/test/util_tests.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1508,8 +1508,10 @@ struct Tracker
15081508
Tracker(Tracker&& t) noexcept : origin(t.origin), copies(t.copies) {}
15091509
Tracker& operator=(const Tracker& t) noexcept
15101510
{
1511-
origin = t.origin;
1512-
copies = t.copies + 1;
1511+
if (this != &t) {
1512+
origin = t.origin;
1513+
copies = t.copies + 1;
1514+
}
15131515
return *this;
15141516
}
15151517
};

0 commit comments

Comments
 (0)