Skip to content

Commit 6b46add

Browse files
committed
Fix #557: use VB semantic model to detect char-typed default for String params
ExplicitDefaultValue is normalized to the parameter's declared type, so checking `ExplicitDefaultValue is not char` was always true for String params. Instead, retrieve the VB syntax default expression and check its actual type via the semantic model. https://claude.ai/code/session_01AkwUvu3XuCdj3D4axoX4UX
1 parent 71042ab commit 6b46add

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

CodeConverter/CSharp/DeclarationNodeVisitor.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,7 @@ public override async Task<CSharpSyntaxNode> VisitMethodBlock(VBSyntax.MethodBlo
675675
convertedStatements = convertedStatements.InsertNodesBefore(firstResumeLayout, _typeContext.HandledEventsAnalysis.GetInitializeComponentClassEventHandlers());
676676
}
677677

678-
(methodBlock, convertedStatements) = FixCharDefaultsForStringParams(declaredSymbol, methodBlock, convertedStatements);
678+
(methodBlock, convertedStatements) = FixCharDefaultsForStringParams(declaredSymbol, methodBlock, convertedStatements, _semanticModel);
679679

680680
var body = _accessorDeclarationNodeConverter.WithImplicitReturnStatements(node, convertedStatements, csReturnVariableOrNull);
681681

@@ -687,7 +687,7 @@ public override async Task<CSharpSyntaxNode> VisitMethodBlock(VBSyntax.MethodBlo
687687
/// Fix: replace the default with null and prepend a null-coalescing assignment in the method body.
688688
/// </summary>
689689
private static (BaseMethodDeclarationSyntax MethodBlock, BlockSyntax ConvertedStatements) FixCharDefaultsForStringParams(
690-
IMethodSymbol declaredSymbol, BaseMethodDeclarationSyntax methodBlock, BlockSyntax convertedStatements)
690+
IMethodSymbol declaredSymbol, BaseMethodDeclarationSyntax methodBlock, BlockSyntax convertedStatements, SemanticModel semanticModel)
691691
{
692692
var prependedStatements = new List<StatementSyntax>();
693693
var updatedParams = methodBlock.ParameterList.Parameters.ToList();
@@ -696,8 +696,13 @@ private static (BaseMethodDeclarationSyntax MethodBlock, BlockSyntax ConvertedSt
696696
for (int i = 0; i < updatedParams.Count && i < vbParams.Length; i++) {
697697
var vbParam = vbParams[i];
698698
if (vbParam.Type.SpecialType != SpecialType.System_String
699-
|| !vbParam.HasExplicitDefaultValue
700-
|| vbParam.ExplicitDefaultValue is not char) continue;
699+
|| !vbParam.HasExplicitDefaultValue) continue;
700+
// ExplicitDefaultValue is normalized to the parameter's declared type (String), so we
701+
// must inspect the VB syntax to detect when the original expression is Char-typed.
702+
var vbSyntaxParam = vbParam.DeclaringSyntaxReferences.FirstOrDefault()?.GetSyntax() as VBSyntax.ParameterSyntax;
703+
var defaultValueNode = vbSyntaxParam?.Default?.Value;
704+
if (defaultValueNode == null) continue;
705+
if (semanticModel.GetTypeInfo(defaultValueNode).Type?.SpecialType != SpecialType.System_Char) continue;
701706

702707
var csParam = updatedParams[i];
703708
var defaultExpr = csParam.Default?.Value;

0 commit comments

Comments
 (0)