diff --git a/changelog/fix_an_error_inclusive_language.md b/changelog/fix_an_error_inclusive_language.md new file mode 100644 index 00000000000..bfdb4056dd4 --- /dev/null +++ b/changelog/fix_an_error_inclusive_language.md @@ -0,0 +1 @@ +* [#10610](https://github.com/rubocop/rubocop/pull/10610): Fix an error for `Naming/InclusiveLanguage` string with invalid byte sequence in UTF-8. ([@ydah][]) diff --git a/lib/rubocop/cop/naming/inclusive_language.rb b/lib/rubocop/cop/naming/inclusive_language.rb index 75f16ce1e2a..d5f2402ff92 100644 --- a/lib/rubocop/cop/naming/inclusive_language.rb +++ b/lib/rubocop/cop/naming/inclusive_language.rb @@ -214,13 +214,14 @@ def scan_for_words(input) end def mask_input(str) - return str if @allowed_regex.nil? - safe_str = if str.valid_encoding? str else str.encode('UTF-8', invalid: :replace, undef: :replace) end + + return safe_str if @allowed_regex.nil? + safe_str.gsub(@allowed_regex) { |match| '*' * match.size } end diff --git a/spec/rubocop/cop/naming/inclusive_language_spec.rb b/spec/rubocop/cop/naming/inclusive_language_spec.rb index e150c4e9656..74cdcd06a0c 100644 --- a/spec/rubocop/cop/naming/inclusive_language_spec.rb +++ b/spec/rubocop/cop/naming/inclusive_language_spec.rb @@ -315,6 +315,12 @@ class Nodewhitelist RUBY end + it 'does not register offenses and not raise `ArgumentError` for invalid byte sequence in UTF-8' do + expect_no_offenses(<<-RUBY) + %W("a\\255\\255") + RUBY + end + context 'when CheckStrings config is false' do let(:check_strings) { false }