Skip to content

Commit

Permalink
Do not remove async/await (#953)
Browse files Browse the repository at this point in the history
  • Loading branch information
josefpihrt authored Sep 28, 2022
1 parent 8b27c6a commit 9aaa45c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
1 change: 1 addition & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Prefix identifier with `@` if necessary ([RCS1220](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1220.md)) ([#943](https://github.com/josefpihrt/roslynator/pull/943).
- Do not suggest to make local variable a const when it is used in ref extension method ([RCS1118](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1118.md)) ([#948](https://github.com/josefpihrt/roslynator/pull/948).
- Fix formatting of argument list ([#952](https://github.com/josefpihrt/roslynator/pull/952).
- Do not remove async/await when 'using declaration' is used ([#953](https://github.com/josefpihrt/roslynator/pull/953).

-----
<!-- Content below does not adhere to 'Keep a Changelog' format -->
Expand Down
16 changes: 15 additions & 1 deletion src/Common/CSharp/Analysis/RemoveAsyncAwaitAnalysis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,21 @@ private static RemoveAsyncAwaitAnalysis AnalyzeMethodBody(
{
SyntaxList<StatementSyntax> statements = body.Statements;

StatementSyntax statement = statements.LastOrDefault(f => !f.IsKind(SyntaxKind.LocalFunctionStatement));
StatementSyntax statement = null;

foreach (StatementSyntax s in statements)
{
if (s is LocalDeclarationStatementSyntax localDeclarationStatement
&& localDeclarationStatement.UsingKeyword.IsKind(SyntaxKind.UsingKeyword))
{
return default;
}

if (!s.IsKind(SyntaxKind.LocalFunctionStatement))
{
statement = s;
}
}

if (statement == null)
return default;
Expand Down
24 changes: 24 additions & 0 deletions src/Tests/Analyzers.Tests/RCS1174RemoveRedundantAsyncAwaitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -970,6 +970,30 @@ async Task<object> SwitchWithDefaultAsync()
}
}
}
");
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.RemoveRedundantAsyncAwait)]
public async Task TestNoDiagnostic_UsingDeclaration()
{
await VerifyNoDiagnosticAsync(@"
using System;
using System.IO;
using System.Threading.Tasks;
class C
{
private async Task<string> M()
{
using var stream = File.OpenRead("""");
return await this.M2(stream);
}
private Task<string> M2(FileStream stream)
{
throw new NotImplementedException();
}
}
");
}
}
Expand Down

0 comments on commit 9aaa45c

Please sign in to comment.