Skip to content

Commit e5cde87

Browse files
facontidavideclaude
andcommitted
Fix clang-tidy warnings across tests, examples, and samples
- Add anonymous namespaces for file-local static variables - Add Rule of Five (destructor/copy/move) for test fixtures - Fix implicit bool conversion (use != 0) - Add NOLINT comments for intentional patterns - Initialize array elements explicitly - Add void casts for intentionally unused return values - Add default member initializers where appropriate - Update run_clang_tidy.sh to include tests directory Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent facf5a0 commit e5cde87

16 files changed

Lines changed: 98 additions & 47 deletions

‎examples/t11_groot_howto.cpp‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ struct Position2D
2626
// the object to/from JSON.
2727
// You still need to call BT::RegisterJsonDefinition<Position2D>()
2828
// in main()
29+
// NOLINTNEXTLINE(misc-use-internal-linkage)
2930
BT_JSON_CONVERTER(Position2D, pos)
3031
{
3132
add_field("x", &pos.x);
@@ -39,6 +40,7 @@ struct Waypoint
3940
double speed = 1.0;
4041
};
4142

43+
// NOLINTNEXTLINE(misc-use-internal-linkage)
4244
BT_JSON_CONVERTER(Waypoint, wp)
4345
{
4446
add_field("name", &wp.name);
@@ -70,6 +72,7 @@ class UpdatePosition : public BT::SyncActionNode
7072

7173
// Helper to generate random offset [-range, +range]
7274
auto randOffset = [](double range) {
75+
// NOLINTNEXTLINE(cert-msc30-c,cert-msc50-cpp,concurrency-mt-unsafe)
7376
return (static_cast<double>(std::rand()) / RAND_MAX - 0.5) * 2.0 * range;
7477
};
7578

@@ -120,8 +123,9 @@ class UpdatePosition : public BT::SyncActionNode
120123
};
121124

122125
// clang-format off
123-
124-
static const char* xml_text = R"(
126+
namespace
127+
{
128+
const char* xml_text = R"(
125129
<root BTCPP_format="4">
126130
127131
<BehaviorTree ID="MainTree">
@@ -150,7 +154,7 @@ static const char* xml_text = R"(
150154
151155
</root>
152156
)";
153-
157+
} // namespace
154158
// clang-format on
155159

156160
int main()
@@ -165,7 +169,7 @@ int main()
165169
// Groot2 editor requires a model of your registered Nodes.
166170
// You don't need to write that by hand, it can be automatically
167171
// generated using the following command.
168-
const std::string xml_models = BT::writeTreeNodesModelXML(factory);
172+
[[maybe_unused]] const std::string xml_models = BT::writeTreeNodesModelXML(factory);
169173

170174
factory.registerBehaviorTreeFromText(xml_text);
171175

@@ -191,7 +195,7 @@ int main()
191195
BT::FileLogger2 logger2(tree, "t11_groot_howto.btlog");
192196
BT::MinitraceLogger minilog(tree, "minitrace.json");
193197

194-
while(1)
198+
while(true)
195199
{
196200
std::cout << "Start" << std::endl;
197201
cross_door.reset();

‎examples/t14_subtree_model.cpp‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ using namespace BT;
2222
*/
2323

2424
// clang-format off
25-
static const char* xml_subtree = R"(
25+
namespace
26+
{
27+
const char* xml_subtree = R"(
2628
<root BTCPP_format="4">
2729
2830
<TreeNodesModel>
@@ -48,7 +50,7 @@ static const char* xml_subtree = R"(
4850
* remapped. We will use the default values for the other two.
4951
*/
5052

51-
static const char* xml_maintree = R"(
53+
const char* xml_maintree = R"(
5254
<root BTCPP_format="4">
5355
5456
<BehaviorTree ID="MainTree">
@@ -62,6 +64,7 @@ static const char* xml_maintree = R"(
6264
6365
</root>
6466
)";
67+
} // namespace
6568

6669
// clang-format on
6770

