Skip to content

Commit

Permalink
rework spm check based on roslyn pkg updates
Browse files Browse the repository at this point in the history
  • Loading branch information
dpvreony committed Aug 14, 2022
1 parent faa2074 commit b95b18a
Show file tree
Hide file tree
Showing 4 changed files with 191 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,20 @@ public sealed override void Initialize(AnalysisContext context)
{
context.EnableConcurrentExecution();
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics);
context.RegisterSyntaxNodeAction(AnalyzeInvocationExpression, SyntaxKind.InvocationExpression);
context.RegisterSyntaxNodeAction(AnalyzeSimpleMemberAccessExpression, SyntaxKind.SimpleMemberAccessExpression);
}

private void AnalyzeInvocationExpression(SyntaxNodeAnalysisContext context)
private void AnalyzeSimpleMemberAccessExpression(SyntaxNodeAnalysisContext context)
{
var invocationExpression = (InvocationExpressionSyntax)context.Node;
var memberAccessExpressionSyntax = (MemberAccessExpressionSyntax)context.Node;

var memberExpression = invocationExpression.Expression as MemberAccessExpressionSyntax;
if (memberExpression == null
|| memberExpression.Expression == null)
if (memberAccessExpressionSyntax == null
|| memberAccessExpressionSyntax.Expression == null)
{
return;
}

var typeInfo = ModelExtensions.GetTypeInfo(context.SemanticModel, memberExpression.Expression);
var typeInfo = ModelExtensions.GetTypeInfo(context.SemanticModel, memberAccessExpressionSyntax.Expression);

if (typeInfo.Type == null)
{
Expand All @@ -85,7 +84,13 @@ private void AnalyzeInvocationExpression(SyntaxNodeAnalysisContext context)

var typeFullName = typeInfo.Type.GetFullName();

context.ReportDiagnostic(Diagnostic.Create(_rule, invocationExpression.GetLocation()));
if (!typeFullName.Equals(ClassName, StringComparison.Ordinal))
{
// need to look at if it's a static member access if we get here.
return;
}

context.ReportDiagnostic(Diagnostic.Create(_rule, memberAccessExpressionSyntax.GetLocation()));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (c) 2019 DHGMS Solutions and Contributors. All rights reserved.
// This file is licensed to you under the MIT license.
// See the LICENSE file in the project root for full license information.

using Dhgms.GripeWithRoslyn.Analyzer.CodeCracker.Extensions;
using Microsoft.CodeAnalysis;

namespace Dhgms.GripeWithRoslyn.Analyzer.Analyzers
{
/// <summary>
/// Analyzer to detect the use of <see cref="System.Net.ServicePointManager"/>.
/// </summary>
[Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalyzer(LanguageNames.CSharp, LanguageNames.VisualBasic)]
public sealed class DoNotUseSystemNetServicePointManagerAnalyzer : BaseInvocationUsingClassAnalyzer
{
internal const string Title = "Do not use System.Net.ServicePointManager.";

private const string MessageFormat = Title;

private const string Category = SupportedCategories.Design;

private const string Description =
"System.Net.ServicePointManager suggests code that may not be flexible, such as making global changes to transport layer security. The recommendation is to use System.Net.HttpClient";

/// <summary>
/// Initializes a new instance of the <see cref="DoNotUseSystemNetServicePointManagerAnalyzer"/> class.
/// </summary>
public DoNotUseSystemNetServicePointManagerAnalyzer()
: base(
DiagnosticIdsHelper.DoNotUseSystemNetServicePointManager,
Title,
MessageFormat,
Category,
Description,
DiagnosticSeverity.Warning)
{
}

/// <inheritdoc/>
protected override string ClassName => "global::System.Net.ServicePointManager";
}
}
2 changes: 2 additions & 0 deletions src/Dhgms.GripeWithRoslyn.Analyzer/DiagnosticIdsHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,7 @@ internal static class DiagnosticIdsHelper
internal static string DoNotUseEntityFrameworkCoreDatabaseEnsureCreated => "GR0019";

internal static string DoNotUseEntityFrameworkCoreDatabaseEnsureCreatedAsync => "GR0020";

internal static string DoNotUseSystemNetServicePointManager => "GR0021";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
// Copyright (c) 2019 DHGMS Solutions and Contributors. All rights reserved.
// This file is licensed to you under the MIT license.
// See the LICENSE file in the project root for full license information.

using Dhgms.GripeWithRoslyn.Analyzer;
using Dhgms.GripeWithRoslyn.Analyzer.Analyzers;
using Dhgms.GripeWithRoslyn.UnitTests.Helpers;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using Xunit;

namespace Dhgms.GripeWithRoslyn.UnitTests.Analyzers
{
/// <summary>
/// Unit Tests for <see cref="DoNotUseSystemNetServicePointManagerAnalyzer"/>.
/// </summary>
public class DoNotUseSystemNetServicePointManagerAnalyzerTest : CodeFixVerifier
{
/// <summary>
/// Test to ensure bad code returns a warning for a method invocation.
/// </summary>
[Fact]
public void ReturnsWarningForMethodInvocation()
{
var test = @"
namespace System.Net
{
public sealed class ServicePoint
{
}
public sealed class System.Net.Security.RemoteCertificateValidationCallback
{
}
public sealed class ServicePointManager
{
public static ServicePoint FindServicePoint(Uri uri)
{
}
public static System.Net.Security.RemoteCertificateValidationCallback ServerCertificateValidationCallback { get; }
}
}
namespace ConsoleApplication1
{
class TypeName
{
public void MethodName()
{
var servicePointManager = new System.Net.ServicePointManager();
servicePointManager.FindServicePoint();
}
}
}";
var methodInvoke = new DiagnosticResult
{
Id = DiagnosticIdsHelper.DoNotUseSystemNetServicePointManager,
Message = DoNotUseSystemNetServicePointManagerAnalyzer.Title,
Severity = DiagnosticSeverity.Warning,
Locations =
new[]
{
new DiagnosticResultLocation("Test0.cs", 29, 17)
}
};

VerifyCSharpDiagnostic(
test,
methodInvoke);
}

/// <summary>
/// Test to ensure bad code returns a warning for a property access.
/// </summary>
[Fact]
public void ReturnsWarningForPropertyAccess()
{
var test = @"
namespace System.Net
{
public sealed class ServicePoint
{
}
public sealed class System.Net.Security.RemoteCertificateValidationCallback
{
}
public sealed class ServicePointManager
{
public static ServicePoint FindServicePoint()
{
}
public static System.Net.Security.RemoteCertificateValidationCallback ServerCertificateValidationCallback { get; }
}
}
namespace ConsoleApplication1
{
class TypeName
{
public void MethodName()
{
var remoteCertificateValidationCallback = System.Net.ServicePointManager.RemoteCertificateValidationCallback;
}
}
}";
var methodInvoke = new DiagnosticResult
{
Id = DiagnosticIdsHelper.DoNotUseSystemNetServicePointManager,
Message = DoNotUseSystemNetServicePointManagerAnalyzer.Title,
Severity = DiagnosticSeverity.Warning,
Locations =
new[]
{
new DiagnosticResultLocation("Test0.cs", 19, 17)
}
};

VerifyCSharpDiagnostic(
test,
methodInvoke);
}

/// <inheritdoc/>
protected override DiagnosticAnalyzer GetCSharpDiagnosticAnalyzer()
{
return new DoNotUseSystemNetServicePointManagerAnalyzer();
}
}
}

0 comments on commit b95b18a

Please sign in to comment.