Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed

- Fix analyzer [RCS1229](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1229) ([PR](https://github.com/dotnet/roslynator/pull/1618))
- Fix analyzer [RCS1174](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1174) ([PR](https://github.com/dotnet/roslynator/pull/1619))

### Added

Expand Down
19 changes: 11 additions & 8 deletions src/Common/CSharp/Analysis/RemoveAsyncAwaitAnalysis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ private static bool VerifyTypes(

foreach (AwaitExpressionSyntax awaitExpression in awaitExpressions)
{
if (!VerifyAwaitType(awaitExpression, typeArgument, semanticModel, cancellationToken))
if (!VerifyAwaitType(awaitExpression, returnType, typeArgument, semanticModel, cancellationToken))
return false;
}

Expand Down Expand Up @@ -400,10 +400,15 @@ private static bool VerifyTypes(
if (typeArgument is null)
return false;

return VerifyAwaitType(awaitExpression, typeArgument, semanticModel, cancellationToken);
return VerifyAwaitType(awaitExpression, returnType, typeArgument, semanticModel, cancellationToken);
}

private static bool VerifyAwaitType(AwaitExpressionSyntax awaitExpression, ITypeSymbol typeArgument, SemanticModel semanticModel, CancellationToken cancellationToken)
private static bool VerifyAwaitType(
AwaitExpressionSyntax awaitExpression,
ITypeSymbol returnType,
ITypeSymbol typeArgument,
SemanticModel semanticModel,
CancellationToken cancellationToken)
{
if (!SymbolEqualityComparer.Default.Equals(typeArgument, semanticModel.GetTypeSymbol(awaitExpression, cancellationToken)))
return false;
Expand All @@ -415,15 +420,13 @@ private static bool VerifyAwaitType(AwaitExpressionSyntax awaitExpression, IType
if (expressionTypeSymbol is null)
return false;

if (expressionTypeSymbol.OriginalDefinition.IsAwaitable(semanticModel, expression.SpanStart))
if (SymbolEqualityComparer.Default.Equals(returnType, expressionTypeSymbol))
return true;

SimpleMemberInvocationExpressionInfo invocationInfo = SyntaxInfo.SimpleMemberInvocationExpressionInfo(expression);

return invocationInfo.Success
&& invocationInfo.Arguments.Count == 1
&& invocationInfo.NameText == "ConfigureAwait"
&& expressionTypeSymbol.OriginalDefinition.IsAwaitable(semanticModel, expression.SpanStart);
return invocationInfo is { Success: true, Arguments.Count: 1, NameText: "ConfigureAwait" }
&& SymbolEqualityComparer.Default.Equals(returnType, semanticModel.GetTypeSymbol(invocationInfo.Expression, cancellationToken));
}

private static IMethodSymbol GetMethodSymbol(
Expand Down
32 changes: 32 additions & 0 deletions src/Tests/Analyzers.Tests/RCS1174RemoveRedundantAsyncAwaitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1066,6 +1066,38 @@ private Task<string> M2(FileStream stream)
throw new NotImplementedException();
}
}
""");
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.RemoveRedundantAsyncAwait)]
public async Task TestNoDiagnostic_Task_Vs_ValueTask()
{
await VerifyNoDiagnosticAsync("""
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

class C
{
private async Task<bool> IsNotEmptyAsync(string text)
{
await Task.Delay(1);
return !string.IsNullOrEmpty(text);
}

private void Execute()
{
IAsyncEnumerable<string> texts = null!;
var notEmptyTexts = texts.WhereAwait(async t => await this.IsNotEmptyAsync(t));
Console.WriteLine(string.Join(", ", notEmptyTexts.ToEnumerable()));
}
}

public static class Extensions
{
public static IAsyncEnumerable<TSource> WhereAwait<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate) => default;
public static IEnumerable<TSource> ToEnumerable<TSource>(this IAsyncEnumerable<TSource> source) => default;
}
""");
}
}
Loading