Lyricdev
GitHub

LYR-SEM0144

a Closeable value nothing closes

warning · spec 06-errors · lint closeable-never-closed, group resources, warn unless said

A value of a 'Closeable' type is made and dropped by an expression statement, or bound with a plain 'let', used, and neither closed nor handed on.

Explanation

A Closeable holds a resource — a file, a socket, a buffered writer's unwritten bytes — that is released by close(). One that nothing closes leaks it, and a buffered writer loses what is still in its buffer. The warning fires when a binding is used through its methods but never closed and never passed, returned or stored elsewhere.

Bind it with using let, which closes it when the scope is left, or call close() on every path.

Wrong

import std.io { BufWriter, ByteBuffer };

fn main(): void throws Error {
    let w = BufWriter.new(ByteBuffer.new());
    try w.writeString("lost");
}
import std.io { BufWriter, ByteBuffer };

fn main(): void throws Error {
    using let w = BufWriter.new(ByteBuffer.new());
    try w.writeString("kept");
}