Last active
October 19, 2024 14:12
-
-
Save dfkeenan/dc415c27c5fb99eaac811355b1ec781e to your computer and use it in GitHub Desktop.
Console application for fixing C# warning CS9192: Argument should be passed with ref or in
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using Microsoft.CodeAnalysis.MSBuild; | |
| using Microsoft.CodeAnalysis; | |
| using Microsoft.CodeAnalysis.CSharp.Syntax; | |
| using Microsoft.CodeAnalysis.Editing; | |
| using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; | |
| using Microsoft.CodeAnalysis.CSharp; | |
| var solutionFilePath = @"SomeSolution.sln"; | |
| using var workspace = MSBuildWorkspace.Create(); | |
| var solution = await workspace.OpenSolutionAsync(solutionFilePath); | |
| var edits = new List<(DocumentId DocumentId, ArgumentSyntax Syntax)>(); | |
| foreach (var project in solution.Projects) | |
| { | |
| var compilation = await project.GetCompilationAsync(); | |
| if (compilation is null) continue; | |
| var diagnostics = compilation.GetDiagnostics().Where(d => d.Id == "CS9192").ToList(); | |
| foreach (var diagnostic in diagnostics) | |
| { | |
| var location = diagnostic.Location; | |
| SyntaxNode? rootNode = location.SourceTree?.GetRoot(); | |
| SyntaxNode? nodeAtLocation = rootNode?.FindNode(location.SourceSpan); | |
| if (nodeAtLocation is ArgumentSyntax argumentSyntax) | |
| { | |
| var document = solution.GetDocument(location.SourceTree); | |
| if (document is not null) | |
| edits.Add((document.Id, argumentSyntax)); | |
| } | |
| } | |
| } | |
| foreach (var editGroup in edits.GroupBy(e => e.DocumentId, e => e.Syntax)) | |
| { | |
| var document = workspace.CurrentSolution.GetDocument(editGroup.Key); | |
| var editor = await DocumentEditor.CreateAsync(document); | |
| foreach (ArgumentSyntax aurgumentSyntax in editGroup) | |
| { | |
| var updatedArgumentSyntax = aurgumentSyntax | |
| .WithRefOrOutKeyword(Token(SyntaxKind.InKeyword)) | |
| .NormalizeWhitespace(); | |
| editor.ReplaceNode(aurgumentSyntax, updatedArgumentSyntax); | |
| } | |
| var updatedDocument = editor.GetChangedDocument(); | |
| var didStuff = workspace.TryApplyChanges(updatedDocument.Project.Solution); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <Project Sdk="Microsoft.NET.Sdk"> | |
| <PropertyGroup> | |
| <OutputType>Exe</OutputType> | |
| <TargetFramework>net8.0</TargetFramework> | |
| <ImplicitUsings>enable</ImplicitUsings> | |
| <Nullable>enable</Nullable> | |
| </PropertyGroup> | |
| <ItemGroup> | |
| <PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.11.0" /> | |
| <PackageReference Include="Microsoft.CodeAnalysis.Workspaces.MSBuild" Version="4.11.0" /> | |
| </ItemGroup> | |
| </Project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment