Skip to content

Commit b2fe360

Browse files
committed
Handle more builder cases
1 parent 8e3822d commit b2fe360

5 files changed

Lines changed: 13 additions & 9 deletions

File tree

IDisposableAnalyzers.Test/Helpers/DisposableTests.Ignores.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1095,6 +1095,7 @@ public void Dispose()
10951095
[TestCase(".Append((string?)null)")]
10961096
[TestCase(".Append(condition, string.Empty)")]
10971097
[TestCase(".Append(!condition, string.Empty)")]
1098+
[TestCase(".Append(true).Append(condition).Append(!condition)")]
10981099
public static void Builder(string expression)
10991100
{
11001101
var syntaxTree = CSharpSyntaxTree.ParseText("""
@@ -1136,7 +1137,7 @@ public void Dispose() { }
11361137
11371138
public void M(bool condition)
11381139
{
1139-
using var s = new S().Append(1, string.Empty).Append(true);
1140+
using var s = new S().Append(true);
11401141
}
11411142
}
11421143
""".AssertReplace(".Append(true)", expression));

IDisposableAnalyzers.Test/IDISP016DoNotUseDisposedInstanceTests/Valid.DisposeCall.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
namespace IDisposableAnalyzers.Test.IDISP016DoNotUseDisposedInstanceTests
22
{
33
using Gu.Roslyn.Asserts;
4-
using Microsoft.CodeAnalysis;
54
using NUnit.Framework;
65

76
public static partial class Valid

IDisposableAnalyzers/Analyzers/ReturnValueAnalyzer.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
namespace IDisposableAnalyzers
22
{
3-
using System.Collections.Generic;
43
using System.Collections.Immutable;
54
using System.Linq;
65
using System.Threading;
76

87
using Gu.Roslyn.AnalyzerExtensions;
9-
using Gu.Roslyn.CodeFixExtensions;
108
using Microsoft.CodeAnalysis;
119
using Microsoft.CodeAnalysis.CSharp;
1210
using Microsoft.CodeAnalysis.CSharp.Syntax;

IDisposableAnalyzers/Helpers/Disposable.Identity.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,15 @@ when target.Declaration is MethodDeclarationSyntax methodDeclaration &&
117117
{
118118
case ThisExpressionSyntax:
119119
return true;
120-
case InvocationExpressionSyntax invocation
121-
when recursion.Target(invocation) is { Symbol.IsStatic: false } next &&
122-
next.Symbol.ContainingType.IsAssignableTo(target.Symbol.ContainingType, recursion.SemanticModel.Compilation):
123-
return IsIdentity(next, recursion);
120+
case InvocationExpressionSyntax invocation:
121+
return recursion.Target(invocation) switch
122+
{
123+
{ Symbol.IsStatic: false } next
124+
=> next.Symbol.ContainingType.IsAssignableTo(target.Symbol.ContainingType, recursion.SemanticModel.Compilation) &&
125+
IsIdentity(next, recursion),
126+
{ } => false,
127+
null => true,
128+
};
124129
}
125130
}
126131
}

ValidCode/StructBuilder/Client.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ public async Task<int> M(bool rth)
2222
.Append(2, "version")
2323
.Append(true, "subscribe")
2424
.Append(rth, "useRTH")
25-
//.Append(!rth, "not")
25+
.Append(!rth, "not")
2626
.Append("abc", "text")
2727
.Append((string?)null, "empty");
28+
2829
await this.socket.SendAsync(request.LengthPrefixed(), SocketFlags.None, CancellationToken.None).ConfigureAwait(false);
2930
return id;
3031
}

0 commit comments

Comments
 (0)