-
-
Notifications
You must be signed in to change notification settings - Fork 619
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Disallow referencing the if
variable in the else
branch
#20835
Comments
@Bolpat I can have a go at this. |
I think this is more the job a linter. The reason for that is that I'm thinking about how this could be implemented and I see a depleasant problem that is that IdentExps sema would be slightly slower in the Also what if one day D adds "inline-variables" as a new kind of primary expression. That would mean that every variables used in the if-condition would have to be checked. |
I’m not sure what that means. You might mean something like C# You might also mean something like If I understood you correctly, it’s not an issue. |
@Bolpat is this still an open task then? |
I don’t see why it wouldn’t. |
A variable declared in the condition of an
if
statement is considered not to be in scope in theelse
branch. That makes sense insofar as the variable (usually) has a trivial/zero value. However, that might lead to accidentally referencing something else, such as a member variable or a global variable, by programmers coming from other languages where anif
variable declaration either extends into theelse
branch, too, (e.g. C++) or even the whole end of the scope containing theif
statement (e.g. C#). That can lead to subtle bugs. It would be preferable to require accessing these via the usual disambiguation mechanism, i.e. require usingthis.Identifier
or.Identifier
even if the local variable is technically not in scope. Code that declares a new variable of the same name as theif
variable should be unaffected: It’s clear that the programmer understood that theif
variable is not in scope and it’s clear to the reader which variable it refers to, as a declaration in theelse
branch is the closest one.The text was updated successfully, but these errors were encountered: