linter to check omitted fields when constructing aggregate types? #25631

issue maflcko opened this issue on July 18, 2022
  1. maflcko commented at 11:28 AM on July 18, 2022: member

    It would be nice to have a linter/tidy/whatever to enforce all non-defaulted fields in an aggregate type are initialized.

    Assuming the code:

    struct A {
        struct Options {
            unsigned cache_size;
            signed delta;
            bool in_memory = false;
        };
        A(const Options&) {} // unsafe (may have fields uninitialized)
        A(unsigned cache_size, signed delta, bool in_memory = false) {}  // safe, all non-default args are initialized
    };
    
    int main() {
        (void)A{
            A::Options{
                .delta = -1,
            },
        };
    }
    

    This leaves cache_size uninitialized when using designated initializers. (The problem exists even without designated initializers, but with designated initializers recently enabled it seems likely that we'll increase the use of aggregate types)

  2. maflcko added the label Feature on Jul 18, 2022
  3. fanquake commented at 11:41 AM on July 18, 2022: member

    Probably another good candidate for a clang-tidy plugin. cc @theuni

  4. maflcko commented at 11:48 AM on July 18, 2022: member

    Upstream might be happy about this as well, so one could consider adding it there.

  5. theuni commented at 2:36 PM on July 18, 2022: member

    Ooh, fun one! I'll give it a go.

  6. theuni commented at 7:00 PM on July 20, 2022: member

    Here's a quick and working (but probably under-specified) plugin using @MarcoFalke's sample above as a test: https://github.com/theuni/bitcoin-tidy/commit/4cbd5bf639ea7d56e4d4fe516494c6ac025a3034

    Example usage: clang-tidy --load=`pwd`/libbitcoin-tidy.so -checks='-*,bitcoin-init-list' ../example_init.cc -- -std=c++17

    1 warning generated.
    /home/cory/dev/bitcoin-tidy/build/../example_init.cc:13:19: warning: Designated initializer with uninitialized member of type: unsigned int [bitcoin-init-list]
            A::Options{
                      ^
    

    Edit: Updated to print the offending uninitialized type.

  7. theuni commented at 3:12 PM on July 26, 2022: member

    I think this might end up being a good "hello world" check for us to start with for a clang-tidy plugin infrastructure. Ping @fanquake.

    I hacked together a quick local test, and the current bitcoind sources pass this check with no warnings: https://github.com/theuni/bitcoin/tree/tidy-init-list

    When an initialization error is introduced it is detected as expected:

    $ make bitcoin-tidy-lint

    /home/cory/dev/bitcoin2/src/init.cpp:1417:37: warning: Designated initializer with uninitialized member of type: int64_t [bitcoin-init-list]
        CTxMemPool::Options mempool_opts{
    
  8. fanquake commented at 4:05 PM on July 26, 2022: member

    I think this might end up being a good "hello world" check for us to start with for a clang-tidy plugin infrastructure. Ping @fanquake.

    I have a WIP branch that integrates compiling the bitcoin-tidy plugin and using it in our run-clang-tody invocation into the CI. We can clean up that branch a bit, and open a PR at some point.

  9. willcl-ark commented at 2:15 PM on October 21, 2025: member

    It looks like this issue has been resolved. If that’s not the case, please let us know by commenting here or opening a new issue.

  10. willcl-ark closed this on Oct 21, 2025


github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-04-24 06:14 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me