-
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 #801 from polyadic/non-defaultable
- Loading branch information
Showing
9 changed files
with
136 additions
and
1 deletion.
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
Funcky.Analyzers/Funcky.Analyzers.Test/NonDefaultableTest.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,70 @@ | ||
using Microsoft.CodeAnalysis.Testing; | ||
using Xunit; | ||
using VerifyCS = Funcky.Analyzers.Test.CSharpAnalyzerVerifier<Funcky.Analyzers.NonDefaultableAnalyzer>; | ||
|
||
namespace Funcky.Analyzers.Test; | ||
|
||
public sealed class NonDefaultableTest | ||
{ | ||
private const string AttributeSource = | ||
""" | ||
namespace Funcky.CodeAnalysis | ||
{ | ||
[System.AttributeUsage(System.AttributeTargets.Struct)] | ||
internal sealed class NonDefaultableAttribute : System.Attribute { } | ||
} | ||
"""; | ||
|
||
[Fact] | ||
public async Task DefaultInstantiationsOfRegularStructsGetNoDiagnostic() | ||
{ | ||
const string inputCode = | ||
""" | ||
class Test | ||
{ | ||
private void Usage() | ||
{ | ||
_ = default(Foo); | ||
} | ||
} | ||
struct Foo { } | ||
"""; | ||
await VerifyCS.VerifyAnalyzerAsync(inputCode + AttributeSource); | ||
} | ||
|
||
[Fact] | ||
public async Task DefaultInstantiationsOfAnnotatedStructsGetError() | ||
{ | ||
const string inputCode = | ||
""" | ||
using Funcky.CodeAnalysis; | ||
class Test | ||
{ | ||
private void Usage() | ||
{ | ||
_ = default(Foo); | ||
_ = default(Funcky.Generic<int>); | ||
} | ||
} | ||
[NonDefaultable] | ||
struct Foo { } | ||
namespace Funcky | ||
{ | ||
[NonDefaultable] | ||
struct Generic<T> { } | ||
} | ||
"""; | ||
|
||
DiagnosticResult[] expectedDiagnostics = | ||
[ | ||
VerifyCS.Diagnostic().WithSpan(7, 13, 7, 25).WithArguments("Foo"), | ||
VerifyCS.Diagnostic().WithSpan(8, 13, 8, 41).WithArguments("Generic<int>"), | ||
]; | ||
|
||
await VerifyCS.VerifyAnalyzerAsync(inputCode + AttributeSource, expectedDiagnostics); | ||
} | ||
} |
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
54 changes: 54 additions & 0 deletions
54
Funcky.Analyzers/Funcky.Analyzers/NonDefaultableAnalyzer.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,54 @@ | ||
using System.Collections.Immutable; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using Microsoft.CodeAnalysis.Operations; | ||
|
||
namespace Funcky.Analyzers; | ||
|
||
[DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
public sealed class NonDefaultableAnalyzer : DiagnosticAnalyzer | ||
{ | ||
public static readonly DiagnosticDescriptor DoNotUseDefault = new DiagnosticDescriptor( | ||
id: $"{DiagnosticName.Prefix}{DiagnosticName.Usage}09", | ||
title: "Do not use default to instantiate this type", | ||
messageFormat: "Do not use default(...) to instantiate '{0}'", | ||
category: nameof(Funcky), | ||
DiagnosticSeverity.Error, | ||
isEnabledByDefault: true, | ||
description: "Values instantiated with default are in an invalid state; any member may throw an exception."); | ||
|
||
private const string AttributeFullName = "Funcky.CodeAnalysis.NonDefaultableAttribute"; | ||
|
||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(DoNotUseDefault); | ||
|
||
public override void Initialize(AnalysisContext context) | ||
{ | ||
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics); | ||
context.EnableConcurrentExecution(); | ||
context.RegisterCompilationStartAction(OnCompilationStart); | ||
} | ||
|
||
private static void OnCompilationStart(CompilationStartAnalysisContext context) | ||
{ | ||
if (context.Compilation.GetTypeByMetadataName(AttributeFullName) is { } nonDefaultableAttribute) | ||
{ | ||
context.RegisterOperationAction(AnalyzeDefaultValueOperation(nonDefaultableAttribute), OperationKind.DefaultValue); | ||
} | ||
} | ||
|
||
private static Action<OperationAnalysisContext> AnalyzeDefaultValueOperation(INamedTypeSymbol nonDefaultableAttribute) | ||
=> context => | ||
{ | ||
var operation = (IDefaultValueOperation)context.Operation; | ||
if (operation.Type is { } type && type.GetAttributes().Any(IsAttribute(nonDefaultableAttribute))) | ||
{ | ||
context.ReportDiagnostic(Diagnostic.Create( | ||
DoNotUseDefault, | ||
operation.Syntax.GetLocation(), | ||
messageArgs: type.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat))); | ||
} | ||
}; | ||
|
||
private static Func<AttributeData, bool> IsAttribute(INamedTypeSymbol attributeClass) | ||
=> attribute => SymbolEqualityComparer.Default.Equals(attribute.AttributeClass, attributeClass); | ||
} |
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
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,5 @@ | ||
namespace Funcky.CodeAnalysis; | ||
|
||
/// <summary>Structs annotated with this attribute should not be instantiated with <see langword="default"/>.</summary> | ||
[AttributeUsage(AttributeTargets.Struct)] | ||
internal sealed class NonDefaultableAttribute : Attribute; |
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
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