mirror of
https://github.com/bitcoin/bitcoin
synced 2026-12-04 10:50:37 +00:00
WIP: refactor: Add [[nodiscard]] to functions returning bool+mutable ref #34520
No reviewers
Labels
No labels
Android
Backport
Block storage
Brainstorming
Bug
Build system
CI failed
Consensus
Data corruption
Descriptors
Docs
DrahtBot Guix build requested
Feature
Fuzzing
GUI
Good First Review
IPC
Insufficient Review
Kernel
Linux/Unix
Mempool
Mining
Needs Backport (31.x)
Needs Backport (32.x)
Needs Benchmark
Needs Conceptual Review
Needs backport (30.x)
Needs rebase
Needs release note
Net
P2P
PSBT
Privacy
Private Broadcast
Questions and Help
RPC/REST/ZMQ
Refactoring
Resource usage
Review club
Scripts and tools
Settings
TX fees and policy
Tests
Tracking Issue
UTXO Db and Indexes
Up for grabs
Upstream
Utils/log/libs
Validation
Waiting for author
Wallet
Windows
good first issue
interfaces
macOS
No milestone
No project
No assignees
2 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
bitcoin/bitcoin!34520
Loading…
Reference in a new issue
No description provided.
Delete branch "2602-nodiscard"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Legacy code often used a function signature of the form
bool Get...(const& in, mut& in_out);to implement a getter that optionally returns a value.In modern code, this can be replaced by
std::optional<_> Get...(const& in);.However, some legacy code remains. To ensure that the "imaginary optional" is not unwrapped when the getter returns false, add a C++17
[[nodiscard]]attribute to all those legacy functions.There were only a few places that ignored the return value. I've fixed them in separate commits.
This should be easy to review via
--word-diff-regex=.and then confirming that the attribute was added to such a getter.The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.
External sites
Reviews
See the guideline and AI policy for information on the review process.
If your review is incorrectly listed, please copy-paste
<!--meta-tag:bot-skip-->into the comment that the bot should ignore.Conflicts
Reviewers, this pull request conflicts with the following ones:
test_IsStandardinto independent test cases by yuvicc)-Wunusedby fanquake)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.
ACK fac5685
ACK fac5685e867b4a9ec334ed1ee31aabacd1c3124e
Concept ACK
This looks like a good thing to add, but I wonder if handling these cases incrementally is the correct approach. Outside of these "getter" functions there are so many cases where adding nodiscard make sense too. There is
modernize-use-nodiscard, but that is too noisy. An alternative might be to roll our own clang tidy plugin that checks for bool return + mutable ref or pointer argument.Do you also want to add these couple of cases (also added one that takes a mutable pointer):
I wondered that, too. The patch here was written in 2018, but fell off the table. While I rebased it, some places where transformed to std::optional (which should be used in a few more places instead of nodiscard).
Are there? I think the attribute should only be used where it really makes sense.
I think this will do the exact opposite of what we want: It ignores functions that have a mutable ref param. It only adds it to functions that don't really need it in the first place.
Happy to take a look at writing a clang-tidy plugin, maybe as a follow-up?
I've also seen this one, but left it for a follow-up, because it is not trivial to see if and why (void) would be the correct fix here. I wanted to keep this pull focussed on mostly mechanical changes for now.
Thx, done
Concept ACK
Shouldn't
proxy.m_tor_stream_isolationalso be conditional onis_valid?Perhaps:
I'm uncomfortable with both declaring
[[nodiscard] bool GetOp(...)and then discarding the return value in consensus code (or non-test code in general).The earlier
scriptSig.IsPushOnly()should guarantee thatGetOpwill never return false, so perhapsAssume(scriptSig.GetOp(pc, opcode, data))would be better here?Yeah, but I guess
std::optionalcould make more sense. Done in https://github.com/bitcoin/bitcoin/pull/34741 and moved this into draft for now.edit: now merged
thx, done
Haven't looked here. This is still in draft and I may take a look on the next rebase.
edit: thx, done
Rebased, and taken out of draft, now that all feedback is addressed.
Code review fa97a912e836943b33d21e83807fb8784e7abceb. FIrst commit does not seem right to me but otherwise these are nice changes that could prevent bugs.
In commit "refactor: Add nodiscard to GetCachedLastHardenedExtPubKey" (4444e4abc181ccaadeb8788eb4ab90f0a63ab485)
Looks like commit message should be updated. It says "mark the return value as unused in one place" which is not what this doing (it is using the return value as an optimization)
In commit "refactor: Add nodiscard to GetOp/GetScriptOp" (fa9e0570a6e1da609d052e6d2bcdc9744b719c42)
I don't think it makes sense to use Assume here. Logically
CountWitnessSigOpscould either be intended to tolerate malformed scripts, in which case it should ignore the GetOp return value, or intended not to tolerate them, in which case it should abort or raise an exception. I don't see how it could realistically be intended to tolerate malformed scripts in release builds but not debug builds.This used to be
(void), but then I changed it toAssumeafter a review comment (https://github.com/bitcoin/bitcoin/pull/34520#discussion_r2888547507). Now I changed toAssert, but I guess tidy uses a release build an nowcontrib/devtools/check-deps.shfails with:Not sure how to fix this. I guess I'd have to move
util/check.htocrypto? According to:re: https://github.com/bitcoin/bitcoin/pull/34520#discussion_r3047316414
Would suggest a plain assert:
Or maybe it is fine for consensus to depend on util though now it is no longer distributed as a separate library. Moving check to crypto could also be ok. Plain assert seems best to me though.
Ok, I may use
assertfor now, but it seems better to answer the library design question. Otherwise, it will come up again in the future. There is alsokernel_utilfrom https://github.com/bitcoin/bitcoin/pull/28690, so possibly there is no need consensus to depend on all ofutil, but it may be fine to depend onkernel_util.Yes, it looks like the underlying problem is that
util/check.hdepends on some globals likeg_detail_test_only_CheckFailuresAreExceptionsNotAbortsandg_enable_dynamic_fuzz_determinismwhich need to live in some library. Otherwisecheck.hcould be a header-only dependency.I don't think it particularly matters where those globals live. The easiest thing to do which preserves existing dependencies and would not be a bad solution IMO would be to move these globals to the
cryptolibrary likehex_basewas moved there in #29015, since everything can depend oncrypto. Or maybe introduce a small core or globals library to hold these and maybe other things likeG_TRANSLATION_FUN. The kernel_util idea from #28690 also seems ok (with some different naming since probably wallet and consensus libraries should not depend on a iibrary with "kernel" in the name)Another option would to just let the
consensuslibrary depend onutil. I don't think there would be harm from that if consensus is not distributed separately.But I don't think it would be good to make library dependency changes in this PR if
assertfits well here, which it seems like it does.🔴 Major · Avoid linking
bitcoin_utilfrombitcoin_consensussrc/script/interpreter.cpp:2168 now uses Assert, which always calls internal_abort_helper and StrFormatFailedCheck from bitcoin_util. bitcoin_consensus does not link bitcoin_util, so check-deps in the tidy CI job and the bare-metal RISC-V consensus link fail. The standard assert avoids both.
Location:
src/script/interpreter.cpp:2168(head).🔵 Suggestion · Annotate the cursor interface
src/txdb.cpp:240 adds nodiscard only to CCoinsViewDBCursor's overrides. Every caller goes through the unannotated CCoinsViewCursor base (coins.h:283), so the attributes never warn. For example, rpc/blockchain.cpp:3460 discards GetKey's result without a warning. Annotate the base declarations instead.
Location:
src/txdb.cpp:240(head).🔵 Suggestion · Include the neighboring node output getters
getNodesStats and getBanned (src/interfaces/node.h:129,132) still lack nodiscard, and their Qt callers discard the result. Impact is low: on failure the output is either cleared or left as the caller's fresh empty map, so the GUI just shows empty tables.
Location:
src/interfaces/node.h:128(head).Review coverage was incomplete.
Reviewed:
1bd568e2c708· Full review report · Previous reports🐙 This pull request conflicts with the target branch and needs rebase.