You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The following compiles without issue with gfortran and seg-faults if x is not present:
integerfunctionf(x) result(y)
integer, intent(in), optional:: x
y = x +1endfunction f
It should be something like:
integerfunctionf(x) result(y)
integer, intent(in), optional:: x
if(present(x)) then
y = x +1else
y =1! defaultend ifendfunction f
The rule would have to catch instances of optional dummy arguments appearing outside of if statements containing present.
It could be tricky to catch every possibility without false positives, as the following works just fine:
integerfunctionf(x) result(y)
integer, value, optional:: x
if(.not.present(x)) x =0
y = x +1endfunction f
It would also need to ensure the condition in the if statement can only be true if present(x) is true, as this also compiles just fine and seg-faults at run time:
integerfunctionf(x) result(y)
integer, intent(in), optional:: x
if (present(x) .or..true.) then
y = x +1else
y =1end ifendfunction f
The text was updated successfully, but these errors were encountered:
This is another one that's quite tricky, because the code might just be passing the argument off to another one with an optional argument:
subroutinefoo(x)
integer, intent(in), optional:: x
call bar(1, x)
endsubroutine foosubroutinebar(a, b)
integer, intent(in), optional:: a, b
endsubroutine bar
The trick with value is neat, I'm not sure I've seen that before!
The following compiles without issue with
gfortran
and seg-faults ifx
is not present:It should be something like:
The rule would have to catch instances of optional dummy arguments appearing outside of
if
statements containingpresent
.It could be tricky to catch every possibility without false positives, as the following works just fine:
It would also need to ensure the condition in the
if
statement can only be true ifpresent(x)
is true, as this also compiles just fine and seg-faults at run time:The text was updated successfully, but these errors were encountered: