Skip to content

Commit 87b7bbe

Browse files
committed
add clang tidy and fix warnings
1 parent 381612b commit 87b7bbe

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+563
-476
lines changed

.clang-tidy

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
Checks: [
2+
"-*",
3+
"bugprone-*",
4+
"cert-*",
5+
"clang-analyzer-*",
6+
"concurrency-*",
7+
"cppcoreguidelines-*",
8+
"misc-*",
9+
"modernize-*",
10+
"performance-*",
11+
"portability-*",
12+
"readability-*",
13+
"-bugprone-easily-swappable-parameters",
14+
"-bugprone-narrowing-conversions",
15+
"-cppcoreguidelines-avoid-c-arrays",
16+
"-cppcoreguidelines-avoid-magic-numbers",
17+
"-cppcoreguidelines-avoid-non-const-global-variables",
18+
"-cppcoreguidelines-pro-bounds-array-to-pointer-decay",
19+
"-cppcoreguidelines-pro-bounds-constant-array-index",
20+
"-cppcoreguidelines-pro-bounds-pointer-arithmetic",
21+
"-cppcoreguidelines-pro-type-union-access",
22+
"-cppcoreguidelines-pro-type-vararg",
23+
"-misc-no-recursion"
24+
]
25+
26+
27+
WarningsAsErrors: '-*,bugprone-*,cert-*,clang-analyzer-*,concurrency-*,cppcoreguidelines-*,misc-*,portability-*,readability-implicit-bool-conversion,-concurrency-mt-unsafe,-readability-function-cognitive-complexity'
28+
29+
CheckOptions:
30+
# ignore macros when computing the cyclomatic complexity. problem caused by RCLCPP LOG macros
31+
- key: readability-function-cognitive-complexity.IgnoreMacros
32+
value: 'true'
33+
34+
# This change makes it compatible with MISRA:2023 rule 4.14.1
35+
- key: misc-non-private-member-variables-in-classes.IgnoreClassesWithAllMemberVariablesBeingPublic
36+
value: 'true'
37+
38+
# Making a copy of a shared_ptr has a non-zero cost, but this cost is small.
39+
# Unfortunately the ROS API (subscriber callbacks) oblige the user to use callbacks functions that will trigger this warning
40+
# This is the reason wht the warning is silenced here
41+
- key: performance-unnecessary-value-param.AllowedTypes
42+
value: 'std::shared_ptr'
43+
44+
# Reference: https://clang.llvm.org/extra/clang-tidy/checks/readability/identifier-naming.html

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,6 @@ CMakeSettings.json
1818
CMakeUserPresets.json
1919

