-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #840 from polyadic/disallow-option-list-pattern-me…
…mbers
- Loading branch information
Showing
7 changed files
with
163 additions
and
6 deletions.
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
Funcky.Analyzers/Funcky.Analyzers.Test/SyntaxSupportOnlyTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
using Microsoft.CodeAnalysis.Testing; | ||
using Xunit; | ||
using VerifyCS = Funcky.Analyzers.Test.CSharpAnalyzerVerifier<Funcky.BuiltinAnalyzers.SyntaxSupportOnlyAnalyzer>; | ||
|
||
namespace Funcky.Analyzers.Test; | ||
|
||
public sealed class SyntaxSupportOnlyTest | ||
{ | ||
// language=csharp | ||
private const string AttributeSource = | ||
""" | ||
namespace Funcky.CodeAnalysis | ||
{ | ||
#pragma warning disable CS9113 // Parameter is unread. | ||
[System.AttributeUsage(System.AttributeTargets.Property)] | ||
internal sealed class SyntaxSupportOnlyAttribute(string syntaxFeature) : System.Attribute; | ||
#pragma warning restore CS9113 // Parameter is unread. | ||
} | ||
"""; | ||
|
||
[Fact] | ||
public async Task DisallowsUseOfPropertiesAnnotatedWithAttribute() | ||
{ | ||
// language=csharp | ||
const string inputCode = | ||
""" | ||
public static class C | ||
{ | ||
public static void M() | ||
{ | ||
var option = new Option(); | ||
_ = option.Count; | ||
} | ||
} | ||
public class Option | ||
{ | ||
[Funcky.CodeAnalysis.SyntaxSupportOnly("list pattern")] | ||
public int Count => 0; | ||
} | ||
"""; | ||
|
||
DiagnosticResult[] expectedDiagnostics = [ | ||
VerifyCS.Diagnostic().WithSpan(6, 13, 6, 25).WithArguments("property", "list pattern"), | ||
]; | ||
await VerifyCS.VerifyAnalyzerAsync(inputCode + Environment.NewLine + AttributeSource, expectedDiagnostics); | ||
} | ||
|
||
[Fact] | ||
public async Task DisallowsUseOfIndexersAnnotatedWithAttribute() | ||
{ | ||
// language=csharp | ||
const string inputCode = | ||
""" | ||
public static class C | ||
{ | ||
public static void M() | ||
{ | ||
var option = new Option(); | ||
_ = option[0]; | ||
} | ||
} | ||
public class Option | ||
{ | ||
[Funcky.CodeAnalysis.SyntaxSupportOnly("foo")] | ||
public int this[int index] => 0; | ||
} | ||
"""; | ||
|
||
DiagnosticResult[] expectedDiagnostics = [ | ||
VerifyCS.Diagnostic().WithSpan(6, 13, 6, 22).WithArguments("property", "foo"), | ||
]; | ||
await VerifyCS.VerifyAnalyzerAsync(inputCode + Environment.NewLine + AttributeSource, expectedDiagnostics); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
Funcky.Analyzers/Funcky.BuiltinAnalyzers/AnalyzerReleases.Unshipped.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
Funcky.Analyzers/Funcky.BuiltinAnalyzers/SyntaxSupportOnlyAnalyzer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
using System.Collections.Immutable; | ||
using System.Diagnostics.CodeAnalysis; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using Microsoft.CodeAnalysis.Operations; | ||
|
||
namespace Funcky.BuiltinAnalyzers; | ||
|
||
[DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
public sealed class SyntaxSupportOnlyAnalyzer : DiagnosticAnalyzer | ||
{ | ||
private const string AttributeFullName = "Funcky.CodeAnalysis.SyntaxSupportOnlyAttribute"; | ||
|
||
private static readonly DiagnosticDescriptor SyntaxSupportOnly = new( | ||
id: "λ0003", | ||
title: "Member is not intended for direct usage", | ||
messageFormat: "This {0} exists only to support the {1} syntax, do not use it directly", | ||
category: nameof(Funcky), | ||
DiagnosticSeverity.Error, | ||
isEnabledByDefault: true, | ||
customTags: [WellKnownDiagnosticTags.NotConfigurable]); | ||
|
||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(SyntaxSupportOnly); | ||
|
||
public override void Initialize(AnalysisContext context) | ||
{ | ||
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); | ||
context.EnableConcurrentExecution(); | ||
context.RegisterCompilationStartAction(OnCompilationStart); | ||
} | ||
|
||
private static void OnCompilationStart(CompilationStartAnalysisContext context) | ||
{ | ||
if (context.Compilation.GetTypeByMetadataName(AttributeFullName) is { } attributeType) | ||
{ | ||
context.RegisterOperationAction(AnalyzePropertyReference(new AttributeType(attributeType)), OperationKind.PropertyReference); | ||
} | ||
} | ||
|
||
private static Action<OperationAnalysisContext> AnalyzePropertyReference(AttributeType attributeType) | ||
=> context => | ||
{ | ||
var propertyReference = (IPropertyReferenceOperation)context.Operation; | ||
if (HasSyntaxSupportOnlyAttribute(propertyReference.Property, attributeType, out var syntaxFeature)) | ||
{ | ||
context.ReportDiagnostic(Diagnostic.Create(SyntaxSupportOnly, context.Operation.Syntax.GetLocation(), messageArgs: ["property", syntaxFeature])); | ||
} | ||
}; | ||
|
||
private static bool HasSyntaxSupportOnlyAttribute(ISymbol symbol, AttributeType attributeType, [NotNullWhen((true))] out string? syntaxFeature) | ||
{ | ||
syntaxFeature = null; | ||
return symbol.GetAttributes().FirstOrDefault(IsAttribute(attributeType.Value)) is { } attributeData | ||
&& attributeData.ConstructorArguments is [{ Value: string syntaxFeatureValue }] | ||
&& (syntaxFeature = syntaxFeatureValue) is var _; | ||
} | ||
|
||
private static Func<AttributeData, bool> IsAttribute(INamedTypeSymbol attributeClass) | ||
=> attributeData | ||
=> SymbolEqualityComparer.Default.Equals(attributeData.AttributeClass, attributeClass); | ||
|
||
private sealed record AttributeType(INamedTypeSymbol Value); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace Funcky.CodeAnalysis; | ||
|
||
#pragma warning disable CS9113 // Parameter is unread. | ||
[AttributeUsage(AttributeTargets.Property)] | ||
internal sealed class SyntaxSupportOnlyAttribute(string syntaxFeature) : Attribute | ||
#pragma warning restore CS9113 // Parameter is unread. | ||
{ | ||
public const string ListPattern = "list pattern"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters