Skip to content

Commit b3c18a2

Browse files
Eliminate DRY violations in INTL0304 implementation
- Remove duplicate IsTaskDelayWithIntMilliseconds from CodeFixes/TaskDelayZero.cs; call the public static method on Analyzers.TaskDelayZero instead - Make Analyzers.TaskDelayZero.IsTaskDelayWithIntMilliseconds public so the CodeFixes assembly can reference it without InternalsVisibleTo - Extract DiagnosticId and message into constants in TaskDelayZeroTests to avoid repeated string literals
1 parent 50117fb commit b3c18a2

3 files changed

Lines changed: 10 additions & 26 deletions

File tree

‎IntelliTect.Analyzer/IntelliTect.Analyzer.CodeFixes/TaskDelayZero.cs‎

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
7373

7474
private static ExpressionSyntax? CreateReplacementExpression(IInvocationOperation invocation)
7575
{
76-
if (!IsTaskDelayWithIntMilliseconds(invocation.TargetMethod))
76+
if (!IntelliTect.Analyzer.Analyzers.TaskDelayZero.IsTaskDelayWithIntMilliseconds(invocation.TargetMethod))
7777
{
7878
return null;
7979
}
@@ -143,24 +143,6 @@ when propertyReference.Instance is null
143143
};
144144
}
145145

146-
private static bool IsTaskDelayWithIntMilliseconds(IMethodSymbol methodSymbol)
147-
{
148-
if (methodSymbol.Name != "Delay")
149-
{
150-
return false;
151-
}
152-
153-
if (methodSymbol.ContainingType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)
154-
!= "global::System.Threading.Tasks.Task")
155-
{
156-
return false;
157-
}
158-
159-
return methodSymbol.Parameters.Length >= 1
160-
&& methodSymbol.Parameters[0].Name == "millisecondsDelay"
161-
&& methodSymbol.Parameters[0].Type.SpecialType == SpecialType.System_Int32;
162-
}
163-
164146
private static async Task<Document> ReplaceInvocationAsync(
165147
Document document,
166148
InvocationExpressionSyntax invocation,

‎IntelliTect.Analyzer/IntelliTect.Analyzer.Test/TaskDelayZeroTests.cs‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ namespace IntelliTect.Analyzer.Tests
1010
[TestClass]
1111
public class TaskDelayZeroTests : CodeFixVerifier
1212
{
13+
private const string DiagnosticId = Analyzers.TaskDelayZero.DiagnosticId;
14+
private const string DiagnosticMessage = "Replace Task.Delay(0) with Task.CompletedTask";
1315
[TestMethod]
1416
public void TaskDelayZero_ProducesInfoMessage()
1517
{
@@ -28,9 +30,9 @@ void M()
2830
VerifyCSharpDiagnostic(source,
2931
new DiagnosticResult
3032
{
31-
Id = "INTL0304",
33+
Id = DiagnosticId,
3234
Severity = DiagnosticSeverity.Info,
33-
Message = "Replace Task.Delay(0) with Task.CompletedTask",
35+
Message = DiagnosticMessage,
3436
Locations =
3537
[
3638
new DiagnosticResultLocation("Test0.cs", 7, 9)
@@ -57,9 +59,9 @@ void M(CancellationToken token)
5759
VerifyCSharpDiagnostic(source,
5860
new DiagnosticResult
5961
{
60-
Id = "INTL0304",
62+
Id = DiagnosticId,
6163
Severity = DiagnosticSeverity.Info,
62-
Message = "Replace Task.Delay(0) with Task.CompletedTask",
64+
Message = DiagnosticMessage,
6365
Locations =
6466
[
6567
new DiagnosticResultLocation("Test0.cs", 8, 9)
@@ -300,9 +302,9 @@ void M(CancellationToken token)
300302
VerifyCSharpDiagnostic(source,
301303
new DiagnosticResult
302304
{
303-
Id = "INTL0304",
305+
Id = DiagnosticId,
304306
Severity = DiagnosticSeverity.Info,
305-
Message = "Replace Task.Delay(0) with Task.CompletedTask",
307+
Message = DiagnosticMessage,
306308
Locations =
307309
[
308310
new DiagnosticResultLocation("Test0.cs", 8, 9)

‎IntelliTect.Analyzer/IntelliTect.Analyzer/Analyzers/TaskDelayZero.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ private static void AnalyzeInvocation(OperationAnalysisContext context)
6464
}
6565
}
6666

67-
internal static bool IsTaskDelayWithIntMilliseconds(IMethodSymbol methodSymbol)
67+
public static bool IsTaskDelayWithIntMilliseconds(IMethodSymbol methodSymbol)
6868
{
6969
if (!string.Equals(methodSymbol.Name, "Delay", StringComparison.Ordinal))
7070
{

0 commit comments

Comments
 (0)