]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
ada: Do not generate incorrect warning about redundant type conversion
authorSteve Baird <baird@adacore.com>
Mon, 16 Jun 2025 23:46:13 +0000 (16:46 -0700)
committerMarc Poulhiès <dkm@gcc.gnu.org>
Fri, 4 Jul 2025 07:41:47 +0000 (09:41 +0200)
If -gnatwr is enabled, then in some cases a type conversion between two
different Boolean types incorrectly results in a warning that the conversion
is redundant.

gcc/ada/ChangeLog:

* sem_res.adb (Resolve_Type_Conversion): Replace code for
detecting a similar case with a more comprehensive test.

gcc/ada/sem_res.adb

index 96e8da6b54e268f5f066611eada1cc4dbef294d6..e44994a681d79b0ef30a86852856df615e0382fb 100644 (file)
@@ -12463,16 +12463,6 @@ package body Sem_Res is
          Orig_N := Original_Node (Expression (Orig_N));
          Orig_T := Target_Typ;
 
-         --  If the node is part of a larger expression, the Target_Type
-         --  may not be the original type of the node if the context is a
-         --  condition. Recover original type to see if conversion is needed.
-
-         if Is_Boolean_Type (Orig_T)
-          and then Nkind (Parent (N)) in N_Op
-         then
-            Orig_T := Etype (Parent (N));
-         end if;
-
          --  If we have an entity name, then give the warning if the entity
          --  is the right type, or if it is a loop parameter covered by the
          --  original type (that's needed because loop parameters have an
@@ -12548,6 +12538,16 @@ package body Sem_Res is
             then
                null;
 
+            --  Do not warn if original source-level conversion was
+            --  between two different types.
+
+            elsif Nkind (Original_Node (N)) = N_Type_Conversion
+              and then
+                Base_Type (Etype (Subtype_Mark (Original_Node (N))))
+                  /= Base_Type (Etype (Expression (Original_Node (N))))
+            then
+               null;
+
             --  Here we give the redundant conversion warning. If it is an
             --  entity, give the name of the entity in the message. If not,
             --  just mention the expression.