Bug 37831 - -Wshadow warns about variables inside classes member functions
Summary: -Wshadow warns about variables inside classes member functions
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.2.1
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks:
 
Reported: 2008-10-15 01:36 UTC by Wolfgang Bangerth
Modified: 2008-10-15 01:51 UTC (History)
1 user (show)

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 Wolfgang Bangerth 2008-10-15 01:36:58 UTC
-Wshadow is silly about this piece of code:
---------------------------------
class Foo {
     int bar_;
   public:
     void bar(int bar)
       {
         bar_ = bar;
       }
    };
---------------------------------
This is a fairly common style. Note that local and member variable have
names that differ by the underscore. Yet:

g/x> c++ -Wshadow -c x.cc
x.cc: In member function 'void Foo::bar(int)':
x.cc:5: warning: declaration of 'bar' shadows a member of 'this'

I think that's being overly pedantic.

W.
Comment 1 Wolfgang Bangerth 2008-10-15 01:38:16 UTC
Note also that the documentation plainly states:

@item -Wshadow
@opindex Wshadow
@opindex Wno-shadow
Warn whenever a local variable shadows another local variable, parameter or
global variable or whenever a built-in function is shadowed.

The current behavior could therefore simply be classified as a bug.

W.
Comment 2 Andrew Pinski 2008-10-15 01:40:56 UTC
bar the function shadows bar the variable.  I think the warning is correct.  The variable _bar is not being taken into account at all.
Comment 3 Wolfgang Bangerth 2008-10-15 01:51:29 UTC
(In reply to comment #2)
> bar the function shadows bar the variable.  I think the warning is correct. 
> The variable _bar is not being taken into account at all.

Ah, bummer, I didn't even look close enough to see that the function and
its argument are named the same. Silly me, I just saw this piece of code
another project was unhappy about and thought I should submit this as a bug.

You're right, and I agree that the warning is valid.

W.