Skip to content

Commit 2af0b13

Browse files
Fix clang-tidy warnings in test code
- Cast uint16_t CRC values to unsigned before bitwise shifts to avoid hicpp-signed-bitwise (uint16_t promotes to signed int) - Initialize local variable to satisfy cppcoreguidelines-init-variables - Add parentheses around MEGA multiplications for readability-math-missing-parentheses - Split multiple declarations to satisfy readability-isolate-declaration - Use unsigned loop variable to avoid bugprone-too-small-loop-variable Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 4ade2c5 commit 2af0b13

File tree

6 files changed

+49
-36
lines changed

6 files changed

+49
-36
lines changed

tests/src/test_api_lifecycle.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,8 @@ static uint16_t crc16_add(uint16_t crc, const void* data, const size_t size)
225225
{
226226
const auto* p = static_cast<const uint_least8_t*>(data);
227227
for (size_t i = 0; i < size; i++) {
228-
crc = static_cast<uint16_t>((crc << 8U) ^ crc_table[((crc >> 8U) ^ p[i]) & 0xFFU]);
228+
crc = static_cast<uint16_t>((static_cast<unsigned>(crc) << 8U) ^
229+
crc_table[(static_cast<unsigned>(crc) >> 8U) ^ p[i]]);
229230
}
230231
return crc;
231232
}
@@ -819,7 +820,7 @@ static void test_redundant_rx_dedup_multiframe()
819820
// Frame 2: 1 payload byte + CRC hi + CRC lo + padding(4) + tail (SOT=0, EOT=1, toggle=0, TID=2).
820821
uint_least8_t frame2[8];
821822
frame2[0] = payload[7];
822-
frame2[1] = static_cast<uint_least8_t>((crc >> 8U) & 0xFFU);
823+
frame2[1] = static_cast<uint_least8_t>((static_cast<unsigned>(crc) >> 8U) & 0xFFU);
823824
frame2[2] = static_cast<uint_least8_t>(crc & 0xFFU);
824825
frame2[3] = 0x00U; // padding
825826
frame2[4] = 0x00U;

tests/src/test_api_roundtrip.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ static uint16_t crc16_ccitt(uint16_t crc, const void* data, const size_t len)
5757
{
5858
const auto* p = static_cast<const uint_least8_t*>(data);
5959
for (size_t i = 0; i < len; i++) {
60-
crc = static_cast<uint16_t>((crc << 8U) ^ crc_lut[((crc >> 8U) ^ p[i]) & 0xFFU]);
60+
crc = static_cast<uint16_t>((static_cast<unsigned>(crc) << 8U) ^
61+
crc_lut[(static_cast<unsigned>(crc) >> 8U) ^ p[i]]);
6162
}
6263
return crc;
6364
}
@@ -731,16 +732,23 @@ static void test_roundtrip_all_transfer_ids()
731732
&rx_inst, &sub, 700U, false, EXTENT, CANARD_DEFAULT_TRANSFER_ID_TIMEOUT_us, &roundtrip_sub_vtable));
732733
sub.user_context = &rx_cap;
733734

