Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prefer to use File methods instead of IO methods #7387

Merged
merged 5 commits into from Feb 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 23 additions & 0 deletions .github/workflows/codeql-analysis.yml
Expand Up @@ -78,3 +78,26 @@ jobs:
uses: github/codeql-action/analyze@32dc499307d133bb5085bae78498c0ac2cf762d5 # v2.2.5
with:
category: "/language:${{matrix.language}}"
upload: False
output: sarif-results

- name: filter-sarif
uses: advanced-security/filter-sarif@v1
with:
patterns: |
+**/*.rb
-test/ruby/test_io.rb:rb/non-constant-kernel-open
-test/open-uri/test_open-uri.rb:rb/non-constant-kernel-open
-test/open-uri/test_ssl.rb:rb/non-constant-kernel-open
-spec/ruby/core/io/binread_spec.rb:rb/non-constant-kernel-open
-spec/ruby/core/io/readlines_spec.rb:rb/non-constant-kernel-open
-spec/ruby/core/io/foreach_spec.rb:rb/non-constant-kernel-open
-spec/ruby/core/io/write_spec.rb:rb/non-constant-kernel-open
input: sarif-results/${{ matrix.language }}.sarif
output: sarif-results/${{ matrix.language }}.sarif
if: ${{ matrix.language == 'ruby' }}

- name: Upload SARIF
uses: github/codeql-action/upload-sarif@v2
with:
sarif_file: sarif-results/${{ matrix.language }}.sarif
10 changes: 5 additions & 5 deletions ext/fiddle/win32/libffi-config.rb
Expand Up @@ -23,7 +23,7 @@
end
end

IO.foreach("#{srcdir}/configure.ac") do |line|
File.foreach("#{srcdir}/configure.ac") do |line|
if /^AC_INIT\((.*)\)/ =~ line
version = $1.split(/,\s*/)[1]
version.gsub!(/\A\[|\]\z/, '')
Expand All @@ -38,11 +38,11 @@
FileUtils.mkdir_p([builddir, "#{builddir}/include", "#{builddir}/src/x86"])
FileUtils.cp("#{basedir}/fficonfig.h", ".", preserve: true)

