Skip to content

Commit

Permalink
MA0100 support labels
Browse files Browse the repository at this point in the history
  • Loading branch information
meziantou committed Sep 21, 2024
1 parent 70714c2 commit 8b1111f
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 18 deletions.
13 changes: 13 additions & 0 deletions src/Meziantou.Analyzer/Internals/OperationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,19 @@ public static IOperation UnwrapConversionOperations(this IOperation operation)
return operation;
}

public static IOperation? UnwrapLabelOperations(this IOperation operation)
{
if (operation is ILabeledOperation label)
{
if (label.Operation is null)
return null;

return UnwrapLabelOperations(label.Operation);
}

return operation;
}

public static bool HasArgumentOfType(this IInvocationOperation operation, ITypeSymbol argumentTypeSymbol)
{
foreach (var arg in operation.Arguments)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Immutable;
using System.Linq;
using System.Threading.Tasks;
using Meziantou.Analyzer.Internals;
using Microsoft.CodeAnalysis;
Expand Down Expand Up @@ -66,7 +67,7 @@ public void AnalyzeReturn(OperationAnalysisContext context)

private static bool IsInUsingOperation(IOperation operation)
{
foreach (var parent in operation.Ancestors())
foreach (var parent in operation.Ancestors().Select(operation => operation.UnwrapLabelOperations()))
{
if (parent is IAnonymousFunctionOperation or ILocalFunctionOperation)
return false;
Expand All @@ -76,9 +77,9 @@ private static bool IsInUsingOperation(IOperation operation)

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

if (blockOperation is IUsingDeclarationOperation)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -487,25 +487,42 @@ await CreateProjectBuilder()
}

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

return System.Threading.Tasks.Task.Delay(1);
}
}
""")
.WithLanguageVersion(Microsoft.CodeAnalysis.CSharp.LanguageVersion.CSharp8)
.ValidateAsync();

return System.Threading.Tasks.Task.Delay(1);
}
}
""";
[Fact]
public Task UsingBeforeAReturnWithLabel()
=> CreateProjectBuilder()
.WithSourceCode("""
class TestClass
{
System.Threading.Tasks.Task Test(bool test)
{
if (test) goto a;
return System.Threading.Tasks.Task.Delay(1);

await CreateProjectBuilder()
.WithSourceCode(originalCode)
a:
using var disposable = (System.IDisposable) null;
[||]return System.Threading.Tasks.Task.Delay(1);
}

}
""")
.WithLanguageVersion(Microsoft.CodeAnalysis.CSharp.LanguageVersion.CSharp8)
.ValidateAsync();
}
}

0 comments on commit 8b1111f

Please sign in to comment.