Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use collection expressions #775

Merged
merged 2 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ private static InvocationExpressionSyntax CreateFromBooleanInvocation(
(ExpressionSyntax)generator.TypeExpressionForStaticMemberAccess(symbols.OptionType),
GetFromBooleanName(returnInvocation))
.WithAdditionalAnnotations(Simplifier.Annotation),
ArgumentList(SeparatedList(new[] { Argument(condition), Argument(returnValue) })))
ArgumentList(SeparatedList([Argument(condition), Argument(returnValue)])))
.WithLeadingTrivia(returnInvocation.GetLeadingTrivia());

private static SimpleNameSyntax GetFromBooleanName(InvocationExpressionSyntax returnInvocation)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#pragma warning disable SA1118 // StyleCop support for collection expressions is missing
using Xunit;
using static Funcky.Analyzers.AlternativeMonadAnalyzer;
using VerifyCS = Funcky.Analyzers.Test.CSharpCodeFixVerifier<Funcky.Analyzers.AlternativeMonadAnalyzer, Funcky.Analyzers.AlternativeMonad.MatchToNullableCodeFix>;
Expand Down Expand Up @@ -57,8 +58,7 @@ public static void M(Option<string> optionOfReferenceType, Option<int> optionOfV
""";
await VerifyCS.VerifyCodeFixAsync(
inputCode + Environment.NewLine + OptionStubCode,
new[]
{
[
VerifyCS.Diagnostic(PreferToNullable).WithSpan(10, 9, 10, 71),
VerifyCS.Diagnostic(PreferToNullable).WithSpan(11, 9, 11, 77),
VerifyCS.Diagnostic(PreferToNullable).WithSpan(12, 9, 12, 77),
Expand All @@ -68,7 +68,7 @@ await VerifyCS.VerifyCodeFixAsync(
VerifyCS.Diagnostic(PreferToNullable).WithSpan(16, 9, 16, 70),
VerifyCS.Diagnostic(PreferToNullable).WithSpan(17, 13, 17, 69),
VerifyCS.Diagnostic(PreferToNullable).WithSpan(18, 13, 18, 69),
},
],
fixedCode + Environment.NewLine + OptionStubCode);
}

Expand Down Expand Up @@ -109,10 +109,9 @@ public static void M(Option<int> optionOfValueType)
""";
await VerifyCS.VerifyCodeFixAsync(
inputCode + Environment.NewLine + OptionStubCode,
new[]
{
[
VerifyCS.Diagnostic(PreferToNullable).WithSpan(11, 14, 11, 63),
},
],
fixedCode + Environment.NewLine + OptionStubCode);
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#pragma warning disable SA1118 // StyleCop support for collection expressions is missing
using Xunit;
using static Funcky.Analyzers.AlternativeMonadAnalyzer;
using VerifyCS = Funcky.Analyzers.Test.CSharpCodeFixVerifier<Funcky.Analyzers.AlternativeMonadAnalyzer, Funcky.Analyzers.AlternativeMonad.MatchToOrElseCodeFix>;
Expand Down Expand Up @@ -83,8 +84,7 @@ public static void M3(Result<int> resultOfInt)
""";
await VerifyCS.VerifyCodeFixAsync(
inputCode + Environment.NewLine + OptionStubCode,
new[]
{
[
VerifyCS.Diagnostic(PreferGetOrElse).WithSpan(10, 9, 10, 50),
VerifyCS.Diagnostic(PreferGetOrElse).WithSpan(11, 9, 11, 50),
VerifyCS.Diagnostic(PreferGetOrElse).WithSpan(12, 9, 12, 52),
Expand All @@ -94,7 +94,7 @@ await VerifyCS.VerifyCodeFixAsync(
VerifyCS.Diagnostic(PreferGetOrElse).WithSpan(21, 9, 21, 51),
VerifyCS.Diagnostic(PreferGetOrElse).WithSpan(26, 9, 26, 56),
VerifyCS.Diagnostic(PreferGetOrElse).WithSpan(31, 9, 31, 54),
},
],
fixedCode + Environment.NewLine + OptionStubCode);
}

Expand Down Expand Up @@ -177,8 +177,7 @@ public static void M3(Result<int> resultOfInt, Result<int> fallback)
""";
await VerifyCS.VerifyCodeFixAsync(
inputCode + Environment.NewLine + OptionStubCode,
new[]
{
[
VerifyCS.Diagnostic(PreferOrElse).WithSpan(10, 9, 10, 71),
VerifyCS.Diagnostic(PreferOrElse).WithSpan(11, 9, 11, 69),
VerifyCS.Diagnostic(PreferOrElse).WithSpan(12, 9, 12, 56),
Expand All @@ -189,7 +188,7 @@ await VerifyCS.VerifyCodeFixAsync(
VerifyCS.Diagnostic(PreferOrElse).WithSpan(22, 9, 22, 58),
VerifyCS.Diagnostic(PreferOrElse).WithSpan(27, 9, 27, 77),
VerifyCS.Diagnostic(PreferOrElse).WithSpan(32, 9, 32, 67),
},
],
fixedCode + Environment.NewLine + OptionStubCode);
}

Expand Down Expand Up @@ -262,14 +261,13 @@ public static void M3(Result<int> resultOfInt)
""";
await VerifyCS.VerifyCodeFixAsync(
inputCode + Environment.NewLine + OptionStubCode,
new[]
{
[
VerifyCS.Diagnostic(PreferSelectMany).WithSpan(10, 9, 10, 84),
VerifyCS.Diagnostic(PreferSelectMany).WithSpan(11, 9, 11, 84),
VerifyCS.Diagnostic(PreferSelectMany).WithSpan(17, 9, 17, 63),
VerifyCS.Diagnostic(PreferSelectMany).WithSpan(22, 9, 22, 100),
VerifyCS.Diagnostic(PreferSelectMany).WithSpan(27, 9, 27, 83),
},
],
fixedCode + Environment.NewLine + OptionStubCode);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Microsoft.CodeAnalysis.Testing;
using Xunit;
using VerifyCS = Funcky.Analyzers.Test.CSharpCodeFixVerifier<Funcky.Analyzers.EnumerableRepeatNeverAnalyzer, Funcky.Analyzers.EnumerableRepeatNeverCodeFix>;

Expand Down Expand Up @@ -60,12 +61,12 @@ public async Task UsingEnumerableRepeatNeverWorksWithDifferentTypes()
[Fact]
public async Task CodeFixWorksWithDifferentUsingStyles()
{
var expectedDiagnostics = new[]
{
DiagnosticResult[] expectedDiagnostics =
[
VerifyCS.Diagnostic(EnumerableRepeatNeverAnalyzer.DiagnosticId).WithSpan(9, 17, 9, 35).WithArguments("\"value\"", "string"),
VerifyCS.Diagnostic(EnumerableRepeatNeverAnalyzer.DiagnosticId).WithSpan(20, 17, 20, 58).WithArguments("\"value\"", "string"),
VerifyCS.Diagnostic(EnumerableRepeatNeverAnalyzer.DiagnosticId).WithSpan(33, 17, 33, 39).WithArguments("\"value\"", "string"),
};
];

await VerifyWithSourceExample.VerifyDiagnosticAndCodeFix<EnumerableRepeatNeverAnalyzer, EnumerableRepeatNeverCodeFix>(expectedDiagnostics, "RepeatNeverQualification");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Microsoft.CodeAnalysis.Testing;
using Xunit;
using VerifyCS = Funcky.Analyzers.Test.CSharpCodeFixVerifier<Funcky.Analyzers.EnumerableRepeatOnceAnalyzer, Funcky.Analyzers.EnumerableRepeatOnceCodeFix>;

Expand Down Expand Up @@ -56,12 +57,12 @@ public async Task UsingEnumerableRepeatOnceViaConstantShowsTheSequenceReturnDiag
[Fact]
public async Task CodeFixWorksWithDifferentUsingStyles()
{
var expectedDiagnostics = new[]
{
DiagnosticResult[] expectedDiagnostics =
[
VerifyCS.Diagnostic(EnumerableRepeatOnceAnalyzer.DiagnosticId).WithSpan(17, 17, 17, 53).WithArguments("10"),
VerifyCS.Diagnostic(EnumerableRepeatOnceAnalyzer.DiagnosticId).WithSpan(28, 17, 28, 53).WithArguments("10"),
VerifyCS.Diagnostic(EnumerableRepeatOnceAnalyzer.DiagnosticId).WithSpan(41, 17, 41, 53).WithArguments("10"),
};
];

await VerifyWithSourceExample.VerifyDiagnosticAndCodeFix<EnumerableRepeatOnceAnalyzer, EnumerableRepeatOnceCodeFix>(expectedDiagnostics, "RepeatOnceQualification");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#pragma warning disable SA1010 // StyleCop support for collection expressions is missing
#pragma warning disable SA1118 // StyleCop support for collection expressions is missing
using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using Microsoft.CodeAnalysis;
Expand Down Expand Up @@ -88,11 +90,10 @@ public static void M()

await VerifyCS.VerifyCodeFixAsync(
inputCode + Environment.NewLine + OptionCode,
new[]
{
[
DiagnosticResult.CompilerError("CS1955").WithSpan(7, 47, 7, 51).WithArguments("Funcky.Monads.Option<int>.None"),
DiagnosticResult.CompilerError("CS1955").WithSpan(8, 47, 8, 51).WithArguments("Funcky.Monads.Option<int>.None"),
},
],
fixedCode + Environment.NewLine + OptionCode);
}

Expand Down Expand Up @@ -129,7 +130,7 @@ public static void M()
[SuppressMessage("MicrosoftCodeAnalysisCorrectness", "RS1001:Missing diagnostic analyzer attribute")]
internal sealed class NullDiagnosticAnalyzer : DiagnosticAnalyzer
{
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray<DiagnosticDescriptor>.Empty;
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => [];

[SuppressMessage("MicrosoftCodeAnalysisCorrectness", "RS1025:Configure generated code analysis")]
[SuppressMessage("MicrosoftCodeAnalysisCorrectness", "RS1026:Enable concurrent execution")]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#pragma warning disable SA1010 // StyleCop support for collection expressions is missing
using Funcky.Extensions;
using VerifyCS = Funcky.Analyzers.Test.CSharpCodeRefactoringVerifier<Funcky.Analyzers.OptionSomeWhereToFromBooleanRefactoring>;

Expand Down Expand Up @@ -45,7 +46,7 @@ public static class Option
}
""";

private static readonly IEnumerable<string> DefaultUsings = Sequence.Return("using Funcky.Monads;");
private static readonly IEnumerable<string> DefaultUsings = ["using Funcky.Monads;"];

private static async Task VerifyRefactoring(string source, string fixedSource, string supportSource, IEnumerable<string>? usings = null)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#pragma warning disable SA1010 // StyleCop support for collection expressions is missing
#pragma warning disable SA1118 // StyleCop support for collection expressions is missing
using Xunit;

namespace Funcky.Analyzers.Test;
Expand Down Expand Up @@ -42,7 +44,7 @@ await VerifyRefactoring(
"Option<IEnumerable<char>> x = Option.Return<IEnumerable<char>>(\"foo\").[||]Where(_ => true);",
"Option<IEnumerable<char>> x = Option.FromBoolean<IEnumerable<char>>(true, \"foo\");",
OptionCode,
usings: Sequence.Return("using Funcky.Monads;", "using System.Collections.Generic;"));
usings: ["using Funcky.Monads;", "using System.Collections.Generic;"]);
}

