Skip to main content
GitHub

TYP02-F. Prohibit implicit typing in all program units

Program units shall explicitly disable implicit typing by using the implicit none or implicit none(type) statement.

By default, Fortran permits implicit typing of variables based on the first letter of their names. Identifiers beginning with the letters I through N are implicitly typed as integer , while all other identifiers are implicitly typed as real .

Reliance on implicit typing is hazardous because a misspelled variable name does not result in a compilation error. Instead, the compiler silently introduces a new variable with an implicit type. Such variables may be uninitialized or may undergo unintended type conversions, leading to subtle and difficult-to-detect logic errors.

The use of implicit none requires all variables to be explicitly declared, enabling the compiler to detect typographical errors and unintended variable creation at compile time. This practice ensures that variable types and attributes are consistent with the programmer's intent and significantly improves code correctness, readability, and maintainability.

Noncompliant Code Example

In this noncompliant example, the programmer intends to perform a floating-point division. However, because num1 and num2 start with the letter N , they are implicitly typed as integer . The result res will contain the result of an integer division, num2 which will suffer a truncation, which is likely incorrect.

Non-compliant code
program ratio_calc
  ! Noncompliant: Implicit typing is enabled by default.
  num1 = 7
  num2 = 2.5
  res  = num1 / num2
  print *, res ! Prints 3.0 instead of 3.5
end program ratio_calc

Compliant Solution

In this compliant solution, implicit none is used to disable implicit typing. All variables are explicitly declared with appropriate types, ensuring that floating-point arithmetic is performed as intended and that typographical errors are diagnosed at compile time.

Compliant code
program ratio_calc
  use iso_fortran_env, only: real32
  implicit none ! Compliant: Disables implicit typing

  integer           :: num1
  real(kind=real32) :: num2, res

  num1 = 7
  num2 = 2.5
  res  = num1 / num2
  print *, res ! Prints 3.5
end program ratio_calc

Risk Assessment

Implicit variable declarations can conceal typographical errors, introduce unintended variables, and cause silent precision loss due to unintended integer arithmetic or implicit type conversions.

RecommendationSeverityLikelihoodDetectableRepairablePriorityLevel
TYP02-FHighLikelyYesNoP18L1

Attachments: