Skip to content

Commit 4f98258

Browse files
committed
+ code fix
1 parent 065fc23 commit 4f98258

File tree

3 files changed

+29
-28
lines changed

3 files changed

+29
-28
lines changed

src/Framework/AspNetCoreAnalyzers/src/Analyzers/Kestrel/ListenOnIPv6AnyAnalyzer.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,6 @@ private void KestrelServerOptionsListenInvocation(SyntaxNodeAnalysisContext cont
6464
{
6565
context.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.KestrelShouldListenOnIPv6AnyInsteadOfIpAny, ipAddressArgumentSyntax.GetLocation()));
6666
}
67-
68-
6967
}
7068

7169
private static bool IsIPAddressType(IParameterSymbol? parameter) => parameter is

src/Framework/AspNetCoreAnalyzers/src/CodeFixes/Kestrel/ListenOnIPv6AnyFixer.cs

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using Microsoft.CodeAnalysis.CodeActions;
1111
using Microsoft.CodeAnalysis.Editing;
1212
using Microsoft.CodeAnalysis.CSharp.Syntax;
13+
using Microsoft.CodeAnalysis.CSharp;
1314

1415
namespace Microsoft.AspNetCore.Fixers.Kestrel;
1516

@@ -29,24 +30,21 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)
2930
"Consider using IPAddress.IPv6Any instead of IPAddress.Any",
3031
async cancellationToken =>
3132
{
32-
throw new System.NotImplementedException();
33-
34-
//var editor = await DocumentEditor.CreateAsync(context.Document, cancellationToken).ConfigureAwait(false);
35-
//var root = await context.Document.GetSyntaxRootAsync(cancellationToken);
36-
//if (root is null)
37-
//{
38-
// return context.Document;
39-
//}
40-
41-
//var classDeclaration = root.FindNode(diagnostic.Location.SourceSpan)
42-
// .FirstAncestorOrSelf<ClassDeclarationSyntax>();
43-
//if (classDeclaration is null)
44-
//{
45-
// return context.Document;
46-
//}
47-
//editor.RemoveNode(classDeclaration, SyntaxRemoveOptions.KeepExteriorTrivia);
48-
//return editor.GetChangedDocument();
49-
33+
var editor = await DocumentEditor.CreateAsync(context.Document, cancellationToken).ConfigureAwait(false);
34+
var root = await context.Document.GetSyntaxRootAsync(cancellationToken);
35+
if (root is null)
36+
{
37+
return context.Document;
38+
}
39+
40+
var argumentSyntax = root.FindNode(diagnostic.Location.SourceSpan).FirstAncestorOrSelf<ArgumentSyntax>();
41+
if (argumentSyntax is null)
42+
{
43+
return context.Document;
44+
}
45+
46+
editor.ReplaceNode(argumentSyntax, argumentSyntax.WithExpression(SyntaxFactory.ParseExpression("IPAddress.IPv6Any")));
47+
return editor.GetChangedDocument();
5048
},
5149
equivalenceKey: DiagnosticDescriptors.KestrelShouldListenOnIPv6AnyInsteadOfIpAny.Id),
5250
diagnostic);

src/Framework/AspNetCoreAnalyzers/test/Kestrel/ListenOnIPv6AnyAnalyzerTests.cs renamed to src/Framework/AspNetCoreAnalyzers/test/Kestrel/ListenOnIPv6AnyAnalyzerAndFixerTests.cs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,33 @@
1111

1212
namespace Microsoft.AspNetCore.Analyzers.Kestrel;
1313

14-
public class ListenOnIPv6AnyAnalyzerTests
14+
public class ListenOnIPv6AnyAnalyzerAndFixerTests
1515
{
1616
[Fact] // do we need any other scenarios except the direct usage one?
1717
public async Task ReportsDiagnostic_IPAddressAsLocalVariable()
1818
{
1919
var source = GetKestrelSetupSource("myIp", "var myIp = IPAddress.Any;");
20-
21-
await VerifyCS.VerifyAnalyzerAsync(source, [
22-
new DiagnosticResult(DiagnosticDescriptors.KestrelShouldListenOnIPv6AnyInsteadOfIpAny).WithLocation(0)
23-
]);
20+
await VerifyCS.VerifyAnalyzerAsync(source, codeSampleDiagnosticResult);
2421
}
2522

2623
[Fact]
2724
public async Task ReportsDiagnostic_ExplicitUsage()
2825
{
2926
var source = GetKestrelSetupSource("IPAddress.Any");
27+
await VerifyCS.VerifyAnalyzerAsync(source, codeSampleDiagnosticResult);
28+
}
3029

31-
await VerifyCS.VerifyAnalyzerAsync(source, [
32-
new DiagnosticResult(DiagnosticDescriptors.KestrelShouldListenOnIPv6AnyInsteadOfIpAny).WithLocation(0)
33-
]);
30+
[Fact]
31+
public async Task CodeFix_ExplicitUsage()
32+
{
33+
var source = GetKestrelSetupSource("IPAddress.Any");
34+
var fixedSource = GetKestrelSetupSource("IPAddress.IPv6Any");
35+
await VerifyCS.VerifyCodeFixAsync(source, codeSampleDiagnosticResult, fixedSource);
3436
}
3537

38+
private static DiagnosticResult codeSampleDiagnosticResult
39+
= new DiagnosticResult(DiagnosticDescriptors.KestrelShouldListenOnIPv6AnyInsteadOfIpAny).WithLocation(0);
40+
3641
static string GetKestrelSetupSource(string ipAddressArgument, string extraInlineCode = null) => $$"""
3742
using Microsoft.Extensions.Hosting;
3843
using Microsoft.AspNetCore.Hosting;

0 commit comments

Comments
 (0)