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

Đang mở
#1,300 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 3 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
68/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
csharp
Lĩnh vực
tooling

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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).
Ngôn ngữ chính
C#
Star
917
Fork
243
Merge trung bình
2 ngày 15 giờ
Pull request đã merge (30 ngày)
9

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của icsharpcode/CodeConverter

Tất cả issue của icsharpcode/CodeConverter

Issue tương tự

Thêm issue về C#

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.