[Fact]
Expand Down Expand Up @@ -88,7 +90,7 @@ await VerifyRefactoring(
"var x = Return(10).[||]Where(_ => true);",
"var x = FromBoolean(true, 10);",
OptionCode,
usings: Sequence.Return("using static Funcky.Monads.Option;"));
usings: ["using static Funcky.Monads.Option;"]);
}

[Fact]
Expand All @@ -98,7 +100,7 @@ await VerifyRefactoring(
"var x = Funcky.Monads.Option.Return(10).[||]Where(_ => true);",
"var x = Funcky.Monads.Option.FromBoolean(true, 10);",
OptionCode,
usings: Enumerable.Empty<string>());
usings: []);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Microsoft.CodeAnalysis.Testing;
using Xunit;
using VerifyCS = Funcky.Analyzers.Test.CSharpCodeFixVerifier<Funcky.Analyzers.UseWithArgumentNamesAnalyzer, Funcky.Analyzers.AddArgumentNameCodeFix>;

Expand Down Expand Up @@ -110,8 +111,8 @@ private void MethodWithKeywordAsArgument(int @int) { }
}
""";

var expectedDiagnostics = new[]
{
DiagnosticResult[] expectedDiagnostics =
[
VerifyCS.Diagnostic().WithSpan(7, 23, 7, 25).WithArguments("y"),
VerifyCS.Diagnostic().WithSpan(8, 16, 8, 18).WithArguments("x"),
VerifyCS.Diagnostic().WithSpan(8, 20, 8, 22).WithArguments("y"),
Expand All @@ -120,7 +121,7 @@ private void MethodWithKeywordAsArgument(int @int) { }
VerifyCS.Diagnostic().WithSpan(12, 13, 12, 15).WithArguments("x"),
VerifyCS.Diagnostic().WithSpan(13, 13, 13, 15).WithArguments("y"),
VerifyCS.Diagnostic().WithSpan(14, 37, 14, 39).WithArguments("int"),
};
];

await VerifyCS.VerifyAnalyzerAsync(inputCode + AttributeSource, expectedDiagnostics);
await VerifyCS.VerifyCodeFixAsync(inputCode + AttributeSource, expectedDiagnostics, fixedCode + AttributeSource);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#pragma warning disable SA1010 // StyleCop support for collection expressions is missing
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp.Testing;
Expand Down Expand Up @@ -41,7 +42,7 @@ public static async Task VerifyCodeFixAsync(string source, string fixedSource)

/// <inheritdoc cref="CodeFixVerifier{TAnalyzer, TCodeFix, TTest, TVerifier}.VerifyCodeFixAsync(string, DiagnosticResult, string)"/>
public static async Task VerifyCodeFixAsync(string source, DiagnosticResult expected, string fixedSource)
=> await VerifyCodeFixAsync(source, new[] { expected }, fixedSource);
=> await VerifyCodeFixAsync(source, [expected], fixedSource);

/// <inheritdoc cref="CodeFixVerifier{TAnalyzer, TCodeFix, TTest, TVerifier}.VerifyCodeFixAsync(string, DiagnosticResult[], string)"/>
public static async Task VerifyCodeFixAsync(string source, DiagnosticResult[] expected, string fixedSource)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#pragma warning disable SA1010 // StyleCop support for collection expressions is missing
using Microsoft.CodeAnalysis.CodeRefactorings;
using Microsoft.CodeAnalysis.Testing;

Expand All @@ -15,7 +16,7 @@ public static async Task VerifyRefactoringAsync(string source, string fixedSourc
/// <inheritdoc cref="CodeRefactoringVerifier{TCodeRefactoring, TTest, TVerifier}.VerifyRefactoringAsync(string, DiagnosticResult, string)"/>
public static async Task VerifyRefactoringAsync(string source, DiagnosticResult expected, string fixedSource)
{
await VerifyRefactoringAsync(source, new[] { expected }, fixedSource);
await VerifyRefactoringAsync(source, [expected], fixedSource);
}

/// <inheritdoc cref="CodeRefactoringVerifier{TCodeRefactoring, TTest, TVerifier}.VerifyRefactoringAsync(string, DiagnosticResult[], string)"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#pragma warning disable SA1010 // StyleCop support for collection expressions is missing
using System.Collections.Immutable;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
Expand All @@ -17,7 +18,7 @@ internal static class CSharpVerifierHelper

private static ImmutableDictionary<string, ReportDiagnostic> GetNullableWarningsFromCompiler()
{
string[] args = { "/warnaserror:nullable" };
string[] args = ["/warnaserror:nullable"];
var commandLineArguments = CSharpCommandLineParser.Default.Parse(args, baseDirectory: Environment.CurrentDirectory, sdkDirectory: Environment.CurrentDirectory);
var nullableWarnings = commandLineArguments.CompilationOptions.SpecificDiagnosticOptions;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public async Task PartitionsItemsIntoLeftAndRight()
Either<int, string>.Right("b"),
Either<int, string>.Left(20));
var (left, right) = await input.PartitionAsync();
Assert.Equal(new[] { 10, 20 }, left);
Assert.Equal(new[] { "a", "b" }, right);
Assert.Equal([10, 20], left);
Assert.Equal(["a", "b"], right);
}

[Fact]
Expand All @@ -32,5 +32,6 @@ public async Task PartitionsItemsIntoLeftAndRightWithSelector()
Assert.Equal(input.Where(IsEven), right);
}

private static bool IsEven(int n) => n % 2 == 0;
private static bool IsEven(int n)
=> n % 2 is 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,31 @@ public async Task ReturnsTwoEmptyListsWhenSourceIsEmpty()
public async Task PartitionsItemsIntoTruesAndFalses()
{
var (evens, odds) = await AsyncEnumerable.Range(0, 6).PartitionAsync(IsEven);
Assert.Equal(new[] { 0, 2, 4 }, evens);
Assert.Equal(new[] { 1, 3, 5 }, odds);
Assert.Equal([0, 2, 4], evens);
Assert.Equal([1, 3, 5], odds);
}

[Fact]
public async Task PartitionsItemsIntoTruesAndFalsesWithAsyncPredicate()
{
var (evens, odds) = await AsyncEnumerable.Range(0, 6).PartitionAwaitAsync(x => ValueTask.FromResult(IsEven(x)));
Assert.Equal(new[] { 0, 2, 4 }, evens);
Assert.Equal(new[] { 1, 3, 5 }, odds);
Assert.Equal([0, 2, 4], evens);
Assert.Equal([1, 3, 5], odds);
}

[Fact]
public async Task PartitionsItemsIntoTruesAndFalsesWithAsyncCancellablePredicate()
{
var (evens, odds) = await AsyncEnumerable.Range(0, 6).PartitionAwaitWithCancellationAsync((x, _) => ValueTask.FromResult(IsEven(x)));
Assert.Equal(new[] { 0, 2, 4 }, evens);
Assert.Equal(new[] { 1, 3, 5 }, odds);
Assert.Equal([0, 2, 4], evens);
Assert.Equal([1, 3, 5], odds);
}

[Fact]
public async Task RightItemsAreEmptyWhenPredicateMatchesAllItems()
{
var (left, right) = await AsyncEnumerable.Range(0, 6).PartitionAsync(True);
Assert.Equal(new[] { 0, 1, 2, 3, 4, 5 }, left);
Assert.Equal([0, 1, 2, 3, 4, 5], left);
Assert.Empty(right);
}

Expand All @@ -47,7 +47,7 @@ public async Task LeftItemsAreEmptyWhenPredicateMatchesNoItems()
{
var (left, right) = await AsyncEnumerable.Range(0, 6).PartitionAsync(False);
Assert.Empty(left);
Assert.Equal(new[] { 0, 1, 2, 3, 4, 5 }, right);
Assert.Equal([0, 1, 2, 3, 4, 5], right);
}

private static bool IsEven(int n) => n % 2 == 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ public async Task TakeEveryWithLessThanIntervalElementsReturnsOnlyFirstElement()
[Fact]
public async Task TakeEveryWithASourceWith5ElementsAndInterval4Returns2Elements()
{
Assert.Equal(new[] { 1, 5 }, await OneToFive.TakeEvery(4).ToListAsync());
Assert.Equal([1, 5], await OneToFive.TakeEvery(4).ToListAsync());
}
}
Loading