Skip to content

Conversation

maflcko
Copy link
Member

@maflcko maflcko commented Oct 21, 2020

This removes unused default: cases for all switch statements on TxoutType and adds the cases (MULTISIG, NULL_DATA, NONSTANDARD) to ExtractDestination for clarity.

Also, the compiler is now able to use -Wswitch.

@practicalswift
Copy link
Contributor

Concept ACK: -Wswitch is good, and termination via assert(false); is better than UB :)

From the standard: "Flowing off the end of a function is equivalent to a return with no value; this results in undefined behavior in a value-returning function." (C++11, [stmt.return])

@jonatack
Copy link
Member

Concept ACK

1 similar comment
@hebasto
Copy link
Member

hebasto commented Nov 12, 2020

Concept ACK

Copy link
Member

@hebasto hebasto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK fa650ca, I have reviewed the code and it looks OK, I agree it can be merged.

@DrahtBot
Copy link
Contributor

DrahtBot commented Nov 21, 2020

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Conflicts

Reviewers, 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.

Copy link
Contributor

@mjdietzx mjdietzx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ack fa650ca

We do switch (dest.which()) { in a few places as well. This seems pretty similar, and I'm wondering if this should be refactored as well? (Probably in a different PR though?)

In #20286 I added TxoutType& typeRet to ExtractDestination here, and I was wondering if this would allow us to make these switch (dest.which()) { statements more readable by doing it on TxoutType as done in this PR. Thoughts, and once #20286 is merged, is this something that should be done?

@maflcko
Copy link
Member Author

maflcko commented Nov 23, 2020

We do switch (dest.which()) { in a few places as well. This seems pretty similar, and I'm wondering if this should be refactored as well? (Probably in a different PR though?)

What do you suggest? which decays the enum class into an integer. The compiler can't do -Wswitch static analysis on those switch statements.

@mjdietzx
Copy link
Contributor

mjdietzx commented Nov 23, 2020

Well once we have (would require #20286):

bool ExtractDestination(const CScript& scriptPubKey, TxoutType& typeRet, CTxDestination& addressRet);

Could we do something like:

TxoutType type;
CTxDestination dest;
ExtractDestination(m_script, type, dest);
switch (type) {
case TxoutType::PUBKEYHASH:
case TxoutType::SCRIPTHASH: return OutputType::LEGACY;
case TxoutType::WITNESS_V0_SCRIPTHASH:
case TxoutType::WITNESS_V0_KEYHASH:
case TxoutType::WITNESS_V1_TAPROOT:
case TxoutType::WITNESS_UNKNOWN: return OutputType::BECH32;
case TxoutType::PUBKEY:
case TxoutType::MULTISIG:
case TxoutType::NONSTANDARD:
case TxoutType::NULL_DATA: return nullopt; // no default case, so the compiler can warn about missing cases
}
assert(false);

Note: I messed around with this, and probably made some mistakes here, but didn't see any unit tests or functional tests fail. So maybe some test coverage is needed here as well?

@maflcko
Copy link
Member Author

maflcko commented Nov 23, 2020

would require #20286

Let's discuss this in the other pull then

@practicalswift
Copy link
Contributor

cr ACK fa650ca: patch looks correct and assert(false); is better than UB :)

@maflcko maflcko merged commit e498aef into bitcoin:master Feb 11, 2021
@maflcko maflcko deleted the 2010-WswitchTxoutType branch February 11, 2021 11:37
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Feb 11, 2021
fa650ca Use -Wswitch for TxoutType where possible (MarcoFalke)
fa59e0b test: Add missing script_standard_Solver_success cases (MarcoFalke)

Pull request description:

  This removes unused `default:` cases for all `switch` statements on `TxoutType` and adds the cases (`MULTISIG`, `NULL_DATA`, `NONSTANDARD`) to `ExtractDestination` for clarity.

  Also, the compiler is now able to use `-Wswitch`.

ACKs for top commit:
  practicalswift:
    cr ACK fa650ca: patch looks correct and `assert(false);` is better than UB :)
  hebasto:
    ACK fa650ca, I have reviewed the code and it looks OK, I agree it can be merged.

Tree-SHA512: 282458b6523bd8923a0c0f5c423d1db2dce2a2d1b1d1dae455415c6fc995bb41ce82c1f9b0a1c0dcc6d874d171e04c30eca585f147582f52c7048c140358630a
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Aug 18, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants