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

オープン
#1,300 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 3 日以内に返信

まだ誰も着手していません。

評価

難易度
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時間
マージ済み PR(30日)
9

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

icsharpcode/CodeConverter のほかの issue

icsharpcode/CodeConverter の issue をすべて見る

似ている issue

C# の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。