Skip to content

Commit

Permalink
Additional cleanup and modernization
Browse files Browse the repository at this point in the history
  • Loading branch information
roji committed Jun 15, 2023
1 parent ef30eb2 commit da6f0a6
Show file tree
Hide file tree
Showing 91 changed files with 289 additions and 492 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -201,17 +201,15 @@ private void ObservableCollectionChanged(object? sender, NotifyCollectionChanged
Clear();
}

if (e.Action == NotifyCollectionChangedAction.Remove
|| e.Action == NotifyCollectionChangedAction.Replace)
if (e.Action is NotifyCollectionChangedAction.Remove or NotifyCollectionChangedAction.Replace)
{
foreach (T entity in e.OldItems!)
{
Remove(entity);
}
}

if (e.Action == NotifyCollectionChangedAction.Add
|| e.Action == NotifyCollectionChangedAction.Replace)
if (e.Action is NotifyCollectionChangedAction.Add or NotifyCollectionChangedAction.Replace)
{
foreach (T entity in e.NewItems!)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public override FixAllProvider GetFixAllProvider()
public override async Task RegisterCodeFixesAsync(CodeFixContext context)
{
var document = context.Document;
var span = context.Span;
var cancellationToken = context.CancellationToken;

// We report only 1 diagnostic per span, so this is ok
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ public virtual void ProcessEntityTypeAnnotationChanged(
IConventionAnnotation? oldAnnotation,
IConventionContext<IConventionAnnotation> context)
{
if (name == CosmosAnnotationNames.PartitionKeyName
|| name == CosmosAnnotationNames.ContainerName)
if (name is CosmosAnnotationNames.PartitionKeyName or CosmosAnnotationNames.ContainerName)
{
foreach (var skipNavigation in entityTypeBuilder.Metadata.GetSkipNavigations())
{
Expand Down
4 changes: 2 additions & 2 deletions src/EFCore.Cosmos/Query/Internal/CosmosEqualsTranslator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ public CosmosEqualsTranslator(ISqlExpressionFactory sqlExpressionFactory)
&& right != null)
{
return left.Type.UnwrapNullableType() == right.Type.UnwrapNullableType()
|| (right.Type == typeof(object) && (right is SqlParameterExpression || right is SqlConstantExpression))
|| (left.Type == typeof(object) && (left is SqlParameterExpression || left is SqlConstantExpression))
|| (right.Type == typeof(object) && right is SqlParameterExpression or SqlConstantExpression)
|| (left.Type == typeof(object) && left is SqlParameterExpression or SqlConstantExpression)
? _sqlExpressionFactory.Equal(left, right)
: _sqlExpressionFactory.Constant(false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,7 @@ public override Expression Visit(Expression expression)
return null;
}

if (expression is NewExpression
|| expression is MemberInitExpression
|| expression is EntityShaperExpression)
if (expression is NewExpression or MemberInitExpression or EntityShaperExpression)
{
return base.Visit(expression);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,7 @@ protected override Expression VisitBinary(BinaryExpression binaryExpression)
var visitedLeft = Visit(left);
var visitedRight = Visit(right);

if ((binaryExpression.NodeType == ExpressionType.Equal
|| binaryExpression.NodeType == ExpressionType.NotEqual)
if (binaryExpression.NodeType is ExpressionType.Equal or ExpressionType.NotEqual
// Visited expression could be null, We need to pass MemberInitExpression
&& TryRewriteEntityEquality(
binaryExpression.NodeType, visitedLeft ?? left, visitedRight ?? right, equalsMethod: false, out var result))
Expand Down Expand Up @@ -279,8 +278,11 @@ bool IsGetTypeMethodCall(Expression expression, out EntityReferenceExpression en
static bool IsTypeConstant(Expression expression, out Type type)
{
type = null;
if (expression is not UnaryExpression { NodeType: ExpressionType.Convert or ExpressionType.ConvertChecked } unaryExpression
|| unaryExpression.Operand is not ConstantExpression constantExpression)
if (expression is not UnaryExpression
{
NodeType: ExpressionType.Convert or ExpressionType.ConvertChecked,
Operand: ConstantExpression constantExpression
})
{
return false;
}
Expand All @@ -291,9 +293,7 @@ static bool IsTypeConstant(Expression expression, out Type type)

static bool TryUnwrapConvertToObject(Expression expression, out Expression operand)
{
if (expression is UnaryExpression convertExpression
&& (convertExpression.NodeType == ExpressionType.Convert
|| convertExpression.NodeType == ExpressionType.ConvertChecked)
if (expression is UnaryExpression { NodeType: ExpressionType.Convert or ExpressionType.ConvertChecked } convertExpression
&& expression.Type == typeof(object))
{
operand = convertExpression.Operand;
Expand Down Expand Up @@ -615,8 +615,7 @@ Expression TranslateContains(Expression untranslatedItem, Expression untranslate
}

static Expression RemoveObjectConvert(Expression expression)
=> expression is UnaryExpression unaryExpression
&& (unaryExpression.NodeType == ExpressionType.Convert || unaryExpression.NodeType == ExpressionType.ConvertChecked)
=> expression is UnaryExpression { NodeType: ExpressionType.Convert or ExpressionType.ConvertChecked } unaryExpression
&& unaryExpression.Type == typeof(object)
? unaryExpression.Operand
: expression;
Expand Down Expand Up @@ -672,9 +671,7 @@ protected override Expression VisitUnary(UnaryExpression unaryExpression)
var operand = Visit(unaryExpression.Operand);

if (operand is EntityReferenceExpression entityReferenceExpression
&& (unaryExpression.NodeType == ExpressionType.Convert
|| unaryExpression.NodeType == ExpressionType.ConvertChecked
|| unaryExpression.NodeType == ExpressionType.TypeAs))
&& unaryExpression.NodeType is ExpressionType.Convert or ExpressionType.ConvertChecked or ExpressionType.TypeAs)
{
return entityReferenceExpression.Convert(unaryExpression.Type);
}
Expand Down Expand Up @@ -779,9 +776,7 @@ ObjectArrayProjectionExpression objectArrayProjectionExpression

private static Expression TryRemoveImplicitConvert(Expression expression)
{
if (expression is UnaryExpression unaryExpression
&& (unaryExpression.NodeType == ExpressionType.Convert
|| unaryExpression.NodeType == ExpressionType.ConvertChecked))
if (expression is UnaryExpression { NodeType: ExpressionType.Convert or ExpressionType.ConvertChecked } unaryExpression)
{
var innerType = unaryExpression.Operand.Type.UnwrapNullableType();
if (innerType.IsEnum)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,10 @@ public CosmosStringMethodTranslator(ISqlExpressionFactory sqlExpressionFactory)
{
var comparisonTypeArgument = arguments[^1];

if (comparisonTypeArgument is SqlConstantExpression { Value: StringComparison comparisonTypeArgumentValue }
&& (comparisonTypeArgumentValue == StringComparison.OrdinalIgnoreCase
|| comparisonTypeArgumentValue == StringComparison.Ordinal))
if (comparisonTypeArgument is SqlConstantExpression
{
Value: StringComparison comparisonTypeArgumentValue and (StringComparison.OrdinalIgnoreCase or StringComparison.Ordinal)
})
{
return StringComparisonWithComparisonTypeArgumentInstance.Equals(method)
? comparisonTypeArgumentValue == StringComparison.OrdinalIgnoreCase
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,7 @@ private SqlExpression TryCompensateForBoolWithValueConverter(SqlExpression sqlEx
TryCompensateForBoolWithValueConverter(sqlUnaryExpression.Operand));
}

if (sqlExpression is SqlBinaryExpression sqlBinaryExpression
&& (sqlBinaryExpression.OperatorType == ExpressionType.AndAlso
|| sqlBinaryExpression.OperatorType == ExpressionType.OrElse))
if (sqlExpression is SqlBinaryExpression { OperatorType: ExpressionType.AndAlso or ExpressionType.OrElse } sqlBinaryExpression)
{
return sqlBinaryExpression.Update(
TryCompensateForBoolWithValueConverter(sqlBinaryExpression.Left),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,7 @@ protected override bool ShouldRetryOn(Exception exception)
};

static bool IsTransient(HttpStatusCode statusCode)
=> statusCode == HttpStatusCode.ServiceUnavailable
|| statusCode == HttpStatusCode.TooManyRequests
|| statusCode == HttpStatusCode.RequestTimeout
|| statusCode == HttpStatusCode.Gone;
=> statusCode is HttpStatusCode.ServiceUnavailable or HttpStatusCode.TooManyRequests or HttpStatusCode.RequestTimeout or HttpStatusCode.Gone;
}

/// <summary>
Expand Down
4 changes: 1 addition & 3 deletions src/EFCore.Design/Design/Internal/CSharpHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1501,9 +1501,7 @@ private static bool IsIdentifierStartCharacter(char ch)
{
if (ch < 'a')
{
return ch >= 'A'
&& (ch <= 'Z'
|| ch == '_');
return ch is >= 'A' and (<= 'Z' or '_');
}

if (ch <= 'z')
Expand Down
3 changes: 1 addition & 2 deletions src/EFCore.Design/Migrations/Design/MigrationsBundle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ public static int Execute(string? context, Assembly assembly, Assembly startupAs
}
catch (Exception ex)
{
if (ex is CommandParsingException
|| ex is OperationException)
if (ex is CommandParsingException or OperationException)
{
Reporter.WriteVerbose(ex.ToString());
}
Expand Down
7 changes: 2 additions & 5 deletions src/EFCore.Design/Scaffolding/Internal/CSharpUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,7 @@ private static bool IsIdentifierStartCharacter(char ch)
{
if (ch < 'a')
{
return ch >= 'A'
&& (ch <= 'Z'
|| ch == '_');
return ch is >= 'A' and (<= 'Z' or '_');
}

if (ch <= 'z')
Expand All @@ -242,8 +240,7 @@ private static bool IsIdentifierPartCharacter(char ch)
{
return ch < 'A'
? ch is >= '0' and <= '9'
: ch <= 'Z'
|| ch == '_';
: ch is <= 'Z' or '_';
}

if (ch <= 'z')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ protected override Expression VisitBinary(BinaryExpression binaryExpression)
return Visit(ConvertObjectArrayEqualityComparison(binaryExpression.Left, binaryExpression.Right));
}

if ((binaryExpression.NodeType == ExpressionType.Equal || binaryExpression.NodeType == ExpressionType.NotEqual)
if (binaryExpression.NodeType is ExpressionType.Equal or ExpressionType.NotEqual
&& (binaryExpression.Left.IsNullConstantExpression() || binaryExpression.Right.IsNullConstantExpression()))
{
var nonNullExpression = binaryExpression.Left.IsNullConstantExpression() ? binaryExpression.Right : binaryExpression.Left;
Expand Down Expand Up @@ -232,8 +232,7 @@ static Expression RemoveConvert(Expression e)
return QueryCompilationContext.NotTranslatedExpression;
}

if ((binaryExpression.NodeType == ExpressionType.Equal
|| binaryExpression.NodeType == ExpressionType.NotEqual)
if (binaryExpression.NodeType is ExpressionType.Equal or ExpressionType.NotEqual
// Visited expression could be null, We need to pass MemberInitExpression
&& TryRewriteEntityEquality(
binaryExpression.NodeType,
Expand All @@ -252,8 +251,7 @@ static Expression RemoveConvert(Expression e)
newRight = ConvertToNullable(newRight);
}

if ((binaryExpression.NodeType == ExpressionType.Equal
|| binaryExpression.NodeType == ExpressionType.NotEqual)
if (binaryExpression.NodeType is ExpressionType.Equal or ExpressionType.NotEqual
&& TryUseComparer(newLeft, newRight, out var updatedExpression))
{
if (binaryExpression.NodeType == ExpressionType.NotEqual)
Expand Down Expand Up @@ -578,7 +576,7 @@ protected override Expression VisitMember(MemberExpression memberExpression)
static bool ShouldApplyNullProtectionForMemberAccess(Type callerType, string memberName)
=> !(callerType.IsGenericType
&& callerType.GetGenericTypeDefinition() == typeof(Nullable<>)
&& (memberName == nameof(Nullable<int>.Value) || memberName == nameof(Nullable<int>.HasValue)));
&& memberName is nameof(Nullable<int>.Value) or nameof(Nullable<int>.HasValue));
}

/// <summary>
Expand Down Expand Up @@ -1061,9 +1059,7 @@ protected override Expression VisitUnary(UnaryExpression unaryExpression)
}

if (newOperand is EntityReferenceExpression entityReferenceExpression
&& (unaryExpression.NodeType == ExpressionType.Convert
|| unaryExpression.NodeType == ExpressionType.ConvertChecked
|| unaryExpression.NodeType == ExpressionType.TypeAs))
&& unaryExpression.NodeType is ExpressionType.Convert or ExpressionType.ConvertChecked or ExpressionType.TypeAs)
{
return entityReferenceExpression.Convert(unaryExpression.Type);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -499,8 +499,7 @@ protected override Expression VisitUnary(UnaryExpression unaryExpression)
{
var operand = Visit(unaryExpression.Operand);

return (unaryExpression.NodeType == ExpressionType.Convert
|| unaryExpression.NodeType == ExpressionType.ConvertChecked)
return unaryExpression.NodeType is ExpressionType.Convert or ExpressionType.ConvertChecked
&& unaryExpression.Type == operand.Type
? operand
: unaryExpression.Update(MatchTypes(operand, unaryExpression.Operand.Type));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ private static ShapedQueryExpression CreateShapedQueryExpressionStatic(IEntityTy

return memberInitExpression.Update(updatedNewExpression, newBindings);

case EntityShaperExpression { ValueBufferExpression: ProjectionBindingExpression projectionBindingExpression } entityShaperExpression:
case EntityShaperExpression { ValueBufferExpression: ProjectionBindingExpression } entityShaperExpression:
return entityShaperExpression;

default:
Expand Down Expand Up @@ -1174,11 +1174,9 @@ protected override Expression VisitMethodCall(MethodCallExpression methodCallExp
}

protected override Expression VisitExtension(Expression extensionExpression)
=> extensionExpression is EntityShaperExpression
|| extensionExpression is ShapedQueryExpression
|| extensionExpression is GroupByShaperExpression
? extensionExpression
: base.VisitExtension(extensionExpression);
=> extensionExpression is EntityShaperExpression or ShapedQueryExpression or GroupByShaperExpression
? extensionExpression
: base.VisitExtension(extensionExpression);

private Expression? TryExpand(Expression? source, MemberIdentity member)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1737,8 +1737,7 @@ public static IEnumerable<StoreObjectIdentifier> GetMappedStoreObjects(
yield return declaringStoreObject.Value;
}

if (storeObjectType == StoreObjectType.Function
|| storeObjectType == StoreObjectType.SqlQuery)
if (storeObjectType is StoreObjectType.Function or StoreObjectType.SqlQuery)
{
yield break;
}
Expand Down
12 changes: 5 additions & 7 deletions src/EFCore.Relational/Infrastructure/RelationalModelValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2047,9 +2047,9 @@ private static void ValidateNonTphMapping(IEntityType rootEntityType, StoreObjec

private static void ValidateTphMapping(IEntityType rootEntityType, StoreObjectType storeObjectType)
{
var isSproc = storeObjectType == StoreObjectType.DeleteStoredProcedure
|| storeObjectType == StoreObjectType.InsertStoredProcedure
|| storeObjectType == StoreObjectType.UpdateStoredProcedure;
var isSproc = storeObjectType is StoreObjectType.DeleteStoredProcedure
or StoreObjectType.InsertStoredProcedure
or StoreObjectType.UpdateStoredProcedure;
var rootSproc = isSproc ? StoredProcedure.FindDeclaredStoredProcedure(rootEntityType, storeObjectType) : null;
var rootId = StoreObjectIdentifier.Create(rootEntityType, storeObjectType);
foreach (var entityType in rootEntityType.GetDerivedTypes())
Expand Down Expand Up @@ -2344,8 +2344,7 @@ private static IEnumerable<StoreObjectIdentifier> GetAllMappedStoreObjects(
yield return declaringStoreObject.Value;
}

if (storeObjectType == StoreObjectType.Function
|| storeObjectType == StoreObjectType.SqlQuery)
if (storeObjectType is StoreObjectType.Function or StoreObjectType.SqlQuery)
{
yield break;
}
Expand All @@ -2372,8 +2371,7 @@ private static IEnumerable<StoreObjectIdentifier> GetAllMappedStoreObjects(
else
{
var declaringStoreObject = StoreObjectIdentifier.Create(property.DeclaringEntityType, storeObjectType);
if (storeObjectType == StoreObjectType.Function
|| storeObjectType == StoreObjectType.SqlQuery)
if (storeObjectType is StoreObjectType.Function or StoreObjectType.SqlQuery)
{
if (declaringStoreObject != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ protected override Expression VisitMethodCall(MethodCallExpression methodCallExp
{
var methodName = methodCallExpression.Method.Name;
if (methodCallExpression.Method.DeclaringType == typeof(RelationalQueryableExtensions)
&& (methodName == nameof(RelationalQueryableExtensions.FromSqlRaw)
|| methodName == nameof(RelationalQueryableExtensions.FromSqlInterpolated)
|| methodName == nameof(RelationalQueryableExtensions.FromSql)))
&& methodName is nameof(RelationalQueryableExtensions.FromSqlRaw)
or nameof(RelationalQueryableExtensions.FromSqlInterpolated)
or nameof(RelationalQueryableExtensions.FromSql))
{
var newSource = (EntityQueryRootExpression)Visit(methodCallExpression.Arguments[0]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,10 +239,8 @@ private static void TryUniquifyColumnNames(
{
if (property.GetAfterSaveBehavior() == PropertySaveBehavior.Save
&& otherProperty.GetAfterSaveBehavior() == PropertySaveBehavior.Save
&& (property.ValueGenerated == ValueGenerated.Never
|| property.ValueGenerated == ValueGenerated.OnUpdateSometimes)
&& (otherProperty.ValueGenerated == ValueGenerated.Never
|| otherProperty.ValueGenerated == ValueGenerated.OnUpdateSometimes))
&& property.ValueGenerated is ValueGenerated.Never or ValueGenerated.OnUpdateSometimes
&& otherProperty.ValueGenerated is ValueGenerated.Never or ValueGenerated.OnUpdateSometimes)
{
// Handle this with a default value convention #9329
property.Builder.ValueGenerated(ValueGenerated.OnUpdateSometimes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -622,8 +622,7 @@ protected override Expression VisitUnary(UnaryExpression unaryExpression)
{
var operand = Visit(unaryExpression.Operand);

return (unaryExpression.NodeType == ExpressionType.Convert
|| unaryExpression.NodeType == ExpressionType.ConvertChecked)
return unaryExpression.NodeType is ExpressionType.Convert or ExpressionType.ConvertChecked
&& unaryExpression.Type == operand.Type
? operand
: unaryExpression.Update(MatchTypes(operand, unaryExpression.Operand.Type));
Expand Down
Loading

0 comments on commit da6f0a6

Please sign in to comment.