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

Started by
15 comments, last by adeyblue 14 years, 5 months ago
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.
Advertisement
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.
This is actually very nice functionality (especially when coming from Java which doesn't even allow this).
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.
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.
Quote:Original post by alvaro
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.


Though its a nasty habit to pick up if you ever plan on working with a dynamic language.
Quote:Original post by janta
// ... (many lines of code)

That's a bit of its own problem in a way, isn't it?

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

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-(

You can try compiling with /Wall, if there's a warning for such a thing you'll see it.
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.

This topic is closed to new replies.

Advertisement