WiseTechGlobal / WTG.Analyzers

Analyzers from WiseTech Global to enforce our styles, behaviours, and prevent common mistakes.
Other
15 stars 3 forks source link

WI00547434 - Concat analyzer #193

Closed theawesomew closed 1 year ago

yaakov-h commented 1 year ago

I also just realised that we may need a bulk-fixer for (new[] { 1 }).Concat(new[] { 2 }).Concat(new[] { 3 }) and similar cases - add a test with it and see if it passes or fails.

theawesomew commented 1 year ago

I also just realised that we may need a bulk-fixer for (new[] { 1 }).Concat(new[] { 2 }).Concat(new[] { 3 }) and similar cases - add a test with it and see if it passes or fails.

I'm fairly confident that something like that would end up as new [] { 1, 2 }.Append(3); in its current state because it will encounter the (new[] { 1 }).Concat(new[] { 2 }).Concat SimpleMemberAccessExpression earlier in the SyntaxTree and it won't evaluate it correctly. ~Maybe I could just change the ordering to getting the Last() in the list of diagnostics and it would evaluate it correctly.~ Nope... it'll evaluate them as different warnings and therefore treat them differently upon getting to the CodeFixer

brian-reichle commented 1 year ago

I'm fairly confident that something like that would end up as new [] { 1, 2 }.Append(3); in its current state because it will encounter the (new[] { 1 }).Concat(new[] { 2 }).Concat SimpleMemberAccessExpression earlier in the SyntaxTree and it won't evaluate it correctly.

When Roslyn is asked to bulk fix a set of diagnostics, it will get the 'bulk fixer' from the fix provider and ask it to apply the fixes. IIRC, the default fixer implementation will attempt to fix all the failures separately and in parallel and then try to merge the fixes. If two of the fixes are too close to each other, they cause a conflict and only one of them is retained. There are a number of reasons why we might want to create a bulk fixer, eg:

Whether its needed or not, you should have a sample containing the expression that Yaakov provided. We already have tests that assert the fixed code matches the result, both when fixing the failures individually and when bulk fixing them all at the same time.

brian-reichle commented 1 year ago

I used BulkAnalysisRunner to run this new rule across part of our code base and auto-fix any issues, then I looked over the changes made. Some of the changes were incorrect.