Skip to content

Commit 6995e2e

Browse files
committed
Winform.IsAddedToComponents
1 parent 1460d10 commit 6995e2e

7 files changed

Lines changed: 38 additions & 74 deletions

File tree

IDisposableAnalyzers.Test/Helpers/DisposableMemberTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using Gu.Roslyn.AnalyzerExtensions;
55
using Gu.Roslyn.Asserts;
66
using Microsoft.CodeAnalysis.CSharp;
7-
using Microsoft.CodeAnalysis.CSharp.Syntax;
87
using NUnit.Framework;
98

109
public static class DisposableMemberTests

IDisposableAnalyzers.Test/IDISP002DisposeMemberTests/Valid.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1088,7 +1088,6 @@ public class Winform : Form
10881088
RoslynAssert.NoAnalyzerDiagnostics(Analyzer, code);
10891089
}
10901090

1091-
[Ignore("tbd")]
10921091
[TestCase("this.components.Add(this.stream)")]
10931092
[TestCase("components.Add(stream)")]
10941093
public static void FieldAddedToFormComponents(string expression)

IDisposableAnalyzers.Test/IDISP006ImplementIDisposableTests/ValidCode.Inheritance.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,6 @@ public class Winform : Form
237237
RoslynAssert.NoAnalyzerDiagnostics(Analyzer, code);
238238
}
239239

240-
[Ignore("tbd")]
241240
[TestCase("this.components.Add(this.stream)")]
242241
[TestCase("components.Add(stream)")]
243242
public static void FieldAddedToFormComponents(string expression)

IDisposableAnalyzers/Helpers/DisposableMember.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,21 @@ internal static Result IsDisposed(FieldOrProperty member, INamedTypeSymbol conte
1919
}
2020

2121
using var walker = DisposeWalker.Borrow(context, semanticModel, cancellationToken);
22-
return walker.IsMemberDisposed(member.Symbol);
22+
var isMemberDisposed = walker.IsMemberDisposed(member.Symbol);
23+
switch (isMemberDisposed)
24+
{
25+
case Result.Yes:
26+
case Result.AssumeYes:
27+
return isMemberDisposed;
28+
default:
29+
if (context.IsAssignableTo(KnownSymbol.SystemWindowsFormsForm, semanticModel.Compilation) &&
30+
Winform.IsAddedToComponents(member, context, semanticModel, cancellationToken))
31+
{
32+
return Result.Yes;
33+
}
34+
35+
return isMemberDisposed;
36+
}
2337
}
2438

2539
internal static bool IsDisposed(FieldOrProperty member, IMethodSymbol disposeMethod, SemanticModel semanticModel, CancellationToken cancellationToken)

IDisposableAnalyzers/Helpers/Temp/FieldOrPropertyAndDeclaration.cs

Lines changed: 0 additions & 69 deletions
This file was deleted.

IDisposableAnalyzers/Helpers/Winform.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,27 @@ bool IsInWinForm()
2525
containingType.IsAssignableTo(KnownSymbol.SystemWindowsFormsForm, semanticModel.Compilation);
2626
}
2727
}
28+
29+
internal static bool IsAddedToComponents(FieldOrProperty member, INamedTypeSymbol context, SemanticModel semanticModel, CancellationToken cancellationToken)
30+
{
31+
if (context.TrySingleDeclaration(cancellationToken, out ClassDeclarationSyntax? containingType))
32+
{
33+
using var walker = UsagesWalker.Borrow(member.Symbol, containingType, semanticModel, cancellationToken);
34+
foreach (var usage in walker.Usages)
35+
{
36+
switch (usage)
37+
{
38+
case { Parent: ArgumentSyntax { Parent: ArgumentListSyntax { Parent: InvocationExpressionSyntax invocation } } }
39+
when IsComponentsAdd(invocation, semanticModel, cancellationToken):
40+
return true;
41+
case { Parent: MemberAccessExpressionSyntax { Expression: ThisExpressionSyntax _, Parent: ArgumentSyntax { Parent: ArgumentListSyntax { Parent: InvocationExpressionSyntax invocation } } } }
42+
when IsComponentsAdd(invocation, semanticModel, cancellationToken):
43+
return true;
44+
}
45+
}
46+
}
47+
48+
return false;
49+
}
2850
}
2951
}

IDisposableAnalyzers/IDisposableAnalyzers.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ BUGFIX IDISP025 seal disposable.</Description>
4444

4545
<ItemGroup>
4646
<PackageReference Include="Gu.Analyzers" Version="1.6.6.6-dev" PrivateAssets="all" />
47-
<PackageReference Include="Gu.Roslyn.Extensions" Version="0.12.2-dev" />
47+
<PackageReference Include="Gu.Roslyn.Extensions" Version="0.12.3-dev" />
4848
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.3.1" />
4949
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.8" PrivateAssets="all" />
5050
<PackageDownload Include="Microsoft.NETCore.App.Ref" Version="[$(AnnotatedReferenceAssemblyVersion)]" />

0 commit comments

Comments
 (0)