Skip to content

Commit a8a7d16

Browse files
Enforce explicit type declarations and update coding styles
Updated .editorconfig to include new style rules for C# coding conventions. Replaced `var` with explicit type declarations across multiple files to improve code readability and maintainability. Added `ConfigureAwait(false)` to some `await` calls to avoid potential deadlocks. Made minor formatting adjustments and clarified access levels of classes and methods. Updated `NoWarn` property in the project file to include `IDE0079`.
1 parent 1c086d0 commit a8a7d16

25 files changed

+232
-197
lines changed

.editorconfig

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ dotnet_style_allow_statement_immediately_after_block_experimental = false
7474
#### C# Coding Conventions ####
7575

7676
# var preferences
77-
csharp_style_var_elsewhere = false
78-
csharp_style_var_for_built_in_types = false
77+
csharp_style_var_elsewhere = false:silent
78+
csharp_style_var_for_built_in_types = false:silent
7979
csharp_style_var_when_type_is_apparent = true:suggestion
8080

8181
# Expression-bodied members
@@ -99,7 +99,7 @@ csharp_style_prefer_switch_expression = true:suggestion
9999
csharp_style_conditional_delegate_call = true:warning
100100

101101
# Modifier preferences
102-
csharp_prefer_static_local_function = true
102+
csharp_prefer_static_local_function = true:suggestion
103103
csharp_preferred_modifier_order = public,private,protected,internal,static,extern,new,virtual,abstract,sealed,override,readonly,unsafe,volatile,async
104104

105105
# Code-block preferences
@@ -108,14 +108,14 @@ csharp_prefer_simple_using_statement = true:suggestion
108108
csharp_style_namespace_declarations = file_scoped:suggestion
109109

110110
# Expression-level preferences
111-
csharp_prefer_simple_default_expression = true
112-
csharp_style_deconstructed_variable_declaration = true
113-
csharp_style_implicit_object_creation_when_type_is_apparent = true
114-
csharp_style_inlined_variable_declaration = true
111+
csharp_prefer_simple_default_expression = true:suggestion
112+
csharp_style_deconstructed_variable_declaration = true:suggestion
113+
csharp_style_implicit_object_creation_when_type_is_apparent = true:suggestion
114+
csharp_style_inlined_variable_declaration = true:suggestion
115115
csharp_style_pattern_local_over_anonymous_function = true
116116
csharp_style_prefer_index_operator = true:silent
117117
csharp_style_prefer_null_check_over_type_check = true:warning
118-
csharp_style_prefer_range_operator = true:silent
118+
csharp_style_prefer_range_operator = false:silent
119119
csharp_style_throw_expression = true:warning
120120
csharp_style_unused_value_assignment_preference = discard_variable:suggestion
121121
csharp_style_unused_value_expression_statement_preference = discard_variable:silent
@@ -125,8 +125,8 @@ csharp_using_directive_placement = outside_namespace:warning
125125

126126
# New line preferences
127127
csharp_style_allow_blank_line_after_colon_in_constructor_initializer_experimental = false:warning
128-
csharp_style_allow_blank_lines_between_consecutive_braces_experimental = true
129-
csharp_style_allow_embedded_statements_on_same_line_experimental = true
128+
csharp_style_allow_blank_lines_between_consecutive_braces_experimental = true:silent
129+
csharp_style_allow_embedded_statements_on_same_line_experimental = true:silent
130130

131131
#### C# Formatting Rules ####
132132

@@ -225,6 +225,16 @@ dotnet_diagnostic.IDE0290.severity = silent
225225
csharp_style_prefer_method_group_conversion = true:silent
226226
csharp_style_prefer_top_level_statements = true:silent
227227
csharp_style_prefer_primary_constructors = true:suggestion
228+
csharp_prefer_system_threading_lock = true:suggestion
229+
csharp_style_prefer_local_over_anonymous_function = true:suggestion
230+
csharp_style_prefer_tuple_swap = true:suggestion
231+
csharp_style_prefer_utf8_string_literals = true:suggestion
232+
csharp_prefer_static_anonymous_function = true:warning
233+
csharp_style_prefer_readonly_struct = true:error
234+
csharp_style_prefer_readonly_struct_member = true:suggestion
235+
csharp_style_allow_blank_line_after_token_in_conditional_expression_experimental = false:error
236+
csharp_style_allow_blank_line_after_token_in_arrow_expression_clause_experimental = false:error
237+
csharp_style_prefer_extended_property_pattern = true:suggestion
228238

