Skip to content

Commit 76ae984

Browse files
kpumukcodex
andauthored
Enable RuboCop Style/UnpackFirst (#3722)
Client: rb Co-authored-by: OpenAI Codex (GPT-5.6) <codex@openai.com>
1 parent 759b0fa commit 76ae984

10 files changed

Lines changed: 22 additions & 19 deletions

File tree

lib/rb/.rubocop.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,3 +250,6 @@ Style/TrailingCommaInArrayLiteral:
250250
Style/TrailingCommaInHashLiteral:
251251
Enabled: true
252252
EnforcedStyleForMultiline: comma
253+
254+
Style/UnpackFirst:
255+
Enabled: true

lib/rb/lib/thrift/protocol/binary_protocol.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ def read_byte
214214

215215
def read_i16
216216
trans.read_into_buffer(@rbuf, 2)
217-
val, = @rbuf.unpack("n")
217+
val = @rbuf.unpack1("n")
218218
if (val > 0x7fff)
219219
val = 0 - ((val - 1) ^ 0xffff)
220220
end
@@ -223,7 +223,7 @@ def read_i16
223223

224224
def read_i32
225225
trans.read_into_buffer(@rbuf, 4)
226-
val, = @rbuf.unpack("N")
226+
val = @rbuf.unpack1("N")
227227
if (val > 0x7fffffff)
228228
val = 0 - ((val - 1) ^ 0xffffffff)
229229
end
@@ -244,7 +244,7 @@ def read_i64
244244

245245
def read_double
246246
trans.read_into_buffer(@rbuf, 8)
247-
val = @rbuf.unpack("G").first
247+
val = @rbuf.unpack1("G")
248248
val
249249
end
250250

lib/rb/lib/thrift/protocol/compact_protocol.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
module Thrift
2222
class CompactProtocol < BaseProtocol
23-
PROTOCOL_ID = [0x82].pack("c").unpack("c").first
23+
PROTOCOL_ID = [0x82].pack("c").unpack1("c")
2424
VERSION = 1
2525
VERSION_MASK = 0x1f
2626
TYPE_MASK = 0xE0
@@ -410,7 +410,7 @@ def read_i64
410410

411411
def read_double
412412
trans.read_into_buffer(@rbuf, 8)
413-
val = @rbuf.reverse.unpack("G").first
413+
val = @rbuf.reverse.unpack1("G")
414414
val
415415
end
416416

lib/rb/lib/thrift/server/nonblocking_server.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ def close_signal_pipes
279279

280280
def slice_frame!(buf)
281281
if buf.length >= 4
282-
size = buf.unpack("N").first
282+
size = buf.unpack1("N")
283283
if buf.length >= size + 4
284284
buf.slice!(0, size + 4)
285285
else

lib/rb/lib/thrift/struct_union.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ def inspect_field(value, field_info)
188188
elsif value.is_a? Set
189189
inspect_collection(value, field_info)
190190
elsif value.is_a?(String) && field_info[:binary]
191-
value.unpack("H*").first
191+
value.unpack1("H*")
192192
else
193193
value.inspect
194194
end

lib/rb/lib/thrift/transport/framed_transport.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def to_s
107107
private
108108

109109
def read_frame
110-
sz = @transport.read_all(4).unpack("N").first
110+
sz = @transport.read_all(4).unpack1("N")
111111

112112
@index = 0
113113
@rbuf = @transport.read_all(sz)

lib/rb/lib/thrift/transport/header_transport.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ def read_frame(req_sz)
285285
rescue EOFError
286286
raise TransportException.new(TransportException::END_OF_FILE, "Unexpected EOF reading frame size")
287287
end
288-
frame_size = first_word.unpack("N").first
288+
frame_size = first_word.unpack1("N")
289289

290290
# Check for unframed binary protocol
291291
if (frame_size & BINARY_VERSION_MASK) == BINARY_VERSION_1
@@ -324,15 +324,15 @@ def read_frame(req_sz)
324324
second_word = frame_buf.read(4)
325325
frame_buf.rewind
326326

327-
magic = second_word.unpack("n").first
327+
magic = second_word.unpack1("n")
328328

329329
if magic == HEADER_MAGIC
330330
if frame_size < 10
331331
raise TransportException.new(TransportException::UNKNOWN, "Header transport frame is too small")
332332
end
333333
set_client_type(HeaderClientType::HEADERS)
334334
@read_buffer = parse_header_format(frame_buf)
335-
elsif (second_word.unpack("N").first & BINARY_VERSION_MASK) == BINARY_VERSION_1
335+
elsif (second_word.unpack1("N") & BINARY_VERSION_MASK) == BINARY_VERSION_1
336336
set_client_type(HeaderClientType::FRAMED_BINARY)
337337
@protocol_id = HeaderSubprotocolID::BINARY
338338
@read_buffer = frame_buf
@@ -382,11 +382,11 @@ def parse_header_format(buf)
382382
buf.read(2)
383383

384384
# Read flags and sequence ID
385-
@flags = buf.read(2).unpack("n").first
386-
@sequence_id = signed_int32(buf.read(4).unpack("N").first)
385+
@flags = buf.read(2).unpack1("n")
386+
@sequence_id = signed_int32(buf.read(4).unpack1("N"))
387387

388388
# Read header length (in 32-bit words)
389-
header_words = buf.read(2).unpack("n").first
389+
header_words = buf.read(2).unpack1("n")
390390
if header_words >= 16_384
391391
raise TransportException.new(TransportException::UNKNOWN, "Header size is unreasonable")
392392
end

lib/rb/lib/thrift/uuid.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def self.uuid_from_bytes(bytes)
4343
raise ProtocolException.new(ProtocolException::INVALID_DATA, "Invalid UUID data length")
4444
end
4545

46-
hex = bytes.unpack("H*").first
46+
hex = bytes.unpack1("H*")
4747
"#{hex[0, 8]}-#{hex[8, 4]}-#{hex[12, 4]}-#{hex[16, 4]}-#{hex[20, 12]}"
4848
end
4949
end

lib/rb/spec/header_protocol_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@
116116
@protocol.trans.flush
117117

118118
data = @buffer.read(@buffer.available)
119-
expect(data[8, 4].unpack("N").first).to eq(123)
119+
expect(data[8, 4].unpack1("N")).to eq(123)
120120
end
121121

122122
it "should write and read structs" do

lib/rb/spec/header_transport_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,11 @@ def read_unframed_message(protocol)
151151
expect(data.bytesize).to be > 16
152152

153153
# First 4 bytes are frame length
154-
frame_size = data[0, 4].unpack("N").first
154+
frame_size = data[0, 4].unpack1("N")
155155
expect(frame_size).to eq(data.bytesize - 4)
156156

157157
# Next 2 bytes should be header magic
158-
magic = data[4, 2].unpack("n").first
158+
magic = data[4, 2].unpack1("n")
159159
expect(magic).to eq(Thrift::HeaderTransport::HEADER_MAGIC)
160160
end
161161

@@ -175,7 +175,7 @@ def read_unframed_message(protocol)
175175
@trans.flush
176176

177177
data = @underlying.read(@underlying.available)
178-
expect(data[8, 4].unpack("N").first).to eq(456)
178+
expect(data[8, 4].unpack1("N")).to eq(456)
179179
end
180180

181181
it "should apply ZLIB transform" do

0 commit comments

Comments
 (0)