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

Aberta
#1,300 0 comentários 0 reações 0 responsáveis Ver no GitHub

Mantenedores costumam responder em até 3 dias

Ninguém assumiu esta issue ainda.

Avaliação

Dificuldade
3/5
Tempo estimado
1-2 dias
Facilidade para iniciantes
68/100
Tipo de issue
Bug
Clareza
Claramente especificada
Status de atividade
Ativa
Stack de tecnologia
csharp
Domínio
tooling

Direção de pesquisa

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.

Escrita pelo modelo de indexação a partir do texto da issue.

Descrição

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).
Linguagem predominante
C#
Estrelas
917
Forks
243
Merge médio
2d 15h
PRs com merge (30d)
9

Preparar o ambiente

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Mais de icsharpcode/CodeConverter

Todas as issues de icsharpcode/CodeConverter

Issues semelhantes

Mais issues de C#

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.