Roslyn: IDE0057: Invalid code fix with string.Substring and null-conditional operator

Created on 27 Aug 2020  路  2Comments  路  Source: dotnet/roslyn

Version Used:
16.8.0 Preview 2.0

Steps to Reproduce:
```C#

nullable enable

public class Test
{
public string? M(string? arg)
=> arg?.Substring(42); // IDE0057
}


**Expected Behavior**:
```C#
#nullable enable
public class Test
{
    public string? M(string? arg)
        => arg?[42..]; // IDE0057
}

Actual Behavior:
The code fix generates invalid code:
```C#

nullable enable

public class Test
{
public static string? M(string? arg)
=> arg?.Substring[42..]; // IDE0057
}
`` Note that the.Substring` is still there.

image

Area-IDE Bug IDE-CodeStyle help wanted

Most helpful comment

Expected Behavior:
I think the Analyzer should not be triggered, as there is no other way to use the range operator.

I think it should be triggered, but produces arg?[42..]

All 2 comments

Expected Behavior:
I think the Analyzer should not be triggered, as there is no other way to use the range operator.

I think it should be triggered, but produces arg?[42..]

@Youssef1313 's desired behavior is correct. That's what we should be generating.

Was this page helpful?
0 / 5 - 0 ratings