Skip to content

Commit

Permalink
remove some redundant casts (#630)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp authored Oct 13, 2023
1 parent 497cce1 commit b72083c
Show file tree
Hide file tree
Showing 16 changed files with 38 additions and 42 deletions.
4 changes: 2 additions & 2 deletions src/Audit.DynamicProxy/AuditEventExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public static InterceptEvent GetAuditInterceptEvent(this AuditScope auditScope)
/// <param name="auditEvent">The audit event.</param>
public static InterceptEvent GetAuditInterceptEvent(this AuditEvent auditEvent)
{
if (auditEvent is AuditEventIntercept)
if (auditEvent is AuditEventIntercept intercept)
{
return (auditEvent as AuditEventIntercept).InterceptEvent;
return intercept.InterceptEvent;
}
// For backwards compatibility
return auditEvent.CustomFields.ContainsKey("InterceptEvent")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ public IEntityFrameworkProviderConfiguratorExtra AuditEntityAction<T>(Action<Aud
{
_auditEntityAction = (ev, ent, obj) =>
{
if (obj is T)
if (obj is T t)
{
action.Invoke(ev, ent, (T)obj);
action.Invoke(ev, ent, t);
}
return Task.FromResult(true);
};
Expand All @@ -134,9 +134,9 @@ public IEntityFrameworkProviderConfiguratorExtra AuditEntityAction<T>(Func<Audit
{
_auditEntityAction = async (ev, ent, obj) =>
{
if (obj is T)
if (obj is T t)
{
await asyncAction.Invoke(ev, ent, (T)obj);
await asyncAction.Invoke(ev, ent, t);
}
return true;
};
Expand All @@ -147,9 +147,9 @@ public IEntityFrameworkProviderConfiguratorExtra AuditEntityAction<T>(Func<Audit
{
_auditEntityAction = (ev, ent, obj) =>
{
if (obj is T)
if (obj is T t)
{
return Task.FromResult(function.Invoke(ev, ent, (T)obj));
return Task.FromResult(function.Invoke(ev, ent, t));
}
return Task.FromResult(true);
};
Expand All @@ -160,9 +160,9 @@ public IEntityFrameworkProviderConfiguratorExtra AuditEntityAction<T>(Func<Audit
{
_auditEntityAction = async (ev, ent, obj) =>
{
if (obj is T)
if (obj is T t)
{
return await asyncFunction.Invoke(ev, ent, (T)obj);
return await asyncFunction.Invoke(ev, ent, t);
}
return true;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,9 @@ public override object InsertEvent(AuditEvent auditEvent)
}
if (save)
{
if (auditDbContext is IAuditBypass)
if (auditDbContext is IAuditBypass bypass)
{
(auditDbContext as IAuditBypass).SaveChangesBypassAudit();
bypass.SaveChangesBypassAudit();
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion src/Audit.FileSystem/FileSystemMonitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ private void ProcessEvent(FileSystemEventArgs e, FileSystemEventType type)
Extension = System.IO.Path.GetExtension(e.FullPath),
FullPath = e.FullPath,
Event = type,
OldName = (e is RenamedEventArgs) ? System.IO.Path.GetFileName((e as RenamedEventArgs).OldFullPath) : null
OldName = (e is RenamedEventArgs args) ? System.IO.Path.GetFileName(args.OldFullPath) : null
};
var fsAuditEvent = new AuditEventFileSystem()
{
Expand Down
4 changes: 2 additions & 2 deletions src/Audit.HttpClient/AuditEventExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ public static HttpAction GetHttpAction(this AuditScope auditScope)
/// <param name="auditEvent">The audit event.</param>
public static HttpAction GetHttpAction(this AuditEvent auditEvent)
{
if (auditEvent is AuditEventHttpClient)
if (auditEvent is AuditEventHttpClient client)
{
return (auditEvent as AuditEventHttpClient).Action;
return client.Action;
}
// For backwards compatibility
return auditEvent.CustomFields.ContainsKey("Action")
Expand Down
4 changes: 2 additions & 2 deletions src/Audit.Mvc/AuditEventExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public static AuditAction GetMvcAuditAction(this AuditScope auditScope)
/// <param name="auditEvent">The audit event.</param>
public static AuditAction GetMvcAuditAction(this AuditEvent auditEvent)
{
if (auditEvent is AuditEventMvcAction)
if (auditEvent is AuditEventMvcAction action)
{
return (auditEvent as AuditEventMvcAction).Action;
return action.Action;
}
// For backwards compatibility
return auditEvent.CustomFields.ContainsKey("Action")
Expand Down
4 changes: 2 additions & 2 deletions src/Audit.Mvc/AuditPageFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ public virtual async Task AfterExecutedAsync(PageHandlerExecutedContext context)
auditAction.RedirectLocation = httpContext.Response.Headers?["Location"];
auditAction.ResponseStatusCode = context.Result == null && context.Exception != null && !context.ExceptionHandled
? 500
: context.Result is StatusCodeResult
? (context.Result as StatusCodeResult).StatusCode
: context.Result is StatusCodeResult result
? result.StatusCode
: httpContext.Response.StatusCode;

var bodyType = context.Result?.GetType().GetFullTypeName();
Expand Down
4 changes: 2 additions & 2 deletions src/Audit.NET.JsonNewtonsoftAdapter/JsonNewtonsoftAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ public T ToObject<T>(object value)
{
return (T)value;
}
if (value is JContainer)
if (value is JContainer container)
{
return (value as JContainer).ToObject<T>();
return container.ToObject<T>();
}
return default(T);
}
Expand Down
3 changes: 1 addition & 2 deletions src/Audit.NET.JsonSystemAdapter/JsonSystemAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,8 @@ public T ToObject<T>(object value)
return (T)value;
}
// TODO: Workaround to convert from JsonElement to Object, until https://github.com/dotnet/runtime/issues/31274 fixed
if (value is JsonElement)
if (value is JsonElement element)
{
var element = (JsonElement)value;
using (var bufferWriter = new MemoryStream())
{
using (var writer = new Utf8JsonWriter(bufferWriter))
Expand Down
7 changes: 3 additions & 4 deletions src/Audit.NET/JsonAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,14 @@ public T ToObject<T>(object value)
return (T)value;
}
#if IS_NK_JSON
if (value is JContainer)
if (value is JContainer container)
{
return (value as JContainer).ToObject<T>(JsonSerializer.Create(Configuration.JsonSettings));
return container.ToObject<T>(JsonSerializer.Create(Configuration.JsonSettings));
}
#else
// TODO: Workaround to convert from JsonElement to Object, until https://github.com/dotnet/runtime/issues/31274 fixed
if (value is JsonElement)
if (value is JsonElement element)
{
var element = (JsonElement)value;
var bufferWriter = new ArrayBufferWriter<byte>();
using (var writer = new Utf8JsonWriter(bufferWriter))
{
Expand Down
4 changes: 2 additions & 2 deletions src/Audit.WCF.Client/AuditEventExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public static WcfClientAction GetWcfClientAction(this AuditScope auditScope)
/// <param name="auditEvent">The audit event.</param>
public static WcfClientAction GetWcfClientAction(this AuditEvent auditEvent)
{
if (auditEvent is AuditEventWcfClient)
if (auditEvent is AuditEventWcfClient client)
{
return (auditEvent as AuditEventWcfClient).WcfClientEvent;
return client.WcfClientEvent;
}
return auditEvent.CustomFields.ContainsKey("WcfClientEvent")
? Configuration.JsonAdapter.ToObject<WcfClientAction>(auditEvent.CustomFields["WcfClientEvent"])
Expand Down
4 changes: 2 additions & 2 deletions src/Audit.WCF/AuditEventExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public static WcfEvent GetWcfAuditAction(this AuditScope auditScope)
/// <param name="auditEvent">The audit event.</param>
public static WcfEvent GetWcfAuditAction(this AuditEvent auditEvent)
{
if (auditEvent is AuditEventWcfAction)
if (auditEvent is AuditEventWcfAction action)
{
return (auditEvent as AuditEventWcfAction).WcfEvent;
return action.WcfEvent;
}
// For backwards compatibility
return auditEvent.CustomFields.ContainsKey("WcfEvent")
Expand Down
3 changes: 1 addition & 2 deletions src/Audit.WCF/AuditOperationInvoker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,9 @@ private AuditWcfEventFault GetWcfFaultData(Exception ex)
{
var result = new AuditWcfEventFault();
result.Exception = ex.GetExceptionInfo();
if (ex is FaultException)
if (ex is FaultException fault)
{
result.FaultType = "Fault";
var fault = ex as FaultException;
if (fault.GetType().GetProperty("Detail") != null)
{
var detail = fault.GetType().GetProperty("Detail").GetGetMethod().Invoke(fault, null);
Expand Down
9 changes: 5 additions & 4 deletions src/Audit.WebApi/AuditApiAttribute.Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,11 @@ internal bool ShouldIncludeResponseBody(HttpStatusCode statusCode)

private HttpStatusCode GetStatusCode(ActionExecutedContext context)
{
var statusCode = context.Result is ObjectResult && (context.Result as ObjectResult).StatusCode.HasValue
? (context.Result as ObjectResult).StatusCode.Value
: context.Result is StatusCodeResult
? (context.Result as StatusCodeResult).StatusCode
var result = context.Result;
var statusCode = result is ObjectResult objectResult && objectResult.StatusCode.HasValue
? objectResult.StatusCode.Value
: result is StatusCodeResult statusCodeResult
? statusCodeResult.StatusCode
: context.HttpContext.Response.StatusCode;
return (HttpStatusCode)statusCode;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Audit.WebApi/AuditEventExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public static AuditApiAction GetWebApiAuditAction(this AuditScope auditScope)
/// <param name="auditEvent">The audit event.</param>
public static AuditApiAction GetWebApiAuditAction(this AuditEvent auditEvent)
{
if (auditEvent is AuditEventWebApi)
if (auditEvent is AuditEventWebApi api)
{
return (auditEvent as AuditEventWebApi).Action;
return api.Action;
}
// For backwards compatibility
return auditEvent.CustomFields.ContainsKey("Action")
Expand Down
4 changes: 1 addition & 3 deletions test/Audit.EntityFramework.UnitTest/InheritanceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,9 @@ public void Test_Ef_Inheritance()
{
DBEntityBase bse = context.Entities.First(x => x.ID == id);

if (!(bse is DBEntity))
if (!(bse is DBEntity ent))
throw new Exception("1");

DBEntity ent = (DBEntity) bse;

ent.Name = "Base 2";
ent.Name2 = "Inherited 2";
ent.Ticks = 456;
Expand Down

0 comments on commit b72083c

Please sign in to comment.