VB -> C#: Nullable comparisons in While conditions produce invalid C#

未关闭
#1,300 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 3 天内回复

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
68/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
csharp
领域
tooling

调研方向

Start with the VB input and expected C# output in the issue, then trace how the converter handles While conditions and nullable comparisons. Add a regression test for the supplied case, including the parenthesized comparison variant. Done means the generated condition is a non-nullable bool and preserves VB's behavior when either value is Nothing.

由索引模型根据 Issue 内容生成。

描述

VB -> C#
VB.Net input code
Imports System

Class TestClass
    Sub TestMethod(startDate As Date?, endDate As Date?)
        Dim currentDate = startDate
        While currentDate <= endDate
            currentDate = currentDate.Value.AddMinutes(1)
        End While
    End Sub
End Class
Erroneous output

The converted while condition evaluates to bool? rather than bool, producing:

CS0266: Cannot implicitly convert type 'bool?' to 'bool'.
An explicit conversion exists (are you missing a cast?)
Expected output
using System;

internal partial class TestClass
{
    public void TestMethod(DateTime? startDate, DateTime? endDate)
    {
        var currentDate = startDate;
        while (currentDate.HasValue && endDate.HasValue && currentDate.Value <= endDate.Value)
            currentDate = currentDate.Value.AddMinutes(1d);
    }
}
Details
  • Product in use: VS extension, built locally from master.
  • Version in use: 10.0.1, local development build.
  • Did you see it working in a previous version, which? No.
  • VB permits nullable comparisons in While conditions. If the condition evaluates to Nothing, the loop body is not executed. C# requires a non-nullable bool condition.
  • The converter does not recognize While as a context requiring a non-nullable Boolean result, so it preserves the comparison's nullable result instead of treating Nothing as false.
  • The issue also occurs when the comparison is enclosed in parentheses: While (currentDate <= endDate).
主要语言
C#
星标
917
派生
243
平均合并
2 天 15 小时
30 天内合并 PR
9

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

icsharpcode/CodeConverter 的其他 Issue

查看 icsharpcode/CodeConverter 的全部 Issue

相似的 Issue

更多 C# Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。