Skip to content

Commit

Permalink
Preventing methods with [Constant] parameters from being passed as re…
Browse files Browse the repository at this point in the history
…ferences (#924)
  • Loading branch information
JeffAshton authored Nov 2, 2023
1 parent c4578d2 commit c33720c
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 3 deletions.
34 changes: 32 additions & 2 deletions src/D2L.CodeStyle.Analyzers/ApiUsage/ConstantAttributeAnalyzer.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#nullable disable

using System.Collections.Immutable;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
Expand All @@ -14,7 +13,8 @@ public sealed class ConstantAttributeAnalyzer : DiagnosticAnalyzer {
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics =>
ImmutableArray.Create(
Diagnostics.NonConstantPassedToConstantParameter,
Diagnostics.InvalidConstantType
Diagnostics.InvalidConstantType,
Diagnostics.ReferenceToMethodWithConstantParameterNotSupport
);

public override void Initialize( AnalysisContext context ) {
Expand Down Expand Up @@ -61,6 +61,15 @@ CompilationStartAnalysisContext context
),
OperationKind.Conversion
);

context.RegisterOperationAction(
ctx => AnalyzeMethodReference(
ctx,
(IMethodReferenceOperation)ctx.Operation,
constantAttribute
),
OperationKind.MethodReference
);
}

private static void AnalyzeParameter(
Expand Down Expand Up @@ -168,6 +177,27 @@ INamedTypeSymbol constantAttribute
);
}

private static void AnalyzeMethodReference(
OperationAnalysisContext context,
IMethodReferenceOperation operation,
INamedTypeSymbol constantAttribute
) {

foreach( IParameterSymbol parameter in operation.Method.Parameters ) {

if( !HasAttribute( parameter, constantAttribute ) ) {
continue;
}

context.ReportDiagnostic(
descriptor: Diagnostics.ReferenceToMethodWithConstantParameterNotSupport,
location: operation.Syntax.GetLocation()
);

return;
}
}

/// <summary>
/// Check if the symbol has a specific attribute attached to it.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/D2L.CodeStyle.Analyzers/D2L.CodeStyle.Analyzers.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<Title>D2L.CodeStyle.Analyzers</Title>
<Product>D2L.CodeStyle</Product>
<Description>D2L.CodeStyle analyzers</Description>
<Version>0.203.0</Version>
<Version>0.204.0</Version>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
<PackageProjectUrl>https://github.com/Brightspace/D2L.CodeStyle</PackageProjectUrl>
<Authors>D2L</Authors>
Expand Down
9 changes: 9 additions & 0 deletions src/D2L.CodeStyle.Analyzers/Diagnostics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -757,5 +757,14 @@ public static class Diagnostics {
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true
);

public static readonly DiagnosticDescriptor ReferenceToMethodWithConstantParameterNotSupport = new DiagnosticDescriptor(
id: "D2L0102",
title: "References to methods with parameters marked as [Constant] is not supported",
messageFormat: "References to methods with parameters marked as [Constant] is currently not supported",
category: "Correctness",
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -280,5 +280,17 @@ string untrusted

#endregion

#region Method Reference Tests

void MethodReferenceTests() {

{ Action<int> action = Types.SomeMethodWithParameter<int>; }
{ Action<int> action = /* ReferenceToMethodWithConstantParameterNotSupport */ Types.SomeMethodWithConstantParameter<int> /**/; }
{ Action<int, int> action = /* ReferenceToMethodWithConstantParameterNotSupport */ Types.SomeMethodWithOneConstantParameter<int> /**/; }
{ Action<int, int> action = /* ReferenceToMethodWithConstantParameterNotSupport */ Types.SomeMethodWithOneOtherConstantParameter<int> /**/; }
{ Action<int, int> action = /* ReferenceToMethodWithConstantParameterNotSupport */ Types.SomeMethodWithTwoConstantParameters<int> /**/; }
}

#endregion
}
}

0 comments on commit c33720c

Please sign in to comment.