# Warn about unreachable code

**URL:** <https://fortran-lang.discourse.group/t/warn-about-unreachable-code/5185>\
**Category:** Uncategorized\
**Created:** [February 12, 2023, 3:45am UTC](https://fortran-lang.discourse.group/t/warn-about-unreachable-code/5185 "2023-02-12T03:45:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Beliavsky](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Beliavsky](https://fortran-lang.discourse.group/u/Beliavsky)\
**Post date:** [February 12, 2023, 3:45am UTC](https://fortran-lang.discourse.group/t/warn-about-unreachable-code/5185/1 "2023-02-12T03:45:06Z")

</div>

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.

```auto
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

```

---

<div class="post-metadata">

**Author:** ![Beliavsky](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Beliavsky](https://fortran-lang.discourse.group/u/Beliavsky)\
**Post date:** [February 12, 2023, 3:55am UTC](https://fortran-lang.discourse.group/t/warn-about-unreachable-code/5185/2 "2023-02-12T03:55:04Z")

</div>

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

```auto
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.