229239
[*.{cs,vb}]
230240
dotnet_style_operator_placement_when_wrapping = beginning_of_line
@@ -243,4 +253,27 @@ dotnet_style_prefer_conditional_expression_over_assignment = true:warning
243253
dotnet_style_prefer_conditional_expression_over_return = true:warning
244254
dotnet_style_explicit_tuple_names = true:suggestion
245255
dotnet_diagnostic.CA1510.severity = none
246-
dotnet_code_quality_unused_parameters = all:suggestion
256+
dotnet_code_quality_unused_parameters = all:suggestion
257+
dotnet_style_prefer_inferred_tuple_names = true:suggestion
258+
dotnet_style_prefer_inferred_anonymous_type_member_names = true:suggestion
259+
dotnet_style_prefer_compound_assignment = true:suggestion
260+
dotnet_style_prefer_simplified_interpolation = true:suggestion
261+
dotnet_style_namespace_match_folder = true:suggestion
262+
dotnet_style_readonly_field = true:suggestion
263+
dotnet_style_predefined_type_for_locals_parameters_members = true:warning
264+
dotnet_style_predefined_type_for_member_access = true:warning
265+
dotnet_style_require_accessibility_modifiers = for_non_interface_members:silent
266+
dotnet_style_allow_multiple_blank_lines_experimental = true:silent
267+
dotnet_style_allow_statement_immediately_after_block_experimental = false:silent
268+
dotnet_style_parentheses_in_arithmetic_binary_operators = never_if_unnecessary:suggestion
269+
dotnet_style_parentheses_in_other_binary_operators = never_if_unnecessary:suggestion
270+
dotnet_style_parentheses_in_relational_binary_operators = always_for_clarity:suggestion
271+
dotnet_style_parentheses_in_other_operators = never_if_unnecessary:suggestion
272+
dotnet_style_qualification_for_field = false:silent
273+
dotnet_style_qualification_for_property = false:suggestion
274+
dotnet_style_qualification_for_method = false:suggestion
275+
dotnet_style_qualification_for_event = false:suggestion
276+
dotnet_diagnostic.CA1832.severity = silent
277+
dotnet_diagnostic.CA1833.severity = silent
278+
dotnet_diagnostic.CA1831.severity = silent
279+
dotnet_diagnostic.IDE0058.severity = silent

Open.ChannelExtensions.ComparisonTests/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ await Enumerable
109109
{
110110
Console.WriteLine("Async Enumerable test...");
111111
var sw = Stopwatch.StartNew();
112-
await foreach (var e in Enumerable
112+
await foreach (int e in Enumerable
113113
.Repeat(Delay, repeat)
114114
.Select((t, i) => t(i))
115115
.ToChannelAsync()

Open.ChannelExtensions.Tests/AssumptionTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public static async Task WaitCancellation()
1010
{
1111
CancellationToken token = tokenSource.Token;
1212

13-
var t = channel.Reader.WaitToReadAsync(token);
13+
ValueTask<bool> t = channel.Reader.WaitToReadAsync(token);
1414
tokenSource.Cancel();
1515

1616
// NOTE: a cancelled WaitToReadAsync will throw.
@@ -21,8 +21,8 @@ public static async Task WaitCancellation()
2121
{
2222
CancellationToken token = tokenSource.Token;
2323

24-
var t1 = channel.Reader.WaitToReadAsync(token);
25-
var t2 = channel.Reader.WaitToReadAsync();
24+
ValueTask<bool> t1 = channel.Reader.WaitToReadAsync(token);
25+
ValueTask<bool> t2 = channel.Reader.WaitToReadAsync();
2626
tokenSource.Cancel();
2727

2828
// NOTE: a cancelled WhenAny will not throw!

Open.ChannelExtensions.Tests/BasicTests.cs

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,14 @@ public static async Task ReadAll(int testSize)
5858
public static async Task ReadAllAsEnumerables1()
5959
{
6060
var channel = Channel.CreateUnbounded<int>();
61-
var read = channel.Reader.ReadAllAsEnumerables(e =>
61+
ValueTask read = channel.Reader.ReadAllAsEnumerables(e =>
6262
{
6363
Thread.Sleep(100);
6464
Assert.Equal(1000, e.Count());
6565
});
6666

6767
await channel.Writer.WriteAll(Enumerable.Range(0, 1000));
68-
for (var i = 0; i < 5; i++)
68+
for (int i = 0; i < 5; i++)
6969
{
7070
await Task.Delay(1000);
7171
await channel.Writer.WriteAll(Enumerable.Range(0, 1000));
@@ -79,14 +79,14 @@ public static async Task ReadAllAsEnumerables1()
7979
public static async Task ReadAllAsEnumerablesAsync1()
8080
{
8181
var channel = Channel.CreateUnbounded<int>();
82-
var read = channel.Reader.ReadAllAsEnumerablesAsync(async e =>
82+
ValueTask read = channel.Reader.ReadAllAsEnumerablesAsync(async e =>
8383
{
8484
await Task.Delay(100);
8585
Assert.Equal(1000, e.Count());
8686
});
8787

8888
await channel.Writer.WriteAll(Enumerable.Range(0, 1000));
89-
for (var i = 0; i < 5; i++)
89+
for (int i = 0; i < 5; i++)
9090
{
9191
await Task.Delay(1000);
9292
await channel.Writer.WriteAll(Enumerable.Range(0, 1000));
@@ -101,17 +101,17 @@ public static async Task ReadAllConcurrentlyAsEnumerablesAsync()
101101
{
102102
var channel = Channel.CreateUnbounded<int>();
103103
int total = 0;
104-
var read = channel.Reader.ReadAllConcurrentlyAsEnumerablesAsync(3, async e =>
104+
Task read = channel.Reader.ReadAllConcurrentlyAsEnumerablesAsync(3, async e =>
105105
{
106-
foreach (var i in e)
106+
foreach (int i in e)
107107
{
108108
await Task.Delay(1);
109109
Interlocked.Increment(ref total);
110110
}
111111
});
112112

113113
await channel.Writer.WriteAll(Enumerable.Range(0, 1000));
114-
for (var i = 0; i < 5; i++)
114+
for (int i = 0; i < 5; i++)
115115
{
116116
await Task.Delay(1000);
117117
await channel.Writer.WriteAll(Enumerable.Range(0, 1000));
@@ -128,11 +128,11 @@ public static async Task ReadAllConcurrentlyAsEnumerables()
128128
{
129129
var channel = Channel.CreateUnbounded<int>();
130130
int total = 0;
131-
var read = channel.Reader.ReadAllConcurrentlyAsEnumerables(3, e =>
131+
Task read = channel.Reader.ReadAllConcurrentlyAsEnumerables(3, e =>
132132
{
133-
foreach (var i in e)
133+
foreach (int i in e)
134134
{
135-
for (var n = 0; n < 2000000; n++)
135+
for (int n = 0; n < 2000000; n++)
136136
{
137137
// loop delay
138138
}
@@ -141,7 +141,7 @@ public static async Task ReadAllConcurrentlyAsEnumerables()
141141
});
142142

143143
await channel.Writer.WriteAll(Enumerable.Range(0, 1000));
144-
for (var i = 0; i < 5; i++)
144+
for (int i = 0; i < 5; i++)
145145
{
146146
await Task.Delay(1000);
147147
await channel.Writer.WriteAll(Enumerable.Range(0, 1000));
@@ -158,10 +158,10 @@ public static async Task ReadAllAsEnumerables2()
158158
{
159159
var channel = Channel.CreateUnbounded<int>();
160160
int total = 0;
161-
var read = channel.Reader.ReadAllAsEnumerables(e => total += e.Count());
161+
ValueTask read = channel.Reader.ReadAllAsEnumerables(e => total += e.Count());
162162

163163
await channel.Writer.WriteAll(Enumerable.Range(0, 1000));
164-
for (var i = 0; i < 5; i++)
164+
for (int i = 0; i < 5; i++)
165165
{
166166
await Task.Delay(1000);
167167
await channel.Writer.WriteAll(Enumerable.Range(0, 1000));
@@ -468,18 +468,18 @@ public static async Task PipeFilterTest()
468468
const int each = 5_000;
469469
const int total = 2 * each;
470470

471-
var source = Enumerable.Range(0, total).ToChannel(300);
471+
ChannelReader<int> source = Enumerable.Range(0, total).ToChannel(300);
472472

473-
var evenFilter = source
474-
.PipeFilter(out var unmatched, 100, 10,
473+
ValueTask<List<int>> evenFilter = source
474+
.PipeFilter(out ChannelReader<int> unmatched, 100, 10,
475475
static e => e % 2 == 0)
476476
.ToListAsync(each);
477477

478-
var oddFilter = unmatched
478+
ValueTask<List<int>> oddFilter = unmatched
479479
.ToListAsync(each);
480480

481-
var even = await evenFilter;
482-
var odd = await oddFilter;
481+
List<int> even = await evenFilter;
482+
List<int> odd = await oddFilter;
483483

484484
even.Count.Should().Be(each);
485485
odd.Count.Should().Be(each);
@@ -493,21 +493,21 @@ public static async Task PipeFilterAsyncTest()
493493
const int each = 5_000;
494494
const int total = 2 * each;
495495

496-
var source = Enumerable.Range(0, total).ToChannel(300);
496+
ChannelReader<int> source = Enumerable.Range(0, total).ToChannel(300);
497497

498-
var evenFilter = source
499-
.PipeFilterAsync(out var unmatched, 10, 100, static async e =>
498+
ValueTask<List<int>> evenFilter = source
499+
.PipeFilterAsync(out ChannelReader<int> unmatched, 10, 100, static async e =>
500500
{
501501
await Task.Yield();
502502
return e % 2 == 0;
503503
})
504504
.ToListAsync(each);
505505

506-
var oddFilter = unmatched
506+
ValueTask<List<int>> oddFilter = unmatched
507507
.ToListAsync(each);
508508

509-
var even = await evenFilter;
510-
var odd = await oddFilter;
509+
List<int> even = await evenFilter;
510+
List<int> odd = await oddFilter;
511511

512512
even.Count.Should().Be(each);
513513
odd.Count.Should().Be(each);
@@ -539,7 +539,7 @@ static ValueTask<int> AsyncReceiver()
539539
await Assert.ThrowsAsync<OperationCanceledException>(
540540
async () =>
541541
{
542-
var reader = Enumerable.Repeat(0, 10)
542+
ChannelReader<int> reader = Enumerable.Repeat(0, 10)
543543
.ToChannel()
544544
.PipeAsync(1, async _ => await AsyncReceiver());
545545

0 commit comments

Comments
 (0)