Warn about unreachable code

Are there any compilers that can warn about unreachable code, for example the line k=i/j in the function below? Neither gfortran -c -Wall -Wextra or ifort -c /check:uninit /warn:all /warn:unused say anything.

function div(i,j) result(k)
implicit none
integer, intent(in) :: i,j
integer             :: k
k = 0
if (j == 0) then
   return
   k = i/j ! should appear after the end if
end if
end function div
1 Like

I removed the comment from the function above and asked GPT “What is wrong with the following Fortran function?” To its credit, it said,

The code contains a logic error: the return statement will cause the function to exit immediately, so the line setting k to i/j will never be executed.

and when asked to fix the code gave

function div(i,j) result(k)
  implicit none
  integer, intent(in) :: i,j
  integer             :: k
  if (j /= 0) then
    k = i/j
  else
    write (*,*) "Error: division by zero"
    k = -999
  end if
end function div

I was just working on a code where I had created this kind of bug.