Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove nullability errors #27181

Merged
merged 1 commit into from
Jan 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ private static int GetHashCode(TElement?[] source, ValueComparer<TElement> eleme
return hash.ToHashCode();
}

[return: NotNullIfNotNull("source")]
private static TElement?[] Snapshot(TElement?[] source, ValueComparer<TElement> elementComparer)
{
var snapshot = new TElement?[source.Length];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ private static int GetHashCode(TElement[] source, ValueComparer<TElement> elemen
return hash.ToHashCode();
}

[return: NotNullIfNotNull("source")]
private static TElement[] Snapshot(TElement[] source, ValueComparer<TElement> elementComparer)
{
var snapshot = new TElement[source.Length];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,7 @@ var body
}

Expression replaceExpression;
if (converter?.ConvertsNulls == true)
if (converter.ConvertsNulls == true)
{
replaceExpression = ReplacingExpressionVisitor.Replace(
converter.ConvertFromProviderExpression.Parameters.Single(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,23 +151,23 @@ public CosmosStringMethodTranslator(ISqlExpressionFactory sqlExpressionFactory)
return TranslateSystemFunction("UPPER", method.ReturnType, instance);
}

if (TrimStartMethodInfoWithoutArgs?.Equals(method) == true
if (TrimStartMethodInfoWithoutArgs.Equals(method) == true
|| (TrimStartMethodInfoWithCharArrayArg.Equals(method)
// Cosmos DB LTRIM does not take arguments
&& ((arguments[0] as SqlConstantExpression)?.Value as Array)?.Length == 0))
{
return TranslateSystemFunction("LTRIM", method.ReturnType, instance);
}

if (TrimEndMethodInfoWithoutArgs?.Equals(method) == true
if (TrimEndMethodInfoWithoutArgs.Equals(method) == true
|| (TrimEndMethodInfoWithCharArrayArg.Equals(method)
// Cosmos DB RTRIM does not take arguments
&& ((arguments[0] as SqlConstantExpression)?.Value as Array)?.Length == 0))
{
return TranslateSystemFunction("RTRIM", method.ReturnType, instance);
}

if (TrimMethodInfoWithoutArgs?.Equals(method) == true
if (TrimMethodInfoWithoutArgs.Equals(method) == true
|| (TrimMethodInfoWithCharArrayArg.Equals(method)
// Cosmos DB TRIM does not take arguments
&& ((arguments[0] as SqlConstantExpression)?.Value as Array)?.Length == 0))
Expand Down
4 changes: 2 additions & 2 deletions src/EFCore.Cosmos/Update/Internal/DocumentSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public virtual JObject CreateDocument(IUpdateEntry entry, int? ordinal)
}

var embeddedValue = entry.GetCurrentValue(embeddedNavigation);
var embeddedPropertyName = fk.DeclaringEntityType.GetContainingPropertyName();
var embeddedPropertyName = fk.DeclaringEntityType.GetContainingPropertyName()!;
if (embeddedValue == null)
{
document[embeddedPropertyName] = null;
Expand Down Expand Up @@ -194,7 +194,7 @@ public virtual JObject CreateDocument(IUpdateEntry entry, int? ordinal)

var embeddedDocumentSource = _database.GetDocumentSource(fk.DeclaringEntityType);
var embeddedValue = entry.GetCurrentValue(ownedNavigation);
var embeddedPropertyName = fk.DeclaringEntityType.GetContainingPropertyName();
var embeddedPropertyName = fk.DeclaringEntityType.GetContainingPropertyName()!;
if (embeddedValue == null)
{
if (document[embeddedPropertyName] != null)
Expand Down
6 changes: 2 additions & 4 deletions src/EFCore.Design/Design/IPluralizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,13 @@ public interface IPluralizer
/// </summary>
/// <param name="identifier">The identifier to be pluralized.</param>
/// <returns>The pluralized identifier.</returns>
[return: NotNullIfNotNull("identifier")]
string? Pluralize(string? identifier);
string Pluralize(string identifier);

/// <summary>
/// Gets the singular version of the given identifier. Returns the same
/// identifier if it is already singularized.
/// </summary>
/// <param name="identifier">The identifier to be singularized.</param>
/// <returns>The singularized identifier.</returns>
[return: NotNullIfNotNull("identifier")]
string? Singularize(string? identifier);
string Singularize(string identifier);
}
8 changes: 4 additions & 4 deletions src/EFCore.Design/Design/Internal/HumanizerPluralizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ public class HumanizerPluralizer : IPluralizer
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual string? Pluralize(string? name)
=> name?.Pluralize(inputIsKnownToBeSingular: false);
public virtual string Pluralize(string name)
=> name.Pluralize(inputIsKnownToBeSingular: false);

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual string? Singularize(string? name)
=> name?.Singularize(inputIsKnownToBePlural: false);
public virtual string Singularize(string name)
=> name.Singularize(inputIsKnownToBePlural: false);
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ public virtual string GetPrincipalEndCandidateNavigationPropertyName(
string dependentEndNavigationPropertyName)
{
var allForeignKeysBetweenDependentAndPrincipal =
foreignKey.PrincipalEntityType?
.GetReferencingForeignKeys()
foreignKey.PrincipalEntityType.GetReferencingForeignKeys()
.Where(fk => foreignKey.DeclaringEntityType == fk.DeclaringEntityType);

return allForeignKeysBetweenDependentAndPrincipal?.Count() > 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static class DatabaseColumnExtensions
/// </summary>
public static string DisplayName(this DatabaseColumn column)
{
var tablePrefix = column.Table?.DisplayName();
var tablePrefix = column.Table.DisplayName();
return (!string.IsNullOrEmpty(tablePrefix) ? tablePrefix + "." : "") + column.Name;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ public static class DatabaseForeignKeyExtensions
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public static string DisplayName(this DatabaseForeignKey foreignKey)
=> foreignKey.Table?.DisplayName() + "(" + string.Join(",", foreignKey.Columns.Select(f => f.Name)) + ")";
=> foreignKey.Table.DisplayName() + "(" + string.Join(",", foreignKey.Columns.Select(f => f.Name)) + ")";
}
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ private static TCollection MaterializeCollection<TElement, TCollection>(
QueryContext queryContext,
IEnumerable<ValueBuffer> innerValueBuffers,
Func<QueryContext, ValueBuffer, TElement> innerShaper,
IClrCollectionAccessor clrCollectionAccessor)
IClrCollectionAccessor? clrCollectionAccessor)
where TCollection : class, ICollection<TElement>
{
var collection = (TCollection)(clrCollectionAccessor?.Create() ?? new List<TElement>());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -440,8 +440,7 @@ private static string TransactionUsed(EventDefinitionBase definition, EventData
{
var d = (EventDefinition<string?>)definition;
var p = (TransactionEventData)payload;
return d.GenerateMessage(
p.Transaction?.IsolationLevel.ToString("G"));
return d.GenerateMessage(p.Transaction.IsolationLevel.ToString("G"));
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions src/EFCore.Relational/Migrations/HistoryRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ protected HistoryRepository(HistoryRepositoryDependencies dependencies)
Dependencies = dependencies;

var relationalOptions = RelationalOptionsExtension.Extract(dependencies.Options);
TableName = relationalOptions?.MigrationsHistoryTableName ?? DefaultTableName;
TableSchema = relationalOptions?.MigrationsHistoryTableSchema;
TableName = relationalOptions.MigrationsHistoryTableName ?? DefaultTableName;
TableSchema = relationalOptions.MigrationsHistoryTableSchema;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public MigrationsAssembly(
{
_contextType = currentContext.Context.GetType();

var assemblyName = RelationalOptionsExtension.Extract(options)?.MigrationsAssembly;
var assemblyName = RelationalOptionsExtension.Extract(options).MigrationsAssembly;
Assembly = assemblyName == null
? _contextType.Assembly
: Assembly.Load(new AssemblyName(assemblyName));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -859,7 +859,7 @@ private PropertyInfoEqualityComparer()
public bool Equals(PropertyInfo? x, PropertyInfo? y)
=> x.IsSameAs(y);

public int GetHashCode([DisallowNull] PropertyInfo obj)
public int GetHashCode(PropertyInfo obj)
=> throw new NotSupportedException();
}

Expand Down
38 changes: 23 additions & 15 deletions src/EFCore.Relational/Query/Internal/BufferedDataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ private void AssertFieldIsReady(int ordinal)
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual BufferedDataReader Initialize(IReadOnlyList<ReaderColumn> columns)
public virtual BufferedDataReader Initialize(IReadOnlyList<ReaderColumn?> columns)
{
if (_underlyingReader == null)
{
Expand Down Expand Up @@ -190,7 +190,7 @@ public virtual BufferedDataReader Initialize(IReadOnlyList<ReaderColumn> columns
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual async Task<BufferedDataReader> InitializeAsync(
IReadOnlyList<ReaderColumn> columns,
IReadOnlyList<ReaderColumn?> columns,
CancellationToken cancellationToken)
{
if (_underlyingReader == null)
Expand Down Expand Up @@ -692,7 +692,7 @@ private sealed class BufferedDataRecord
private TypeCase[] _columnTypeCases;

private DbDataReader _underlyingReader;
private IReadOnlyList<ReaderColumn> _columns;
private IReadOnlyList<ReaderColumn?> _columns;
private int[] _indexMap;
private readonly bool _detailedErrorsEnabled;

Expand Down Expand Up @@ -882,7 +882,7 @@ public Task<bool> ReadAsync(CancellationToken cancellationToken)
=> Task.FromResult(Read());
#pragma warning restore IDE0060 // Remove unused parameter

public BufferedDataRecord Initialize(DbDataReader reader, IReadOnlyList<ReaderColumn> columns)
public BufferedDataRecord Initialize(DbDataReader reader, IReadOnlyList<ReaderColumn?> columns)
{
_underlyingReader = reader;
_columns = columns;
Expand All @@ -909,7 +909,7 @@ public BufferedDataRecord Initialize(DbDataReader reader, IReadOnlyList<ReaderCo

public async Task<BufferedDataRecord> InitializeAsync(
DbDataReader reader,
IReadOnlyList<ReaderColumn> columns,
IReadOnlyList<ReaderColumn?> columns,
CancellationToken cancellationToken)
{
_underlyingReader = reader;
Expand Down Expand Up @@ -947,6 +947,11 @@ private void ReadRow()
for (var i = 0; i < FieldCount; i++)
{
var column = _columns[i];
if (column == null)
{
continue;
}

var nullIndex = _nullOrdinalToIndexMap[i];
switch (_columnTypeCases[i])
{
Expand Down Expand Up @@ -1229,13 +1234,11 @@ private void InitializeFields()
var fieldCount = FieldCount;
if (FieldCount < _columns.Count)
{
if (_columns.Count > 0
&& _columns[0].Name != null)
// Non-composed FromSql
var firstMissingColumn = _columns.Select(c => c?.Name).Where(c => c != null).Except(_columnNames).FirstOrDefault();
if (firstMissingColumn != null)
{
// Non-composed FromSql
var missingColumns = _columns.Select(c => c.Name).Except(_columnNames);

throw new InvalidOperationException(RelationalStrings.FromSqlMissingColumn(missingColumns.First()));
throw new InvalidOperationException(RelationalStrings.FromSqlMissingColumn(firstMissingColumn));
}

throw new InvalidOperationException(RelationalStrings.TooFewReaderFields(_columns.Count, FieldCount));
Expand All @@ -1244,16 +1247,21 @@ private void InitializeFields()
_columnTypeCases = Enumerable.Repeat(TypeCase.Empty, fieldCount).ToArray();
_ordinalToIndexMap = Enumerable.Repeat(-1, fieldCount).ToArray();
if (_columns.Count > 0
&& _columns[0].Name != null)
&& _columns.Any(e => e?.Name != null))
{
// Non-Composed FromSql
var readerColumns = _fieldNameLookup.Value;

_indexMap = new int[_columns.Count];
var newColumnMap = new ReaderColumn[fieldCount];
var newColumnMap = new ReaderColumn?[fieldCount];
for (var i = 0; i < _columns.Count; i++)
{
var column = _columns[i];
if (column == null)
{
continue;
}

if (!readerColumns.TryGetValue(column.Name!, out var ordinal))
{
throw new InvalidOperationException(RelationalStrings.FromSqlMissingColumn(column.Name));
Expand All @@ -1268,7 +1276,7 @@ private void InitializeFields()

if (FieldCount != _columns.Count)
{
var newColumnMap = new ReaderColumn[fieldCount];
var newColumnMap = new ReaderColumn?[fieldCount];
for (var i = 0; i < _columns.Count; i++)
{
newColumnMap[i] = _columns[i];
Expand Down Expand Up @@ -1884,7 +1892,7 @@ private static void ThrowReadValueException(
int ordinal,
ReaderColumn column)
{
var value = reader.GetFieldValue<object>(ordinal);
var value = reader.GetFieldValue<object?>(ordinal);
var property = column.Property;
var expectedType = column.Type.MakeNullable(column.IsNullable);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ public virtual SelectExpression Expand(
switch (fromSql.Arguments)
{
case ParameterExpression parameterExpression:
// parameter value will never be null. It could be empty object[]
var parameterValues = (object[])_parametersValues[parameterExpression.Name!]!;
// parameter value will never be null. It could be empty object?[]
var parameterValues = (object?[])_parametersValues[parameterExpression.Name!]!;
_canCache = false;

var subParameters = new List<IRelationalParameter>(parameterValues.Length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public RelationalCommandCache(
IQuerySqlGeneratorFactory querySqlGeneratorFactory,
IRelationalParameterBasedSqlProcessorFactory relationalParameterBasedSqlProcessorFactory,
SelectExpression selectExpression,
IReadOnlyList<ReaderColumn>? readerColumns,
IReadOnlyList<ReaderColumn?>? readerColumns,
bool useRelationalNulls)
{
_memoryCache = memoryCache;
Expand All @@ -51,7 +51,7 @@ public RelationalCommandCache(
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual IReadOnlyList<ReaderColumn>? ReaderColumns { get; }
public virtual IReadOnlyList<ReaderColumn?>? ReaderColumns { get; }

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,6 @@ private static List<object> BuildListFromEnumerable(IEnumerable collection)

private static bool TryGetInExpressionCandidateInfo(
SqlExpression sqlExpression,
[MaybeNullWhen(false)]
out (ColumnExpression ColumnExpression, object ConstantValue, RelationalTypeMapping TypeMapping, ExpressionType OperationType)
candidateInfo)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1146,7 +1146,7 @@ outerKey is NewArrayExpression newArrayExpression
: navigation.DeclaringEntityType.GetViewOrTableMappings().Select(tm => tm.Table)
.Except(navigation.DeclaringEntityType.BaseType.GetViewOrTableMappings().Select(tm => tm.Table))
.Single();
if (table.GetReferencingRowInternalForeignKeys(foreignKey.PrincipalEntityType)?.Contains(foreignKey) == true)
if (table.GetReferencingRowInternalForeignKeys(foreignKey.PrincipalEntityType).Contains(foreignKey) == true)
{
// Mapped to same table
// We get identifying column to figure out tableExpression to pull columns from and nullability of most principal side
Expand Down
Loading