Skip to content

Commit

Permalink
Static analysis: Use ??= (#26916)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajcvickers authored Dec 7, 2021
1 parent 69591bc commit 8cbb52e
Show file tree
Hide file tree
Showing 22 changed files with 45 additions and 173 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,7 @@ protected virtual void ValidateSharedContainerCompatibility(
break;
}

if (firstEntityType == null)
{
firstEntityType = entityType;
}
firstEntityType ??= entityType;

if (entityType.ClrType.IsInstantiable()
&& entityType.GetContainingPropertyName() == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@ public static IServiceCollection AddEntityFrameworkDesignTimeServices(
IOperationReporter? reporter = null,
Func<IServiceProvider>? applicationServiceProviderAccessor = null)
{
if (reporter == null)
{
reporter = new OperationReporter(handler: null);
}
reporter ??= new OperationReporter(handler: null);

new EntityFrameworkRelationalDesignServicesBuilder(services)
.TryAddProviderSpecificServices(
Expand Down
9 changes: 3 additions & 6 deletions src/EFCore.Design/Migrations/Design/MigrationsScaffolder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -352,12 +352,9 @@ public virtual MigrationFiles RemoveMigration(
modelSnapshotName,
model);

if (modelSnapshotFile == null)
{
modelSnapshotFile = Path.Combine(
GetDirectory(projectDir, null, GetSubNamespace(rootNamespace, modelSnapshotNamespace)),
modelSnapshotFileName);
}
modelSnapshotFile ??= Path.Combine(
GetDirectory(projectDir, null, GetSubNamespace(rootNamespace, modelSnapshotNamespace)),
modelSnapshotFileName);

Dependencies.OperationReporter.WriteInformation(DesignStrings.RevertingSnapshot);
File.WriteAllText(modelSnapshotFile, modelSnapshotCode, Encoding.UTF8);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,7 @@ public virtual ScaffoldedModel ScaffoldModel(
_reporter.WriteWarning(DesignStrings.SensitiveInformationWarning);
}

if (codeOptions.ConnectionString == null)
{
codeOptions.ConnectionString = connectionString;
}
codeOptions.ConnectionString ??= connectionString;

var databaseModel = _databaseModelFactory.Create(resolvedConnectionString, databaseOptions);
var modelConnectionString = (string?)(databaseModel[ScaffoldingAnnotationNames.ConnectionString]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -831,10 +831,7 @@ void IPrintableExpression.Print(ExpressionPrinter expressionPrinter)

private InMemoryQueryExpression Clone()
{
if (_cloningExpressionVisitor == null)
{
_cloningExpressionVisitor = new CloningExpressionVisitor();
}
_cloningExpressionVisitor ??= new CloningExpressionVisitor();

return (InMemoryQueryExpression)_cloningExpressionVisitor.Visit(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,8 @@ public LambdaExpression ProcessShaper(Expression shaperExpression)
_expressions.Add(result);
result = Expression.Block(_variables, _expressions);

if (_valueBufferParameter == null)
{
// If parameter is null then the projection is not really server correlated so we can just put anything.
_valueBufferParameter = Expression.Parameter(typeof(ValueBuffer));
}
// If parameter is null then the projection is not really server correlated so we can just put anything.
_valueBufferParameter ??= Expression.Parameter(typeof(ValueBuffer));

return Expression.Lambda(result, QueryCompilationContext.QueryContextParameter, _valueBufferParameter);
}
Expand Down Expand Up @@ -95,10 +92,7 @@ protected override Expression VisitExtension(Expression extensionExpression)
variable = Expression.Parameter(projectionBindingExpression.Type);
_variables.Add(variable);
var queryExpression = (InMemoryQueryExpression)projectionBindingExpression.QueryExpression;
if (_valueBufferParameter == null)
{
_valueBufferParameter = queryExpression.CurrentParameter;
}
_valueBufferParameter ??= queryExpression.CurrentParameter;

var projectionIndex = queryExpression.GetProjection(projectionBindingExpression).GetConstantValue<int>();

Expand Down Expand Up @@ -215,10 +209,7 @@ protected override Expression VisitBinary(BinaryExpression binaryExpression)

var projectionBindingExpression = (ProjectionBindingExpression)newExpression.Arguments[0];
var queryExpression = (InMemoryQueryExpression)projectionBindingExpression.QueryExpression;
if (_valueBufferParameter == null)
{
_valueBufferParameter = queryExpression.CurrentParameter;
}
_valueBufferParameter ??= queryExpression.CurrentParameter;

_materializationContextBindings[parameterExpression]
= queryExpression.GetProjection(projectionBindingExpression).GetConstantValue<Dictionary<IProperty, int>>();
Expand Down
5 changes: 1 addition & 4 deletions src/EFCore.InMemory/Storage/Internal/InMemoryStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,7 @@ public virtual int ExecuteTransaction(
// Must be called from inside the lock
private IInMemoryTable EnsureTable(IEntityType entityType)
{
if (_tables == null)
{
_tables = CreateTables();
}
_tables ??= CreateTables();

IInMemoryTable? baseTable = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,7 @@ public static string GetDefaultColumnName(this IReadOnlyProperty property, in St
break;
}

if (builder == null)
{
builder = new StringBuilder();
}
builder ??= new StringBuilder();

builder.Insert(0, "_");
builder.Insert(0, ownership.PrincipalToDependent!.Name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,21 +117,15 @@ public virtual void ProcessEntityTypeBaseTypeChanged(
baseCheckConstraint.EntityType.DisplayName()));
}

if (checkConstraintsToBeRemoved == null)
{
checkConstraintsToBeRemoved = new List<IConventionCheckConstraint>();
}
checkConstraintsToBeRemoved ??= new List<IConventionCheckConstraint>();

checkConstraintsToBeRemoved.Add(checkConstraint);
continue;
}

if (baseCheckConstraint != null)
{
if (checkConstraintsToBeDetached == null)
{
checkConstraintsToBeDetached = new List<IConventionCheckConstraint>();
}
checkConstraintsToBeDetached ??= new List<IConventionCheckConstraint>();

checkConstraintsToBeDetached.Add(checkConstraint);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@ public TableNameFromDbSetConvention(
}
else
{
if (ambiguousTypes == null)
{
ambiguousTypes = new List<Type>();
}
ambiguousTypes ??= new List<Type>();

ambiguousTypes.Add(set.Type);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,7 @@ public virtual void ProcessModelFinalizing(

if (!foundMappedProperty)
{
if (entityTypesMissingConcurrencyColumn == null)
{
entityTypesMissingConcurrencyColumn = new Dictionary<IConventionEntityType, IReadOnlyProperty>();
}
entityTypesMissingConcurrencyColumn ??= new Dictionary<IConventionEntityType, IReadOnlyProperty>();

// store the entity type which is missing the
// concurrency token property, mapped to an example
Expand Down Expand Up @@ -166,10 +163,7 @@ public virtual void ProcessModelFinalizing(
continue;
}

if (concurrencyColumns == null)
{
concurrencyColumns = new Dictionary<string, List<IReadOnlyProperty>>();
}
concurrencyColumns ??= new Dictionary<string, List<IReadOnlyProperty>>();

if (!concurrencyColumns.TryGetValue(columnName, out var properties))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,7 @@ public virtual bool CanSetName(string? name, ConfigurationSource configurationSo
return null;
}

if (checkConstraintsToBeDetached == null)
{
checkConstraintsToBeDetached = new List<IConventionCheckConstraint>();
}
checkConstraintsToBeDetached ??= new List<IConventionCheckConstraint>();

checkConstraintsToBeDetached.Add(derivedCheckConstraint);
}
Expand Down
12 changes: 3 additions & 9 deletions src/EFCore.Relational/Metadata/Internal/RelationalModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1001,18 +1001,12 @@ private static void PopulateRowInternalForeignKeys(TableBase table)
&& !foreignKey.PrincipalEntityType.IsAssignableFrom(foreignKey.DeclaringEntityType)
&& ((ITableBase)table).EntityTypeMappings.Any(m => m.EntityType == foreignKey.PrincipalEntityType))
{
if (rowInternalForeignKeys == null)
{
rowInternalForeignKeys = new SortedSet<IForeignKey>(ForeignKeyComparer.Instance);
}
rowInternalForeignKeys ??= new SortedSet<IForeignKey>(ForeignKeyComparer.Instance);

rowInternalForeignKeys.Add(foreignKey);

if (referencingInternalForeignKeyMap == null)
{
referencingInternalForeignKeyMap =
new SortedDictionary<IEntityType, IEnumerable<IForeignKey>>(EntityTypeFullNameComparer.Instance);
}
referencingInternalForeignKeyMap ??=
new SortedDictionary<IEntityType, IEnumerable<IForeignKey>>(EntityTypeFullNameComparer.Instance);

var principalEntityType = foreignKey.PrincipalEntityType;
if (!referencingInternalForeignKeyMap.TryGetValue(principalEntityType, out var internalReferencingForeignKeys))
Expand Down
5 changes: 1 addition & 4 deletions src/EFCore.Relational/Migrations/MigrationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -837,10 +837,7 @@ public virtual CreateTableBuilder<TColumns> CreateTable<TColumns>(
foreach (var property in typeof(TColumns).GetTypeInfo().DeclaredProperties)
{
var addColumnOperation = ((IInfrastructure<AddColumnOperation>)property.GetMethod!.Invoke(columnsObject, null)!).Instance;
if (addColumnOperation.Name == null)
{
addColumnOperation.Name = property.Name;
}
addColumnOperation.Name ??= property.Name;
// TODO: addColumnOperation.Validate();

columnMap.Add(property, addColumnOperation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,10 +241,7 @@ private sealed class ColumnExpressionFindingExpressionVisitor : ExpressionVisito
var tableAlias = columnExpression.TableAlias!;
if (_columnReferenced!.ContainsKey(tableAlias))
{
if (_columnReferenced[tableAlias] == null)
{
_columnReferenced[tableAlias] = new HashSet<string>();
}
_columnReferenced[tableAlias] ??= new HashSet<string>();

_columnReferenced[tableAlias]!.Add(columnExpression.Name);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2855,10 +2855,7 @@ public void PrepareForAggregate()
[EntityFrameworkInternal]
public SelectExpression Clone()
{
if (_cloningExpressionVisitor == null)
{
_cloningExpressionVisitor = new CloningExpressionVisitor();
}
_cloningExpressionVisitor ??= new CloningExpressionVisitor();

return (SelectExpression)_cloningExpressionVisitor.Visit(this);
}
Expand Down
10 changes: 2 additions & 8 deletions src/EFCore.Relational/Storage/RelationalCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -508,10 +508,7 @@ public virtual RelationalDataReader ExecuteReader(RelationalCommandParameterObje
reader = new BufferedDataReader(reader, detailedErrorsEnabled).Initialize(readerColumns);
}

if (_relationalReader == null)
{
_relationalReader = CreateRelationalDataReader();
}
_relationalReader ??= CreateRelationalDataReader();

_relationalReader.Initialize(parameterObject.Connection, command, reader, commandId, logger);

Expand Down Expand Up @@ -633,10 +630,7 @@ await logger.CommandErrorAsync(
.ConfigureAwait(false);
}

if (_relationalReader == null)
{
_relationalReader = CreateRelationalDataReader();
}
_relationalReader ??= CreateRelationalDataReader();

_relationalReader.Initialize(parameterObject.Connection, command, reader, commandId, logger);

Expand Down
50 changes: 9 additions & 41 deletions src/EFCore.Relational/Storage/RelationalTypeMappingSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,7 @@ protected override CoreTypeMapping FindMapping(in TypeMappingInfo mappingInfo)
}
}

if (isFixedLength == null)
{
isFixedLength = principal.IsFixedLength();
}
isFixedLength ??= principal.IsFixedLength();
}

var storeTypeNameBase = ParseStoreTypeName(storeTypeName, out var unicode, out var size, out var precision, out var scale);
Expand Down Expand Up @@ -281,25 +278,10 @@ protected override CoreTypeMapping FindMapping(in TypeMappingInfo mappingInfo)
storeTypeBaseName = ParseStoreTypeName(
storeTypeName, out var parsedUnicode, out var parsedSize, out var parsedPrecision, out var parsedScale);

if (size == null)
{
size = parsedSize;
}

if (precision == null)
{
precision = parsedPrecision;
}

if (scale == null)
{
scale = parsedScale;
}

if (isUnicode == null)
{
isUnicode = parsedUnicode;
}
size ??= parsedSize;
precision ??= parsedPrecision;
scale ??= parsedScale;
isUnicode ??= parsedUnicode;
}

var isFixedLength = (bool?)typeConfiguration[RelationalAnnotationNames.IsFixedLength];
Expand Down Expand Up @@ -411,25 +393,11 @@ protected override CoreTypeMapping FindMapping(in TypeMappingInfo mappingInfo)
{
storeTypeBaseName = ParseStoreTypeName(
storeTypeName, out var parsedUnicode, out var parsedSize, out var parsedPrecision, out var parsedScale);
if (size == null)
{
size = parsedSize;
}

if (precision == null)
{
precision = parsedPrecision;
}

if (scale == null)
{
scale = parsedScale;
}

if (unicode == null)
{
unicode = parsedUnicode;
}
size ??= parsedSize;
precision ??= parsedPrecision;
scale ??= parsedScale;
unicode ??= parsedUnicode;
}

return FindMappingWithConversion(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,7 @@ protected virtual IEnumerable<IReadOnlyModificationCommand> CreateModificationCo
var isMainEntry = true;
if (table.IsShared)
{
if (sharedTablesCommandsMap == null)
{
sharedTablesCommandsMap = new Dictionary<(string, string?), SharedTableEntryMap<IModificationCommand>>();
}
sharedTablesCommandsMap ??= new Dictionary<(string, string?), SharedTableEntryMap<IModificationCommand>>();

var tableKey = (table.Name, table.Schema);
if (!sharedTablesCommandsMap.TryGetValue(tableKey, out var sharedCommandsMap))
Expand Down
5 changes: 1 addition & 4 deletions src/EFCore.Relational/Update/ModificationCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,7 @@ public virtual IColumnModification AddColumnModification(in ColumnModificationPa
{
var modification = CreateColumnModification(columnModificationParameters);

if (_columnModifications == null)
{
_columnModifications = new List<IColumnModification>();
}
_columnModifications ??= new List<IColumnModification>();

_columnModifications.Add(modification);

Expand Down
Loading

0 comments on commit 8cbb52e

Please sign in to comment.