‎examples/t15_nodes_mocking.cpp‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
#include "behaviortree_cpp/bt_factory.h"
44

55
// clang-format off
6-
7-
static const char* xml_text = R"(
6+
namespace
7+
{
8+
const char* xml_text = R"(
89
<root BTCPP_format="4">
910
1011
<BehaviorTree ID="MainTree">
@@ -33,7 +34,7 @@ static const char* xml_text = R"(
3334
3435
</root>
3536
)";
36-
37+
} // namespace
3738
// clang-format on
3839

3940
/**
@@ -44,7 +45,7 @@ static const char* xml_text = R"(
4445
* @return
4546
*/
4647

47-
int main(int argc, char** argv)
48+
int main(int /*argc*/, char** /*argv*/)
4849
{
4950
using namespace DummyNodes;
5051
BT::BehaviorTreeFactory factory;

‎examples/t16_global_blackboard.cpp‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ using namespace BT;
2525
*/
2626

2727
// clang-format off
28-
static const char* xml_main = R"(
28+
namespace
29+
{
30+
const char* xml_main = R"(
2931
<root BTCPP_format="4">
3032
3133
<BehaviorTree ID="MainTree">
@@ -43,7 +45,7 @@ static const char* xml_main = R"(
4345
</BehaviorTree>
4446
</root>
4547
)";
46-
48+
} // namespace
4749
// clang-format on
4850

4951
class PrintNumber : public BT::SyncActionNode

‎fuzzing/script_fuzzer.cpp‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,21 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
5656
env.vars->set("result", result);
5757

5858
BT::Any read_back;
59-
env.vars->get("result", read_back);
59+
(void)env.vars->get("result", read_back);
60+
}
61+
catch(const BT::RuntimeError&) // NOLINT(bugprone-empty-catch)
62+
{
63+
// Intentionally empty - fuzzer expects exceptions
6064
}
61-
catch(const BT::RuntimeError&)
62-
{}
6365
}
6466
}
6567

6668
BT::ParseScriptAndExecute(env, script);
6769
}
68-
catch(const std::exception&)
69-
{}
70+
catch(const std::exception&) // NOLINT(bugprone-empty-catch)
71+
{
72+
// Intentionally empty - fuzzer expects exceptions
73+
}
7074

7175
return 0;
7276
}

‎run_clang_tidy.sh‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ if [[ "${1:-}" == "--help" ]]; then
2929
fi
3030

3131

32-
clang_tidy_paths="$ws_dir/src $ws_dir/include"
32+
clang_tidy_paths="$ws_dir/src $ws_dir/include $ws_dir/tests"
3333
cmake_build_path="$ws_dir/${1:-build}"
3434

3535
skip_list=(

‎sample_nodes/dummy_nodes.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
// This function must be implemented in the .cpp file to create
44
// a plugin that can be loaded at run-time
5+
// NOLINTNEXTLINE(misc-use-anonymous-namespace)
56
BT_REGISTER_NODES(factory)
67
{
78
DummyNodes::RegisterNodes(factory);

‎sample_nodes/dummy_nodes.h‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,14 @@ NodeStatus SayHello();
1717
class GripperInterface
1818
{
1919
public:
20-
GripperInterface() : _opened(true)
21-
{}
20+
GripperInterface() = default;
2221

2322
NodeStatus open();
2423

2524
NodeStatus close();
2625

2726
private:
28-
bool _opened;
27+
bool _opened = true;
2928
};
3029

3130
//--------------------------------------

‎sample_nodes/movebase_node.h‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@
66
// Custom type
77
struct Pose2D
88
{
9-
double x, y, theta;
9+
double x = 0;
10+
double y = 0;
11+
double theta = 0;
1012
};
1113

1214
// Add this to you main() to register this function into JsonExporter:
1315
//
1416
// BT::JsonExporter::get().addConverter<Pose2D>();
1517

18+
// NOLINTNEXTLINE(misc-use-internal-linkage)
1619
BT_JSON_CONVERTER(Pose2D, pose)
1720
{
1821
add_field("x", &pose.x);

‎tests/gtest_factory.cpp‎

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212

1313
using namespace BT;
1414

15+
namespace
16+
{
1517
// clang-format off
1618

17-
static const char* xml_text = R"(
19+
const char* xml_text = R"(
1820
1921
<root BTCPP_format="4" >
2022
@@ -51,7 +53,7 @@ static const char* xml_text = R"(
5153
</root>
5254
)";
5355

54-
static const char* xml_text_subtree = R"(
56+
const char* xml_text_subtree = R"(
5557
5658
<root BTCPP_format="4" main_tree_to_execute="MainTree" >
5759
@@ -79,7 +81,7 @@ static const char* xml_text_subtree = R"(
7981
8082
</root> )";
8183

82-
static const char* xml_text_subtree_part1 = R"(
84+
const char* xml_text_subtree_part1 = R"(
8385
8486
<root BTCPP_format="4">
8587
<BehaviorTree ID="MainTree">
@@ -90,7 +92,7 @@ static const char* xml_text_subtree_part1 = R"(
9092
</BehaviorTree>
9193
</root> )";
9294

93-
static const char* xml_text_subtree_part2 = R"(
95+
const char* xml_text_subtree_part2 = R"(
9496
9597
<root BTCPP_format="4">
9698
<BehaviorTree ID="DoorClosedSubtree">
@@ -105,6 +107,7 @@ static const char* xml_text_subtree_part2 = R"(
105107
</root> )";
106108

107109
// clang-format on
110+
} // namespace
108111

109112
TEST(BehaviorTreeFactory, NotRegisteredNode)
110113
{
@@ -189,9 +192,11 @@ TEST(BehaviorTreeFactory, Issue7)
189192
EXPECT_THROW(parser.loadFromText(xml_text_issue), RuntimeError);
190193
}
191194

195+
namespace
196+
{
192197
// clang-format off
193198

194-
static const char* xml_ports_subtree = R"(
199+
const char* xml_ports_subtree = R"(
195200
196201
<root BTCPP_format="4" main_tree_to_execute="MainTree">
197202
@@ -217,6 +222,7 @@ static const char* xml_ports_subtree = R"(
217222
</root> )";
218223

219224
// clang-format on
225+
} // namespace
220226

221227
TEST(BehaviorTreeFactory, SubTreeWithRemapping)
222228
{
@@ -268,6 +274,8 @@ TEST(BehaviorTreeFactory, SubTreeWithRemapping)
268274
ASSERT_FALSE(talk_bb->getAnyLocked("talk_out"));
269275
}
270276

277+
namespace
278+
{
271279
std::string FilePath(const std::filesystem::path& relative_path)
272280
{
273281
// clang-format off
@@ -285,6 +293,7 @@ std::string FilePath(const std::filesystem::path& relative_path)
285293
}
286294
return {};
287295
}
296+
} // namespace
288297

289298
TEST(BehaviorTreeFactory, CreateTreeFromFile)
290299
{
@@ -398,13 +407,16 @@ TEST(BehaviorTreeReload, ReloadSameTree)
398407
}
399408
}
400409

410+
namespace
411+
{
401412
KeyValueVector makeTestMetadata()
402413
{
403414
return {
404415
std::make_pair<std::string, std::string>("foo", "hello"),
405416
std::make_pair<std::string, std::string>("bar", "42"),
406417
};
407418
}
419+
} // namespace
408420

409421
class ActionWithMetadata : public SyncActionNode
410422
{
@@ -548,7 +560,7 @@ TEST(BehaviorTreeFactory, FactoryDestroyedBeforeTick)
548560
{
549561
const auto* manifest_ptr =
550562
static_cast<const BT::TreeNode*>(node.get())->config().manifest;
551-
if(manifest_ptr)
563+
if(manifest_ptr != nullptr)
552564
{
553565
auto it = tree.manifests.find(manifest_ptr->registration_ID);
554566
ASSERT_NE(it, tree.manifests.end());

0 commit comments

Comments
 (0)