Aller au contenu

Disposable Not Disposed (ARCH6080)

Ce contenu n’est pas encore disponible dans votre langue.

Key ruby.performance.disposable-not-disposed · Severity error · Category performance

A closeable resource created without the block form is left open and leaks if an exception is raised. Wrap the resource in the block form so it closes automatically.

def write_report(path, data)
file = File.open(path, "w")
file.write(data)
file.close
end
def write_report(path, data)
File.open(path, "w") do |file|
file.write(data)
end
end

Wrap the resource in the block form instead of opening and closing it manually; it closes when the block ends.