Skip to content

COR020 initialisation-in-declaration ​

warning enabled by default

A procedure's local variable is initialised in its declaration, which implies save.

Why ​

In Fortran an initialiser in a declaration is not executed at each call: it gives the variable the SAVE attribute and sets it once, before the program starts. integer :: count = 0 keeps the value of the previous call, and in multi-threaded code (OpenMP, OpenACC) the single static copy is shared by every thread, a data race. Assign the initial value in an executable statement, or write save explicitly when persistence is intended.

Reported ​

fortran
subroutine accumulate(x)
  real, intent(in) :: x
  real :: total = 0.0   ! set once, then keeps growing across calls

Preferred ​

fortran
subroutine accumulate(x)
  real, intent(in) :: x
  real :: total
  total = 0.0

Suppressing ​

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

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

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

References ​

Sections of the shipped knowledge packs behind this rule:

  • hpc-practice:fortran-practice#implicit-save-trap (fobis kb show hpc-practice:fortran-practice#implicit-save-trap)
  • fortran-std:ch19-scope-association#anti-patterns (fobis kb show fortran-std:ch19-scope-association#anti-patterns)

Equivalent ​

Fortitude rule C081 (see Lint Rules).