Skip to content

Commit 90a96ce

Browse files
authored
Define a preprocessor flag for enabling warnings on unused ConnectionHandle (#71)
* Define a preprocessor flag for enabling warnings on unused ConnectionHandle * add KDBINDINGS_ENABLE_WARN_UNUSED as a cmake option * move preprocessor macros directly in a standard header file * explicity casts the return value to void to ignore warning for CI
1 parent 457bc6e commit 90a96ce

File tree

10 files changed

+43
-25
lines changed

10 files changed

+43
-25
lines changed

examples/01-simple-connection/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ int main()
2222
Signal<std::string, int> signal;
2323

2424
// Connect a lambda
25-
signal.connect([](std::string arg1, int arg2) {
25+
(void)signal.connect([](std::string arg1, int arg2) {
2626
std::cout << arg1 << " " << arg2 << std::endl;
2727
});
2828

examples/04-simple-property/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ int main()
3535
{
3636
Widget w("A cool widget");
3737

38-
w.value.valueChanged().connect([](int newValue) {
38+
(void)w.value.valueChanged().connect([](int newValue) {
3939
std::cout << "The new value is " << newValue << std::endl;
4040
});
4141

examples/05-property-bindings/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ int main()
3131
Image img;
3232
std::cout << "The initial size of the image = " << img.byteSize.get() << " bytes" << std::endl;
3333

34-
img.byteSize.valueChanged().connect([](const int &newValue) {
34+
(void)img.byteSize.valueChanged().connect([](const int &newValue) {
3535
std::cout << "The new size of the image = " << newValue << " bytes" << std::endl;
3636
});
3737

examples/06-lazy-property-bindings/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ int main()
3333
Image img;
3434
std::cout << "The initial size of the image = " << img.byteSize.get() << " bytes" << std::endl;
3535

36-
img.byteSize.valueChanged().connect([](const int &newValue) {
36+
(void)img.byteSize.valueChanged().connect([](const int &newValue) {
3737
std::cout << "The new size of the image = " << newValue << " bytes" << std::endl;
3838
});
3939

src/kdbindings/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
# Contact KDAB at <[email protected]> for commercial licensing options.
77
#
88

9+
option(KDBINDINGS_ENABLE_WARN_UNUSED "Enable warnings for unused ConnectionHandles" ON)
10+
911
set(HEADERS
1012
binding.h
1113
binding_evaluator.h
@@ -20,6 +22,7 @@ set(HEADERS
2022
connection_evaluator.h
2123
connection_handle.h
2224
utils.h
25+
KDBindingsConfig.h
2326
)
2427

2528
if(CMAKE_VERSION VERSION_GREATER_EQUAL "3.19.0")
@@ -33,6 +36,11 @@ add_library(KDAB::KDBindings ALIAS KDBindings)
3336
set_target_properties(KDBindings PROPERTIES
3437
INTERFACE_COMPILE_FEATURES cxx_std_17
3538
)
39+
40+
if(KDBINDINGS_ENABLE_WARN_UNUSED)
41+
target_compile_definitions(KDBindings INTERFACE KDBINDINGS_ENABLE_WARN_UNUSED=1)
42+
endif()
43+
3644
target_include_directories(KDBindings INTERFACE
3745
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/..>
3846
$<INSTALL_INTERFACE:include>

src/kdbindings/KDBindingsConfig.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#pragma once
2+
3+
#ifdef KDBINDINGS_ENABLE_WARN_UNUSED
4+
#define KDBINDINGS_WARN_UNUSED [[nodiscard]]
5+
#else
6+
#define KDBINDINGS_WARN_UNUSED
7+
#endif

src/kdbindings/signal.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#pragma once
1313

14+
1415
#include <assert.h>
1516
#include <memory>
1617
#include <stdexcept>
@@ -21,6 +22,8 @@
2122
#include <kdbindings/genindex_array.h>
2223
#include <kdbindings/utils.h>
2324

25+
#include <kdbindings/KDBindingsConfig.h>
26+
2427
/**
2528
* @brief The main namespace of the KDBindings library.
2629
*
@@ -253,7 +256,7 @@ class Signal
253256
* @return An instance of ConnectionHandle, that can be used to disconnect
254257
* or temporarily block the connection.
255258
*/
256-
ConnectionHandle connect(std::function<void(Args...)> const &slot)
259+
KDBINDINGS_WARN_UNUSED ConnectionHandle connect(std::function<void(Args...)> const &slot)
257260
{
258261
ensureImpl();
259262

@@ -278,7 +281,7 @@ class Signal
278281
* The Signal class itself is not thread-safe. While the ConnectionEvaluator is inherently
279282
* thread-safe, ensure that any concurrent access to this Signal is protected externally to maintain thread safety.
280283
*/
281-
ConnectionHandle connectDeferred(const std::shared_ptr<ConnectionEvaluator> &evaluator, std::function<void(Args...)> const &slot)
284+
KDBINDINGS_WARN_UNUSED ConnectionHandle connectDeferred(const std::shared_ptr<ConnectionEvaluator> &evaluator, std::function<void(Args...)> const &slot)
282285
{
283286
ensureImpl();
284287

tests/binding/tst_binding.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,15 +185,15 @@ TEST_CASE("Binding expression updates")
185185
Private::makeNode(input)),
186186
evaluator)
187187
};
188-
prop1.valueChanged().connect([&ordering](int) { ordering.emplace_back(1); });
188+
(void)prop1.valueChanged().connect([&ordering](int) { ordering.emplace_back(1); });
189189

190190
auto prop2 = Property<int>{
191191
std::make_unique<Binding<int>>(
192192
Private::makeNode([](auto x) { return 3 * x; },
193193
Private::makeNode(input)),
194194
evaluator)
195195
};
196-
prop2.valueChanged().connect([&ordering](int) { ordering.emplace_back(2); });
196+
(void)prop2.valueChanged().connect([&ordering](int) { ordering.emplace_back(2); });
197197

198198
input = 8;
199199
evaluator.evaluateAll();

tests/property/tst_property.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class ClassWithProperty
7979

8080
ClassWithProperty()
8181
{
82-
property().valueChanged.connect(
82+
(void)property().valueChanged.connect(
8383
[this]() {
8484
this->changed.emit();
8585
});
@@ -92,7 +92,7 @@ TEST_CASE("An object with a Signal that is wrapped in a property can emit the Si
9292
auto handler = [&called]() { called = true; };
9393

9494
ClassWithProperty outer;
95-
outer.changed.connect(handler);
95+
(void)outer.changed.connect(handler);
9696

9797
outer.property().emitSignal();
9898

@@ -159,7 +159,7 @@ TEST_CASE("Signals")
159159
auto handler = [&notified]() { notified = true; };
160160

161161
auto p = new Property<int>{ 5 };
162-
p->destroyed().connect(handler);
162+
(void)p->destroyed().connect(handler);
163163

164164
delete p;
165165
REQUIRE(notified == true);
@@ -254,7 +254,7 @@ TEST_CASE("Property Updaters")
254254
updatedValue = value;
255255
slotCalled = true;
256256
};
257-
property.valueChanged().connect(handler);
257+
(void)property.valueChanged().connect(handler);
258258

259259
updater->set(123);
260260
REQUIRE(property.get() == 123);
@@ -299,7 +299,7 @@ TEST_CASE("Moving")
299299

300300
auto property = Property<std::unique_ptr<int>>{ std::make_unique<int>(42) };
301301
property.valueChanged().connect(handlerVoid);
302-
property.valueChanged().connect(handlerValue);
302+
(void)property.valueChanged().connect(handlerValue);
303303

304304
auto movedProperty{ std::move(property) };
305305
movedProperty.set(std::make_unique<int>(123));

tests/signal/tst_signal.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,13 @@ TEST_CASE("Signal connections")
138138
auto evaluator = std::make_shared<ConnectionEvaluator>();
139139

140140
std::thread thread1([&] {
141-
signal1.connectDeferred(evaluator, [&val](int value) {
141+
(void)signal1.connectDeferred(evaluator, [&val](int value) {
142142
val += value;
143143
});
144144
});
145145

146146
std::thread thread2([&] {
147-
signal2.connectDeferred(evaluator, [&val](int value) {
147+
(void)signal2.connectDeferred(evaluator, [&val](int value) {
148148
val += value;
149149
});
150150
});
@@ -169,11 +169,11 @@ TEST_CASE("Signal connections")
169169
int val2 = 4;
170170
auto evaluator = std::make_shared<ConnectionEvaluator>();
171171

172-
signal1.connectDeferred(evaluator, [&val1](int value) {
172+
(void)signal1.connectDeferred(evaluator, [&val1](int value) {
173173
val1 += value;
174174
});
175175

176-
signal2.connectDeferred(evaluator, [&val2](int value) {
176+
(void)signal2.connectDeferred(evaluator, [&val2](int value) {
177177
val2 += value;
178178
});
179179

@@ -224,7 +224,7 @@ TEST_CASE("Signal connections")
224224
int val = 4;
225225
auto evaluator = std::make_shared<ConnectionEvaluator>();
226226

227-
signal.connectDeferred(evaluator, [&val](int value) {
227+
(void)signal.connectDeferred(evaluator, [&val](int value) {
228228
val += value;
229229
});
230230

@@ -246,7 +246,7 @@ TEST_CASE("Signal connections")
246246
bool anotherCalled = false;
247247

248248
auto handle = signal->connectDeferred(evaluator, [&called]() { called = true; });
249-
anotherSignal.connectDeferred(evaluator, [&anotherCalled]() { anotherCalled = true; });
249+
(void)anotherSignal.connectDeferred(evaluator, [&anotherCalled]() { anotherCalled = true; });
250250

251251
signal->emit();
252252
anotherSignal.emit();
@@ -368,7 +368,7 @@ TEST_CASE("Signal connections")
368368
});
369369

370370
int lambdaCallCount2 = 0;
371-
signal.connect([&]() {
371+
(void)signal.connect([&]() {
372372
++lambdaCallCount2;
373373
});
374374

@@ -396,7 +396,7 @@ TEST_CASE("Signal connections")
396396
handle = &result;
397397

398398
int lambdaCallCount2 = 0;
399-
signal.connect([&]() {
399+
(void)signal.connect([&]() {
400400
++lambdaCallCount2;
401401
});
402402

@@ -413,12 +413,12 @@ TEST_CASE("Signal connections")
413413
{
414414
Signal<> signal;
415415
int lambdaCallCount = 0;
416-
signal.connect([&]() {
416+
(void)signal.connect([&]() {
417417
++lambdaCallCount;
418418
});
419419

420420
int lambdaCallCount2 = 0;
421-
signal.connect([&]() {
421+
(void)signal.connect([&]() {
422422
++lambdaCallCount2;
423423
});
424424

@@ -454,7 +454,7 @@ TEST_CASE("Moving")
454454
auto handler = [&count]() { ++count; };
455455

456456
Signal<> signal;
457-
signal.connect(handler);
457+
(void)signal.connect(handler);
458458

459459
Signal<> movedSignal{ std::move(signal) };
460460
movedSignal.emit();
@@ -467,7 +467,7 @@ TEST_CASE("Moving")
467467
auto handler = [&count]() { ++count; };
468468

469469
Signal<> signal;
470-
signal.connect(handler);
470+
(void)signal.connect(handler);
471471

472472
Signal<> movedSignal = std::move(signal);
473473
movedSignal.emit();

0 commit comments

Comments
 (0)