[patch, fortran, committed] Add a few cases where -Wwarn-unused-but-set-variable should not warn
Thomas Koenig <[email protected]> Sun, 14 Jun 2026 09:42:11 +0200
| Newsgroups | gmane.comp.gcc.patches,gmane.comp.gcc.fortran |
|---|---|
| Message-ID | <[email protected]> |
Hello world, I just committed the patch below as obvious and simple after regression-testing as r17-1546-g93c0eb70fe729f656dbfff811a8ca45fc9607361 . (Unlike https://gcc.gnu.org/pipermail/fortran/2026-June/064097.html , which I submitted for review). Best regards Thomas Add a few cases where -Wwarn-unused-but-set-variable should not warn. When variables are use-associated, volatile or asynchronous, reference or definition may not be visible in the local namespace. Thus, they need to be excluded from unused vs set warnings. gcc/fortran/ChangeLog: PR fortran/30438 * resolve.cc (find_unused_vs_set): Exclude variables from cwarnings if use_assoc, volatile_ or asynchronous are set. gcc/testsuite/ChangeLog: PR fortran/30438 * gfortran.dg/warn_unused_but_set_variable_3.f90: New test.
p13.diff
(text/x-patch, 1.4 KB)
diff --git a/gcc/fortran/resolve.cc b/gcc/fortran/resolve.cc
index 0a7f85f90a0..74d2b20ca9d 100644
--- a/gcc/fortran/resolve.cc
+++ b/gcc/fortran/resolve.cc
@@ -20789,7 +20789,8 @@ find_unused_vs_set (gfc_symbol *sym)
|| attr->omp_declare_target_indirect || attr->oacc_declare_create
|| attr->oacc_declare_copyin || attr->oacc_declare_deviceptr
|| attr->oacc_declare_device_resident || attr->oacc_declare_link
- || attr->result || attr->warning_emitted || !attr->referenced)
+ || attr->result || attr->warning_emitted || attr->use_assoc
+ || attr->volatile_ || attr->asynchronous || !attr->referenced)
return;
if (warn_unused_intent_out && attr->value_set == VALUE_INTENT_OUT
diff --git a/gcc/testsuite/gfortran.dg/warn_unused_but_set_variable_3.f90 b/gcc/testsuite/gfortran.dg/warn_unused_but_set_variable_3.f90
new file mode 100644
index 00000000000..8c9cbdf6be1
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/warn_unused_but_set_variable_3.f90
@@ -0,0 +1,24 @@
+! { dg-do compile }
+! { dg-additional-options "-Wunused-but-set-variable" }
+! Some checks of things that should not warn
+module mymod
+ implicit none
+ real :: x
+contains
+ subroutine print_x
+ print *,x
+ end subroutine print_x
+ subroutine vol
+ end subroutine vol
+end module mymod
+
+program memain
+ use mymod
+ implicit none
+ integer, volatile :: a
+ real, asynchronous :: b
+ x = 42.
+ call print_x
+ a = 43.
+ b = 44.
+end program memain