Skip to content

Commit a9fac65

Browse files
committed
IDISP004 warn when awaited inline
fix #428
1 parent 5e5d1a6 commit a9fac65

3 files changed

Lines changed: 51 additions & 0 deletions

File tree

IDisposableAnalyzers.Test/Helpers/DisposableTests.Ignores.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,5 +1244,31 @@ public static ValueTask<IDisposable> MAsync()
12441244
var value = syntaxTree.FindExpression("new Disposable()");
12451245
Assert.AreEqual(false, Disposable.Ignores(value, semanticModel, CancellationToken.None));
12461246
}
1247+
1248+
[Test]
1249+
public static void AwaitAwaitHttpClientGetAsync()
1250+
{
1251+
var syntaxTree = CSharpSyntaxTree.ParseText("""
1252+
namespace N;
1253+
1254+
using System.Net.Http;
1255+
using System.Threading.Tasks;
1256+
1257+
public class Issue248
1258+
{
1259+
private static readonly HttpClient Client = new();
1260+
1261+
public static async Task<string> M()
1262+
{
1263+
string versions = await (await Client.GetAsync(string.Empty)).Content.ReadAsStringAsync();
1264+
return versions;
1265+
}
1266+
}
1267+
""");
1268+
var compilation = CSharpCompilation.Create("test", new[] { syntaxTree }, Settings.Default.MetadataReferences);
1269+
var semanticModel = compilation.GetSemanticModel(syntaxTree);
1270+
var value = syntaxTree.FindInvocation("Client.GetAsync(string.Empty)");
1271+
Assert.AreEqual(true, Disposable.Ignores(value, semanticModel, CancellationToken.None));
1272+
}
12471273
}
12481274
}

IDisposableAnalyzers.Test/IDISP004DoNotIgnoreCreatedTests/Diagnostics.Invocation.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,5 +396,28 @@ public C()
396396
""";
397397
RoslynAssert.Diagnostics(Analyzer, ExpectedDiagnostic, code);
398398
}
399+
400+
[Test]
401+
public static void AwaitAwaitHttpClientGetAsync()
402+
{
403+
var code = """
404+
namespace N;
405+
406+
using System.Net.Http;
407+
using System.Threading.Tasks;
408+
409+
public class Issue248
410+
{
411+
private static readonly HttpClient Client = new();
412+
413+
public static async Task<string> M()
414+
{
415+
string versions = await (await ↓Client.GetAsync("versions")).Content.ReadAsStringAsync();
416+
return versions;
417+
}
418+
}
419+
""";
420+
RoslynAssert.Diagnostics(Analyzer, ExpectedDiagnostic, code);
421+
}
399422
}
400423
}

IDisposableAnalyzers/Helpers/Disposable.Ignores.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ private static bool Ignores(ExpressionSyntax candidate, Recursion recursion)
6363
=> true,
6464
{ Parent: EqualsValueClauseSyntax { Parent: VariableDeclaratorSyntax { Identifier.ValueText: "_" } } }
6565
=> true,
66+
{ Parent: AwaitExpressionSyntax { Parent: ParenthesizedExpressionSyntax { } expression } }
67+
=> Ignores(expression, recursion),
6668
{ Parent: AnonymousFunctionExpressionSyntax _ }
6769
=> false,
6870
{ Parent: StatementSyntax _ }

0 commit comments

Comments
 (0)