https://gcc.gnu.org/g:93c0eb70fe729f656dbfff811a8ca45fc9607361

commit r17-1546-g93c0eb70fe729f656dbfff811a8ca45fc9607361
Author: Thomas Koenig <[email protected]>
Date:   Sun Jun 14 09:33:41 2026 +0200

    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.

Diff:
---
 gcc/fortran/resolve.cc                             |  3 ++-
 .../gfortran.dg/warn_unused_but_set_variable_3.f90 | 24 ++++++++++++++++++++++
 2 files changed, 26 insertions(+), 1 deletion(-)

diff --git a/gcc/fortran/resolve.cc b/gcc/fortran/resolve.cc
index 281eede2f366..57561e8686fc 100644
--- a/gcc/fortran/resolve.cc
+++ b/gcc/fortran/resolve.cc
@@ -20812,7 +20812,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 000000000000..8c9cbdf6be1d
--- /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

Reply via email to