When the syntax visitor encounter a node kind that doesn't have overridden Visit method, it simply returns 0.
And you don't override VisitLocalDeclarationStatement
, so that's exactly what happens, no matter what expression is inside that statement.
What you need to handle complex expressions inside local declarations is something like:
public override int VisitLocalDeclarationStatement(
LocalDeclarationStatementSyntax node)
{
return node.Declaration.Variables.Select(Visit).Max();
}
public override int VisitVariableDeclarator(VariableDeclaratorSyntax node)
{
if (node.Initializer == null)
return 0;
return Visit(node.Initializer.Value);
}
You will also need similar code for all cases when a lambda can appear inside another expression.
This is the reason why my original NestingLevelVisitor
throws an exception is DefaultVisit
: this way, you can detect node kinds that you don't handle yet and you are forced to explicitly specify how to handle them (instead of defaulting everything to “return 0”).