Skip to content

Create `Cop` that flags memory-heavy uses of I/O

Follow-up from #205749 (closed)

Several functions on File and IO read the source in its entirey (i.e. until hitting EOF), which can lead to memory bloat if the file is large. For instance File.readlines returns an array where each element points to a file, so on top of the actual file contents, Ruby needs to maintain the array and object references to each line.

It is a lot more efficient to consume IO line-by-line and discarding all previously processed rows in the process, e.g.:

while line = file.readline
   # process line
end

The task is to write a Cop that flags those methods on IO and its descendants that are memory-unsafe and ideally proposes an auto-correction for how the existing code can be rewritten to be more efficient.

Edited by Matthias Käppler