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

Define Code Style via Editorconfig #785

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
90 changes: 90 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,93 @@ indent_size = 2
[*.verified.cs]
trim_trailing_whitespace = false
insert_final_newline = false

# We have to set the severity via dotnet_diagnostic.ID.severity because
# otherwise the warnings are only shown in the IDE, not in the build:
# See: https://github.com/dotnet/roslyn/issues/49439

# Some rules can't be enforced during builds unfortunately,
# but are enforced by `dotnet format`.
# https://github.com/dotnet/roslyn/blob/9f87b444da9c48a4d492b19f8337339056bf2b95/src/Analyzers/Core/Analyzers/EnforceOnBuildValues.cs#L95

[*.cs]
# "This." and "Me." qualifiers
dotnet_style_qualification_for_field = false
dotnet_style_qualification_for_property = false
dotnet_style_qualification_for_method = false
dotnet_style_qualification_for_event = false
dotnet_diagnostic.IDE0003.severity = warning # Remove `this` or `Me` qualification

# Remove unnecessary usings
dotnet_diagnostic.IDE0005.severity = warning # Remove unnecessary import

# Prefer `var` over explicit type
csharp_style_var_for_built_in_types = true
csharp_style_var_when_type_is_apparent = true
csharp_style_var_elsewhere = true
dotnet_diagnostic.IDE0007.severity = warning

# Missing Switch Cases
dotnet_diagnostic.IDE0010.severity = warning # Add missing cases to switch statement

# Braces
csharp_prefer_braces = true
dotnet_diagnostic.IDE0011.severity = warning # Add braces

# `out var`
csharp_style_inlined_variable_declaration = true # Prefer `out` variables to be declared inline in the argument list of a method call when possible
dotnet_diagnostic.IDE0018.severity = warning

# TODO: Go through all language rules: https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/language-rules

# Formatting
# See: https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/csharp-formatting-options
dotnet_diagnostic.IDE0055.severity = warning

dotnet_sort_system_directives_first = true

## New-line options
csharp_new_line_before_open_brace = all
csharp_new_line_before_else = true
csharp_new_line_before_catch = true
csharp_new_line_before_finally = true
csharp_new_line_before_members_in_anonymous_types = true
csharp_new_line_between_query_expression_clauses = true

## Indentation options
csharp_indent_case_contents = true
csharp_indent_switch_labels = true
csharp_indent_labels = flush_left
csharp_indent_block_contents = true
csharp_indent_braces = false
csharp_indent_case_contents_when_block = true

## Spacing Options
csharp_space_after_cast = false
csharp_space_after_keywords_in_control_flow_statements = true
csharp_space_between_parentheses = false
csharp_space_before_colon_in_inheritance_clause = true
csharp_space_after_colon_in_inheritance_clause = true
csharp_space_around_binary_operators = before_and_after
csharp_space_between_method_declaration_parameter_list_parentheses = false
csharp_space_between_method_declaration_empty_parameter_list_parentheses = false
csharp_space_between_method_declaration_name_and_open_parenthesis = false
csharp_space_between_method_call_parameter_list_parentheses = false
csharp_space_between_method_call_empty_parameter_list_parentheses = false
csharp_space_between_method_call_name_and_opening_parenthesis = false
csharp_space_after_comma = true
csharp_space_before_comma = false
csharp_space_after_dot = false
csharp_space_before_dot = false
csharp_space_after_semicolon_in_for_statement = true
csharp_space_before_semicolon_in_for_statement = false
csharp_space_around_declaration_statements = false
csharp_space_before_open_square_brackets = false
csharp_space_between_empty_square_brackets = false
csharp_space_between_square_brackets = false

## Wrap Options
csharp_preserve_single_line_statements = false

# Naming
# TODO: Define naming (see https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/naming-rules)
18 changes: 18 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,24 @@ jobs:
- name: Run Tests
run: dotnet test --configuration Release --no-build

