feat(tidy): add fairmq-bugprone-use-after-send

- Report uses of a MessagePtr after a Send() whose result is discarded.
  After the send the message is empty with zeromq, and still points to
  the sent buffer with shmem, so the same code acts differently per
  transport.
- Walk the CFG from the send with ExprSequence, like
  bugprone-use-after-move. A loop back to the send counts as a use.
- Assignment, reset(), Rebuild(), a new declaration in a loop and
  passing by non-const reference (e.g. to Receive()) give a new message.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Claude
2026-11-09 23:14:01 +00:00
parent 3b3446bfcb
commit e9afe76155
6 changed files with 309 additions and 0 deletions

View File

@@ -34,6 +34,7 @@ The `fairmq-*` checks are a clang-tidy module ([Module.cxx](/source/fairmq/tidy/
| Check | Finds | Fix |
| --- | --- | --- |
| `fairmq-bugprone-unchecked-transfer-result` | Calls of `Receive()` whose result is not used. With the option `CheckSend`, also calls of `Send()`. | No fix |
| `fairmq-bugprone-use-after-send` | Uses of a `MessagePtr` after a `Send()` whose result is not used | No fix |
| `fairmq-modernize-channels-access` | Uses of the deprecated member `fair::mq::Device::fChannels` | `fChannels.at(name).at(index)` becomes `GetChannel(name, index)`. Other uses become `GetChannels()`. |
| `fairmq-modernize-deprecated-functions` | Calls of deprecated functions: `Device::GetStateName()`, `Device::GetTransitionName()`, `Socket::GetConstant()`, `Socket::Close()` | The free function, for example `fair::mq::GetStateName()`. No fix for `Close()`. |
| `fairmq-modernize-deprecated-headers` | `#include` lines of the deprecated headers, for example `<FairMQDevice.h>` | The new header, for example `<fairmq/Device.h>` |
@@ -152,6 +153,20 @@ CheckOptions:
fairmq-bugprone-unchecked-transfer-result.CheckSend: true
```
### fairmq-bugprone-use-after-send
After a successful `Send()`, the message is not yours anymore. With the zeromq transport, the message is empty. With the shmem transport, the message still points to the buffer that the receiver reads, so a write is a data race. The same code acts differently for each transport.
```cpp
auto msg(NewMessage(16));
Send(msg, "data");
std::memset(msg->GetData(), 0, 16); // reported
```
The check follows the control flow of the function, like the upstream check `bugprone-use-after-move`. A loop that comes back to the `Send()` with the same message is also a use. These statements give the variable a new message, and the check stops at them: an assignment, `reset()`, `Rebuild()`, a declaration in the loop, and a call that takes the message by non-const reference, for example `Receive(msg, ...)`.
The check looks only at a `Send()` whose result the code does not use. If the code checks the result, the send can have failed, and a use of the message can be correct.
## Build fairmq-tidy
Set the CMake option `BUILD_TIDY_TOOL` (default `OFF`) when you configure FairMQ: