-
Notifications
You must be signed in to change notification settings - Fork 37.7k
refactor: Make const references to avoid unnecessarily copying objects and enable two clang-tidy checks #25707
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Concept ACK
-
I agree with removing unused variables, as this makes the code cleaner and free of redundancies.
-
I verified that all the variables in the first and second commit either had their value reassigned before being used or were completely unused in their lifetime.
-
I verified that the variables in the third commit had their values unchanged, so it makes sense to make them a const variable.
-
I think merging the first and second commit would be a good idea as they are similar.
-
I would give the test file another view to confirm no other instance of unused variable has been left in the test file.
@@ -169,7 +169,7 @@ const std::set<BlockFilterType>& AllBlockFilterTypes() | |||
|
|||
static std::once_flag flag; | |||
std::call_once(flag, []() { | |||
for (auto entry : g_filter_types) { | |||
for (const auto& entry : g_filter_types) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Those things keep coming up, so it would be good to have a clang-tidy check for this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added in 4407213.
test/functional/interface_zmq.py
Outdated
@@ -372,7 +372,6 @@ def test_sequence(self): | |||
# since it greatly depends on inner-workings of blocks/mempool | |||
# during "deep" re-orgs. Probably should "re-construct" | |||
# blockchain/mempool state from notifications instead. | |||
block_count = self.nodes[0].getblockcount() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dead Python code should be getting caught by Vulture, see https://github.com/bitcoin/bitcoin/blob/master/test/lint/lint-python-dead-code.py. Maybe we need to drop the minimum confidence.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can decrease confidence. But this line worries me a little.
Any value below 100 introduces the risk of false positives, which would create an unacceptable maintenance burden.
Can we afford false positives for the chance of catching the few missed cases?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we afford false positives for the chance of catching the few missed cases?
It should either be tweaked to the point that it catches obviously dead code, or just removed. No point running a dead code linter that doesn't catch dead code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No point running a dead code linter that doesn't catch dead code.
I agree with it.
4403522
to
5f99c7a
Compare
I've removed more unused variables and added more const references. |
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. ConflictsReviewers, this pull request conflicts with the following ones:
If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first. |
In a9c0555. Don't mix refactoring Python test code & c++ networking code into the same commit. I'd actually suggest splitting the Python changes from this PR, given it's unclear if Vulture is working. That should be followed up with, and without it, it's not clear if your Python changes are exhaustive. You should order your commits so that the changes follow logically. i.e perform refactors that allow turning on clang-tidy checks, before turning on the checks, not the other way around. Please write commit messages that are meaningful, and explain your changes. Limit your commit titles to ~ 50 chars. (i.e 4407213). |
This avoids initializing variables with the copy-constructor of a non-trivially copyable type.
5f99c7a
to
46c4dda
Compare
Thank you for the reviews. I have addressed your comments @fanquake :
Though, I haven't been able to tweak vulture to detect the unused variables I found manually. |
6d69575
to
46c4dda
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK 46c4dda
Checks enabled: 'performance-for-range-copy' and 'performance-unnecessary-copy-initialization'
46c4dda
to
ae7ae36
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK ae7ae36
@@ -476,7 +476,7 @@ bool Socks5(const std::string& strDest, uint16_t port, const ProxyCredentials* a | |||
if (recvr != IntrRecvError::OK) { | |||
return error("Error reading from proxy"); | |||
} | |||
if ((recvr = InterruptibleRecv(pchRet3, 2, g_socks5_recv_timeout, sock)) != IntrRecvError::OK) { | |||
if (InterruptibleRecv(pchRet3, 2, g_socks5_recv_timeout, sock) != IntrRecvError::OK) { | |||
return error("Error reading from proxy"); | |||
} | |||
LogPrint(BCLog::NET, "SOCKS5 connected %s\n", strDest); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for reference, the changes in this file are not detected by clang-tidy, nor do they have anything to do with const
, but they look correct
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
review ACK ae7ae36
…sarily copying objects and enable two clang-tidy checks ae7ae36 tidy: Enable two clang-tidy checks (Aurèle Oulès) 081b0e5 refactor: Make const refs vars where applicable (Aurèle Oulès) Pull request description: I added const references to some variables to avoid unnecessarily copying objects. Also added two clang-tidy checks : [performance-for-range-copy](https://releases.llvm.org/11.1.0/tools/clang/tools/extra/docs/clang-tidy/checks/performance-for-range-copy.html) and [performance-unnecessary-copy-initialization](https://releases.llvm.org/12.0.0/tools/clang/tools/extra/docs/clang-tidy/checks/performance-unnecessary-copy-initialization.html). ACKs for top commit: vasild: ACK ae7ae36 MarcoFalke: review ACK ae7ae36 Tree-SHA512: f6ac6b0cd0eee1e0c34d2f186484bc0f7ec6071451cccb33fa88a67d93d92b304e2fac378b88f087e94657745bca4e966dbc443759587400eb01b1f3061fde8c
I added const references to some variables to avoid unnecessarily copying objects.
Also added two clang-tidy checks : performance-for-range-copy and performance-unnecessary-copy-initialization.