Skip to content

COR081 magic-io-unit ​

warning opt-in

An I/O statement uses a literal unit number.

Why ​

Literal unit numbers collide: a library and the program both opening unit 10 silently share (or close) one file, and units 5 and 6 are only conventionally the terminal. Open files with newunit= and use input_unit/output_unit/error_unit from iso_fortran_env.

Reported ​

fortran
open(10, file='mesh.dat')
read(10, *) n

Preferred ​

fortran
open(newunit=u, file='mesh.dat')
read(u, *) n

Suppressing ​

For one occurrence, with the reason on the same line (or alone on the line before the statement):

fortran
! fobis: ignore[COR081] <why this occurrence is fine>

For the whole project, ignore = COR081 in the fobos [lint] section.

Equivalent ​

Fortitude rule C032 (see Lint Rules).