style:
name: Check Formatting and Style
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-dotnet@v3
name: Install Current .NET SDK
- name: Restore dependencies
run: dotnet restore
# We need to run a build first so that the attribute from our source generator is available.
# Otherwise we get warnings about unused usings.
- name: Build
run: dotnet build Funcky.SourceGenerator --no-restore && dotnet build Funcky.FsCheck --no-restore
- run: dotnet format style --verify-no-changes --no-restore
name: Check Style
- run: dotnet format whitespace --verify-no-changes --no-restore
name: Check Whitespace

trimming-test:
name: Trimming Test
runs-on: ubuntu-latest
Expand Down
3 changes: 0 additions & 3 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
<PropertyGroup>
<CheckEolTargetFramework>false</CheckEolTargetFramework>
</PropertyGroup>
<ItemGroup Label="Code Style">
<PackageReference Include="Messerli.CodeStyle" PrivateAssets="all" />
</ItemGroup>
<PropertyGroup Label="Code Style">
<AnalysisLevel>5.0</AnalysisLevel>
</PropertyGroup>
Expand Down
1 change: 0 additions & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
</ItemGroup>
<ItemGroup Label="Build Dependencies">
<PackageVersion Include="PolySharp" Version="1.14.0" />
<PackageVersion Include="Messerli.CodeStyle" Version="2.3.0" />
<PackageVersion Include="Microsoft.CodeAnalysis.PublicApiAnalyzers" Version="3.3.3" />
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="1.1.1" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private static bool MatchRepeatOnce(
{
valueArgument = null;
return MatchMethod(operation, enumerableType, nameof(Enumerable.Repeat))
&& MatchArguments(operation, out valueArgument, AnyArgument, out _, ConstantArgument(1));
&& MatchArguments(operation, out valueArgument, AnyArgument, out _, ConstantArgument(1));
}

private static Diagnostic CreateDiagnostic(IInvocationOperation operation, IArgumentOperation valueArgument)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ private static bool OriginatesInRazorFile(IOperation operation)

private static IEnumerable<INamedTypeSymbol> GetBaseTypes(INamedTypeSymbol type)
{
for (INamedTypeSymbol? baseType = type.BaseType; baseType is not null; baseType = baseType.BaseType)
for (var baseType = type.BaseType; baseType is not null; baseType = baseType.BaseType)
{
yield return baseType;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public async Task GivenASequenceOfSequencesInterleaveReturnsTheExpectedSequence(
var innerSum = sequences.Select(async element => await element.CountAsync()).Aggregate(0, (total, part) => total + part.Result);
Assert.Equal(innerSum, await sequences.Interleave().CountAsync());

int expected = 1;
var expected = 1;
await foreach (var element in sequences.Interleave())
{
Assert.Equal(expected, element);
Expand Down
2 changes: 1 addition & 1 deletion Funcky.Async/AsyncSequence/AsyncSequence.CycleRange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ private async IAsyncEnumerator<T> GetEnumeratorInternal()
// this can change on Dispose!
var bufferCount = _buffer.Count;

for (int cycle = 1; IsCycling(cycle); ++cycle)
for (var cycle = 1; IsCycling(cycle); ++cycle)
{
for (var index = 0; index < bufferCount; ++index)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ private static async IAsyncEnumerable<TSource> InterleaveInternal<TSource>(
}
}

#pragma warning disable IDISP007 // The entire point of this method is to dispose.
#pragma warning disable IDISP007 // The entire point of this method is to dispose.
private static async Task DisposeEnumerator<T>(IAsyncEnumerator<T> enumerator) => await enumerator.DisposeAsync().ConfigureAwait(false);
#pragma warning restore IDISP007
#pragma warning restore IDISP007

private static ImmutableList<IAsyncEnumerator<TSource>> GetInterleaveEnumerators<TSource>(
IEnumerable<IAsyncEnumerable<TSource>> source,
Expand Down
4 changes: 2 additions & 2 deletions Funcky.Async/Extensions/AsyncEnumerableExtensions/Pairwise.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ private static async IAsyncEnumerable<TResult> PairwiseInternal<TSource, TResult
Func<TSource, TSource, TResult> resultSelector,
[EnumeratorCancellation] CancellationToken cancellationToken = default)
{
#pragma warning disable CA2007 // Consider calling ConfigureAwait on the awaited task
#pragma warning disable CA2007 // Consider calling ConfigureAwait on the awaited task
await using var enumerator = source.ConfigureAwait(false).WithCancellation(cancellationToken).GetAsyncEnumerator();
#pragma warning restore CA2007 // Consider calling ConfigureAwait on the awaited task
#pragma warning restore CA2007 // Consider calling ConfigureAwait on the awaited task

if (await enumerator.MoveNextAsync() == false)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ public static partial class AsyncEnumerableExtensions
[Pure]
public static async IAsyncEnumerable<ValueWithFirst<TSource>> WithFirst<TSource>(this IAsyncEnumerable<TSource> source)
{
#pragma warning disable CA2007 // Consider calling ConfigureAwait on the awaited task
#pragma warning disable CA2007 // Consider calling ConfigureAwait on the awaited task
await using var enumerator = source.ConfigureAwait(false).GetAsyncEnumerator();
#pragma warning restore CA2007 // Consider calling ConfigureAwait on the awaited task
#pragma warning restore CA2007 // Consider calling ConfigureAwait on the awaited task

if (!await enumerator.MoveNextAsync())
{
Expand Down
4 changes: 2 additions & 2 deletions Funcky.Async/Extensions/AsyncEnumerableExtensions/WithLast.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ public static partial class AsyncEnumerableExtensions
[Pure]
public static async IAsyncEnumerable<ValueWithLast<TSource>> WithLast<TSource>(this IAsyncEnumerable<TSource> source)
{
#pragma warning disable CA2007 // Consider calling ConfigureAwait on the awaited task
#pragma warning disable CA2007 // Consider calling ConfigureAwait on the awaited task
await using var enumerator = source.ConfigureAwait(false).GetAsyncEnumerator();
#pragma warning restore CA2007 // Consider calling ConfigureAwait on the awaited task
#pragma warning restore CA2007 // Consider calling ConfigureAwait on the awaited task

if (!await enumerator.MoveNextAsync())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ public static async IAsyncEnumerable<TResult> ZipLongest<TLeft, TRight, TResult>
where TLeft : notnull
where TRight : notnull
{
#pragma warning disable CA2007 // Consider calling ConfigureAwait on the awaited task
#pragma warning disable CA2007 // Consider calling ConfigureAwait on the awaited task
await using var leftEnumerator = left.ConfigureAwait(false).GetAsyncEnumerator();
await using var rightEnumerator = right.ConfigureAwait(false).GetAsyncEnumerator();
#pragma warning restore CA2007 // Consider calling ConfigureAwait on the awaited task
#pragma warning restore CA2007 // Consider calling ConfigureAwait on the awaited task

while ((await MoveNextOrNone(leftEnumerator, rightEnumerator).ConfigureAwait(false)).TryGetValue(out var next))
{
Expand Down
2 changes: 2 additions & 0 deletions Funcky.Test/Extensions/EnumerableExtensions/ChunkTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ public void GivenAnEnumerableNotAMultipleOfSizeWeHaveASmallerLastSlice()
var numbers = Sequence.Return("a", "b", "c", "d", "e", "g", "h", "i", "j").ToList();

const int chunkSize = 4;
#pragma warning disable IDE0007 // False positive
IEnumerable<IReadOnlyList<string>> chunked = numbers.Chunk(chunkSize);
#pragma warning restore IDE0007

Assert.Collection(
chunked,
Expand Down
6 changes: 3 additions & 3 deletions Funcky.Test/Extensions/EnumerableExtensions/InterleaveTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public void GivenAnEmptySequenceOfSequencesInterleaveReturnsAnEmptySequence()
{
IEnumerable<IEnumerable<int>> emptySequence = [];

IEnumerable<int> interleaved = emptySequence.Interleave();
var interleaved = emptySequence.Interleave();

Assert.Empty(interleaved);
}
Expand All @@ -42,7 +42,7 @@ public void GivenTwoSequencesOfUnequalLengthIGetAnInterleavedResult()
IEnumerable<int> evens = [2, 4, 6];
IEnumerable<int> expected = [1, 2, 3, 4, 5, 6, 7, 9, 11];

IEnumerable<int> interleaved = odds.Interleave(evens);
var interleaved = odds.Interleave(evens);

Assert.Equal(expected, interleaved);
}
Expand Down Expand Up @@ -99,7 +99,7 @@ public void GivenASequenceOfSequencesInterleaveReturnsTheExpectedSequence()

Assert.Equal(sequences.Select(s => s.Count()).Sum(), sequences.Interleave().Count());

int expected = 1;
var expected = 1;
foreach (var element in sequences.Interleave())
{
Assert.Equal(expected, element);
Expand Down
14 changes: 7 additions & 7 deletions Funcky.Test/FunctionalClass/FlipTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public Property GivenAFunctionWith8ParametersTheFirstTwoParametersGetFlipped(int
[Property]
public Property GivenAnActionWith2ParametersTheFirstTwoParametersGetFlipped(int number, string text)
{
string side = string.Empty;
var side = string.Empty;
Action<int, string> f = (number, text) => side = $"number:{number}, text:{text}";

f(number, text);
Expand All @@ -73,7 +73,7 @@ public Property GivenAnActionWith2ParametersTheFirstTwoParametersGetFlipped(int
[Property]
public Property GivenAnActionWith3ParametersTheFirstTwoParametersGetFlipped(int number, string text)
{
string side = string.Empty;
var side = string.Empty;
Action<int, string, bool> f = (number, text, p3) => side = $"number:{number}, text:{text}, {p3}";

f(number, text, true);
Expand All @@ -86,7 +86,7 @@ public Property GivenAnActionWith3ParametersTheFirstTwoParametersGetFlipped(int
[Property]
public Property GivenAnActionWith4ParametersTheFirstTwoParametersGetFlipped(int number, string text)
{
string side = string.Empty;
var side = string.Empty;
Action<int, string, bool, bool> f = (number, text, p3, p4) => side = $"number:{number}, text:{text}, {p3}, {p4}";

f(number, text, true, false);
Expand All @@ -99,7 +99,7 @@ public Property GivenAnActionWith4ParametersTheFirstTwoParametersGetFlipped(int
[Property]
public Property GivenAnActionWith5ParametersTheFirstTwoParametersGetFlipped(int number, string text)
{
string side = string.Empty;
var side = string.Empty;
Action<int, string, bool, bool, bool> f = (number, text, p3, p4, p5) => side = $"number:{number}, text:{text}, {p3}, {p4}, {p5}";

f(number, text, true, false, false);
Expand All @@ -112,7 +112,7 @@ public Property GivenAnActionWith5ParametersTheFirstTwoParametersGetFlipped(int
[Property]
public Property GivenAnActionWith6ParametersTheFirstTwoParametersGetFlipped(int number, string text)
{
string side = string.Empty;
var side = string.Empty;
Action<int, string, bool, bool, bool, bool> f = (number, text, p3, p4, p5, p6) => side = $"number:{number}, text:{text}, {p3}, {p4}, {p5}, {p6}";

f(number, text, true, false, false, true);
Expand All @@ -125,7 +125,7 @@ public Property GivenAnActionWith6ParametersTheFirstTwoParametersGetFlipped(int
[Property]
public Property GivenAnActionWith7ParametersTheFirstTwoParametersGetFlipped(int number, string text)
{
string side = string.Empty;
var side = string.Empty;
Action<int, string, bool, bool, bool, bool, bool> f = (number, text, p3, p4, p5, p6, p7) => side = $"number:{number}, text:{text}, {p3}, {p4}, {p5}, {p6}, {p7}";

f(number, text, true, false, false, true, true);
Expand All @@ -138,7 +138,7 @@ public Property GivenAnActionWith7ParametersTheFirstTwoParametersGetFlipped(int
[Property]
public Property GivenAnActionWith8ParametersTheFirstTwoParametersGetFlipped(int number, string text)
{
string side = string.Empty;
var side = string.Empty;
Action<int, string, bool, bool, bool, bool, bool, bool> f = (number, text, p3, p4, p5, p6, p7, p8) => side = $"number:{number}, text:{text}, {p3}, {p4}, {p5}, {p6}, {p7}, {p8}";

f(number, text, true, false, false, true, true, true);
Expand Down
2 changes: 2 additions & 0 deletions Funcky.Test/UpCastTest.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#pragma warning disable IDE0007 // Use implicit type

using System.Collections.Immutable;

namespace Funcky.Test;
Expand Down
6 changes: 3 additions & 3 deletions Funcky.Xunit/FunctionalAssert/Error.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ public static partial class FunctionalAssert
{
/// <summary>Asserts that the given <paramref name="result"/> is <c>Error</c>.</summary>
/// <exception cref="AssertActualExpectedException">Thrown when <paramref name="result"/> is <c>Ok</c>.</exception>
#if STACK_TRACE_HIDDEN_SUPPORTED
#if STACK_TRACE_HIDDEN_SUPPORTED
[System.Diagnostics.StackTraceHidden]
#else
#else
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
#endif
#endif
[SuppressMessage("Microsoft.Usage", "CA2200", Justification = "Stack trace erasure intentional.")]
public static Exception Error<TValidResult>(Result<TValidResult> result)
where TValidResult : notnull
Expand Down
12 changes: 6 additions & 6 deletions Funcky.Xunit/FunctionalAssert/Left.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ public static partial class FunctionalAssert
{
/// <summary>Asserts that the given <paramref name="either"/> is <c>Left</c> and contains the given <paramref name="expectedLeft"/>.</summary>
/// <exception cref="AssertActualExpectedException">Thrown when <paramref name="either"/> is <c>Right</c>.</exception>
#if STACK_TRACE_HIDDEN_SUPPORTED
#if STACK_TRACE_HIDDEN_SUPPORTED
[System.Diagnostics.StackTraceHidden]
#else
#else
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
#endif
#endif
public static void Left<TLeft, TRight>(TLeft expectedLeft, Either<TLeft, TRight> either)
where TLeft : notnull
where TRight : notnull
Expand All @@ -33,11 +33,11 @@ public static void Left<TLeft, TRight>(TLeft expectedLeft, Either<TLeft, TRight>
/// <summary>Asserts that the given <paramref name="either"/> is <c>Left</c>.</summary>
/// <exception cref="AssertActualExpectedException">Thrown when <paramref name="either"/> is <c>Right</c>.</exception>
/// <returns>Returns the value in <paramref name="either"/> if it was <c>Left</c>.</returns>
#if STACK_TRACE_HIDDEN_SUPPORTED
#if STACK_TRACE_HIDDEN_SUPPORTED
[System.Diagnostics.StackTraceHidden]
#else
#else
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
#endif
#endif
[SuppressMessage("Microsoft.Usage", "CA2200", Justification = "Stack trace erasure intentional.")]
[SuppressMessage("ReSharper", "PossibleIntendedRethrow", Justification = "Stack trace erasure intentional.")]
public static TLeft Left<TLeft, TRight>(Either<TLeft, TRight> either)
Expand Down
6 changes: 3 additions & 3 deletions Funcky.Xunit/FunctionalAssert/None.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ public static partial class FunctionalAssert
{
/// <summary>Asserts that the given <paramref name="option"/> is <c>None</c>.</summary>
/// <exception cref="AssertActualExpectedException">Thrown when <paramref name="option"/> is <c>Some</c>.</exception>
#if STACK_TRACE_HIDDEN_SUPPORTED
#if STACK_TRACE_HIDDEN_SUPPORTED
[System.Diagnostics.StackTraceHidden]
#else
#else
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
#endif
#endif
[SuppressMessage("Microsoft.Usage", "CA2200", Justification = "Stack trace erasure intentional.")]
[SuppressMessage("ReSharper", "PossibleIntendedRethrow", Justification = "Stack trace erasure intentional.")]
public static void None<TItem>(Option<TItem> option)
Expand Down
Loading
Loading