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.
(c++) warning when local variable is overriden by another local variable
Hi
Do you know a way to have Visual Studio spit out a warning in the following scenario:
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.
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.
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 jantaThat's a bit of its own problem in a way, isn't it?// ... (many lines of code)
Quote:Original post by nobodynewsQuote:Original post by jantaThat's a bit of its own problem in a way, isn't it?// ... (many lines of code)
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-(
That reminds me that a bunch of co-workers and I think that
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:
Again, symptomatic of lazy copy paste(and nondescript loop counter naming), but still rather annoying when it happens.
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
Popular Topics
Advertisement