I have been running into these -Wuninitialized warnings in gfortran - 12.1.0 (but also earlier versions going back to at least 9.4.0) and I was wondering if they are false positives.
program main
implicit none
character(len=1), allocatable :: a(:)
a = ["a"]
print*, a
end program main
Output
test.f90:4:13:
4 | a = ["a"]
| ^
Warning: ‘a.offset’ is used uninitialized [-Wuninitialized]
test.f90:3:41:
3 | character(len=1), allocatable :: a(:)
| ^
note: ‘a’ declared here
test.f90:4:13:
4 | a = ["a"]
| ^
Warning: ‘a.dim[0].lbound’ is used uninitialized [-Wuninitialized]
test.f90:3:41:
3 | character(len=1), allocatable :: a(:)
| ^
note: ‘a’ declared here
test.f90:4:13:
4 | a = ["a"]
| ^
Warning: ‘a.dim[0].ubound’ is used uninitialized [-Wuninitialized]
test.f90:3:41:
3 | character(len=1), allocatable :: a(:)
| ^
note: ‘a’ declared here
test.f90:4:13:
4 | a = ["a"]
| ^
Warning: ‘a.dim[0].lbound’ may be used uninitialized [-Wmaybe-uninitialized]
test.f90:3:41:
3 | character(len=1), allocatable :: a(:)
| ^
note: ‘a’ declared here
test.f90:4:13:
4 | a = ["a"]
| ^
Warning: ‘a.dim[0].ubound’ may be used uninitialized [-Wmaybe-uninitialized]
test.f90:3:41:
3 | character(len=1), allocatable :: a(:)
| ^
note: ‘a’ declared here
test.f90:4:13:
4 | a = ["a"]
| ^
Warning: ‘a.dim[0].ubound’ may be used uninitialized [-Wmaybe-uninitialized]
test.f90:3:41:
3 | character(len=1), allocatable :: a(:)
| ^
note: ‘a’ declared here
test.f90:4:13:
4 | a = ["a"]
| ^
Warning: ‘a.dim[0].lbound’ may be used uninitialized [-Wmaybe-uninitialized]
test.f90:3:41:
3 | character(len=1), allocatable :: a(:)
| ^
note: ‘a’ declared here
Any ideas what is going on? Is that the expected behaviour and I am missing something?
Yes, the -Wuninitialized option of gfortran produces so many false positives that it should usually be turned off, especially if you use the allocation-on-assignment feature of Fortran. For example, for the code
implicit none
integer, allocatable :: v(:)
v = [10,20]
print*,v
end
you get
c:\fortran\test>gfortran -Wall -Wextra xgfortran_warn.f90
xgfortran_warn.f90:3:11:
3 | v = [10,20]
| ^
Warning: 'v.offset' is used uninitialized [-Wuninitialized]
xgfortran_warn.f90:2:28:
2 | integer, allocatable :: v(:)
| ^
note: 'v' declared here
xgfortran_warn.f90:3:11:
3 | v = [10,20]
| ^
Warning: 'v.dim[0].lbound' is used uninitialized [-Wuninitialized]
xgfortran_warn.f90:2:28:
2 | integer, allocatable :: v(:)
| ^
note: 'v' declared here
xgfortran_warn.f90:3:11:
3 | v = [10,20]
| ^
Warning: 'v.dim[0].ubound' is used uninitialized [-Wuninitialized]
xgfortran_warn.f90:2:28:
2 | integer, allocatable :: v(:)
| ^
note: 'v' declared here
xgfortran_warn.f90:3:11:
3 | v = [10,20]
| ^
Warning: 'v.dim[0].lbound' may be used uninitialized [-Wmaybe-uninitialized]
xgfortran_warn.f90:2:28:
2 | integer, allocatable :: v(:)
| ^
note: 'v' declared here
xgfortran_warn.f90:3:11:
3 | v = [10,20]
| ^
Warning: 'v.dim[0].ubound' may be used uninitialized [-Wmaybe-uninitialized]
xgfortran_warn.f90:2:28:
2 | integer, allocatable :: v(:)
| ^
note: 'v' declared here
xgfortran_warn.f90:3:11:
3 | v = [10,20]
| ^
Warning: 'v.dim[0].ubound' may be used uninitialized [-Wmaybe-uninitialized]
xgfortran_warn.f90:2:28:
2 | integer, allocatable :: v(:)
| ^
note: 'v' declared here
xgfortran_warn.f90:3:11:
3 | v = [10,20]
| ^
Warning: 'v.dim[0].lbound' may be used uninitialized [-Wmaybe-uninitialized]
xgfortran_warn.f90:2:28:
2 | integer, allocatable :: v(:)
| ^
note: 'v' declared here
I see, thanks @Beliavsky. I am curious if there is a bug report tracking this in Bugzilla; I couldn’t find anything, so maybe it would be a good idea to file one. I find the the uninitialised warnings in C++ extremely useful, so it would be great if gfortran could also improve.
OK, but that would make the reader of the code wonder why the allocate(a(0)) statement appears. I would rather use the -Wno-uninitialized option than add such code.
ETA: gfortran -Wuninitialized is silent about uninitialized variable abc in the code below, which further suggests disabling the option.
implicit none
integer, allocatable :: v(:), abc(:)
character(len=1), allocatable :: a(:)
a = ["a"]
print*, a
v = [10,20]
print*,v
allocate (abc(1))
print*,abc
end