Skip to content
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

tidy: enable bugprone-use-after-move #25733

Merged
merged 2 commits into from Aug 30, 2022

Conversation

fanquake
Copy link
Member

@fanquake fanquake commented Jul 29, 2022

Would have caught #25640.

Currently // NOLINTs around:

test/util_tests.cpp:2513:34: error: 't2' used after it was moved [bugprone-use-after-move,-warnings-as-errors]
    BOOST_CHECK(v2[0].origin == &t2);
                                 ^
test/util_tests.cpp:2511:15: note: move occurred here
    auto v2 = Vector(std::move(t2));
              ^
test/util_tests.cpp:2519:34: error: 't2' used after it was moved [bugprone-use-after-move,-warnings-as-errors]
    BOOST_CHECK(v3[1].origin == &t2);
                                 ^
test/util_tests.cpp:2516:15: note: move occurred here
    auto v3 = Vector(t1, std::move(t2));
              ^
test/util_tests.cpp:2527:34: error: 't3' used after it was moved [bugprone-use-after-move,-warnings-as-errors]
    BOOST_CHECK(v4[2].origin == &t3);
                                 ^
test/util_tests.cpp:2523:15: note: move occurred here
    auto v4 = Vector(std::move(v3[0]), v3[1], std::move(t3));

See: https://releases.llvm.org/14.0.0/tools/clang/tools/extra/docs/clang-tidy/checks/bugprone-use-after-move.html

@fanquake fanquake added the Tests label Jul 29, 2022
@jonatack
Copy link
Contributor

jonatack commented Jul 29, 2022

Concept ACK, looks like it is finding cases in test/util_tests.cpp as well.

test/util_tests.cpp:2497:34: error: 't2' used after it was moved [bugprone-use-after-move,-warnings-as-errors]
    BOOST_CHECK(v2[0].origin == &t2);
                                 ^
test/util_tests.cpp:2495:15: note: move occurred here
    auto v2 = Vector(std::move(t2));
              ^
test/util_tests.cpp:2503:34: error: 't2' used after it was moved [bugprone-use-after-move,-warnings-as-errors]
    BOOST_CHECK(v3[1].origin == &t2);
                                 ^
test/util_tests.cpp:2500:15: note: move occurred here
    auto v3 = Vector(t1, std::move(t2));
              ^
test/util_tests.cpp:2511:34: error: 't3' used after it was moved [bugprone-use-after-move,-warnings-as-errors]
    BOOST_CHECK(v4[2].origin == &t3);
                                 ^
test/util_tests.cpp:2507:15: note: move occurred here
    auto v4 = Vector(std::move(v3[0]), v3[1], std::move(t3));

@fanquake fanquake force-pushed the tidy_enable_bugprone_use_after_move branch 2 times, most recently from 930ee87 to e25b0c6 Compare August 1, 2022 09:16
@DrahtBot
Copy link
Contributor

DrahtBot commented Aug 5, 2022

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

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #25780 (tidy: Enable more clang-tidy bugprone checks by aureleoules)

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.

@ryanofsky
Copy link
Contributor

Concept ACK, since this does seem like it could catch unintentional mistakes, but I have a little skepticism because it can be perfectly ok to use an object after moving. For example, vector's move constructor guarantees the moved from object will be empty (https://en.cppreference.com/w/cpp/container/vector/vector).

Currently some things are failing in CI as jonatack pointed out:

auto v2 = Vector(std::move(t2));
BOOST_CHECK_EQUAL(v2.size(), 1U);
BOOST_CHECK(v2[0].origin == &t2);

But maybe we could work around these with a custom move function (util::SafelyMoveFrom?) that does the same as std::move but avoids triggering the error. Or, I wonder if something like std::move(*&t2) would trick the linter

@fanquake
Copy link
Member Author

Or, I wonder if something like std::move(*&t2) would trick the linter

Looks like it does (locally). I've pushed this up for now, but will defer to you as to what change / how you'd rather handle this in the util tests.

@fanquake fanquake force-pushed the tidy_enable_bugprone_use_after_move branch from e25b0c6 to 696434d Compare August 19, 2022 08:25
src/test/util_tests.cpp Outdated Show resolved Hide resolved
Copy link
Contributor

@ryanofsky ryanofsky left a comment

Choose a reason for hiding this comment

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

Code review ACK 696434d. It's nice to enable tidy checks to catch unintended uses after moves. Workaround for intended uses seems ok, and NOLINT alternate suggestion would be ok too.

