Skip to content

Commit 5b2167b

Browse files
authored
Fix analyzer RCS1129 (#1618)
1 parent 026a949 commit 5b2167b

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

ChangeLog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Fixed
11+
12+
- Fix analyzer [RCS1229](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1229) ([PR](https://github.com/dotnet/roslynator/pull/1618))
13+
1014
### Added
1115

1216
- Add analyzer "Put expression body on its own line" [RCS0062](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS0062) ([PR](https://github.com/dotnet/roslynator/pull/1593) by @cbersch)

src/Analyzers/CSharp/Analysis/UseAsyncAwaitAnalyzer.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ private bool IsCompletedTask(ExpressionSyntax expression)
276276

277277
return string.Equals(simpleMemberAccess.Name.Identifier.ValueText, "CompletedTask", StringComparison.Ordinal)
278278
&& SemanticModel.GetSymbol(expression, CancellationToken) is IPropertySymbol propertySymbol
279-
&& IsTaskOrTaskOrT(propertySymbol.ContainingType);
279+
&& IsTaskOrTaskOfT(propertySymbol.ContainingType);
280280
}
281281
else
282282
{
@@ -293,7 +293,7 @@ private bool IsCompletedTask(ExpressionSyntax expression)
293293
if (SemanticModel.GetSymbol(expression, CancellationToken) is IMethodSymbol methodSymbol
294294
&& (methodSymbol.Arity == 0 || methodSymbol.Arity == 1)
295295
&& methodSymbol.Parameters.Length == 1
296-
&& IsTaskOrTaskOrT(methodSymbol.ContainingType))
296+
&& IsTaskOrTaskOfT(methodSymbol.ContainingType))
297297
{
298298
return true;
299299
}
@@ -307,10 +307,10 @@ private bool IsCompletedTask(ExpressionSyntax expression)
307307
return false;
308308
}
309309

310-
private static bool IsTaskOrTaskOrT(INamedTypeSymbol typeSymbol)
310+
private static bool IsTaskOrTaskOfT(INamedTypeSymbol typeSymbol)
311311
{
312-
return typeSymbol.HasMetadataName(MetadataNames.System_Threading_Tasks_Task)
313-
|| typeSymbol.HasMetadataName(MetadataNames.System_Threading_Tasks_Task_T);
312+
return typeSymbol.ContainingNamespace.HasMetadataName(MetadataNames.System_Threading_Tasks)
313+
&& typeSymbol.MetadataName is "Task" or "Task`1" or "ValueTask" or "ValueTask`1";
314314
}
315315

316316
public override void VisitLocalFunctionStatement(LocalFunctionStatementSyntax node)

src/Tests/Analyzers.Tests/RCS1229UseAsyncAwaitTests.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,26 @@ NonAwaitableTaskType<int> M2()
809809
class NonAwaitableTaskType { }
810810
[AsyncMethodBuilder(null)]
811811
class NonAwaitableTaskType<T> { }
812+
");
813+
}
814+
815+
[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UseAsyncAwait)]
816+
public async Task TestNoDiagnostic_ValueTask()
817+
{
818+
await VerifyNoDiagnosticAsync(@"
819+
using System;
820+
using System.IO;
821+
using System.Threading.Tasks;
822+
823+
class C
824+
{
825+
ValueTask M()
826+
{
827+
using var memoryStream = new MemoryStream();
828+
829+
return ValueTask.CompletedTask;
830+
}
831+
}
812832
");
813833
}
814834
}

0 commit comments

Comments
 (0)