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

IDE0304 should preserve the leading comment #74208

Open
Bartleby2718 opened this issue Jun 28, 2024 · 5 comments · May be fixed by #74278
Open

IDE0304 should preserve the leading comment #74208

Bartleby2718 opened this issue Jun 28, 2024 · 5 comments · May be fixed by #74278
Labels
Area-IDE Bug help wanted The issue is "up for grabs" - add a comment if you are interested in working on it
Projects
Milestone

Comments

@Bartleby2718
Copy link

Version Used:

.NET SDK 8.0.205

Steps to Reproduce:

  1. Add the example code from https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/ide0304#example to a .cs file
// Code with violation.
var builder = ImmutableArray.CreateBuilder<int>();
builder.Add(1);
builder.AddRange(new int[] { 5, 6, 7 });
ImmutableArray<int> i = builder.ToImmutable();
  1. Use the suggested codefix for IDE0304

sharplab repro: https://sharplab.io/#gist:9c306bb8a2c25fa09562b016a4d5a550

Diagnostic Id: IDE0304 (Use collection expression for builder)

Expected Behavior: The code should be updated to

// Code with violation.
ImmutableArray<int> i = [1, .. new int[] { 5, 6, 7 }];

Actual Behavior: The code is updated to

ImmutableArray<int> i = [1, .. new int[] { 5, 6, 7 }];

Note the absence of the comment // Code with violation.

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-IDE untriaged Issues and PRs which have not yet been triaged by a lead labels Jun 28, 2024
@CyrusNajmabadi CyrusNajmabadi added Bug help wanted The issue is "up for grabs" - add a comment if you are interested in working on it and removed untriaged Issues and PRs which have not yet been triaged by a lead labels Jun 28, 2024
@CyrusNajmabadi CyrusNajmabadi added this to InQueue in Small Fixes via automation Jun 28, 2024
@CyrusNajmabadi CyrusNajmabadi added this to the Backlog milestone Jun 28, 2024
@DannySliekers
Copy link

Hey, can i pick this up if possible?

@CyrusNajmabadi
Copy link
Member

@DannySliekers Absolutely!

@DannySliekers
Copy link

So I did some investigating and found that the issue seems to lie at the removal of the LocalDeclarationStatement, as none of the leading and trailing trivia gets preserved:

// Remove the actual declaration of the builder.
subEditor.RemoveNode(analysisResult.LocalDeclarationStatement);

For the case of leading trivia i assume we want to include all leading trivia in the fixed code, right?
Example with trivia on same line:

// Code with violation.
/* test */ var builder = ImmutableArray.CreateBuilder<int>();
builder.Add(1);
builder.AddRange(new int[] { 5, 6, 7 });
ImmutableArray<int> i = builder.ToImmutable();

// Desired code
/* test */ ImmutableArray<int> i = [1, .. new int[] { 5, 6, 7 }];

For the case in which the declaration of the builder contains trailing trivia seems to be a little bit more complicated.
This seems like a desired result to me:

// Code with violation.
var builder = ImmutableArray.CreateBuilder<int>(); // test
builder.Add(1);
builder.AddRange(new int[] { 5, 6, 7 });
ImmutableArray<int> i = builder.ToImmutable();

//Desired code
ImmutableArray<int> i = [1, .. new int[] { 5, 6, 7 }]; // test

If the fix in the code above is desired, there is a case where there would be multiple options of preserving trivia:

var builder = ImmutableArray.CreateBuilder<int>(); // test
builder.Add(1);
builder.AddRange(new int[] { 5, 6, 7 });
ImmutableArray<int> i = builder.ToImmutable(); // test2

//Desired code #1? 
ImmutableArray<int> i = [1, .. new int[] { 5, 6, 7 }]; // test2 // test

//Desired code #2? 
ImmutableArray<int> i = [1, .. new int[] { 5, 6, 7 }]; // test // test2

Seems to me like there are multiple options in the case of trailing trivia at the declaration of the builder, and I would like to know what would be desirable.

@CyrusNajmabadi
Copy link
Member

For the case of leading trivia i assume we want to include all leading trivia in the fixed code, right?

We try to handle reasonable code patterns, and we don't expend effort on uncommon ones.

So, for example, this is not common: /* test */ var builder = ImmutableArray.CreateBuilder<int>();.

@CyrusNajmabadi
Copy link
Member

@DannySliekers i would just work on a fix that resolves exactly the case reported.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-IDE Bug help wanted The issue is "up for grabs" - add a comment if you are interested in working on it
Projects
Small Fixes
  
InQueue
Development

Successfully merging a pull request may close this issue.

3 participants