Skip to content

Commit

Permalink
Feat: change Throw to ThrowIf
Browse files Browse the repository at this point in the history
  • Loading branch information
Bibletoon committed Sep 29, 2021
1 parent a260ea6 commit 9c5a7ff
Show file tree
Hide file tree
Showing 67 changed files with 511 additions and 1,006 deletions.
5 changes: 1 addition & 4 deletions src/libraries/Common/tests/System/IO/PositionValueStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ public PositionValueStream(int totalCount)

public override int Read(byte[] buffer, int offset, int count)
{
if (_remaining < 0)
{
ObjectDisposedException.Throw(this);
}
ObjectDisposedException.ThrowIf(_remaining < 0, this);

if (_remaining == 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ protected override void Dispose(bool disposing)
public async override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
ValidateBufferArguments(buffer, offset, count);
ThrowIfDisposed();
ObjectDisposedException.ThrowIf(_disposed != 0, this);
if (!CanRead)
{
throw new InvalidOperationException("The stream does not support reading.");
Expand All @@ -197,7 +197,7 @@ public async override Task<int> ReadAsync(byte[] buffer, int offset, int count,
public async override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken)
{
bool canRead = CanRead; // Prevent race with Dispose.
ThrowIfDisposed();
ObjectDisposedException.ThrowIf(_disposed != 0, this);
if (!canRead)
{
throw new InvalidOperationException("The stream does not support reading.");
Expand All @@ -218,7 +218,7 @@ public async override ValueTask<int> ReadAsync(Memory<byte> buffer, Cancellation
public async override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
ValidateBufferArguments(buffer, offset, count);
ThrowIfDisposed();
ObjectDisposedException.ThrowIf(_disposed != 0, this);
if (!CanWrite)
{
throw new InvalidOperationException("The stream does not support writing.");
Expand All @@ -237,7 +237,7 @@ public async override Task WriteAsync(byte[] buffer, int offset, int count, Canc
public async override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken)
{
bool canWrite = CanWrite; // Prevent race with Dispose.
ThrowIfDisposed();
ObjectDisposedException.ThrowIf(_disposed != 0, this);
if (!canWrite)
{
throw new InvalidOperationException("The stream does not support writing.");
Expand Down Expand Up @@ -267,14 +267,6 @@ public override void SetLength(long value)
throw new NotSupportedException("This stream does not support seek operations.");
}

private void ThrowIfDisposed()
{
if (_disposed != 0)
{
ObjectDisposedException.Throw(this);
}
}

private static IOException WrapException(string resourceFormatString, Exception innerException)
{
return new IOException(resourceFormatString, innerException);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public MemoryCache(IOptions<MemoryCacheOptions> optionsAccessor, ILoggerFactory
/// <inheritdoc />
public ICacheEntry CreateEntry(object key)
{
CheckDisposed();
ObjectDisposedException.ThrowIf(_disposed, this);
ValidateCacheKey(key);

return new CacheEntry(key, this);
Expand Down Expand Up @@ -222,7 +222,7 @@ internal void SetEntry(CacheEntry entry)
public bool TryGetValue(object key, out object result)
{
ValidateCacheKey(key);
CheckDisposed();
ObjectDisposedException.ThrowIf(_disposed, this);

DateTimeOffset utcNow = _options.Clock.UtcNow;

Expand Down Expand Up @@ -264,7 +264,7 @@ public void Remove(object key)
{
ValidateCacheKey(key);

CheckDisposed();
ObjectDisposedException.ThrowIf(_disposed, this);
if (_entries.TryRemove(key, out CacheEntry entry))
{
if (_options.SizeLimit.HasValue)
Expand Down Expand Up @@ -494,16 +494,6 @@ protected virtual void Dispose(bool disposing)
}
}

private void CheckDisposed()
{
if (_disposed)
{
Throw();
}

static void Throw() => ObjectDisposedException.Throw(typeof(MemoryCache));
}

private static void ValidateCacheKey(object key)
{
if (key == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,13 @@ public ExportProvider? SourceProvider
{
get
{
ThrowIfDisposed();
ObjectDisposedException.ThrowIf(_isDisposed, this);

return _sourceProvider;
}
set
{
ThrowIfDisposed();
ObjectDisposedException.ThrowIf(_isDisposed, this);

Requires.NotNull(value!, nameof(value));
using (_lock.LockStateForWrite())
Expand Down Expand Up @@ -202,7 +202,8 @@ private ImportEngine ImportEngine
/// </remarks>
protected override IEnumerable<Export>? GetExportsCore(ImportDefinition definition, AtomicComposition? atomicComposition)
{
ThrowIfDisposed();
ObjectDisposedException.ThrowIf(_isDisposed, this);

EnsureRunning();

// Determine whether there is a composition atomicComposition-specific list of parts to use,
Expand Down Expand Up @@ -236,7 +237,7 @@ private ImportEngine ImportEngine

public void Compose(CompositionBatch batch)
{
ThrowIfDisposed();
ObjectDisposedException.ThrowIf(_isDisposed, this);
EnsureRunning();

Requires.NotNull(batch, nameof(batch));
Expand Down Expand Up @@ -399,21 +400,12 @@ private Export CreateExport(ComposablePart part, ExportDefinition export)

private object? GetExportedValue(ComposablePart part, ExportDefinition export)
{
ThrowIfDisposed();
ObjectDisposedException.ThrowIf(_isDisposed, this);
EnsureRunning();

return CompositionServices.GetExportedValueFromComposedPart(ImportEngine, part, export);
}

[DebuggerStepThrough]
private void ThrowIfDisposed()
{
if (_isDisposed)
{
ObjectDisposedException.Throw(this);
}
}

[DebuggerStepThrough]
private void EnsureCanRun()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -392,14 +392,12 @@ public int Value
{
get
{
if (this.IsDisposed)
ObjectDisposedException.Throw(this);
ObjectDisposedException.ThrowIf(this.IsDisposed, this);
return this._value;
}
set
{
if (this.IsDisposed)
ObjectDisposedException.Throw(this);
ObjectDisposedException.ThrowIf(this.IsDisposed, this);
this._value = value;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,7 @@ public bool Enabled
_enabled = value;
if (_timer == null)
{
if (_disposed)
{
ObjectDisposedException.Throw(this);
}
ObjectDisposedException.ThrowIf(_disposed, this);

int i = (int)Math.Ceiling(_interval);
_cookie = new object();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public void Dispose()
// Stacks should never be popped out of order.
// We want to trap this condition in production.
Debug.Fail("Scope was popped out of order.");
ObjectDisposedException.Throw(this);
throw new ObjectDisposedException(GetType().FullName);
}

m_deserializationToken.Dispose(); // it's a readonly struct, but Dispose still works properly
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ private CNativeBuffer Buffer
if (null == value)
{
Debug.Fail("object is disposed");
ObjectDisposedException.Throw(this);
throw new ObjectDisposedException(GetType().Name);
}
return value;
}
Expand Down
4 changes: 2 additions & 2 deletions src/libraries/System.Data.OleDb/src/OleDbDataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ private UnsafeNativeMethods.IRowset IRowset()
if (null == irowset)
{
Debug.Assert(false, "object is disposed");
ObjectDisposedException.Throw(this);
throw new ObjectDisposedException(GetType().Name);
}
return irowset;
}
Expand All @@ -248,7 +248,7 @@ private UnsafeNativeMethods.IRow IRow()
if (null == irow)
{
Debug.Assert(false, "object is disposed");
ObjectDisposedException.Throw(this);
throw new ObjectDisposedException(GetType().Name);
}
return irow;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1046,8 +1046,7 @@ public void ModifyOverflowPolicy(OverflowAction action, int retentionDays)

private void OpenForRead(string currentMachineName)
{
if (this.boolFlags[Flag_disposed])
ObjectDisposedException.Throw(this);
ObjectDisposedException.ThrowIf(this.boolFlags[Flag_disposed], this);

string logname = GetLogName(currentMachineName);

Expand Down Expand Up @@ -1082,8 +1081,7 @@ private void OpenForRead(string currentMachineName)
private void OpenForWrite(string currentMachineName)
{
//Cannot allocate the writeHandle if the object has been disposed, since finalization has been suppressed.
if (this.boolFlags[Flag_disposed])
ObjectDisposedException.Throw(this);
ObjectDisposedException.ThrowIf(this.boolFlags[Flag_disposed], this);

if (sourceName == null || sourceName.Length == 0)
throw new ArgumentException(SR.NeedSourceToOpen);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ public StreamWriter StandardInput
{
get
{
CheckDisposed();
ObjectDisposedException.ThrowIf(_disposed, this);
if (_standardInput == null)
{
throw new InvalidOperationException(SR.CantGetStandardIn);
Expand All @@ -707,7 +707,7 @@ public StreamReader StandardOutput
{
get
{
CheckDisposed();
ObjectDisposedException.ThrowIf(_disposed, this);
if (_standardOutput == null)
{
throw new InvalidOperationException(SR.CantGetStandardOut);
Expand All @@ -733,7 +733,7 @@ public StreamReader StandardError
{
get
{
CheckDisposed();
ObjectDisposedException.ThrowIf(_disposed, this);
if (_standardError == null)
{
throw new InvalidOperationException(SR.CantGetStandardError);
Expand Down Expand Up @@ -1159,7 +1159,7 @@ private SafeProcessHandle GetOrOpenProcessHandle()
if (!_haveProcessHandle)
{
//Cannot open a new process handle if the object has been disposed, since finalization has been suppressed.
CheckDisposed();
ObjectDisposedException.ThrowIf(_disposed, this);

SetProcessHandle(GetProcessHandle());
}
Expand Down Expand Up @@ -1232,7 +1232,7 @@ public bool Start()
}

//Cannot start a new process and store its handle if the object has been disposed, since finalization has been suppressed.
CheckDisposed();
ObjectDisposedException.ThrowIf(_disposed, this);

SerializationGuard.ThrowIfDeserializationInProgress("AllowProcessCreation", ref s_cachedSerializationSwitch);

Expand Down Expand Up @@ -1615,7 +1615,7 @@ public void BeginErrorReadLine()
/// </devdoc>
public void CancelOutputRead()
{
CheckDisposed();
ObjectDisposedException.ThrowIf(_disposed, this);
if (_output != null)
{
_output.CancelOperation();
Expand All @@ -1636,7 +1636,7 @@ public void CancelOutputRead()
/// </devdoc>
public void CancelErrorRead()
{
CheckDisposed();
ObjectDisposedException.ThrowIf(_disposed, this);
if (_error != null)
{
_error.CancelOperation();
Expand Down Expand Up @@ -1687,16 +1687,6 @@ internal void ErrorReadNotifyUser(string? data)
}
}

/// <summary>Throws a System.ObjectDisposedException if the Proces was disposed</summary>
/// <exception cref="System.ObjectDisposedException">If the Proces has been disposed.</exception>
private void CheckDisposed()
{
if (_disposed)
{
ObjectDisposedException.Throw(this);
}
}

private static Win32Exception CreateExceptionForErrorStartingProcess(string errorMessage, int errorCode, string fileName, string? workingDirectory)
{
string directoryForException = string.IsNullOrEmpty(workingDirectory) ? Directory.GetCurrentDirectory() : workingDirectory;
Expand Down
Loading

0 comments on commit 9c5a7ff

Please sign in to comment.