Skip to content

Commit

Permalink
BUGFIX: IDISP003 figure out when assigned in switch. #125.
Browse files Browse the repository at this point in the history
  • Loading branch information
JohanLarsson committed Nov 4, 2018
1 parent 28a31cf commit 3adee1b
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void Dispose()
}";

[Test]
public void CreateVariable()
public void LocalDeclaration()
{
var testCode = @"
namespace RoslynSandbox
Expand All @@ -46,6 +46,83 @@ public void Meh()
AnalyzerAssert.Valid(Analyzer, testCode);
}

[Test]
public void LocalAssignedInSwitch()
{
var testCode = @"
namespace RoslynSandbox
{
using System;
using System.IO;
public static class Foo
{
public static IDisposable Bar(int i)
{
IDisposable result;
switch (i)
{
case 1:
result = File.OpenRead(string.Empty);
break;
case 2:
result = File.OpenRead(string.Empty);
break;
default:
result = null;
break;
}
return result;
}
}
}";

AnalyzerAssert.Valid(Analyzer, testCode);
}

[Test]
public void LocalAssignedInIfElseSwitch()
{
var testCode = @"
namespace RoslynSandbox
{
using System;
using System.IO;
public static class Foo
{
public static IDisposable Bar(int i)
{
IDisposable result;
if (i == 0)
{
result = null;
}
else
{
switch (i)
{
case 1:
result = File.OpenRead(string.Empty);
break;
case 2:
result = File.OpenRead(string.Empty);
break;
default:
result = null;
break;
}
}
return result;
}
}
}";

AnalyzerAssert.Valid(Analyzer, testCode);
}

[Test]
public void AssignVariableInitializedWithNull()
{
Expand Down
2 changes: 1 addition & 1 deletion IDisposableAnalyzers/Helpers/Disposable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ internal static bool IsDisposedAfter(ISymbol local, ExpressionSyntax location, S
{
foreach (var invocation in walker.Invocations)
{
if (location.IsExecutedBefore(invocation) == ExecutedBefore.Yes &&
if (location.IsExecutedBefore(invocation).IsEither(ExecutedBefore.Maybe, ExecutedBefore.Yes) &&
DisposeCall.IsDisposing(invocation, local, semanticModel, cancellationToken))
{
return true;
Expand Down
9 changes: 9 additions & 0 deletions IDisposableAnalyzers/Helpers/ExecutedBeforeExt.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace IDisposableAnalyzers
{
using Gu.Roslyn.AnalyzerExtensions;

internal static class ExecutedBeforeExt
{
internal static bool IsEither(this ExecutedBefore result, ExecutedBefore first, ExecutedBefore other) => result == first || result == other;
}
}
2 changes: 1 addition & 1 deletion IDisposableAnalyzers/Helpers/ResultExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ internal static class ResultExt

internal static bool IsEither(this Result result, Result first, Result second, Result third) => result == first || result == second || result == third;
}
}
}
26 changes: 26 additions & 0 deletions ValidCode/Misc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,32 @@ private set
}
}

public static IDisposable AssignLocalInSwitch(int i)
{
IDisposable result;
if (i == 0)
{
result = null;
}
else
{
switch (i)
{
case 1:
result = File.OpenRead(string.Empty);
break;
case 2:
result = File.OpenRead(string.Empty);
break;
default:
result = null;
break;
}
}

return result;
}

public void Dispose()
{
this.subscription.Dispose();
Expand Down
2 changes: 1 addition & 1 deletion paket.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
STORAGE: NONE
NUGET
remote: http://www.nuget.org/api/v2
Gu.Roslyn.Extensions (0.4.3.2-dev)
Gu.Roslyn.Extensions (0.4.4-dev)
Microsoft.CodeAnalysis.CSharp.Workspaces (>= 2.0)
ManagedEsent (1.9.4) - restriction: >= net46
Microsoft.CodeAnalysis.Analyzers (2.6.2) - restriction: >= netstandard1.3
Expand Down

0 comments on commit 3adee1b

Please sign in to comment.