Jump to content
  • Advertisement
Sign in to follow this  
janta

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

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
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 this post


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

Share this post


Link to post
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 this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!