diff --git a/src/codec/mod.rs b/src/codec/mod.rs
index 148c865..3b5f6df 100644
--- a/src/codec/mod.rs
+++ b/src/codec/mod.rs
@@ -25,6 +25,7 @@ pub(crate) mod tcp;
 /// As defined by the spec for both RTU and TCP.
 const MAX_PDU_SIZE: usize = 253;
 
+#[cfg(any(test, feature = "rtu", feature = "tcp"))]
 #[allow(clippy::cast_possible_truncation)]
 fn u16_len(len: usize) -> u16 {
     // This type conversion should always be safe, because either