-
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 branch 'main' into update-xunit
- Loading branch information
Showing
37 changed files
with
296 additions
and
61 deletions.
There are no files selected for viewing
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
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
2 changes: 1 addition & 1 deletion
2
Funcky.Analyzers/Funcky.Analyzers.Test/Funcky.Analyzers.Test.csproj
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
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
10 changes: 8 additions & 2 deletions
10
Funcky.Async/Extensions/AsyncEnumerableExtensions/WithPrevious.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
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
12 changes: 12 additions & 0 deletions
12
....Test/OrNoneGeneratorSnapshotTests.GenerateMethodWithDefaultValuedArgument.00.verified.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,12 @@ | ||
//HintName: .g.cs | ||
// <auto-generated/> | ||
#nullable enable | ||
|
||
namespace Funcky.Extensions | ||
{ | ||
public static partial class ParseExtensions | ||
{ | ||
[global::System.Diagnostics.Contracts.Pure] | ||
public static Funcky.Monads.Option<global::Funcky.Extensions.Target> ParseTargetOrNone(this string candidate, string? hasDefault = null) => global::Funcky.Extensions.Target.TryParse(candidate, out var result, hasDefault) ? result : default(Funcky.Monads.Option<global::Funcky.Extensions.Target>); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
....Test/OrNoneGeneratorSnapshotTests.GenerateMethodWithDefaultValuedArgument.01.verified.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,15 @@ | ||
//HintName: OrNoneFromTryPatternAttribute.g.cs | ||
namespace Funcky.Internal | ||
{ | ||
[global::System.Diagnostics.Conditional("COMPILE_TIME_ONLY")] | ||
[global::System.AttributeUsage(global::System.AttributeTargets.Class, AllowMultiple = true)] | ||
internal class OrNoneFromTryPatternAttribute : global::System.Attribute | ||
{ | ||
public OrNoneFromTryPatternAttribute(global::System.Type type, string method) | ||
=> (Type, Method) = (type, method); | ||
|
||
public global::System.Type Type { get; } | ||
|
||
public string Method { get; } | ||
} | ||
} |
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
9 changes: 0 additions & 9 deletions
9
Funcky.SourceGenerator/Extensions/IncrementalValuesProviderExtensions.cs
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.