Skip to content

Commit 6850cc8

Browse files
authored
Enable CA1844 (#39634)
Contributes to #24055 Fixes #39555
1 parent 15635f7 commit 6850cc8

File tree

26 files changed

+149
-15
lines changed

26 files changed

+149
-15
lines changed

.editorconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,9 @@ dotnet_diagnostic.CA1842.severity = warning
166166
# CA1843: Do not use 'WaitAll' with a single task
167167
dotnet_diagnostic.CA1843.severity = warning
168168

169+
# CA1844: Provide memory-based overrides of async methods when subclassing 'Stream'
170+
dotnet_diagnostic.CA1844.severity = warning
171+
169172
# CA1845: Use span-based 'string.Concat'
170173
dotnet_diagnostic.CA1845.severity = warning
171174

src/Components/Shared/src/ArrayBuilder.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,13 @@ public int Append(in T item)
7676
}
7777

7878
internal int Append(T[] source, int startIndex, int length)
79+
=> Append(source.AsSpan(startIndex, length));
80+
81+
internal int Append(ReadOnlySpan<T> source)
7982
{
8083
// Expand storage if needed. Using same doubling approach as would
8184
// be used if you inserted the items one-by-one.
82-
var requiredCapacity = _itemsInUse + length;
85+
var requiredCapacity = _itemsInUse + source.Length;
8386
if (_items.Length < requiredCapacity)
8487
{
8588
var candidateCapacity = Math.Max(_items.Length * 2, _minCapacity);
@@ -91,9 +94,9 @@ internal int Append(T[] source, int startIndex, int length)
9194
GrowBuffer(candidateCapacity);
9295
}
9396

94-
Array.Copy(source, startIndex, _items, _itemsInUse, length);
97+
source.CopyTo(_items.AsSpan(_itemsInUse));
9598
var startIndexOfAppendedItems = _itemsInUse;
96-
_itemsInUse += length;
99+
_itemsInUse += source.Length;
97100
return startIndexOfAppendedItems;
98101
}
99102

src/Components/Shared/src/ArrayBuilderMemoryStream.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ public override int Read(byte[] buffer, int offset, int count)
5151
public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
5252
=> throw new NotSupportedException();
5353

54+
public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken)
55+
=> throw new NotSupportedException();
56+
5457
/// <inheritdoc />
5558
public override void Write(byte[] buffer, int offset, int count)
5659
{
@@ -59,6 +62,17 @@ public override void Write(byte[] buffer, int offset, int count)
5962
ArrayBuilder.Append(buffer, offset, count);
6063
}
6164

65+
public override void Write(ReadOnlySpan<byte> buffer)
66+
{
67+
ArrayBuilder.Append(buffer);
68+
}
69+
70+
public override ValueTask WriteAsync(ReadOnlyMemory<byte> memory, CancellationToken cancellationToken)
71+
{
72+
ArrayBuilder.Append(memory.Span);
73+
return default;
74+
}
75+
6276
/// <inheritdoc />
6377
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
6478
{

src/Hosting/TestHost/src/ResponseBodyReaderStream.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,24 @@ public override long Position
5757

5858
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) => throw new NotSupportedException();
5959

60+
public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken) => throw new NotSupportedException();
61+
6062
#endregion NotSupported
6163

6264
public override int Read(byte[] buffer, int offset, int count)
6365
{
6466
return ReadAsync(buffer, offset, count, CancellationToken.None).GetAwaiter().GetResult();
6567
}
6668

67-
public override async Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
69+
public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
6870
{
6971
VerifyBuffer(buffer, offset, count);
72+
73+
return ReadAsync(buffer.AsMemory(offset, count), cancellationToken).AsTask();
74+
}
75+
76+
public override async ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default)
77+
{
7078
CheckAborted();
7179

7280
if (_readerComplete)
@@ -90,9 +98,9 @@ public override async Task<int> ReadAsync(byte[] buffer, int offset, int count,
9098
}
9199

92100
var readableBuffer = result.Buffer;
93-
var actual = Math.Min(readableBuffer.Length, count);
101+
var actual = Math.Min(readableBuffer.Length, buffer.Length);
94102
readableBuffer = readableBuffer.Slice(0, actual);
95-
readableBuffer.CopyTo(new Span<byte>(buffer, offset, count));
103+
readableBuffer.CopyTo(buffer.Span);
96104
_pipe.Reader.AdvanceTo(readableBuffer.End);
97105
return (int)actual;
98106
}

