Skip to content

Commit cd2a031

Browse files
committed
Static analysis: use null propagation
Part of #26805
1 parent ad1cf49 commit cd2a031

10 files changed

+17
-59
lines changed

Diff for: src/EFCore.Cosmos/Query/Internal/CosmosShapedQueryCompilingExpressionVisitor.CosmosProjectionBindingRemovingExpressionVisitorBase.cs

+1-4
Original file line numberDiff line numberDiff line change
@@ -461,10 +461,7 @@ private static void IncludeCollection<TIncludingEntity, TIncludedEntity>(
461461
foreach (var relatedEntity in relatedEntities)
462462
{
463463
fixup(includingEntity, relatedEntity);
464-
if (inverseNavigation != null)
465-
{
466-
inverseNavigation.SetIsLoadedWhenNoTracking(relatedEntity);
467-
}
464+
inverseNavigation?.SetIsLoadedWhenNoTracking(relatedEntity);
468465
}
469466
}
470467
else

Diff for: src/EFCore.InMemory/Query/Internal/InMemoryQueryableMethodTranslatingExpressionVisitor.cs

+1-6
Original file line numberDiff line numberDiff line change
@@ -1218,12 +1218,7 @@ protected override Expression VisitExtension(Expression extensionExpression)
12181218
? entityType.FindNavigation(member.MemberInfo)
12191219
: entityType.FindNavigation(member.Name!);
12201220

1221-
if (navigation == null)
1222-
{
1223-
return null;
1224-
}
1225-
1226-
var targetEntityType = navigation.TargetEntityType;
1221+
var targetEntityType = navigation?.TargetEntityType;
12271222
if (targetEntityType == null
12281223
|| !targetEntityType.IsOwned())
12291224
{

Diff for: src/EFCore.InMemory/Query/Internal/InMemoryShapedQueryCompilingExpressionVisitor.ShaperExpressionProcessingExpressionVisitor.cs

+1-4
Original file line numberDiff line numberDiff line change
@@ -330,10 +330,7 @@ private static void IncludeCollection<TEntity, TIncludingEntity, TIncludedEntity
330330
if (!trackingQuery)
331331
{
332332
fixup(includingEntity, relatedEntity);
333-
if (inverseNavigation != null)
334-
{
335-
inverseNavigation.SetIsLoadedWhenNoTracking(relatedEntity);
336-
}
333+
inverseNavigation?.SetIsLoadedWhenNoTracking(relatedEntity);
337334
}
338335
}
339336
}

Diff for: src/EFCore.Relational/Query/RelationalQueryableMethodTranslatingExpressionVisitor.cs

+1-6
Original file line numberDiff line numberDiff line change
@@ -1068,12 +1068,7 @@ protected override Expression VisitExtension(Expression extensionExpression)
10681068
? entityType.FindNavigation(member.MemberInfo)
10691069
: entityType.FindNavigation(member.Name!);
10701070

1071-
if (navigation == null)
1072-
{
1073-
return null;
1074-
}
1075-
1076-
var targetEntityType = navigation.TargetEntityType;
1071+
var targetEntityType = navigation?.TargetEntityType;
10771072
if (targetEntityType == null
10781073
|| !targetEntityType.IsOwned())
10791074
{

Diff for: src/EFCore.Relational/Query/RelationalShapedQueryCompilingExpressionVisitor.ShaperProcessingExpressionVisitor.cs

+2-8
Original file line numberDiff line numberDiff line change
@@ -1425,10 +1425,7 @@ static RelationalDataReader InitializeReader(
14251425
if (!trackingQuery)
14261426
{
14271427
fixup(entity, relatedEntity);
1428-
if (inverseNavigation != null)
1429-
{
1430-
inverseNavigation.SetIsLoadedWhenNoTracking(relatedEntity);
1431-
}
1428+
inverseNavigation?.SetIsLoadedWhenNoTracking(relatedEntity);
14321429
}
14331430
}
14341431

@@ -1519,10 +1516,7 @@ static async Task<RelationalDataReader> InitializeReaderAsync(
15191516
if (!trackingQuery)
15201517
{
15211518
fixup(entity, relatedEntity);
1522-
if (inverseNavigation != null)
1523-
{
1524-
inverseNavigation.SetIsLoadedWhenNoTracking(relatedEntity);
1525-
}
1519+
inverseNavigation?.SetIsLoadedWhenNoTracking(relatedEntity);
15261520
}
15271521
}
15281522

Diff for: src/EFCore.SqlServer/Metadata/Conventions/SqlServerTemporalConvention.cs

+3-6
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,9 @@ public virtual void ProcessEntityTypeAnnotationChanged(
113113
typeof(DateTime),
114114
periodPropertyName);
115115

116-
if (periodPropertyBuilder != null)
117-
{
118-
// set column name explicitly so that we don't try to uniquefy it to some other column
119-
// in case another property is defined that maps to the same column
120-
periodPropertyBuilder.HasColumnName(periodPropertyName);
121-
}
116+
// set column name explicitly so that we don't try to uniquefy it to some other column
117+
// in case another property is defined that maps to the same column
118+
periodPropertyBuilder?.HasColumnName(periodPropertyName);
122119
}
123120
}
124121
}

