Skip to content

Commit 6b82e4e

Browse files
committed
Don't emit CA1849 for nameof expressions
1 parent b4ed6a3 commit 6b82e4e

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/UseAsyncMethodInAsyncContext.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,11 @@ public override void Initialize(AnalysisContext context)
137137
}
138138
else
139139
{
140-
InspectAndReportBlockingMemberAccess(context, ((IPropertyReferenceOperation)context.Operation).Property, syncBlockingSymbols, SymbolKind.Property);
140+
var propertyReferenceOperation = (IPropertyReferenceOperation)context.Operation;
141+
if (propertyReferenceOperation.Parent is not INameOfOperation)
142+
{
143+
InspectAndReportBlockingMemberAccess(context, propertyReferenceOperation.Property, syncBlockingSymbols, SymbolKind.Property);
144+
}
141145
}
142146
}
143147
}, OperationKind.Invocation, OperationKind.PropertyReference);

src/NetAnalyzers/UnitTests/Microsoft.NetCore.Analyzers/Runtime/UseAsyncMethodInAsyncContextTests.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1322,6 +1322,27 @@ public async Task RunAsync(IDbContextFactory<DbContext> factory) {
13221322
}.RunAsync();
13231323
}
13241324

1325+
[Theory]
1326+
[InlineData("Task<object>.Result")]
1327+
[InlineData("ValueTask<object>.Result")]
1328+
[WorkItem(6993, "https://github.com/dotnet/roslyn-analyzers/issues/6993")]
1329+
public Task WhenUsingNameOf_NoDiagnostic(string taskExpression)
1330+
{
1331+
var code = $$"""
1332+
using System.Threading.Tasks;
1333+
1334+
class Test
1335+
{
1336+
public async Task<string> Foo()
1337+
{
1338+
await Task.CompletedTask;
1339+
return nameof({{taskExpression}});
1340+
}
1341+
}
1342+
""";
1343+
return VerifyCS.VerifyAnalyzerAsync(code);
1344+
}
1345+
13251346
private static async Task CreateCSTestAndRunAsync(string testCS)
13261347
{
13271348
var csTestVerify = new VerifyCS.Test

0 commit comments

Comments
 (0)