# (c++) warning when local variable is overriden by another local variable

This topic is 3033 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi Do you know a way to have Visual Studio spit out a warning in the following scenario:
float f = 0.0f;
// ... (many lines of code)
if(...)
{
float f = 10.0f;
// ...
}
// ... (use f)

I've tried with level-4 warnings on VS2008, the compiler is (understandably) perfectly ok with it.

##### Share on other sites
I don't know of any way to make the compiler complain about this, but I think it's perfectly reasonable that it doesn't. One of the advantages of limited scopes is that collisions are not a problem.

##### Share on other sites
This is actually very nice functionality (especially when coming from Java which doesn't even allow this).

##### Share on other sites
I consider it both dangerous and bad practice, which is why I wish visual studio had a warning (people who like to use that language functionality could still disable it)
But that's just me of course. Thanks anyway.

##### Share on other sites
You're probably going to have to use a static code analysis tool, along the lines of lint in order to get warnings for this behaviour.
I'm not experienced with these tools, so unfortunately I can't tell you which (or any) support this warning.

##### Share on other sites
Quote:
 Original post by alvaroI don't know of any way to make the compiler complain about this, but I think it's perfectly reasonable that it doesn't. One of the advantages of limited scopes is that collisions are not a problem.

Though its a nasty habit to pick up if you ever plan on working with a dynamic language.

##### Share on other sites
Quote:
 Original post by janta// ... (many lines of code)
That's a bit of its own problem in a way, isn't it?

##### Share on other sites
Quote:
Original post by nobodynews
Quote:
 Original post by janta// ... (many lines of code)
That's a bit of its own problem in a way, isn't it?

It is indeed but well, real world projects, stuff like that... Stuff isn't always done the way we wish it were.

And not so many lines are necessary to make a programmer waste a handful of hours figuring out why "f" does not have the value he expects it to have. Especially when he's looking at complex code and suspects an entirely different problem... like a thread synchronization issue... x-(

##### Share on other sites
You can try compiling with /Wall, if there's a warning for such a thing you'll see it.

##### Share on other sites
That reminds me that a bunch of co-workers and I think that
if ( conditional );{    DoSomething();}

Should be a warning. There are almost 0 cases where you'd actually want that to be correct, but a lot of cases (careless copy-paste) where you can quickly end up with that code.

The variable masking is annoying too. My favorite problem there is that I'd like to know when I have:
for( int i = 0; i < 10; ++i )    for( int i = 0; i < 10; ++i )        foo.bar;

Again, symptomatic of lazy copy paste(and nondescript loop counter naming), but still rather annoying when it happens.

##### Share on other sites
Quote:
 Original post by KulSeranThat reminds me that a bunch of co-workers and I think thatif ( conditional );{ DoSomething();}Should be a warning. There are almost 0 cases where you'd actually want that to be correct.

In MSVC on default warning level (/W3) I get:
warning C4390: ';' : empty controlled statement found; is this the intent?

I didn't try gcc though.

##### Share on other sites
Quote:
 In MSVC on default warning level (/W3) I get:warning C4390: ';' : empty controlled statement found; is this the intent?I didn't try gcc though.

Nice to know at least one compiler does that!
The one we used at work did not with /Wall and settings warnings as errors.

##### Share on other sites
Quote:
 Original post by KulSeranThe variable masking is annoying too. My favorite problem there is that I'd like to know when I have:for( int i = 0; i < 10; ++i ) for( int i = 0; i < 10; ++i ) foo.bar;

Visual C++ warns you when you do that. It'll say that it doesnt know which i you are refering to on the 2nd for cycle. Can't remember the exact message though, and i'm not at home right now to test it.

However, it won't warn you if you do this:
for( int i = 0; i < 10; ++i )    for( int j = 0; j < 10; ++i )        foo.bar[j];

##### Share on other sites
Quote:
Original post by KulSeran
Quote:
 In MSVC on default warning level (/W3) I get:warning C4390: ';' : empty controlled statement found; is this the intent?I didn't try gcc though.

Nice to know at least one compiler does that!
Gcc certainly does too, it tells you something like "suggest placing {} around empty statement". So basically you have to write
if(blah){ ; }

so the compiler will quietly assume that you really meant what you wrote, otherwise it will assume it's a typo. It raises similar complaints with operator precedence that may either be ambiguous or not what most people would expect, anyway. This can improve your code quality a lot and will save you many wasted hours of searching for really stupid errors.

@Janta:
You might try and search the manual for "shadow", as what you describe is called shadowing. Gcc will, with default warnings enabled, tell you something "variable x shadows variable x first declared here: <line number>" (can be explicitely turned on/off, too).
I would be very surprised if Visual C++ couldn't be convinced to provide a similar warning, as this too is a rather important hint both for hard to track errors and for avoiding confusion among others reading your code.

##### Share on other sites
I just found warning C6244 but the same sample code does not produce that warning on my machine even with /wall
Manually enabling this warning with a pragma says "unknown warning type"

I'm using VS2008 Pro... maybe the Team edition is required?

##### Share on other sites
Quote:
 Original post by jantaI just found warning C6244 but the same sample code does not produce that warning on my machine even with /wallManually enabling this warning with a pragma says "unknown warning type"How about everyone else?I'm using VS2008 Pro... maybe the Team edition is required?

Looks like those warnings are given by the Code Analysis for C/C++ tool, which is part of the Visual Studio Team System.

A shame :(

##### Share on other sites
Quote:
 Original post by samothGcc certainly does too, it tells you something like "suggest placing {} around empty statement". So basically you have to write if(blah){ ; }

Gcc with -Wall does not, but -Wextra enables this warning for if, while, for and do-while.

##### Share on other sites
Quote:
 Original post by mattdLooks like those warnings are given by the Code Analysis for C/C++ tool, which is part of the Visual Studio Team System.A shame :(

/analyze does work with non-trial versions of VS2008 Professional. You do have to add it to the command line manually though.