HomePhabricator

Avoid copies in range-for loops and add a warning to detect them

Description

Avoid copies in range-for loops and add a warning to detect them

Summary:

See title. Fixing these would otherwise be a continuous process, adding
the warning should keep them from cropping up.

Note that the warning seems to be Clang-only for now.

Some more explanations from the PR comments:

From the pull-request to llvm that added the feature, it warns about:

        for (const Foo &x : Foos), where the range Foos does not return
a copy. This warning will suggest using the non-reference type so the
copy is obvious.

        for (const Foo x : Foos), where the range Foos does return a
reference, but is copied into x. This warning will suggest using the
reference type to prevent a copy from being made.

        for (const Bar &x : Foos), where Bar is constructed from Foo. In
this case, suggest using the non-reference "const Bar" to indicate a
copy is intended to be made, or "const Foo &" to prevent a copy from
being made.

Backport of core PR13480
https://github.com/bitcoin/bitcoin/pull/13480/files

Depends on D3795.

Test Plan:
With Clang:

make check
ninja check

Reviewers: #bitcoin_abc, deadalnix

Reviewed By: #bitcoin_abc, deadalnix

Differential Revision: https://reviews.bitcoinabc.org/D3796