Diff for: src/EFCore.SqlServer/Metadata/Conventions/SqlServerValueGenerationConvention.cs

+2-8
Original file line numberDiff line numberDiff line change
@@ -75,20 +75,14 @@ public override void ProcessEntityTypeAnnotationChanged(
7575
&& annotation?.Value is string propertyName)
7676
{
7777
var periodProperty = entityTypeBuilder.Metadata.FindProperty(propertyName);
78-
if (periodProperty != null)
79-
{
80-
periodProperty.Builder.ValueGenerated(GetValueGenerated(periodProperty));
81-
}
78+
periodProperty?.Builder.ValueGenerated(GetValueGenerated(periodProperty));
8279

8380
// cleanup the previous period property - its possible that it won't be deleted
8481
// (e.g. when removing period with default name, while the property with that same name has been explicitly defined)
8582
if (oldAnnotation?.Value is string oldPropertyName)
8683
{
8784
var oldPeriodProperty = entityTypeBuilder.Metadata.FindProperty(oldPropertyName);
88-
if (oldPeriodProperty != null)
89-
{
90-
oldPeriodProperty.Builder.ValueGenerated(GetValueGenerated(oldPeriodProperty));
91-
}
85+
oldPeriodProperty?.Builder.ValueGenerated(GetValueGenerated(oldPeriodProperty));
9286
}
9387
}
9488

Diff for: src/EFCore/Metadata/Conventions/InversePropertyAttributeConvention.cs

+3-5
Original file line numberDiff line numberDiff line change
@@ -273,11 +273,9 @@ private static bool TryRemoveIfAmbiguous(
273273

274274
var existingAmbiguousNavigation = FindActualEntityType(ambiguousInverse.Value.Item2)!
275275
.FindSkipNavigation(ambiguousInverse.Value.Item1);
276-
if (existingAmbiguousNavigation != null)
277-
{
278-
existingAmbiguousNavigation.DeclaringEntityType.Builder.HasNoSkipNavigation(
279-
existingAmbiguousNavigation, fromDataAnnotation: true);
280-
}
276+
277+
existingAmbiguousNavigation?.DeclaringEntityType.Builder.HasNoSkipNavigation(
278+
existingAmbiguousNavigation, fromDataAnnotation: true);
281279

282280
remainingInverseNavigation = entityType.FindSkipNavigation(navigationMemberInfo)?.ForeignKey!.Builder;
283281
return true;

Diff for: src/EFCore/Metadata/Internal/InternalSkipNavigationBuilder.cs

+2-8
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,7 @@ public virtual bool CanSetForeignKey(ForeignKey? foreignKey, ConfigurationSource
141141
return null;
142142
}
143143

144-
if (inverse != null)
145-
{
146-
inverse.UpdateConfigurationSource(configurationSource);
147-
}
144+
inverse?.UpdateConfigurationSource(configurationSource);
148145

149146
using var _ = Metadata.DeclaringEntityType.Model.DelayConventions();
150147

@@ -156,10 +153,7 @@ public virtual bool CanSetForeignKey(ForeignKey? foreignKey, ConfigurationSource
156153

157154
Metadata.SetInverse(inverse, configurationSource);
158155

159-
if (inverse != null)
160-
{
161-
inverse.SetInverse(Metadata, configurationSource);
162-
}
156+
inverse?.SetInverse(Metadata, configurationSource);
163157

164158
return this;
165159
}

Diff for: src/EFCore/Query/Internal/NavigationExpandingExpressionVisitor.ExpressionVisitors.cs

+1-4
Original file line numberDiff line numberDiff line change
@@ -328,10 +328,7 @@ private Expression ExpandForeignKey(
328328
&& entityReference.IncludePaths.TryGetValue(navigation, out var pendingIncludeTree))
329329
{
330330
var cachedEntityReference = UnwrapEntityReference(expansion);
331-
if (cachedEntityReference != null)
332-
{
333-
cachedEntityReference.IncludePaths.Merge(pendingIncludeTree);
334-
}
331+
cachedEntityReference?.IncludePaths.Merge(pendingIncludeTree);
335332
}
336333

337334
return expansion;

0 commit comments

Comments
 (0)