Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement FixAllProvider for RCS1014 #1070

Merged

Conversation

jamesHargreaves12
Copy link
Contributor

@jamesHargreaves12 jamesHargreaves12 commented Apr 13, 2023

This PR introduces a new FixAllProvider for RCS1014, designed to apply code fixes sequentially in reverse order by location within the document. This approach is safe because applying a given code fix for RCS1014 will not impact the text prior to it.

The FixAllProvider is constructed to minimize its coupling to any RCS1014-specific logic, making it easier to extend for other code fixes that satisfy the following property:

  • Applying a code fix for Diagnostic X at location Y would not change the location, validity or code fixes of a Diagnostics of the same type as X but that occurs earlier in the file

As an aside, when first looking at this, I tried using WellKnownFixAllProviders.BatchFixAll rather than implementing something new. On occasion, this produced garbage code. For example:

    public static TheoryData<string[], int[]?, int[]?, int[]?, int[]?, int?[]?, int[]> M()
    {
        var l = nameof(X);
        var r = nameof(Y);
        return new()
        {
            { new[] { l, l, r, r }, null, null, new[] { 1 }, null, null, new[] { 1, 3, 0, 2 } },
            { new[] { l, l, l, r, r, r }, null, null, null, null, null, new[] { 2 } },
        };
    }

Gets converted to the following:

    public static TheoryData<string[], int[]?, int[]?, int[]?, int[]?, int?[]?, int[]> M()
    {
        var l = nameof(X);
        var r = nameof(Y);
        return new()
        {
            { new string[] { l, l, r, r }, null, null, new[] { 1 }, null, null, new[] { 1, 3, 0, 2 } },
            { new int[] { 1 }, null, null, new[] { l, l, l, r, r, r }, null, null, null, null, null, new int[] { 2 } },
        };
    }

This is an issue within the merge algorithm of WellKnownFixAllProviders.BatchFixAll. After reviewing related issues on the Roslyn GitHub, it became evident that this FixAllProvider is not expected to work in all scenarios, and implementing a custom solution is preferred.

@josefpihrt
Copy link
Collaborator

Hi,

it would be better to open a new issue first and then open a PR.

I used your code sample (+ I used TheoryData from xUnit). I applied code fix "Use explicitly typed array" with option "Fix all occurrences in document". The result is as expected:

    public static TheoryData<string[], int[]?, int[]?, int[]?, int[]?, int?[]?, int[]> M()
    {
        var l = nameof(X);
        var r = nameof(Y);
        return new()
        {
            { new string[] { l, l, r, r }, null, null, new int[] { 1 }, null, null, new int[] { 1, 3, 0, 2 } },
            { new string[] { l, l, l, r, r, r }, null, null, null, null, null, new int[] { 2 } },
        };
    }

@jamesHargreaves12
Copy link
Contributor Author

I am surprised that you didn't run into the same issue. But I have opened an Issue as requested that contains a more full description of how to reproduce the issue. #1075

ChangeLog.md Outdated Show resolved Hide resolved
@josefpihrt josefpihrt merged commit 47b002c into dotnet:main Aug 16, 2023
Haarmees pushed a commit to Haarmees/Roslynator that referenced this pull request Oct 30, 2023
Co-authored-by: Josef Pihrt <josef@pihrt.net>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants