Bug 100956 - Unused variable warnings ignore "if constexpr" blocks where variables are conditionally used
Summary: Unused variable warnings ignore "if constexpr" blocks where variables are con...
Status: RESOLVED DUPLICATE of bug 81676
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 9.2.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks:
 
Reported: 2021-06-08 01:23 UTC by Matt Bentley
Modified: 2021-06-09 00:14 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Matt Bentley 2021-06-08 01:23:18 UTC
For example, as part of a container class:

void remove_memory_blocks(pointer back_element_in_final_block)
{
   if constexpr(!std::is_trivially_destructible<element_type>::value)
   {
      // destroy each element in each memory block until the back_element is reached
   }

   // remove memory blocks
}


If element_type is_trivially_destructible, G++ will warn that back_element_in_final_block is unused every time the function is called.

Ideally this warning should be performed before constexpr blocks are removed by a parser (I have no idea what the procedure is for GCC, I'm just guessing here)
Comment 1 Jonathan Wakely 2021-06-08 12:35:32 UTC
The warning is already fixed in the GCC 10 branch.

N.B. if you have a loop that does nothing but run trivial destructors, GCC will optimize it out anyway.

*** This bug has been marked as a duplicate of bug 81676 ***
Comment 2 Matt Bentley 2021-06-09 00:14:53 UTC
Thank you - I'm aware GCC might optimize it out (and failed to test with GCC10), at least in O2 mode, but other compilers might not, hence the code.