Skip to content

Commit c5cf521

Browse files
authored
Use safe navigation operator to clean up the codes (#5004)
**Which issue(s) this PR fixes**: None. **What this PR does / why we need it**: The safe navigation operator was introduced at [Ruby 2.3](https://www.ruby-lang.org/en/news/2015/12/25/ruby-2-3-0-released/). I think it makes the code cleaner. **Docs Changes**: Not needed. **Release Note**: Minor code refactoring Signed-off-by: Shizuo Fujita <[email protected]>
1 parent 25ff215 commit c5cf521

File tree

11 files changed

+21
-21
lines changed

11 files changed

+21
-21
lines changed

lib/fluent/config/configure_proxy.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def initialize(name, root: false, param_name: nil, final: nil, init: nil, requir
4747
# should override "@name".
4848
@root_section = root
4949

50-
@param_name = param_name && param_name.to_sym
50+
@param_name = param_name&.to_sym
5151
@init = init
5252
@required = required
5353
@multi = multi

lib/fluent/config/section.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ def self.check_unused_section(proxy, conf, plugin_class)
253253
elems = conf.respond_to?(:elements) ? conf.elements : []
254254
elems.each { |e|
255255
next if plugin_class.nil? && Fluent::Config::V1Parser::ELEM_SYMBOLS.include?(e.name) # skip pre-defined non-plugin elements because it doesn't have proxy section
256-
next if e.unused_in && e.unused_in.empty? # the section is used at least once
256+
next if e.unused_in&.empty? # the section is used at least once
257257

258258
if proxy.sections.any? { |name, subproxy| e.name == subproxy.name.to_s || e.name == subproxy.alias.to_s }
259259
e.unused_in = []

lib/fluent/plugin/buffer.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ def update_timekeys
525525
chunks = @stage.values
526526
chunks.concat(@queue)
527527
@timekeys = chunks.each_with_object({}) do |chunk, keys|
528-
if chunk.metadata && chunk.metadata.timekey
528+
if chunk.metadata&.timekey
529529
t = chunk.metadata.timekey
530530
keys[t] = keys.fetch(t, 0) + 1
531531
end
@@ -958,7 +958,7 @@ def backup(chunk_unique_id)
958958
def optimistic_queued?(metadata = nil)
959959
if metadata
960960
n = @queued_num[metadata]
961-
n && n.nonzero?
961+
n&.nonzero?
962962
else
963963
!@queue.empty?
964964
end

lib/fluent/plugin/out_exec_filter.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,8 @@ def start
193193
log.warn "child process exits with error code", code: status.to_i, status: status.exitstatus, signal: status.termsig
194194
end
195195
c.mutex.synchronize do
196-
(c.writeio && c.writeio.close) rescue nil
197-
(c.readio && c.readio.close) rescue nil
196+
c.writeio&.close rescue nil
197+
c.readio&.close rescue nil
198198
c.pid = c.readio = c.writeio = nil
199199
end
200200
end

lib/fluent/plugin/out_forward.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -577,8 +577,8 @@ def initialize(sender, server, failure:, connection_manager:, ack_handler:)
577577

578578
@handshake = HandshakeProtocol.new(
579579
log: @log,
580-
hostname: sender.security && sender.security.self_hostname,
581-
shared_key: server.shared_key || (sender.security && sender.security.shared_key) || '',
580+
hostname: sender.security&.self_hostname,
581+
shared_key: server.shared_key || sender.security&.shared_key || '',
582582
password: server.password || '',
583583
username: server.username || '',
584584
)

lib/fluent/plugin/output.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ def after_shutdown
552552
@output_flush_threads.each do |state|
553553
# to wakeup thread and make it to stop by itself
554554
state.mutex.synchronize {
555-
if state.thread && state.thread.status
555+
if state.thread&.status
556556
state.next_clock = 0
557557
state.cond_var.signal
558558
end
@@ -1131,7 +1131,7 @@ def rollback_write(chunk_id, update_retry: true)
11311131

11321132
def try_rollback_write
11331133
@dequeued_chunks_mutex.synchronize do
1134-
while @dequeued_chunks.first && @dequeued_chunks.first.expired?
1134+
while @dequeued_chunks.first&.expired?
11351135
info = @dequeued_chunks.shift
11361136
if @buffer.takeback_chunk(info.chunk_id)
11371137
@rollback_count_metrics.inc
@@ -1376,7 +1376,7 @@ def submit_flush_once
13761376
@output_flush_thread_current_position = (@output_flush_thread_current_position + 1) % @buffer_config.flush_thread_count
13771377
state = @output_flush_threads[@output_flush_thread_current_position]
13781378
state.mutex.synchronize {
1379-
if state.thread && state.thread.status # "run"/"sleep"/"aborting" or false(successfully stop) or nil(killed by exception)
1379+
if state.thread&.status # "run"/"sleep"/"aborting" or false(successfully stop) or nil(killed by exception)
13801380
state.next_clock = 0
13811381
state.cond_var.signal
13821382
else
@@ -1422,7 +1422,7 @@ def enqueue_thread_wait
14221422
def flush_thread_wakeup
14231423
@output_flush_threads.each do |state|
14241424
state.mutex.synchronize {
1425-
if state.thread && state.thread.status
1425+
if state.thread&.status
14261426
state.next_clock = 0
14271427
state.cond_var.signal
14281428
end

lib/fluent/plugin_helper/child_process.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ def shutdown
143143
@_child_process_mutex.synchronize{ @_child_process_processes.keys }.each do |pid|
144144
process_info = @_child_process_processes[pid]
145145
next if !process_info
146-
process_info.writeio && process_info.writeio.close rescue nil
146+
process_info.writeio&.close rescue nil
147147
end
148148

149149
super

lib/fluent/plugin_helper/http_server/request.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def query
4545
end
4646

4747
def body
48-
@request.body && @request.body.read
48+
@request.body&.read
4949
end
5050
end
5151
end

lib/fluent/plugin_helper/storage.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def storage_create(usage: '', type: nil, conf: nil, default_type: nil)
3838
end
3939

4040
s = @_storages[usage]
41-
if s && s.running
41+
if s&.running
4242
return s.storage
4343
elsif s
4444
# storage is already created, but not loaded / started

test/plugin/test_in_tail.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1766,7 +1766,7 @@ def test_unwatched_files_should_be_removed
17661766
}
17671767
)
17681768
ensure
1769-
d.instance_shutdown if d && d.instance
1769+
d.instance_shutdown if d&.instance
17701770
cleanup_directory(@tmp_dir)
17711771
end
17721772

@@ -2540,7 +2540,7 @@ def test_ENOENT_error_after_setup_watcher
25402540
assert($log.out.logs.any?{|log| log.include?("stat() for #{path} failed. Continuing without tailing it.\n") },
25412541
$log.out.logs.join("\n"))
25422542
ensure
2543-
d.instance_shutdown if d && d.instance
2543+
d.instance_shutdown if d&.instance
25442544
end
25452545

25462546
def test_EACCES_error_after_setup_watcher
@@ -2567,7 +2567,7 @@ def test_EACCES_error_after_setup_watcher
25672567
$log.out.logs.join("\n"))
25682568
end
25692569
ensure
2570-
d.instance_shutdown if d && d.instance
2570+
d.instance_shutdown if d&.instance
25712571
if File.exist?("#{@tmp_dir}/noaccess")
25722572
FileUtils.chmod(0755, "#{@tmp_dir}/noaccess")
25732573
FileUtils.rm_rf("#{@tmp_dir}/noaccess")
@@ -2589,7 +2589,7 @@ def test_EACCES
25892589
end
25902590
assert($log.out.logs.any?{|log| log.include?("expand_paths: stat() for #{path} failed with Errno::EACCES. Skip file.\n") })
25912591
ensure
2592-
d.instance_shutdown if d && d.instance
2592+
d.instance_shutdown if d&.instance
25932593
end
25942594

25952595
def test_warn_without_directory_permission

0 commit comments

Comments
 (0)