From 6b82e4eb9ef7431e0b8b52f8acba272c9238eba3 Mon Sep 17 00:00:00 2001 From: Collin Alpert Date: Sun, 29 Oct 2023 11:42:37 +0100 Subject: [PATCH] Don't emit CA1849 for nameof expressions --- .../Runtime/UseAsyncMethodInAsyncContext.cs | 6 +++++- .../UseAsyncMethodInAsyncContextTests.cs | 21 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/UseAsyncMethodInAsyncContext.cs b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/UseAsyncMethodInAsyncContext.cs index 5a575c277e..d3af695bee 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/UseAsyncMethodInAsyncContext.cs +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/UseAsyncMethodInAsyncContext.cs @@ -137,7 +137,11 @@ public override void Initialize(AnalysisContext context) } else { - InspectAndReportBlockingMemberAccess(context, ((IPropertyReferenceOperation)context.Operation).Property, syncBlockingSymbols, SymbolKind.Property); + var propertyReferenceOperation = (IPropertyReferenceOperation)context.Operation; + if (propertyReferenceOperation.Parent is not INameOfOperation) + { + InspectAndReportBlockingMemberAccess(context, propertyReferenceOperation.Property, syncBlockingSymbols, SymbolKind.Property); + } } } }, OperationKind.Invocation, OperationKind.PropertyReference); diff --git a/src/NetAnalyzers/UnitTests/Microsoft.NetCore.Analyzers/Runtime/UseAsyncMethodInAsyncContextTests.cs b/src/NetAnalyzers/UnitTests/Microsoft.NetCore.Analyzers/Runtime/UseAsyncMethodInAsyncContextTests.cs index e9db9ffbcf..f808388f7d 100644 --- a/src/NetAnalyzers/UnitTests/Microsoft.NetCore.Analyzers/Runtime/UseAsyncMethodInAsyncContextTests.cs +++ b/src/NetAnalyzers/UnitTests/Microsoft.NetCore.Analyzers/Runtime/UseAsyncMethodInAsyncContextTests.cs @@ -1322,6 +1322,27 @@ public async Task RunAsync(IDbContextFactory factory) { }.RunAsync(); } + [Theory] + [InlineData("Task.Result")] + [InlineData("ValueTask.Result")] + [WorkItem(6993, "https://github.com/dotnet/roslyn-analyzers/issues/6993")] + public Task WhenUsingNameOf_NoDiagnostic(string taskExpression) + { + var code = $$""" + using System.Threading.Tasks; + + class Test + { + public async Task Foo() + { + await Task.CompletedTask; + return nameof({{taskExpression}}); + } + } + """; + return VerifyCS.VerifyAnalyzerAsync(code); + } + private static async Task CreateCSTestAndRunAsync(string testCS) { var csTestVerify = new VerifyCS.Test