Bug 52050 - Want an option to warn about a declaration inside a for/while/if statements.
Summary: Want an option to warn about a declaration inside a for/while/if statements.
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 4.6.2
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks: new-warning, new_warning
  Show dependency treegraph
 
Reported: 2012-01-29 22:50 UTC by Mans Rullgard
Modified: 2023-05-20 04:19 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2012-01-29 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Mans Rullgard 2012-01-29 22:50:24 UTC
Consider this code:

void foo(void)
{
    for (int i = 0; i < 2; i++);
}

Compiling the above code with -std=c99 -Wdeclaration-after-statement
does not produce a warning as expected.
Comment 1 Steven Bosscher 2012-01-29 23:26:47 UTC
Yup. Also fails for me with GCC 4.3.2 and GCC 4.1.1. I don't have anything older available.
Comment 2 Mans Rullgard 2012-01-29 23:34:12 UTC
I tested and found it failing with 3.4.6, 4.1.2, 4.2.4, 4.3.6, 4.4.6, 4.5.3, and 4.6.2.
Comment 3 Andrew Pinski 2012-01-30 00:36:54 UTC
I think -Wdeclaration-after-statement would the wrong option really.  
The declaration in "for (int i = 0; i < 2; i++)" is not after a statement.  I would think we should have a different warning option for this.  Also the warning option is there mostly to warn as we turn on this extension by default for GNU C90 while the declaration inside a for loop support is not turned on for GNU C90.
Comment 4 Mans Rullgard 2012-01-30 00:45:05 UTC
I'm not here to argue semantics.  I use -Wdeclaration-after-statement to avoid accidentally introducing code that will fail with compilers that do not support this.  Having an equivalent for the for loop construct would have saved me some time today.  If all compilers supported C99 fully, there would be no need for any of this, but sadly this is not the case.
Comment 5 Richard Biener 2012-01-30 09:23:45 UTC
I agree that a separate warning option should be used here.
Comment 6 Marek Polacek 2015-12-16 14:12:08 UTC
This would be easy to implement, if it's still wanted.  I suggest the name "-Wdecl-in-for-loop".
Comment 7 Andrew Pinski 2023-05-20 04:19:00 UTC
This is now warning with -Wc90-c99-compat (since GCC 9). Though it does not have its own option.