Skip to content

Commit

Permalink
MA0100 detects using statements
Browse files Browse the repository at this point in the history
  • Loading branch information
meziantou committed Sep 21, 2024
1 parent 423b37f commit ee4866d
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,18 @@ private static bool IsInUsingOperation(IOperation operation)

if (parent is IUsingOperation)
return true;

if (parent is IBlockOperation block)
{
foreach (var blockOperation in block.Operations)
{
if (blockOperation == parent)
break;

if (blockOperation is IUsingDeclarationOperation)
return true;
}
}
}

return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -465,4 +465,47 @@ await CreateProjectBuilder()
.WithSourceCode(originalCode)
.ValidateAsync();
}

[Fact]
public async Task ReturnInUsingStatement()
{
var originalCode = """
class TestClass
{
System.Threading.Tasks.Task Test()
{
using var disposable = (System.IDisposable)null;
[||]return System.Threading.Tasks.Task.Delay(1);
}
}
""";

await CreateProjectBuilder()
.WithSourceCode(originalCode)
.WithLanguageVersion(Microsoft.CodeAnalysis.CSharp.LanguageVersion.CSharp8)
.ValidateAsync();
}

[Fact]
public async Task UsingBlockBeforeAReturn()
{
var originalCode = """
class TestClass
{
System.Threading.Tasks.Task Test()
{
using (var disposable = (System.IDisposable)null)
{
}

return System.Threading.Tasks.Task.Delay(1);
}
}
""";

await CreateProjectBuilder()
.WithSourceCode(originalCode)
.WithLanguageVersion(Microsoft.CodeAnalysis.CSharp.LanguageVersion.CSharp8)
.ValidateAsync();
}
}

0 comments on commit ee4866d

Please sign in to comment.