hdr = IO.binread("#{srcdir}/include/ffi.h.in")
hdr = File.binread("#{srcdir}/include/ffi.h.in")
hdr.gsub!(/@(\w+)@/) {conf[$1] || $&}
hdr.gsub!(/^(#if\s+)@\w+@/, '\10')
IO.binwrite("#{builddir}/include/ffi.h", hdr)
File.binwrite("#{builddir}/include/ffi.h", hdr)

mk = IO.binread("#{basedir}/libffi.mk.tmpl")
mk = File.binread("#{basedir}/libffi.mk.tmpl")
mk.gsub!(/@(\w+)@/) {conf[$1] || $&}
IO.binwrite("Makefile", mk)
File.binwrite("Makefile", mk)
2 changes: 1 addition & 1 deletion lib/net/http/status.rb
Expand Up @@ -4,7 +4,7 @@

if $0 == __FILE__
require 'open-uri'
IO.foreach(__FILE__) do |line|
File.foreach(__FILE__) do |line|
puts line
break if line.start_with?('end')
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rdoc/rd/block_parser.rb
Expand Up @@ -360,7 +360,7 @@ def get_included(file)
file_name = File.join dir, file

if File.exist? file_name then
included = IO.readlines file_name
included = File.readlines file_name
break
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/ruby/enc/test_case_comprehensive.rb
Expand Up @@ -37,7 +37,7 @@ def initialize(method_name, attributes, first_data, follow_data=first_data)
end

def self.read_data_file(filename)
IO.foreach(expand_filename(filename), encoding: Encoding::ASCII_8BIT) do |line|
File.foreach(expand_filename(filename), encoding: Encoding::ASCII_8BIT) do |line|
if $. == 1
if filename == 'UnicodeData'
elsif line.start_with?("# #{filename}-#{UNICODE_VERSION}.txt")
Expand Down
2 changes: 1 addition & 1 deletion test/ruby/enc/test_emoji_breaks.rb
Expand Up @@ -75,7 +75,7 @@ def read_data
EMOJI_DATA_FILES.each do |file|
version_mismatch = true
file_tests = []
IO.foreach(file.fullname, encoding: Encoding::UTF_8) do |line|
File.foreach(file.fullname, encoding: Encoding::UTF_8) do |line|
line.chomp!
if $.==1
if line=="# #{file.basename}-#{file.version}.txt"
Expand Down
2 changes: 1 addition & 1 deletion test/ruby/enc/test_grapheme_breaks.rb
Expand Up @@ -44,7 +44,7 @@ def test_data_files_available
if file_available?
def read_data
tests = []
IO.foreach(GRAPHEME_BREAK_TEST_FILE, encoding: Encoding::UTF_8) do |line|
File.foreach(GRAPHEME_BREAK_TEST_FILE, encoding: Encoding::UTF_8) do |line|
if $. == 1 and not line.start_with?("# GraphemeBreakTest-#{UNICODE_VERSION}.txt")
raise "File Version Mismatch"
end
Expand Down
2 changes: 1 addition & 1 deletion test/ruby/enc/test_regex_casefold.rb
Expand Up @@ -19,7 +19,7 @@ def check_downcase_properties(expected, start, *flags)
end

def read_tests
IO.readlines("#{UNICODE_DATA_PATH}/CaseFolding.txt", encoding: Encoding::ASCII_8BIT)
File.readlines("#{UNICODE_DATA_PATH}/CaseFolding.txt", encoding: Encoding::ASCII_8BIT)
.collect.with_index { |linedata, linenumber| [linenumber.to_i+1, linedata.chomp] }
.reject { |number, data| data =~ /^(#|$)/ }
.collect do |linenumber, linedata|
Expand Down
2 changes: 1 addition & 1 deletion test/ruby/test_exception.rb
Expand Up @@ -1266,7 +1266,7 @@ def test_blocking_backtrace
begin;
class Bug < RuntimeError
def backtrace
IO.readlines(IO::NULL)
File.readlines(IO::NULL)
end
end
bug = Bug.new '[ruby-core:85939] [Bug #14577]'
Expand Down
2 changes: 1 addition & 1 deletion test/test_unicode_normalize.rb
Expand Up @@ -24,7 +24,7 @@ class TestUnicodeNormalize
NormTest = Struct.new :source, :NFC, :NFD, :NFKC, :NFKD, :line

def self.read_tests
lines = IO.readlines(expand_filename('NormalizationTest'), encoding: 'utf-8')
lines = File.readlines(expand_filename('NormalizationTest'), encoding: 'utf-8')
firstline = lines.shift
define_method "test_0_normalizationtest_firstline" do
assert_include(firstline, "NormalizationTest-#{UNICODE_VERSION}.txt")
Expand Down
6 changes: 3 additions & 3 deletions tool/enc-case-folding.rb
Expand Up @@ -61,7 +61,7 @@ def load(filename)
@version = nil
turkic = []

IO.foreach(filename, mode: "rb") do |line|
File.foreach(filename, mode: "rb") do |line|
@version ||= line[/-([0-9.]+).txt/, 1]
next unless res = pattern.match(line)
ch_from = res[1].to_i(16)
Expand Down Expand Up @@ -230,7 +230,7 @@ def initialize(mapping_directory)
@specials = []
@specials_length = 0
@version = nil
IO.foreach(File.join(mapping_directory, 'UnicodeData.txt'), mode: "rb") do |line|
File.foreach(File.join(mapping_directory, 'UnicodeData.txt'), mode: "rb") do |line|
next if line =~ /^</
code, _, _, _, _, _, _, _, _, _, _, _, upper, lower, title = line.chomp.split ';'
unless upper and lower and title and (upper+lower+title)==''
Expand All @@ -239,7 +239,7 @@ def initialize(mapping_directory)
end

@filename = File.join(mapping_directory, 'SpecialCasing.txt')
IO.foreach(@filename, mode: "rb") do |line|
File.foreach(@filename, mode: "rb") do |line|
@version ||= line[/-([0-9.]+).txt/, 1]
line.chomp!
line, comment = line.split(/ *#/)
Expand Down
2 changes: 1 addition & 1 deletion tool/enc-unicode.rb
Expand Up @@ -59,7 +59,7 @@ def parse_unicode_data(file)
data = {'Any' => (0x0000..0x10ffff).to_a, 'Assigned' => [],
'ASCII' => (0..0x007F).to_a, 'NEWLINE' => [0x0a], 'Cn' => []}
beg_cp = nil
IO.foreach(file) do |line|
File.foreach(file) do |line|
fields = line.split(';')
cp = fields[0].to_i(16)

Expand Down
2 changes: 1 addition & 1 deletion tool/extlibs.rb
Expand Up @@ -185,7 +185,7 @@ def process(list)
extracted = false
dest = File.dirname(list)
url = chksums = nil
IO.foreach(list) do |line|
File.foreach(list) do |line|
line.sub!(/\s*#.*/, '')
if /^(\w+)\s*=\s*(.*)/ =~ line
vars[$1] = vars.expand($2)
Expand Down
2 changes: 1 addition & 1 deletion tool/lib/memory_status.rb
Expand Up @@ -12,7 +12,7 @@ module Memory
PROC_FILE = procfile
VM_PAT = pat
def self.read_status
IO.foreach(PROC_FILE, encoding: Encoding::ASCII_8BIT) do |l|
File.foreach(PROC_FILE, encoding: Encoding::ASCII_8BIT) do |l|
yield($1.downcase.intern, $2.to_i * 1024) if VM_PAT =~ l
end
end
Expand Down
4 changes: 2 additions & 2 deletions tool/mkconfig.rb
Expand Up @@ -205,7 +205,7 @@ module RbConfig
print " CONFIG[\"DESTDIR\"] = DESTDIR\n"

versions = {}
IO.foreach(File.join(srcdir, "version.h")) do |l|
File.foreach(File.join(srcdir, "version.h")) do |l|
m = /^\s*#\s*define\s+RUBY_(PATCHLEVEL)\s+(-?\d+)/.match(l)
if m
versions[m[1]] = m[2]
Expand All @@ -226,7 +226,7 @@ module RbConfig
end
end
if versions.size != 4
IO.foreach(File.join(srcdir, "include/ruby/version.h")) do |l|
File.foreach(File.join(srcdir, "include/ruby/version.h")) do |l|
m = /^\s*#\s*define\s+RUBY_API_VERSION_(\w+)\s+(-?\d+)/.match(l)
if m
versions[m[1]] ||= m[2]
Expand Down
2 changes: 1 addition & 1 deletion win32/mkexports.rb
Expand Up @@ -49,7 +49,7 @@ def initialize(objs)
end

def read_substitution(header, syms, winapis)
IO.foreach(header) do |line|
File.foreach(header) do |line|
if /^#define (\w+)\((.*?)\)\s+(?:\(void\))?(rb_w32_\w+)\((.*?)\)\s*$/ =~ line and
$2.delete(" ") == $4.delete(" ")
export, internal = $1, $3
Expand Down