Files
FairMQ/test/tidy/fairmq-bugprone-run-loop-ignores-state.cxx
Claude 349573fb5f feat(tidy): add fairmq-bugprone-run-loop-ignores-state
- Report endless loops (while (true), for (;;), ...) in an override of
  Device::Run() that do not call NewStatePending() or WaitFor(). Such a
  device does not stop when a state change is requested.
- Skip loops that call Send() or Receive(), which an interrupt stops,
  and loops with a break, return, throw or goto.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 09:29:18 +00:00

56 lines
1.4 KiB
C++

#include <fairmq/Device.h>
#include <thread>
struct Endless : fair::mq::Device
{
void Run() override
{
int n = 0;
// CHECK-MESSAGES: :[[@LINE+1]]:9: warning: endless loop in 'Run()' does not check for a state change; the device does not stop on request; use 'while (!NewStatePending())' [fairmq-bugprone-run-loop-ignores-state]
while (true) {
++n;
}
// CHECK-MESSAGES: :[[@LINE+1]]:9: warning: endless loop in 'Run()' does not check for a state change; the device does not stop on request; use 'while (!NewStatePending())' [fairmq-bugprone-run-loop-ignores-state]
for (;;) {
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
}
};
// Not reported: the loops look at the state, transfer data or end.
struct Fine : fair::mq::Device
{
void Run() override
{
while (!NewStatePending()) {
}
while (true) {
if (NewStatePending()) {
break;
}
}
for (;;) {
if (!WaitFor(std::chrono::seconds(1))) {
return;
}
}
while (1) {
auto msg(NewMessage());
Send(msg, "data");
}
for (int i = 0; i < 10; ++i) {
}
}
};
// Not reported: not Run() of a device.
struct Other
{
void Run()
{
while (true) {
}
}
};