From fc3389d4792223042684fd8d57129c6569975d52 Mon Sep 17 00:00:00 2001 From: Christian Menges Date: Tue, 20 Jun 2023 21:34:34 +0200 Subject: [PATCH 1/4] rubocop: Use match? instead of match/=~ when MatchData is not used Signed-off-by: Christian Menges --- lib/fluent/command/ctl.rb | 4 ++-- lib/fluent/command/plugin_config_formatter.rb | 2 +- lib/fluent/config/dsl.rb | 2 +- lib/fluent/counter/server.rb | 2 +- lib/fluent/counter/validator.rb | 6 +++--- lib/fluent/engine.rb | 2 +- lib/fluent/match.rb | 2 +- lib/fluent/plugin/base.rb | 2 +- lib/fluent/plugin/filter_record_transformer.rb | 2 +- lib/fluent/plugin/in_http.rb | 12 ++++++------ lib/fluent/plugin/out_exec_filter.rb | 2 +- lib/fluent/plugin/output.rb | 2 +- lib/fluent/plugin/parser_json.rb | 2 +- lib/fluent/plugin_helper/record_accessor.rb | 2 +- lib/fluent/plugin_id.rb | 2 +- test/plugin_helper/test_server.rb | 2 +- 16 files changed, 24 insertions(+), 24 deletions(-) diff --git a/lib/fluent/command/ctl.rb b/lib/fluent/command/ctl.rb index e4d9130187..f908d40f05 100644 --- a/lib/fluent/command/ctl.rb +++ b/lib/fluent/command/ctl.rb @@ -92,7 +92,7 @@ def usage(msg = nil) def call if Fluent.windows? - if @pid_or_svcname =~ /^[0-9]+$/ + if /^[0-9]+$/.match?(@pid_or_svcname) # Use as PID return call_windows_event(@command, "fluentd_#{@pid_or_svcname}") end @@ -172,7 +172,7 @@ def parse_options! usage("PID or SVCNAME isn't specified!") if @pid_or_svcname.nil? || @pid_or_svcname.empty? else usage("PID isn't specified!") if @pid_or_svcname.nil? || @pid_or_svcname.empty? - usage("Invalid PID: #{pid}") unless @pid_or_svcname =~ /^[0-9]+$/ + usage("Invalid PID: #{pid}") unless /^[0-9]+$/.match?(@pid_or_svcname) end end end diff --git a/lib/fluent/command/plugin_config_formatter.rb b/lib/fluent/command/plugin_config_formatter.rb index aa61c5711d..00d660da0b 100644 --- a/lib/fluent/command/plugin_config_formatter.rb +++ b/lib/fluent/command/plugin_config_formatter.rb @@ -61,7 +61,7 @@ def call @plugin.class.ancestors.reverse_each do |plugin_class| next unless plugin_class.respond_to?(:dump_config_definition) unless @verbose - next if plugin_class.name =~ /::PluginHelper::/ + next if /::PluginHelper::/.match?(plugin_class.name) end dumped_config_definition = plugin_class.dump_config_definition dumped_config[plugin_class.name] = dumped_config_definition unless dumped_config_definition.empty? diff --git a/lib/fluent/config/dsl.rb b/lib/fluent/config/dsl.rb index fc4b158ce7..f23a2688ed 100644 --- a/lib/fluent/config/dsl.rb +++ b/lib/fluent/config/dsl.rb @@ -110,7 +110,7 @@ def method_missing(name, *args, &block) def include(*args) ::Kernel.raise ::ArgumentError, "#{name} block requires arguments for include path" if args.nil? || args.size != 1 - if args.first =~ /\.rb$/ + if /\.rb$/.match?(args.first) path = File.expand_path(args.first) data = File.read(path) self.instance_eval(data, path) diff --git a/lib/fluent/counter/server.rb b/lib/fluent/counter/server.rb index c35d3073d2..359c6a0df2 100644 --- a/lib/fluent/counter/server.rb +++ b/lib/fluent/counter/server.rb @@ -27,7 +27,7 @@ class Server DEFAULT_PORT = 24321 def initialize(name, opt = {}) - raise 'Counter server name is invalid' unless Validator::VALID_NAME =~ name + raise 'Counter server name is invalid' unless Validator::VALID_NAME.match?(name) @name = name @opt = opt @addr = @opt[:addr] || DEFAULT_ADDR diff --git a/lib/fluent/counter/validator.rb b/lib/fluent/counter/validator.rb index 94ab6d2465..7e8fca91b8 100644 --- a/lib/fluent/counter/validator.rb +++ b/lib/fluent/counter/validator.rb @@ -82,7 +82,7 @@ def validate_key!(name) raise Fluent::Counter::InvalidParams.new('The type of `key` should be String') end - unless VALID_NAME =~ name + unless VALID_NAME.match?(name) raise Fluent::Counter::InvalidParams.new('`key` is the invalid format') end end @@ -92,7 +92,7 @@ def validate_scope!(name) raise Fluent::Counter::InvalidParams.new('The type of `scope` should be String') end - unless VALID_SCOPE_NAME =~ name + unless VALID_SCOPE_NAME.match?(name) raise Fluent::Counter::InvalidParams.new('`scope` is the invalid format') end end @@ -109,7 +109,7 @@ def validate_name!(hash) raise Fluent::Counter::InvalidParams.new('The type of `name` should be String') end - unless VALID_NAME =~ name + unless VALID_NAME.match?(name) raise Fluent::Counter::InvalidParams.new("`name` is the invalid format") end end diff --git a/lib/fluent/engine.rb b/lib/fluent/engine.rb index c4202c41d8..afac3167ca 100644 --- a/lib/fluent/engine.rb +++ b/lib/fluent/engine.rb @@ -68,7 +68,7 @@ def log end def parse_config(io, fname, basepath = Dir.pwd, v1_config = false) - if fname =~ /\.rb$/ + if /\.rb$/.match?(fname) require 'fluent/config/dsl' Config::DSL::Parser.parse(io, File.join(basepath, fname)) else diff --git a/lib/fluent/match.rb b/lib/fluent/match.rb index 17122e07ef..7b6c076adc 100644 --- a/lib/fluent/match.rb +++ b/lib/fluent/match.rb @@ -115,7 +115,7 @@ def initialize(pat) stack.last << regex.pop regex.push '' - elsif c =~ /[a-zA-Z0-9_]/ + elsif /[a-zA-Z0-9_]/.match?(c) regex.last << c else diff --git a/lib/fluent/plugin/base.rb b/lib/fluent/plugin/base.rb index b0cee6ca03..eb20612a61 100644 --- a/lib/fluent/plugin/base.rb +++ b/lib/fluent/plugin/base.rb @@ -190,7 +190,7 @@ def called_in_test? # Thread::Backtrace::Location#path returns base filename or absolute path. # #absolute_path returns absolute_path always. # https://bugs.ruby-lang.org/issues/12159 - if location.absolute_path =~ /\/test_[^\/]+\.rb$/ # location.path =~ /test_.+\.rb$/ + if /\/test_[^\/]+\.rb$/.match?(location.absolute_path) # location.path =~ /test_.+\.rb$/ return true end end diff --git a/lib/fluent/plugin/filter_record_transformer.rb b/lib/fluent/plugin/filter_record_transformer.rb index 02ba686a39..83af76ff6d 100644 --- a/lib/fluent/plugin/filter_record_transformer.rb +++ b/lib/fluent/plugin/filter_record_transformer.rb @@ -316,7 +316,7 @@ def expand(__str_to_eval__, tag, time, record, tag_parts, tag_prefix, tag_suffix end (Object.instance_methods).each do |m| - undef_method m unless m.to_s =~ /^__|respond_to_missing\?|object_id|public_methods|instance_eval|method_missing|define_singleton_method|respond_to\?|new_ostruct_member|^class$/ + undef_method m unless /^__|respond_to_missing\?|object_id|public_methods|instance_eval|method_missing|define_singleton_method|respond_to\?|new_ostruct_member|^class$/.match?(m.to_s) end end end diff --git a/lib/fluent/plugin/in_http.rb b/lib/fluent/plugin/in_http.rb index 98961cf9e1..c59368d2f2 100644 --- a/lib/fluent/plugin/in_http.rb +++ b/lib/fluent/plugin/in_http.rb @@ -439,9 +439,9 @@ def on_headers_complete(headers) when /\AContent-Encoding\Z/i @content_encoding = v when /\AConnection\Z/i - if v =~ /close/i + if /close/i.match?(v) @keep_alive = false - elsif v =~ /Keep-alive/i + elsif /Keep-alive/i.match?(v) @keep_alive = true end when /\AOrigin\Z/i @@ -566,16 +566,16 @@ def on_message_complete if @format_name != 'default' params[EVENT_RECORD_PARAMETER] = @body - elsif @content_type =~ /^application\/x-www-form-urlencoded/ + elsif /^application\/x-www-form-urlencoded/.match?(@content_type) params.update WEBrick::HTTPUtils.parse_query(@body) elsif @content_type =~ /^multipart\/form-data; boundary=(.+)/ boundary = WEBrick::HTTPUtils.dequote($1) params.update WEBrick::HTTPUtils.parse_form_data(@body, boundary) - elsif @content_type =~ /^application\/json/ + elsif /^application\/json/.match?(@content_type) params['json'] = @body - elsif @content_type =~ /^application\/msgpack/ + elsif /^application\/msgpack/.match?(@content_type) params['msgpack'] = @body - elsif @content_type =~ /^application\/x-ndjson/ + elsif /^application\/x-ndjson/.match?(@content_type) params['ndjson'] = @body end path_info = uri.path diff --git a/lib/fluent/plugin/out_exec_filter.rb b/lib/fluent/plugin/out_exec_filter.rb index e5981d1eec..3428876dd0 100644 --- a/lib/fluent/plugin/out_exec_filter.rb +++ b/lib/fluent/plugin/out_exec_filter.rb @@ -163,7 +163,7 @@ def configure(conf) 0 elsif (@child_respawn == 'inf') || (@child_respawn == '-1') -1 - elsif @child_respawn =~ /^\d+$/ + elsif /^\d+$/.match?(@child_respawn) @child_respawn.to_i else raise ConfigError, "child_respawn option argument invalid: none(or 0), inf(or -1) or positive number" diff --git a/lib/fluent/plugin/output.rb b/lib/fluent/plugin/output.rb index 7b8a8ef997..0aed67db95 100644 --- a/lib/fluent/plugin/output.rb +++ b/lib/fluent/plugin/output.rb @@ -824,7 +824,7 @@ def extract_placeholders(str, chunk) if str.include?('${tag}') rvalue = rvalue.gsub('${tag}', metadata.tag) end - if str =~ CHUNK_TAG_PLACEHOLDER_PATTERN + if CHUNK_TAG_PLACEHOLDER_PATTERN.match?(str) hash = {} tag_parts = metadata.tag.split('.') tag_parts.each_with_index do |part, i| diff --git a/lib/fluent/plugin/parser_json.rb b/lib/fluent/plugin/parser_json.rb index 840f6a963c..829aa4b72a 100644 --- a/lib/fluent/plugin/parser_json.rb +++ b/lib/fluent/plugin/parser_json.rb @@ -60,7 +60,7 @@ def configure_json_parser(name) rescue LoadError => ex name = :yajl if log - if /\boj\z/ =~ ex.message + if /\boj\z/.match?(ex.message) log.info "Oj is not installed, and failing back to Yajl for json parser" else log.warn ex.message diff --git a/lib/fluent/plugin_helper/record_accessor.rb b/lib/fluent/plugin_helper/record_accessor.rb index 6c1210330b..9a37a60ff4 100644 --- a/lib/fluent/plugin_helper/record_accessor.rb +++ b/lib/fluent/plugin_helper/record_accessor.rb @@ -119,7 +119,7 @@ def self.parse_dot_notation(param) def self.validate_dot_keys(keys) keys.each { |key| next unless key.is_a?(String) - if /\s+/.match(key) + if /\s+/.match?(key) raise Fluent::ConfigError, "whitespace character is not allowed in dot notation. Use bracket notation: #{key}" end } diff --git a/lib/fluent/plugin_id.rb b/lib/fluent/plugin_id.rb index 87b1bc7ff1..af9b6019d3 100644 --- a/lib/fluent/plugin_id.rb +++ b/lib/fluent/plugin_id.rb @@ -49,7 +49,7 @@ def plugin_id_for_test? # Thread::Backtrace::Location#path returns base filename or absolute path. # #absolute_path returns absolute_path always. # https://bugs.ruby-lang.org/issues/12159 - if location.absolute_path =~ /\/test_[^\/]+\.rb$/ # location.path =~ /test_.+\.rb$/ + if /\/test_[^\/]+\.rb$/.match?(location.absolute_path) # location.path =~ /test_.+\.rb$/ return true end end diff --git a/test/plugin_helper/test_server.rb b/test/plugin_helper/test_server.rb index cb44b8363d..347938eca3 100644 --- a/test/plugin_helper/test_server.rb +++ b/test/plugin_helper/test_server.rb @@ -1329,7 +1329,7 @@ def assert_certificate(cert, expected_extensions) # OpenSSL 1.1.1: "TLSv1.2" if tls_version == "TLSv1/SSLv3" || tls_version == "TLSv1.2" matched = true - unless cipher_name.match(/#{conf.ciphers}/) + unless cipher_name.match?(/#{conf.ciphers}/) matched = false break end From 21ca454199b1d424deb3e7907908fd5937857224 Mon Sep 17 00:00:00 2001 From: Christian Menges Date: Tue, 20 Jun 2023 21:47:58 +0200 Subject: [PATCH 2/4] rubocop: Use tr instead of gsub Signed-off-by: Christian Menges --- lib/fluent/config/v1_parser.rb | 4 ++-- lib/fluent/plugin/in_http.rb | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/fluent/config/v1_parser.rb b/lib/fluent/config/v1_parser.rb index 7f36b44e43..7432e6c924 100644 --- a/lib/fluent/config/v1_parser.rb +++ b/lib/fluent/config/v1_parser.rb @@ -150,8 +150,8 @@ def parse_include(attrs, elems) def eval_include(attrs, elems, uri) # replace space(s)(' ') with '+' to prevent invalid uri due to space(s). # See: https://github.com/fluent/fluentd/pull/2780#issuecomment-576081212 - u = URI.parse(uri.gsub(/ /, '+')) - if u.scheme == 'file' || (!u.scheme.nil? && u.scheme.length == 1) || u.path == uri.gsub(/ /, '+') # file path + u = URI.parse(uri.tr(' ', '+')) + if u.scheme == 'file' || (!u.scheme.nil? && u.scheme.length == 1) || u.path == uri.tr(' ', '+') # file path # When the Windows absolute path then u.scheme.length == 1 # e.g. C: path = URI.decode_www_form_component(u.path) diff --git a/lib/fluent/plugin/in_http.rb b/lib/fluent/plugin/in_http.rb index c59368d2f2..e738f2db69 100644 --- a/lib/fluent/plugin/in_http.rb +++ b/lib/fluent/plugin/in_http.rb @@ -428,7 +428,7 @@ def on_headers_complete(headers) @content_type = "" @content_encoding = "" headers.each_pair {|k,v| - @env["HTTP_#{k.gsub('-','_').upcase}"] = v + @env["HTTP_#{k.tr('-','_').upcase}"] = v case k when /\AExpect\z/i expect = v @@ -585,7 +585,7 @@ def on_message_complete query_params = WEBrick::HTTPUtils.parse_query(uri.query) query_params.each_pair {|k,v| - params["QUERY_#{k.gsub('-','_').upcase}"] = v + params["QUERY_#{k.tr('-','_').upcase}"] = v } end From 0a5718541c9848530973ed7e4c9d568f081fae56 Mon Sep 17 00:00:00 2001 From: Christian Menges Date: Tue, 20 Jun 2023 21:49:29 +0200 Subject: [PATCH 3/4] rubocop: Use squeeze instead of gsub Signed-off-by: Christian Menges --- lib/fluent/plugin/in_tail.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/fluent/plugin/in_tail.rb b/lib/fluent/plugin/in_tail.rb index fb29dd249f..ef2f6b6c77 100644 --- a/lib/fluent/plugin/in_tail.rb +++ b/lib/fluent/plugin/in_tail.rb @@ -778,7 +778,7 @@ def initialize(target_info, pe, log, read_from_head, follow_inodes, update_watch attr_accessor :group_watcher def tag - @parsed_tag ||= @path.tr('/', '.').gsub(/\.+/, '.').gsub(/^\./, '') + @parsed_tag ||= @path.tr('/', '.').squeeze('.').gsub(/^\./, '') end def register_watcher(watcher) From 6047e1c6824b7eaf8bf7fa05c8edceffb7e4175e Mon Sep 17 00:00:00 2001 From: Christian Menges Date: Tue, 20 Jun 2023 21:52:03 +0200 Subject: [PATCH 4/4] rubocop: Use string as argument instead of regexp Signed-off-by: Christian Menges --- test/plugin/test_base.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/plugin/test_base.rb b/test/plugin/test_base.rb index 479ecc49f6..e6062a163b 100644 --- a/test/plugin/test_base.rb +++ b/test/plugin/test_base.rb @@ -108,7 +108,7 @@ class FluentPluginBaseTest::DummyPlugin2 < Fluent::Plugin::TestBase @p.extend m assert_equal [], logger.logs - ret = @p.string_safe_encoding("abc\xff.\x01f"){|s| s.split(/\./) } + ret = @p.string_safe_encoding("abc\xff.\x01f"){|s| s.split(".") } assert_equal ['abc?', "\u0001f"], ret assert_equal 1, logger.logs.size assert{ logger.logs.first.include?("invalid byte sequence is replaced in ") }