src/Hosting/TestHost/src/ResponseBodyWriterStream.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,9 @@ public override async Task WriteAsync(byte[] buffer, int offset, int count, Canc
6969
{
7070
await _responseWriter.WriteAsync(new ReadOnlyMemory<byte>(buffer, offset, count), cancellationToken);
7171
}
72+
73+
public override async ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken)
74+
{
75+
await _responseWriter.WriteAsync(buffer, cancellationToken);
76+
}
7277
}

src/Http/Http/src/Internal/ReferenceReadStream.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ public override Task WriteAsync(byte[] buffer, int offset, int count, Cancellati
123123
throw new NotSupportedException();
124124
}
125125

126+
public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken)
127+
=> throw new NotSupportedException();
128+
126129
public override void SetLength(long value)
127130
{
128131
throw new NotSupportedException();

src/Http/WebUtilities/src/BufferedReadStream.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,12 @@ public override void Write(byte[] buffer, int offset, int count)
189189
_inner.Write(buffer, offset, count);
190190
}
191191

192+
/// <inheritdoc/>
193+
public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken)
194+
{
195+
return _inner.WriteAsync(buffer, cancellationToken);
196+
}
197+
192198
/// <inheritdoc/>
193199
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
194200
{

src/Http/WebUtilities/src/FileBufferingReadStream.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,12 @@ public override void Write(byte[] buffer, int offset, int count)
402402
throw new NotSupportedException();
403403
}
404404

405+
/// <inheritdoc/>
406+
public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken)
407+
{
408+
throw new NotSupportedException();
409+
}
410+
405411
/// <inheritdoc/>
406412
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
407413
{

src/Http/WebUtilities/src/FileBufferingWriteStream.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ public override int Read(byte[] buffer, int offset, int count)
101101
public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
102102
=> throw new NotSupportedException();
103103

104+
/// <inheritdoc/>
105+
public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken)
106+
=> throw new NotSupportedException();
107+
104108
/// <inheritdoc />
105109
public override void Write(byte[] buffer, int offset, int count)
106110
{

src/Http/WebUtilities/src/MultipartReaderStream.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,11 @@ public override void Write(byte[] buffer, int offset, int count)
123123
throw new NotSupportedException();
124124
}
125125

126+
public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default)
127+
{
128+
throw new NotSupportedException();
129+
}
130+
126131
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
127132
{
128133
throw new NotSupportedException();
@@ -207,7 +212,10 @@ public override int Read(byte[] buffer, int offset, int count)
207212
return UpdatePosition(read);
208213
}
209214

210-
public override async Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
215+
public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
216+
=> ReadAsync(buffer.AsMemory(offset, count), cancellationToken).AsTask();
217+
218+
public override async ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken)
211219
{
212220
if (_finished)
213221
{
@@ -231,7 +239,9 @@ public override async Task<int> ReadAsync(byte[] buffer, int offset, int count,
231239
if (matchOffset > bufferedData.Offset)
232240
{
233241
// Sync, it's already buffered
234-
read = _innerStream.Read(buffer, offset, Math.Min(count, matchOffset - bufferedData.Offset));
242+
var slice = buffer[..Math.Min(buffer.Length, matchOffset - bufferedData.Offset)];
243+
244+
read = _innerStream.Read(slice.Span);
235245
return UpdatePosition(read);
236246
}
237247

@@ -259,7 +269,7 @@ public override async Task<int> ReadAsync(byte[] buffer, int offset, int count,
259269
}
260270

261271
// No possible boundary match within the buffered data, return the data from the buffer.
262-
read = _innerStream.Read(buffer, offset, Math.Min(count, bufferedData.Count));
272+
read = _innerStream.Read(buffer.Span[..Math.Min(buffer.Length, bufferedData.Count)]);
263273
return UpdatePosition(read);
264274
}
265275

0 commit comments

Comments
 (0)