c: Avoid false positive for useless casts and generic [PR125261]
The compiler now avoids issuing false warnings for useless casts in C code, particularly with generic selections.
This commit reduces false positive warnings for -Wuseless-cast in C code, specifically related to generic selections. The fix guards the -Wuseless-cast warning with c_inhibit_evaluation_warnings. It increments this counter during generic association when a prior match (other than the default) has been seen, improving accuracy in detecting genuinely useless casts. New test case gcc.dg/pr125261.c validates the fix.
In Details
This commit modifies c-parser.cc and c-typeck.cc to refine the -Wuseless-cast diagnostic. By using c_inhibit_evaluation_warnings the code avoids issuing the warning when it's not actually a useless cast. The logic in c_parser_generic_selection increments c_inhibit_evaluation_warnings when a non-default generic association is matched before the default association. This helps to avoid false positives especially when the default association comes last. A new test case gcc.dg/pr125261.c is added to verify the fix for PR c/125261.
For Context
The -Wuseless-cast compiler warning informs the programmer about casts that have no effect on the value being cast. Generic selections in C allow you to choose different expressions based on the type of an argument. This commit fixes an issue where the compiler would incorrectly warn about useless casts in code that uses generic selections, even when the cast was necessary to ensure the correct type was selected. The changes reduce the number of false warnings, improving the developer experience.