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

More nullable annotations #23938

Merged
1 commit merged into from
Feb 2, 2021
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 @@ -18,8 +18,8 @@ namespace Microsoft.EntityFrameworkCore.ChangeTracking.Internal
public class ObservableBackedBindingList<T> : SortableBindingList<T>
{
private bool _addingNewInstance;
private T _addNewInstance;
private T _cancelNewInstance;
private T? _addNewInstance;
private T? _cancelNewInstance;

private readonly ICollection<T> _observableCollection;
private bool _inCollectionChanged;
Expand Down Expand Up @@ -102,7 +102,7 @@ public override void EndNew(int itemIndex)
&& itemIndex < Count
&& Equals(base[itemIndex], _addNewInstance))
{
AddToObservableCollection(_addNewInstance);
AddToObservableCollection(_addNewInstance!);
_addNewInstance = default;
_addingNewInstance = false;
}
Expand Down Expand Up @@ -180,7 +180,7 @@ protected override void SetItem(int index, T item)
}
}

private void ObservableCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
private void ObservableCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
{
// Don't try to change the binding list if the original change came from the binding list
// and the ObservableCollection is just being changed to match it.
Expand All @@ -202,7 +202,7 @@ private void ObservableCollectionChanged(object sender, NotifyCollectionChangedE
if (e.Action == NotifyCollectionChangedAction.Remove
|| e.Action == NotifyCollectionChangedAction.Replace)
{
foreach (T entity in e.OldItems)
foreach (T entity in e.OldItems!)
{
Remove(entity);
}
Expand All @@ -211,7 +211,7 @@ private void ObservableCollectionChanged(object sender, NotifyCollectionChangedE
if (e.Action == NotifyCollectionChangedAction.Add
|| e.Action == NotifyCollectionChangedAction.Replace)
{
foreach (T entity in e.NewItems)
foreach (T entity in e.NewItems!)
{
Add(entity);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class SortableBindingList<T> : BindingList<T>
{
private bool _isSorted;
private ListSortDirection _sortDirection;
private PropertyDescriptor _sortProperty;
private PropertyDescriptor? _sortProperty;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand Down Expand Up @@ -87,7 +87,7 @@ protected override ListSortDirection SortDirectionCore
/// 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>
protected override PropertyDescriptor SortPropertyCore
protected override PropertyDescriptor? SortPropertyCore
=> _sortProperty;

/// <summary>
Expand Down Expand Up @@ -115,12 +115,22 @@ public PropertyComparer(PropertyDescriptor prop, ListSortDirection direction)
_prop = prop;
_direction = direction;

var property = typeof(Comparer<>).MakeGenericType(prop.PropertyType).GetTypeInfo().GetDeclaredProperty("Default");
_comparer = (IComparer)property.GetValue(null, null);
var property = typeof(Comparer<>).MakeGenericType(prop.PropertyType).GetTypeInfo().GetDeclaredProperty("Default")!;
_comparer = (IComparer)property.GetValue(null, null)!;
}

public override int Compare(T left, T right)
public override int Compare(T? left, T? right)
{
if (left is null)
{
return right is null ? 0 : -1;
}

if (right is null)
{
return 1;
}

var leftValue = _prop.GetValue(left);
var rightValue = _prop.GetValue(right);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace Microsoft.EntityFrameworkCore.ChangeTracking
public class ObservableCollectionListSource<T> : ObservableCollection<T>, IListSource
where T : class
{
private IBindingList _bindingList;
private IBindingList? _bindingList;

/// <summary>
/// Initializes a new instance of the <see cref="ObservableCollectionListSource{T}" /> class.
Expand Down Expand Up @@ -70,6 +70,6 @@ bool IListSource.ContainsListCollection
/// An <see cref="IBindingList" /> in sync with the ObservableCollection.
/// </returns>
IList IListSource.GetList()
=> _bindingList ?? (_bindingList = this.ToBindingList());
=> _bindingList ??= this.ToBindingList();
}
}
2 changes: 0 additions & 2 deletions src/EFCore.Abstractions/DbFunctionAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Utilities;

#nullable enable

namespace Microsoft.EntityFrameworkCore
{
/// <summary>
Expand Down
1 change: 1 addition & 0 deletions src/EFCore.Abstractions/EFCore.Abstractions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<AssemblyName>Microsoft.EntityFrameworkCore.Abstractions</AssemblyName>
<RootNamespace>Microsoft.EntityFrameworkCore</RootNamespace>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
Expand Down
3 changes: 3 additions & 0 deletions src/EFCore.Abstractions/EntityTypeConfigurationAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using Microsoft.EntityFrameworkCore.Utilities;

namespace Microsoft.EntityFrameworkCore
{
Expand All @@ -16,6 +17,8 @@ public sealed class EntityTypeConfigurationAttribute : Attribute
/// <param name="entityConfigurationType"> The IEntityTypeConfiguration&lt;&gt; type to use. </param>
public EntityTypeConfigurationAttribute(Type entityConfigurationType)
{
Check.NotNull(entityConfigurationType, nameof(entityConfigurationType));

EntityTypeConfigurationType = entityConfigurationType;
}

Expand Down
8 changes: 5 additions & 3 deletions src/EFCore.Abstractions/IndexAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Linq;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Utilities;
using CA = System.Diagnostics.CodeAnalysis;

namespace Microsoft.EntityFrameworkCore
{
Expand All @@ -16,13 +17,13 @@ namespace Microsoft.EntityFrameworkCore
public sealed class IndexAttribute : Attribute
{
private bool? _isUnique;
private string _name;
private string? _name;

/// <summary>
/// Initializes a new instance of the <see cref="IndexAttribute" /> class.
/// </summary>
/// <param name="propertyNames"> The properties which constitute the index, in order (there must be at least one). </param>
public IndexAttribute([CanBeNull] params string[] propertyNames)
public IndexAttribute([NotNull] params string[] propertyNames)
{
Check.NotEmpty(propertyNames, nameof(propertyNames));
Check.HasNoEmptyElements(propertyNames, nameof(propertyNames));
Expand All @@ -38,7 +39,8 @@ public IndexAttribute([CanBeNull] params string[] propertyNames)
/// <summary>
/// The name of the index.
/// </summary>
public string Name
[CA.DisallowNull]
public string? Name
{
get => _name;
[param: NotNull] set => _name = Check.NotNull(value, nameof(value));
Expand Down
4 changes: 2 additions & 2 deletions src/EFCore.Abstractions/Infrastructure/ILazyLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void SetLoaded(
/// </summary>
/// <param name="entity"> The entity on which the navigation property is located. </param>
/// <param name="navigationName"> The navigation property name. </param>
void Load([NotNull] object entity, [NotNull] [CallerMemberName] string navigationName = null);
void Load([NotNull] object entity, [NotNull] [CallerMemberName] string navigationName = "");

/// <summary>
/// Loads a navigation property if it has not already been loaded.
Expand All @@ -55,6 +55,6 @@ Task LoadAsync(
#pragma warning restore CA1068 // CancellationToken parameters must come last
[NotNull] object entity,
CancellationToken cancellationToken = default,
[NotNull] [CallerMemberName] string navigationName = null);
[NotNull] [CallerMemberName] string navigationName = "");
}
}
10 changes: 4 additions & 6 deletions src/EFCore.Abstractions/Infrastructure/LazyLoaderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,13 @@ public static class LazyLoaderExtensions
/// <returns>
/// The loaded navigation property value, or the navigation property value unchanged if the loader is <see langword="null" />.
/// </returns>
public static TRelated Load<TRelated>(
[CanBeNull] this ILazyLoader loader,
public static TRelated? Load<TRelated>(
[CanBeNull] this ILazyLoader? loader,
[NotNull] object entity,
[CanBeNull] ref TRelated navigationField,
// ReSharper disable once AssignNullToNotNullAttribute
[NotNull] [CallerMemberName] string navigationName = null)
[CanBeNull] ref TRelated? navigationField,
[NotNull] [CallerMemberName] string navigationName = "")
where TRelated : class
{
// ReSharper disable once AssignNullToNotNullAttribute
loader?.Load(entity, navigationName);

return navigationField;
Expand Down
2 changes: 0 additions & 2 deletions src/EFCore.Abstractions/Query/NotParameterizedAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

using System;

#nullable enable

namespace Microsoft.EntityFrameworkCore.Query
{
/// <summary>
Expand Down
1 change: 1 addition & 0 deletions src/EFCore.Analyzers/EFCore.Analyzers.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<RootNamespace>Microsoft.EntityFrameworkCore</RootNamespace>
<IncludeBuildOutput>false</IncludeBuildOutput>
<NuspecFile>$(MSBuildProjectName).nuspec</NuspecFile>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
Expand Down
9 changes: 4 additions & 5 deletions src/EFCore.Analyzers/InternalUsageDiagnosticAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -315,11 +315,10 @@ private static bool IsInternal(OperationAnalysisContext context, ITypeSymbol sym
&& (IsInInternalNamespace(symbol) || HasInternalAttribute(symbol));

private static bool HasInternalAttribute(ISymbol symbol)
=> symbol != null
&& symbol.GetAttributes().Any(
a =>
a.AttributeClass.ToDisplayString()
== "Microsoft.EntityFrameworkCore.Infrastructure.EntityFrameworkInternalAttribute");
=> symbol.GetAttributes().Any(
a =>
a.AttributeClass!.ToDisplayString()
== "Microsoft.EntityFrameworkCore.Infrastructure.EntityFrameworkInternalAttribute");

private static bool IsInInternalNamespace(ISymbol symbol)
{
Expand Down
2 changes: 2 additions & 0 deletions src/EFCore.Cosmos/Diagnostics/CosmosEventId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using System.Diagnostics;
using Microsoft.Extensions.Logging;

#nullable enable

namespace Microsoft.EntityFrameworkCore.Diagnostics
{
/// <summary>
Expand Down
10 changes: 6 additions & 4 deletions src/EFCore.Cosmos/Diagnostics/CosmosQueryEventData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using System.Diagnostics;
using JetBrains.Annotations;

#nullable enable

namespace Microsoft.EntityFrameworkCore.Diagnostics
{
/// <summary>
Expand All @@ -27,8 +29,8 @@ public CosmosQueryEventData(
[NotNull] EventDefinitionBase eventDefinition,
[NotNull] Func<EventDefinitionBase, EventData, string> messageGenerator,
[NotNull] string containerId,
[CanBeNull] string partitionKey,
[NotNull] IReadOnlyList<(string Name, object Value)> parameters,
[CanBeNull] string? partitionKey,
[NotNull] IReadOnlyList<(string Name, object? Value)> parameters,
[NotNull] string querySql,
bool logSensitiveData)
: base(eventDefinition, messageGenerator)
Expand All @@ -48,12 +50,12 @@ public CosmosQueryEventData(
/// <summary>
/// The key of the Cosmos partition that the query is using.
/// </summary>
public virtual string PartitionKey { get; }
public virtual string? PartitionKey { get; }

/// <summary>
/// Name/values for each parameter in the Cosmos Query.
/// </summary>
public virtual IReadOnlyList<(string Name, object Value)> Parameters { get; }
public virtual IReadOnlyList<(string Name, object? Value)> Parameters { get; }

/// <summary>
/// The SQL representing the query.
Expand Down
6 changes: 4 additions & 2 deletions src/EFCore.Cosmos/Diagnostics/CosmosReadItemEventData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using System.Diagnostics;
using JetBrains.Annotations;

#nullable enable

namespace Microsoft.EntityFrameworkCore.Diagnostics
{
/// <summary>
Expand All @@ -26,7 +28,7 @@ public CosmosReadItemEventData(
[NotNull] Func<EventDefinitionBase, EventData, string> messageGenerator,
[NotNull] string resourceId,
[NotNull] string containerId,
[CanBeNull] string partitionKey,
[CanBeNull] string? partitionKey,
bool logSensitiveData)
: base(eventDefinition, messageGenerator)
{
Expand All @@ -49,7 +51,7 @@ public CosmosReadItemEventData(
/// <summary>
/// The key of the Cosmos partition that the query is using.
/// </summary>
public virtual string PartitionKey { get; }
public virtual string? PartitionKey { get; }

/// <summary>
/// Indicates whether or not the application allows logging of sensitive data.
Expand Down
12 changes: 7 additions & 5 deletions src/EFCore.Cosmos/Diagnostics/Internal/CosmosLoggerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

#nullable enable

// ReSharper disable once CheckNamespace
namespace Microsoft.EntityFrameworkCore.Cosmos.Diagnostics.Internal
{
Expand All @@ -33,7 +35,7 @@ public static class CosmosLoggerExtensions
public static void ExecutingSqlQuery(
[NotNull] this IDiagnosticsLogger<DbLoggerCategory.Database.Command> diagnostics,
[NotNull] string containerId,
[CanBeNull] string partitionKey,
[CanBeNull] string? partitionKey,
[NotNull] CosmosSqlQuery cosmosSqlQuery)
{
var definition = CosmosResources.LogExecutingSqlQuery(diagnostics);
Expand Down Expand Up @@ -68,7 +70,7 @@ public static void ExecutingSqlQuery(

private static string ExecutingSqlQuery(EventDefinitionBase definition, EventData payload)
{
var d = (EventDefinition<string, string, string, string, string>)definition;
var d = (EventDefinition<string, string?, string, string, string>)definition;
var p = (CosmosQueryEventData)payload;
return d.GenerateMessage(
p.ContainerId,
Expand Down Expand Up @@ -114,12 +116,12 @@ public static void ExecutingReadItem(

private static string ExecutingReadItem(EventDefinitionBase definition, EventData payload)
{
var d = (EventDefinition<string, string, string>)definition;
var d = (EventDefinition<string, string, string?>)definition;
var p = (CosmosReadItemEventData)payload;
return d.GenerateMessage(p.LogSensitiveData ? p.ResourceId : "?", p.ContainerId, p.LogSensitiveData ? p.PartitionKey : "?");
}

private static string FormatParameters(IReadOnlyList<(string Name, object Value)> parameters, bool shouldLogParameterValues)
private static string FormatParameters(IReadOnlyList<(string Name, object? Value)> parameters, bool shouldLogParameterValues)
=> FormatParameters(parameters.Select(p => new SqlParameter(p.Name, p.Value)).ToList(), shouldLogParameterValues);

private static string FormatParameters(IReadOnlyList<SqlParameter> parameters, bool shouldLogParameterValues)
Expand All @@ -146,7 +148,7 @@ private static string FormatParameter(SqlParameter parameter, bool shouldLogPara
return builder.ToString();
}

private static void FormatParameterValue(StringBuilder builder, object parameterValue)
private static void FormatParameterValue(StringBuilder builder, object? parameterValue)
{
if (parameterValue == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

using Microsoft.EntityFrameworkCore.Diagnostics;

#nullable enable

namespace Microsoft.EntityFrameworkCore.Cosmos.Diagnostics.Internal
{
/// <summary>
Expand All @@ -19,14 +21,14 @@ public class CosmosLoggingDefinitions : LoggingDefinitions
/// 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 EventDefinitionBase LogExecutingSqlQuery;
public EventDefinitionBase? LogExecutingSqlQuery;

/// <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 EventDefinitionBase LogExecutingReadItem;
public EventDefinitionBase? LogExecutingReadItem;
}
}
Loading