Skip to content

Commit

Permalink
LocalVariableHidesMemberIssue: suppress warning if the variable is in…
Browse files Browse the repository at this point in the history
…itialized using the member.
  • Loading branch information
dgrunwald committed Jul 9, 2013
1 parent 034517c commit 90d6545
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ public override void VisitVariableInitializer (VariableInitializer variableIniti

if (!(ctx.Resolve (variableInitializer) is LocalResolveResult))
return;
var mre = variableInitializer.Initializer as MemberReferenceExpression;
if (mre != null && mre.MemberName == variableInitializer.Name && mre.Target is ThisReferenceExpression) {
// Special case: the variable is initialized from the member it is hiding
// In this case, the hiding is obviously intentional and we shouldn't show a warning.
return;
}
CheckLocal(variableInitializer, variableInitializer.Name, variableInitializer.NameToken);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,5 +206,23 @@ void Method ()
}";
Test<LocalVariableHidesMemberIssue> (input, 0);
}

[Test]
public void SuppressIssueIfVariableInitializedFromField ()
{
var input = @"
class TestClass
{
int i;
void Method ()
{
int i = this.i;
}
}";
// Given the initializer, member hiding is obviously intended in this case;
// so we suppress the warning.
Test<LocalVariableHidesMemberIssue> (input, 0);
}
}
}

0 comments on commit 90d6545

Please sign in to comment.