2020
tags
21+
/clang_tidy_output.log
22+
/.clang-tidy-venv/*
23+
/llvm.sh

.pre-commit-config.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,15 @@ repos:
4343
- id: clang-format
4444
args: ['-fallback-style=none', '-i']
4545

46+
# C++ static analysis (installs clang-tidy automatically via pip)
47+
# - repo: https://github.com/mxmlnrdr/clang_tidy_hook
48+
# rev: v0.3.1
49+
# hooks:
50+
# - id: clang-tidy
51+
# args:
52+
# - --config-file=.clang-tidy
53+
# - -p=build
54+
4655
# Spell check
4756
- repo: https://github.com/codespell-project/codespell
4857
rev: v2.4.1

include/behaviortree_cpp/actions/pop_from_queue.hpp

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -71,18 +71,12 @@ class PopFromQueue : public SyncActionNode
7171
{
7272
return NodeStatus::FAILURE;
7373
}
74-
else
75-
{
76-
T val = items.front();
77-
items.pop_front();
78-
setOutput("popped_item", val);
79-
return NodeStatus::SUCCESS;
80-
}
81-
}
82-
else
83-
{
84-
return NodeStatus::FAILURE;
74+
T val = items.front();
75+
items.pop_front();
76+
setOutput("popped_item", val);
77+
return NodeStatus::SUCCESS;
8578
}
79+
return NodeStatus::FAILURE;
8680
}
8781

8882
static PortsList providedPorts()
@@ -125,11 +119,8 @@ class QueueSize : public SyncActionNode
125119
{
126120
return NodeStatus::FAILURE;
127121
}
128-
else
129-
{
130-
setOutput("size", int(items.size()));
131-
return NodeStatus::SUCCESS;
132-
}
122+
setOutput("size", int(items.size()));
123+
return NodeStatus::SUCCESS;
133124
}
134125
return NodeStatus::FAILURE;
135126
}

include/behaviortree_cpp/actions/sleep_node.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class SleepNode : public StatefulActionNode
3535

3636
private:
3737
TimerQueue<> timer_;
38-
uint64_t timer_id_;
38+
uint64_t timer_id_ = 0;
3939

4040
std::atomic_bool timer_waiting_ = false;
4141
std::mutex delay_mutex_;

include/behaviortree_cpp/utils/convert_impl.hpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,19 @@ void convertNumber(const SRC& source, DST& target)
154154
throw std::runtime_error("Value is negative and can't be converted to unsigned");
155155
}
156156
}
157-
// these conversions are always safe:
157+
// these conversions are always safe (no check needed):
158158
// - same type
159159
// - float -> double
160-
if constexpr(is_same<SRC, DST>() || (is_same<SRC, float>() && is_same<DST, double>()))
160+
// - floating point to bool (C-style: any non-zero is true)
161+
if constexpr(is_same<SRC, DST>() || (is_same<SRC, float>() && is_same<DST, double>()) ||
162+
(std::is_floating_point<SRC>::value && is_same<DST, bool>()))
161163
{
162-
// No check needed
164+
target = static_cast<DST>(source);
165+
}
166+
// integer to bool: only 0 and 1 are valid
167+
else if constexpr(is_integer<SRC>() && is_same<DST, bool>())
168+
{
169+
checkLowerLimit<SRC, DST>(source);
163170
target = static_cast<DST>(source);
164171
}
165172
else if constexpr(both_integers)
@@ -179,11 +186,6 @@ void convertNumber(const SRC& source, DST& target)
179186
}
180187
target = static_cast<DST>(source);
181188
}
182-
// special case: bool accept truncation
183-
else if constexpr(is_convertible_to_bool<SRC>() && is_same<DST, bool>())
184-
{
185-
target = static_cast<DST>(source);
186-
}
187189
// casting to/from floating points might cause truncation.
188190
else if constexpr(std::is_floating_point<SRC>::value ||
189191
std::is_floating_point<DST>::value)

include/behaviortree_cpp/utils/locked_reference.hpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class LockedPtr
2525

2626
~LockedPtr()
2727
{
28-
if(mutex_)
28+
if(mutex_ != nullptr)
2929
{
3030
mutex_->unlock();
3131
}
@@ -34,16 +34,17 @@ class LockedPtr
3434
LockedPtr(LockedPtr const&) = delete;
3535
LockedPtr& operator=(LockedPtr const&) = delete;
3636

37-
LockedPtr(LockedPtr&& other)
37+
LockedPtr(LockedPtr&& other) noexcept
3838
{
3939
std::swap(ref_, other.ref_);
4040
std::swap(mutex_, other.mutex_);
4141
}
4242

43-
LockedPtr& operator=(LockedPtr&& other)
43+
LockedPtr& operator=(LockedPtr&& other) noexcept
4444
{
4545
std::swap(ref_, other.ref_);
4646
std::swap(mutex_, other.mutex_);
47+
return *this;
4748
}
4849

4950
operator bool() const
@@ -53,15 +54,15 @@ class LockedPtr
5354

5455
void lock()
5556
{
56-
if(mutex_)
57+
if(mutex_ != nullptr)
5758
{
5859
mutex_->lock();
5960
}
6061
}
6162

6263
void unlock()
6364
{
64-
if(mutex_)
65+
if(mutex_ != nullptr)
6566
{
6667
mutex_->unlock();
6768
}

include/behaviortree_cpp/utils/safe_any.hpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ class Any
7070
Any(const Any& other) : _any(other._any), _original_type(other._original_type)
7171
{}
7272

73-
Any(Any&& other) : _any(std::move(other._any)), _original_type(other._original_type)
73+
Any(Any&& other) noexcept
74+
: _any(std::move(other._any)), _original_type(other._original_type)
7475
{}
7576

7677
explicit Any(const double& value) : _any(value), _original_type(typeid(double))
@@ -117,6 +118,8 @@ class Any
117118

118119
Any& operator=(const Any& other);
119120

121+
Any& operator=(Any&& other) noexcept;
122+
120123
[[nodiscard]] bool isNumber() const;
121124

122125
[[nodiscard]] bool isIntegral() const;
@@ -305,7 +308,17 @@ inline bool isCastingSafe(const std::type_index& type, const T& val)
305308

306309
inline Any& Any::operator=(const Any& other)
307310
{
308-
this->_any = other._any;
311+
if(this != &other)
312+
{
313+
this->_any = other._any;
314+
this->_original_type = other._original_type;
315+
}
316+
return *this;
317+
}
318+
319+
inline Any& Any::operator=(Any&& other) noexcept
320+
{
321+
this->_any = std::move(other._any);
309322
this->_original_type = other._original_type;
310323
return *this;
311324
}

include/behaviortree_cpp/utils/simple_string.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ class SimpleString
5353
if(this != &other)
5454
{
5555
this->~SimpleString();
56-
// Ensure clean state before swap
5756
_storage = {};
5857
std::swap(_storage, other._storage);
5958
}

include/behaviortree_cpp/utils/wildcards.hpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
#pragma once
22

3+
#include <cstddef>
4+
#include <cstdint>
5+
#include <string_view>
6+
#include <vector>
7+
38
/**
49
* @file wildcards.hpp
510
* @brief Simple wildcard matching function supporting '*' and '?'.
@@ -35,7 +40,7 @@ inline bool wildcards_match(std::string_view str, std::string_view pattern)
3540
return cached == 1;
3641
}
3742

38-
bool result;
43+
bool result = false;
3944
if(pattern[j] == '*')
4045
{
4146
result = match_ref(match_ref, i, j + 1);

0 commit comments

Comments
 (0)