```bash
test/util_tests.cpp:2513:34: error: 't2' used after it was moved [bugprone-use-after-move,-warnings-as-errors]
    BOOST_CHECK(v2[0].origin == &t2);
                                 ^
test/util_tests.cpp:2511:15: note: move occurred here
    auto v2 = Vector(std::move(t2));
              ^
test/util_tests.cpp:2519:34: error: 't2' used after it was moved [bugprone-use-after-move,-warnings-as-errors]
    BOOST_CHECK(v3[1].origin == &t2);
                                 ^
test/util_tests.cpp:2516:15: note: move occurred here
    auto v3 = Vector(t1, std::move(t2));
              ^
test/util_tests.cpp:2527:34: error: 't3' used after it was moved [bugprone-use-after-move,-warnings-as-errors]
    BOOST_CHECK(v4[2].origin == &t3);
                                 ^
test/util_tests.cpp:2523:15: note: move occurred here
    auto v4 = Vector(std::move(v3[0]), v3[1], std::move(t3));
```
Will error with:
```bash
coins.cpp:102:22: error: 'coin' used after it was moved [bugprone-use-after-move,-warnings-as-errors]
           (uint32_t)coin.nHeight,
                     ^
coins.cpp:96:21: note: move occurred here
    it->second.coin = std::move(coin);
```

until bitcoin#25663 is merged.

See:
https://releases.llvm.org/14.0.0/tools/clang/tools/extra/docs/clang-tidy/checks/bugprone-use-after-move.html
@fanquake fanquake force-pushed the tidy_enable_bugprone_use_after_move branch from 696434d to f345dc3 Compare August 30, 2022 14:21
Copy link
Contributor

@ryanofsky ryanofsky left a comment

Choose a reason for hiding this comment

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

Code review ACK f345dc3. Only change since last review is switching to NOLINT directives

@maflcko maflcko merged commit 52dcb1d into bitcoin:master Aug 30, 2022
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Aug 31, 2022
f345dc3 tidy: enable bugprone-use-after-move (fanquake)
94f2235 test: work around bugprone-use-after-move warnings in util tests (fanquake)

Pull request description:

  Would have caught bitcoin#25640.

  Currently `// NOLINT`s around:
  ```bash
  test/util_tests.cpp:2513:34: error: 't2' used after it was moved [bugprone-use-after-move,-warnings-as-errors]
      BOOST_CHECK(v2[0].origin == &t2);
                                   ^
  test/util_tests.cpp:2511:15: note: move occurred here
      auto v2 = Vector(std::move(t2));
                ^
  test/util_tests.cpp:2519:34: error: 't2' used after it was moved [bugprone-use-after-move,-warnings-as-errors]
      BOOST_CHECK(v3[1].origin == &t2);
                                   ^
  test/util_tests.cpp:2516:15: note: move occurred here
      auto v3 = Vector(t1, std::move(t2));
                ^
  test/util_tests.cpp:2527:34: error: 't3' used after it was moved [bugprone-use-after-move,-warnings-as-errors]
      BOOST_CHECK(v4[2].origin == &t3);
                                   ^
  test/util_tests.cpp:2523:15: note: move occurred here
      auto v4 = Vector(std::move(v3[0]), v3[1], std::move(t3));
  ```

  See: https://releases.llvm.org/14.0.0/tools/clang/tools/extra/docs/clang-tidy/checks/bugprone-use-after-move.html

ACKs for top commit:
  ryanofsky:
    Code review ACK f345dc3. Only change since last review is switching to NOLINT directives

Tree-SHA512: afadecbaf1069653f4be5d6e66a5800ffd975c0b1a960057abc6367b616c181cd518897a874a8f3fd5e5e1f45fcc165f7a9a3171136cd4deee641214c4b765b8
@fanquake fanquake deleted the tidy_enable_bugprone_use_after_move branch August 31, 2022 07:28
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Oct 18, 2022
f345dc3 tidy: enable bugprone-use-after-move (fanquake)
94f2235 test: work around bugprone-use-after-move warnings in util tests (fanquake)

Pull request description:

  Would have caught bitcoin#25640.

  Currently `// NOLINT`s around:
  ```bash
  test/util_tests.cpp:2513:34: error: 't2' used after it was moved [bugprone-use-after-move,-warnings-as-errors]
      BOOST_CHECK(v2[0].origin == &t2);
                                   ^
  test/util_tests.cpp:2511:15: note: move occurred here
      auto v2 = Vector(std::move(t2));
                ^
  test/util_tests.cpp:2519:34: error: 't2' used after it was moved [bugprone-use-after-move,-warnings-as-errors]
      BOOST_CHECK(v3[1].origin == &t2);
                                   ^
  test/util_tests.cpp:2516:15: note: move occurred here
      auto v3 = Vector(t1, std::move(t2));
                ^
  test/util_tests.cpp:2527:34: error: 't3' used after it was moved [bugprone-use-after-move,-warnings-as-errors]
      BOOST_CHECK(v4[2].origin == &t3);
                                   ^
  test/util_tests.cpp:2523:15: note: move occurred here
      auto v4 = Vector(std::move(v3[0]), v3[1], std::move(t3));
  ```

  See: https://releases.llvm.org/14.0.0/tools/clang/tools/extra/docs/clang-tidy/checks/bugprone-use-after-move.html

ACKs for top commit:
  ryanofsky:
    Code review ACK f345dc3. Only change since last review is switching to NOLINT directives

Tree-SHA512: afadecbaf1069653f4be5d6e66a5800ffd975c0b1a960057abc6367b616c181cd518897a874a8f3fd5e5e1f45fcc165f7a9a3171136cd4deee641214c4b765b8
@bitcoin bitcoin locked and limited conversation to collaborators Aug 31, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants