Skip to content

Commit 82c9b3a

Browse files
author
Oren (electricessence)
committed
Inspection improvements.
1 parent 4e70449 commit 82c9b3a

File tree

7 files changed

+49
-12
lines changed

7 files changed

+49
-12
lines changed

.editorconfig

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[*.cs]
2+
3+
# CA1707: Identifiers should not contain underscores
4+
dotnet_diagnostic.CA1707.severity = silent
5+
6+
# CA1303: Do not pass literals as localized parameters
7+
dotnet_diagnostic.CA1303.severity = silent
8+
9+
# CA1051: Do not declare visible instance fields
10+
dotnet_diagnostic.CA1051.severity = silent
11+
12+
# CA1063: Implement IDisposable Correctly
13+
dotnet_diagnostic.CA1063.severity = silent
14+
15+
# CA1816: Dispose methods should call SuppressFinalize
16+
dotnet_diagnostic.CA1816.severity = silent
17+
18+
# CA2008: Do not create tasks without passing a TaskScheduler
19+
dotnet_diagnostic.CA2008.severity = silent

ActionRunner.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public class ActionRunner : ICancellable
99
public ActionRunner(Action action, TaskScheduler? scheduler = default)
1010
{
1111
_action = action;
12-
_scheduler = scheduler; // No need to hold a refernce to the default, just keep it null.
12+
Scheduler = scheduler; // No need to hold a refernce to the default, just keep it null.
1313
LastStart = DateTime.MaxValue;
1414
LastComplete = DateTime.MaxValue;
1515
}
@@ -22,7 +22,7 @@ public static ActionRunner Create<T>(Func<T> action, TaskScheduler? scheduler =
2222

2323
Action? _action;
2424
// ReSharper disable once NotAccessedField.Global
25-
protected TaskScheduler? _scheduler;
25+
protected TaskScheduler? Scheduler { get; private set; }
2626

2727
protected int _count;
2828
public int Count => _count;
@@ -60,7 +60,7 @@ public void Dispose()
6060
{
6161
Cancel();
6262
_action = null;
63-
_scheduler = null;
63+
Scheduler = null;
6464
}
6565

6666
Action GetAction()
@@ -96,7 +96,9 @@ CancellableTask Prepare()
9696
}
9797
Interlocked.CompareExchange(ref _task, null, task);
9898
},
99-
TaskContinuationOptions.ExecuteSynchronously);
99+
CancellationToken.None,
100+
TaskContinuationOptions.ExecuteSynchronously,
101+
Scheduler);
100102
return task;
101103
}
102104

CancellableTask.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ namespace Open.Threading.Tasks
99
/// </summary>
1010
public class CancellableTask : Task, ICancellable
1111
{
12-
protected CancellationTokenSource? TokenSource;
12+
#pragma warning disable CA2213 // Is disposed in Cancel method.
13+
CancellationTokenSource? TokenSource;
14+
#pragma warning restore CA2213
1315

1416
public bool Cancel(bool onlyIfNotRunning)
1517
{

Open.Threading.Tasks.csproj

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,19 @@ Part of the "Open" set of libraries.</Description>
2727
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
2828
</ItemGroup>
2929

30+
<ItemGroup>
31+
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="3.0.0">
32+
<PrivateAssets>all</PrivateAssets>
33+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
34+
</PackageReference>
35+
</ItemGroup>
36+
3037
<ItemGroup>
38+
<None Remove=".editorconfig" />
3139
<None Remove=".git" />
3240
<None Remove=".gitignore" />
3341
<None Remove="LICENSE" />
34-
<None Remove="README.md" />
42+
<AdditionalFiles Include=".editorconfig" />
3543
<None Include="logo.png">
3644
<Pack>True</Pack>
3745
<PackagePath></PackagePath>

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
A set of utilities and extensions for working with Tasks.
44

5-
[![NuGet](http://img.shields.io/nuget/v/Open.Threading.Tasks.svg)](https://www.nuget.org/packages/Open.Threading.Tasks/)
5+
[![NuGet](https://img.shields.io/nuget/v/Open.Threading.Tasks.svg)](https://www.nuget.org/packages/Open.Threading.Tasks/)

TaskExtensions.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ public static class TaskExtensions
1010
/// </summary>
1111
public static bool IsActive(this Task target)
1212
{
13-
if (target is null)
14-
throw new NullReferenceException();
13+
if (target is null) throw new ArgumentNullException(nameof(target));
1514

1615
switch (target.Status)
1716
{
@@ -38,8 +37,7 @@ public static bool IsActive(this Task target)
3837
/// <returns>True if start attempt was successful.</returns>
3938
public static bool EnsureStarted(this Task target, TaskScheduler? scheduler = default)
4039
{
41-
if (target is null) throw new NullReferenceException();
42-
40+
if (target is null) throw new ArgumentNullException(nameof(target));
4341
if (target.Status != TaskStatus.Created) return false;
4442
try
4543
{
@@ -70,6 +68,7 @@ public static bool EnsureStarted(this Task target, TaskScheduler? scheduler = de
7068
public static TTask OnFullfilled<TTask>(this TTask target, Action action)
7169
where TTask : Task
7270
{
71+
if (target is null) throw new ArgumentNullException(nameof(target));
7372
target.ContinueWith(task =>
7473
{
7574
if (task.Status == TaskStatus.RanToCompletion) action();
@@ -87,6 +86,7 @@ public static TTask OnFullfilled<TTask>(this TTask target, Action action)
8786
/// <returns>The target object. Allows for method chaining.</returns>
8887
public static Task<T> OnFullfilled<T>(this Task<T> target, Action<T> action)
8988
{
89+
if (target is null) throw new ArgumentNullException(nameof(target));
9090
target.ContinueWith(task =>
9191
{
9292
if (task.Status == TaskStatus.RanToCompletion) action(task.Result);
@@ -106,6 +106,7 @@ public static Task<T> OnFullfilled<T>(this Task<T> target, Action<T> action)
106106
public static TTask OnFullfilled<TTask, T>(this TTask target, Func<T> action)
107107
where TTask : Task
108108
{
109+
if (target is null) throw new ArgumentNullException(nameof(target));
109110
target.ContinueWith(task =>
110111
{
111112
if (task.Status == TaskStatus.RanToCompletion) action();
@@ -124,6 +125,7 @@ public static TTask OnFullfilled<TTask, T>(this TTask target, Func<T> action)
124125
public static TTask OnFaulted<TTask>(this TTask target, Action<Exception> action)
125126
where TTask : Task
126127
{
128+
if (target is null) throw new ArgumentNullException(nameof(target));
127129
target.ContinueWith(task =>
128130
{
129131
if (task.IsFaulted) action(task.Exception);
@@ -143,6 +145,7 @@ public static TTask OnFaulted<TTask>(this TTask target, Action<Exception> action
143145
public static TTask OnCancelled<TTask>(this TTask target, Action action)
144146
where TTask : Task
145147
{
148+
if (target is null) throw new ArgumentNullException(nameof(target));
146149
target.ContinueWith(task =>
147150
{
148151
if (!task.IsCanceled) action();
@@ -162,6 +165,7 @@ public static TTask OnCancelled<TTask>(this TTask target, Action action)
162165
public static TTask OnCancelled<TTask, T>(this TTask target, Func<T> action)
163166
where TTask : Task
164167
{
168+
if (target is null) throw new ArgumentNullException(nameof(target));
165169
target.ContinueWith(task =>
166170
{
167171
if (!task.IsCanceled) action();

TimeoutHandler.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44

55
namespace Open.Threading.Tasks
66
{
7-
public class TimeoutHandler : IDisposable
7+
public sealed class TimeoutHandler : IDisposable
88
{
9+
#pragma warning disable CA2213 // Is disposed properly.
910
CancellationTokenSource? TokenSource;
11+
#pragma warning restore CA2213
1012
TimeoutHandler(TimeSpan delay, Action<TimeSpan> onTimeout)
1113
{
1214
TokenSource = new CancellationTokenSource();

0 commit comments

Comments
 (0)