1using System.Collections.Immutable;
2using System.Composition;
4using System.Threading.Tasks;
14 [ExportCodeFixProvider(LanguageNames.CSharp)]
19 public override ImmutableArray<string>
FixableDiagnosticIds => ImmutableArray.Create(Constants.AnalyzerIdentifiers.FindOperationsWithIncorrectReturnTypes);
21 public sealed
override FixAllProvider
GetFixAllProvider() => WellKnownFixAllProviders.BatchFixer;
25 var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(
false);
27 context.CancellationToken.ThrowIfCancellationRequested();
29 var diagnostic = context.Diagnostics.First();
30 var methodNode = root.FindNode(diagnostic.Location.SourceSpan) as MethodDeclarationSyntax;
32 context.CancellationToken.ThrowIfCancellationRequested();
34 await AddCodeFixAsync(context, root, diagnostic, methodNode);
37 private static async Task AddCodeFixAsync(CodeFixContext context, SyntaxNode root,
38 Diagnostic diagnostic, MethodDeclarationSyntax methodNode)
40 var model = await context.Document.GetSemanticModelAsync(context.CancellationToken);
41 var methodSymbol = model.GetDeclaredSymbol(methodNode);
43 if(methodSymbol.IsAsync)
45 var newRoot = root.ReplaceNode(methodNode.ReturnType,
46 SyntaxFactory.IdentifierName(typeof(Task).Name));
48 if (!root.HasUsing(FindOperationsWithIncorrectReturnTypeResolveCorrectTypeCodeFixConstants.SystemThreadingTasksNamespace))
50 newRoot = (newRoot as CompilationUnitSyntax).AddUsings(
51 SyntaxFactory.UsingDirective(SyntaxFactory.ParseName(
52 FindOperationsWithIncorrectReturnTypeResolveCorrectTypeCodeFixConstants.SystemThreadingTasksNamespace)));
55 context.RegisterCodeFix(
57 FindOperationsWithIncorrectReturnTypeResolveCorrectTypeCodeFixConstants.ChangeReturnTypeToTaskDescription,
58 _ => Task.FromResult(context.Document.WithSyntaxRoot(newRoot)),
59 FindOperationsWithIncorrectReturnTypeResolveCorrectTypeCodeFixConstants.ChangeReturnTypeToTaskDescription), diagnostic);
63 var newRoot = root.ReplaceNode(methodNode.ReturnType,
64 SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.VoidKeyword)));
65 context.RegisterCodeFix(
67 FindOperationsWithIncorrectReturnTypeResolveCorrectTypeCodeFixConstants.ChangeReturnTypeToVoidDescription,
68 _ => Task.FromResult(context.Document.WithSyntaxRoot(newRoot)),
69 FindOperationsWithIncorrectReturnTypeResolveCorrectTypeCodeFixConstants.ChangeReturnTypeToVoidDescription), diagnostic);
sealed override FixAllProvider GetFixAllProvider()
override async Task RegisterCodeFixesAsync(CodeFixContext context)
override ImmutableArray< string > FixableDiagnosticIds