734-
for (uint_least8_t tid = 0; tid <= CANARD_TRANSFER_ID_MAX; tid++) {
735+
for (unsigned tid = 0; tid <= CANARD_TRANSFER_ID_MAX; tid++) {
735736
tx_cap = full_tx_capture_t{};
736737
rx_cap = rx_capture_t{};
737738
// Advance the RX clock past the TID timeout so same TID is accepted again.
738739
rx_ctx.now_val = static_cast<canard_us_t>(tid) * (CANARD_DEFAULT_TRANSFER_ID_TIMEOUT_us + 1);
739740

740-
const uint_least8_t payload_data[1] = { static_cast<uint_least8_t>(tid) };
741+
const uint_least8_t payload_data[1] = { static_cast<uint_least8_t>(tid & 0xFFU) };
741742
const canard_bytes_chain_t payload = { .bytes = { .size = 1, .data = payload_data }, .next = nullptr };
742-
TEST_ASSERT_TRUE(canard_publish(
743-
&tx_inst, DEADLINE + rx_ctx.now_val, 1U, canard_prio_nominal, 700U, false, tid, payload, nullptr));
743+
TEST_ASSERT_TRUE(canard_publish(&tx_inst,
744+
DEADLINE + rx_ctx.now_val,
745+
1U,
746+
canard_prio_nominal,
747+
700U,
748+
false,
749+
static_cast<uint_least8_t>(tid),
750+
payload,
751+
nullptr));
744752

745753
canard_poll(&tx_inst, 1U);
746754
feed_captured_frames(&rx_inst, tx_cap, rx_ctx.now_val);

tests/src/test_api_rx_edge.cpp

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ static const uint16_t crc_lut[256] = {
5252
static uint16_t crc16_ccitt(uint16_t crc, const uint_least8_t* data, size_t len)
5353
{
5454
for (size_t i = 0; i < len; i++) {
55-
crc = static_cast<uint16_t>((crc << 8U) ^ crc_lut[((crc >> 8U) ^ data[i]) & 0xFFU]);
55+
crc = static_cast<uint16_t>((static_cast<unsigned>(crc) << 8U) ^
56+
crc_lut[(static_cast<unsigned>(crc) >> 8U) ^ data[i]]);
5657
}
5758
return crc;
5859
}
@@ -232,7 +233,7 @@ static void test_rx_v1_multiframe_2frame_classic()
232233
const uint_least8_t padding[4] = { 0, 0, 0, 0 };
233234
crc = crc16_ccitt(0xFFFFU, payload, 8U);
234235
crc = crc16_ccitt(crc, padding, 4U);
235-
const uint_least8_t crc_hi = static_cast<uint_least8_t>((crc >> 8U) & 0xFFU);
236+
const uint_least8_t crc_hi = static_cast<uint_least8_t>((static_cast<unsigned>(crc) >> 8U) & 0xFFU);
236237
const uint_least8_t crc_lo = static_cast<uint_least8_t>(crc & 0xFFU);
237238

238239
uint_least8_t frame2[8];
@@ -296,7 +297,7 @@ static void test_rx_v1_multiframe_3frame()
296297
const uint_least8_t pad[4] = { 0, 0, 0, 0 };
297298
uint16_t crc = crc16_ccitt(0xFFFFU, payload, 15U);
298299
crc = crc16_ccitt(crc, pad, 4U);
299-
const uint_least8_t crc_hi = static_cast<uint_least8_t>((crc >> 8U) & 0xFFU);
300+
const uint_least8_t crc_hi = static_cast<uint_least8_t>((static_cast<unsigned>(crc) >> 8U) & 0xFFU);
300301
const uint_least8_t crc_lo = static_cast<uint_least8_t>(crc & 0xFFU);
301302

302303
uint_least8_t frame1[8];
@@ -370,7 +371,7 @@ static void test_rx_v1_multiframe_fd()
370371
const uint_least8_t pad[52] = {};
371372
uint16_t crc = crc16_ccitt(0xFFFFU, payload, 70U);
372373
crc = crc16_ccitt(crc, pad, 52U);
373-
const uint_least8_t crc_hi = static_cast<uint_least8_t>((crc >> 8U) & 0xFFU);
374+
const uint_least8_t crc_hi = static_cast<uint_least8_t>((static_cast<unsigned>(crc) >> 8U) & 0xFFU);
374375
const uint_least8_t crc_lo = static_cast<uint_least8_t>(crc & 0xFFU);
375376

376377
// Frame 1 (64 bytes).
@@ -407,7 +408,7 @@ static void test_rx_v1_multiframe_fd()
407408

408409
// Recompute CRC without padding.
409410
crc = crc16_ccitt(0xFFFFU, payload, 70U);
410-
const uint_least8_t crc_hi2 = static_cast<uint_least8_t>((crc >> 8U) & 0xFFU);
411+
const uint_least8_t crc_hi2 = static_cast<uint_least8_t>((static_cast<unsigned>(crc) >> 8U) & 0xFFU);
411412
const uint_least8_t crc_lo2 = static_cast<uint_least8_t>(crc & 0xFFU);
412413

413414
// Frame 2 (10 bytes): 7 payload + CRC(2) + tail.
@@ -456,7 +457,7 @@ static void test_rx_multiframe_crc_error()
456457
crc = crc16_ccitt(crc, pad, 4U);
457458

458459
// Corrupt the CRC by flipping the low byte.
459-
const uint_least8_t bad_crc_hi = static_cast<uint_least8_t>((crc >> 8U) & 0xFFU);
460+
const uint_least8_t bad_crc_hi = static_cast<uint_least8_t>((static_cast<unsigned>(crc) >> 8U) & 0xFFU);
460461
const uint_least8_t bad_crc_lo = static_cast<uint_least8_t>((crc & 0xFFU) ^ 0x01U); // flipped bit
461462

462463
uint_least8_t frame1[8];
@@ -507,7 +508,7 @@ static void test_rx_multiframe_single_bit_flip()
507508
const uint_least8_t pad[4] = {};
508509
uint16_t crc = crc16_ccitt(0xFFFFU, payload, 8U);
509510
crc = crc16_ccitt(crc, pad, 4U);
510-
const uint_least8_t crc_hi = static_cast<uint_least8_t>((crc >> 8U) & 0xFFU);
511+
const uint_least8_t crc_hi = static_cast<uint_least8_t>((static_cast<unsigned>(crc) >> 8U) & 0xFFU);
511512
const uint_least8_t crc_lo = static_cast<uint_least8_t>(crc & 0xFFU);
512513

513514
// Frame 1 with a single bit flip in byte 3.
@@ -596,7 +597,7 @@ static void test_rx_extent_truncation_multiframe()
596597
const uint_least8_t pad[4] = {};
597598
uint16_t crc = crc16_ccitt(0xFFFFU, payload, 8U);
598599
crc = crc16_ccitt(crc, pad, 4U);
599-
const uint_least8_t crc_hi = static_cast<uint_least8_t>((crc >> 8U) & 0xFFU);
600+
const uint_least8_t crc_hi = static_cast<uint_least8_t>((static_cast<unsigned>(crc) >> 8U) & 0xFFU);
600601
const uint_least8_t crc_lo = static_cast<uint_least8_t>(crc & 0xFFU);
601602

602603
uint_least8_t frame1[8];
@@ -789,7 +790,7 @@ static void test_rx_priority_preemption()
789790
uint_least8_t frame2_lo[8];
790791
frame2_lo[0] = payload_lo[7];
791792
std::memset(&frame2_lo[1], 0, 4);
792-
frame2_lo[5] = static_cast<uint_least8_t>((crc_lo_full >> 8U) & 0xFFU);
793+
frame2_lo[5] = static_cast<uint_least8_t>((static_cast<unsigned>(crc_lo_full) >> 8U) & 0xFFU);
793794
frame2_lo[6] = static_cast<uint_least8_t>(crc_lo_full & 0xFFU);
794795
frame2_lo[7] = make_v1_tail(false, true, false, 0U);
795796

@@ -805,7 +806,7 @@ static void test_rx_priority_preemption()
805806
uint_least8_t frame2_hi[8];
806807
frame2_hi[0] = payload_hi[7];
807808
std::memset(&frame2_hi[1], 0, 4);
808-
frame2_hi[5] = static_cast<uint_least8_t>((crc_hi_full >> 8U) & 0xFFU);
809+
frame2_hi[5] = static_cast<uint_least8_t>((static_cast<unsigned>(crc_hi_full) >> 8U) & 0xFFU);
809810
frame2_hi[6] = static_cast<uint_least8_t>(crc_hi_full & 0xFFU);
810811
frame2_hi[7] = make_v1_tail(false, true, false, 1U);
811812

@@ -855,21 +856,24 @@ static void test_rx_priority_preemption_interleaved()
855856
uint16_t crc_lo = crc16_ccitt(crc16_ccitt(0xFFFFU, payload_lo, 8U), pad, 4U);
856857
uint16_t crc_hi = crc16_ccitt(crc16_ccitt(0xFFFFU, payload_hi, 8U), pad, 4U);
857858

858-
uint_least8_t f1_lo[8], f2_lo[8], f1_hi[8], f2_hi[8];
859+
uint_least8_t f1_lo[8];
860+
uint_least8_t f2_lo[8];
861+
uint_least8_t f1_hi[8];
862+
uint_least8_t f2_hi[8];
859863

860864
std::memcpy(f1_lo, payload_lo, 7U);
861865
f1_lo[7] = make_v1_tail(true, false, true, 0U);
862866
f2_lo[0] = payload_lo[7];
863867
std::memset(&f2_lo[1], 0, 4);
864-
f2_lo[5] = static_cast<uint_least8_t>((crc_lo >> 8U) & 0xFFU);
868+
f2_lo[5] = static_cast<uint_least8_t>((static_cast<unsigned>(crc_lo) >> 8U) & 0xFFU);
865869
f2_lo[6] = static_cast<uint_least8_t>(crc_lo & 0xFFU);
866870
f2_lo[7] = make_v1_tail(false, true, false, 0U);
867871

868872
std::memcpy(f1_hi, payload_hi, 7U);
869873
f1_hi[7] = make_v1_tail(true, false, true, 1U);
870874
f2_hi[0] = payload_hi[7];
871875
std::memset(&f2_hi[1], 0, 4);
872-
f2_hi[5] = static_cast<uint_least8_t>((crc_hi >> 8U) & 0xFFU);
876+
f2_hi[5] = static_cast<uint_least8_t>((static_cast<unsigned>(crc_hi) >> 8U) & 0xFFU);
873877
f2_hi[6] = static_cast<uint_least8_t>(crc_hi & 0xFFU);
874878
f2_hi[7] = make_v1_tail(false, true, false, 1U);
875879

@@ -1015,7 +1019,7 @@ static void test_rx_wrong_toggle_rejected()
10151019
const uint_least8_t pad[4] = {};
10161020
uint16_t crc = crc16_ccitt(0xFFFFU, payload, 8U);
10171021
crc = crc16_ccitt(crc, pad, 4U);
1018-
const uint_least8_t crc_hi = static_cast<uint_least8_t>((crc >> 8U) & 0xFFU);
1022+
const uint_least8_t crc_hi = static_cast<uint_least8_t>((static_cast<unsigned>(crc) >> 8U) & 0xFFU);
10191023
const uint_least8_t crc_lo = static_cast<uint_least8_t>(crc & 0xFFU);
10201024

10211025
// Frame 1 (SOT): toggle=1 (correct for v1).
@@ -1063,7 +1067,7 @@ static void test_rx_interface_affinity()
10631067
const uint_least8_t pad[4] = {};
10641068
uint16_t crc = crc16_ccitt(0xFFFFU, payload, 8U);
10651069
crc = crc16_ccitt(crc, pad, 4U);
1066-
const uint_least8_t crc_hi = static_cast<uint_least8_t>((crc >> 8U) & 0xFFU);
1070+
const uint_least8_t crc_hi = static_cast<uint_least8_t>((static_cast<unsigned>(crc) >> 8U) & 0xFFU);
10671071
const uint_least8_t crc_lo = static_cast<uint_least8_t>(crc & 0xFFU);
10681072

10691073
uint_least8_t frame1[8];
@@ -1183,10 +1187,10 @@ static void test_rx_v0_multiframe_roundtrip()
11831187
// = crc_lo | (crc_hi << 8) = v0crc. Correct!
11841188

11851189
uint_least8_t frame1[8];
1186-
frame1[0] = static_cast<uint_least8_t>(v0crc & 0xFFU); // crc_lo
1187-
frame1[1] = static_cast<uint_least8_t>((v0crc >> 8U) & 0xFFU); // crc_hi
1188-
std::memcpy(&frame1[2], payload, 5U); // first 5 payload bytes
1189-
frame1[7] = make_v0_tail(true, false, false, 2U); // SOT=1, EOT=0, toggle=0 (v0 starts toggle=0)
1190+
frame1[0] = static_cast<uint_least8_t>(v0crc & 0xFFU); // crc_lo
1191+
frame1[1] = static_cast<uint_least8_t>((static_cast<unsigned>(v0crc) >> 8U) & 0xFFU); // crc_hi
1192+
std::memcpy(&frame1[2], payload, 5U); // first 5 payload bytes
1193+
frame1[7] = make_v0_tail(true, false, false, 2U); // SOT=1, EOT=0, toggle=0 (v0 starts toggle=0)
11901194

11911195
uint_least8_t frame2[4];
11921196
std::memcpy(frame2, &payload[5], 3U); // remaining 3 payload bytes

tests/src/test_intrusive_rx_session.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2103,7 +2103,7 @@ static void test_transfer_id_rollover_31_to_0(void)
21032103
TEST_ASSERT_EQUAL_UINT8(0, fx.capture.transfer_id);
21042104

21052105
// Duplicate TID=0 → rejected (same TID, same prio, within timeout).
2106-
TEST_ASSERT_TRUE(feed(&fx, 2 * MEGA + 1, &fr0, 0));
2106+
TEST_ASSERT_TRUE(feed(&fx, (2 * MEGA) + 1, &fr0, 0));
21072107
TEST_ASSERT_EQUAL_size_t(2, fx.capture.call_count); // No new callback.
21082108

21092109
fixture_destroy_all_sessions(&fx);
@@ -2131,7 +2131,7 @@ static void test_session_timeout_exact_boundary(void)
21312131

21322132
// At ts=3000001: stale = (3000001 - 2000000) > 1000000 = true. Stale.
21332133
// affine && stale → 2 of 3 → admit.
2134-
TEST_ASSERT_TRUE(feed(&fx, 3 * MEGA + 1, &fr5, 0));
2134+
TEST_ASSERT_TRUE(feed(&fx, (3 * MEGA) + 1, &fr5, 0));
21352135
TEST_ASSERT_EQUAL_size_t(2, fx.capture.call_count);
21362136
TEST_ASSERT_EQUAL_UINT8(5, fx.capture.transfer_id);
21372137

tests/src/test_intrusive_tx.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,15 +1158,15 @@ static void test_tx_spool_crc_split_across_frames(void)
11581158
for (size_t i = 0; i < 6U; i++) {
11591159
TEST_ASSERT_EQUAL_HEX8(data[7U + i], f2->data[i]);
11601160
}
1161-
TEST_ASSERT_EQUAL_HEX8((byte_t)((crc >> 8U) & 0xFFU), f2->data[6]); // CRC high byte
1161+
TEST_ASSERT_EQUAL_HEX8((byte_t)(((unsigned)crc >> 8U) & 0xFFU), f2->data[6]); // CRC high byte
11621162
TEST_ASSERT_EQUAL_HEX8(2U, f2->data[7] & (TAIL_SOT | TAIL_EOT | TAIL_TOGGLE | CANARD_TRANSFER_ID_MAX));
11631163
// toggle=0 (second frame)
11641164

11651165
// Frame 3: CRC low byte + tail. Frame size = tx_ceil(1+1) = 2.
11661166
const tx_frame_t* const f3 = f2->next;
11671167
TEST_ASSERT_NOT_NULL(f3);
11681168
TEST_ASSERT_EQUAL_size_t(2U, canard_dlc_to_len[f3->dlc]);
1169-
TEST_ASSERT_EQUAL_HEX8((byte_t)(crc & 0xFFU), f3->data[0]); // CRC low byte
1169+
TEST_ASSERT_EQUAL_HEX8((byte_t)((unsigned)crc & 0xFFU), f3->data[0]); // CRC low byte
11701170
TEST_ASSERT_EQUAL_HEX8(TAIL_EOT | TAIL_TOGGLE | 2U, f3->data[1] & 0xFFU); // EOT, toggle=1 (third frame)
11711171

11721172
// Cleanup.
@@ -1471,8 +1471,8 @@ static void test_tx_spool_v0_crc_byte_order(void)
14711471
TEST_ASSERT_NOT_NULL(head);
14721472
// v0 prepends CRC in LE: the first 2 bytes of the stream are [crc_low, crc_high].
14731473
// Frame 1 data[0..6] are the first 7 stream bytes. Stream = [crc_lo, crc_hi, payload...].
1474-
TEST_ASSERT_EQUAL_HEX8((byte_t)(crc & 0xFFU), head->data[0]);
1475-
TEST_ASSERT_EQUAL_HEX8((byte_t)((crc >> 8U) & 0xFFU), head->data[1]);
1474+
TEST_ASSERT_EQUAL_HEX8((byte_t)((unsigned)crc & 0xFFU), head->data[0]);
1475+
TEST_ASSERT_EQUAL_HEX8((byte_t)(((unsigned)crc >> 8U) & 0xFFU), head->data[1]);
14761476

14771477
// Cleanup.
14781478
tx_frame_t* f = head;
@@ -1678,8 +1678,8 @@ static void test_tx_predict_frame_count_exhaustive(void)
16781678
for (size_t mi = 0; mi < sizeof(mtus) / sizeof(mtus[0]); mi++) {
16791679
const size_t mtu = mtus[mi];
16801680
for (size_t si = 0; si < sizeof(sizes) / sizeof(sizes[0]); si++) {
1681-
const size_t sz = sizes[si];
1682-
size_t expected;
1681+
const size_t sz = sizes[si];
1682+
size_t expected = 0U;
16831683
if (sz < mtu) {
16841684
expected = 1U;
16851685
} else {

tests/src/test_intrusive_util.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -633,8 +633,8 @@ static void test_crc_residue_property(void)
633633
// Append CRC in big-endian order.
634634
uint8_t augmented[sizeof(data) + CRC_SIZE_BYTES];
635635
memcpy(augmented, data, sizeof(data));
636-
augmented[sizeof(data)] = (uint8_t)(crc >> 8U); // high byte
637-
augmented[sizeof(data) + 1] = (uint8_t)(crc & 0xFFU); // low byte
636+
augmented[sizeof(data)] = (uint8_t)((unsigned)crc >> 8U); // high byte
637+
augmented[sizeof(data) + 1] = (uint8_t)((unsigned)crc & 0xFFU); // low byte
638638
const uint16_t residue = crc_add(CRC_INITIAL, sizeof(augmented), augmented);
639639
TEST_ASSERT_EQUAL_HEX16(CRC_RESIDUE, residue);
640640

0 commit comments

Comments
 (0)