# Warning: ‘.str’ may be used uninitialized

**URL:** <https://fortran-lang.discourse.group/t/warning-str-may-be-used-uninitialized/5539>\
**Category:** Uncategorized\
**Created:** [April 10, 2023, 4:41am UTC](https://fortran-lang.discourse.group/t/warning-str-may-be-used-uninitialized/5539 "2023-04-10T04:41:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![zaikunzhang](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/zaikunzhang/32/785_2.png) [@zaikunzhang](https://fortran-lang.discourse.group/u/zaikunzhang)\
**Post date:** [April 10, 2023, 4:41am UTC](https://fortran-lang.discourse.group/t/warning-str-may-be-used-uninitialized/5539/1 "2023-04-10T04:41:50Z")

</div>

Code:

```auto
! test_char.f90
program test_char
implicit none
character(len=:), allocatable :: str
str = 'hello world'
end program test_char

```

Test:

```auto
$ gfortran --version && gfortran -g -Wall -std=f2018 test_char.f90

GNU Fortran (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0

test_char.f90:9:19:

    9 | str = 'hello world'
      | ^
Warning: ‘.str’ may be used uninitialized [-Wmaybe-uninitialized]

```

**Questions** :

1. Is the code standard-conforming?
2. Why `str` is considered maybe uninitialized?
3. What is the dot preceding `str`?

**Remarks**

1. I am aware that `-Wall` implies `-Wmaybe-uninitialized`. What interests me is not why this option is turned on or whether it should be, but rather why the code triggers this warning.
2. The following compilation finishes without warning.

```auto
	ifort -g -warn all -check all -debug extended test_char.f90
	ifx -g -warn all -check all -debug extended test_char.f90
	nagfor -g -C test_char.f90
	flang -g -Wall -Wextra test_char.f90
	nvfortran -g -Wall -Wextra test_char.f90
	aocc-flang -g -Wall -Wextra test_char.f90
	sunf95 -g -w3 -ansi -xcheck=%all -C test_char.f90

```

Thanks.

---

<div class="post-metadata">

**Author:** ![band-a-prend](https://avatars.discourse-cdn.com/v4/letter/b/7c8e57/32.png) [@band-a-prend](https://fortran-lang.discourse.group/u/band-a-prend)\
**Post date:** [April 10, 2023, 10:29am UTC](https://fortran-lang.discourse.group/t/warning-str-may-be-used-uninitialized/5539/2 "2023-04-10T10:29:41Z")

</div>

It seems it’s a gfortran issue [91442 – Wrong "may be used uninitialized" warning with allocation on assignment](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91442)  
(or even early [56670 – Allocatable-length character var causes bogus warning with -Wuninitialized](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56670))

As mentioned there the addition of optimization key e.g. ‘-O2’ removes this warning.

---

<div class="post-metadata">

**Author:** ![zaikunzhang](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/zaikunzhang/32/785_2.png) [@zaikunzhang](https://fortran-lang.discourse.group/u/zaikunzhang)\
**Post date:** [April 10, 2023, 4:11pm UTC](https://fortran-lang.discourse.group/t/warning-str-may-be-used-uninitialized/5539/3 "2023-04-10T16:11:56Z")

</div>

Thank you @band-a-prend for the information.

---

<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:** [April 10, 2023, 5:21pm UTC](https://fortran-lang.discourse.group/t/warning-str-may-be-used-uninitialized/5539/4 "2023-04-10T17:21:25Z")

</div>

A related thread was [Gfortran uninitialized warnings](https://fortran-lang.discourse.group/t/gfortran-uninitialized-warnings/3838). These warnings of gfortran are sometimes spurious, so I turn them off.

---

<div class="post-metadata">

**Author:** ![nbehrnd](https://avatars.discourse-cdn.com/v4/letter/n/e0b2c6/32.png) [@nbehrnd](https://fortran-lang.discourse.group/u/nbehrnd)\
**Post date:** [April 13, 2023, 8:25pm UTC](https://fortran-lang.discourse.group/t/warning-str-may-be-used-uninitialized/5539/5 "2023-04-13T20:25:34Z")

</div>

With copy-paste of your code and a more recent / contemporary release of gfortran 12.2.0 as provided by Linux Debian 12/bookworm (currently branch testing), I do not observe an error warning. To quote the CLI

```plaintext
$ gfortran --version && gfortran -g -Wall -std=f2018 test_char.f90
GNU Fortran (Debian 12.2.0-14) 12.2.0
Copyright (C) 2022 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

```

---

<div class="post-metadata">

**Author:** ![zaikunzhang](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/zaikunzhang/32/785_2.png) [@zaikunzhang](https://fortran-lang.discourse.group/u/zaikunzhang)\
**Post date:** [April 15, 2023, 4:40am UTC](https://fortran-lang.discourse.group/t/warning-str-may-be-used-uninitialized/5539/6 "2023-04-15T04:40:59Z")

</div>

Thank you @nbehrnd . The same on my side. It is great to see the bug fixed in gfortran-12.
