Skip to content

Commit b848377

Browse files
authored
NRT annotations (#23938)
* Infrastructure * Diagnostics * Proxies * Abstractions * Analyzers Part of #19007
1 parent 9a8c6c7 commit b848377

File tree

253 files changed

+1299
-863
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

253 files changed

+1299
-863
lines changed

src/EFCore.Abstractions/ChangeTracking/Internal/ObservableBackedBindingList.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ namespace Microsoft.EntityFrameworkCore.ChangeTracking.Internal
1818
public class ObservableBackedBindingList<T> : SortableBindingList<T>
1919
{
2020
private bool _addingNewInstance;
21-
private T _addNewInstance;
22-
private T _cancelNewInstance;
21+
private T? _addNewInstance;
22+
private T? _cancelNewInstance;
2323

2424
private readonly ICollection<T> _observableCollection;
2525
private bool _inCollectionChanged;
@@ -102,7 +102,7 @@ public override void EndNew(int itemIndex)
102102
&& itemIndex < Count
103103
&& Equals(base[itemIndex], _addNewInstance))
104104
{
105-
AddToObservableCollection(_addNewInstance);
105+
AddToObservableCollection(_addNewInstance!);
106106
_addNewInstance = default;
107107
_addingNewInstance = false;
108108
}
@@ -180,7 +180,7 @@ protected override void SetItem(int index, T item)
180180
}
181181
}
182182

183-
private void ObservableCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
183+
private void ObservableCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
184184
{
185185
// Don't try to change the binding list if the original change came from the binding list
186186
// and the ObservableCollection is just being changed to match it.
@@ -202,7 +202,7 @@ private void ObservableCollectionChanged(object sender, NotifyCollectionChangedE
202202
if (e.Action == NotifyCollectionChangedAction.Remove
203203
|| e.Action == NotifyCollectionChangedAction.Replace)
204204
{
205-
foreach (T entity in e.OldItems)
205+
foreach (T entity in e.OldItems!)
206206
{
207207
Remove(entity);
208208
}
@@ -211,7 +211,7 @@ private void ObservableCollectionChanged(object sender, NotifyCollectionChangedE
211211
if (e.Action == NotifyCollectionChangedAction.Add
212212
|| e.Action == NotifyCollectionChangedAction.Replace)
213213
{
214-
foreach (T entity in e.NewItems)
214+
foreach (T entity in e.NewItems!)
215215
{
216216
Add(entity);
217217
}

src/EFCore.Abstractions/ChangeTracking/Internal/SortableBindingList.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class SortableBindingList<T> : BindingList<T>
2020
{
2121
private bool _isSorted;
2222
private ListSortDirection _sortDirection;
23-
private PropertyDescriptor _sortProperty;
23+
private PropertyDescriptor? _sortProperty;
2424

2525
/// <summary>
2626
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
@@ -87,7 +87,7 @@ protected override ListSortDirection SortDirectionCore
8787
/// any release. You should only use it directly in your code with extreme caution and knowing that
8888
/// doing so can result in application failures when updating to a new Entity Framework Core release.
8989
/// </summary>
90-
protected override PropertyDescriptor SortPropertyCore
90+
protected override PropertyDescriptor? SortPropertyCore
9191
=> _sortProperty;
9292

9393
/// <summary>
@@ -115,12 +115,22 @@ public PropertyComparer(PropertyDescriptor prop, ListSortDirection direction)
115115
_prop = prop;
116116
_direction = direction;
117117

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

122-
public override int Compare(T left, T right)
122+
public override int Compare(T? left, T? right)
123123
{
124+
if (left is null)
125+
{
126+
return right is null ? 0 : -1;
127+
}
128+
129+
if (right is null)
130+
{
131+
return 1;
132+
}
133+
124134
var leftValue = _prop.GetValue(left);
125135
var rightValue = _prop.GetValue(right);
126136

src/EFCore.Abstractions/ChangeTracking/ObservableCollectionListSource.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ namespace Microsoft.EntityFrameworkCore.ChangeTracking
2626
public class ObservableCollectionListSource<T> : ObservableCollection<T>, IListSource
2727
where T : class
2828
{
29-
private IBindingList _bindingList;
29+
private IBindingList? _bindingList;
3030

3131
/// <summary>
3232
/// Initializes a new instance of the <see cref="ObservableCollectionListSource{T}" /> class.
@@ -70,6 +70,6 @@ bool IListSource.ContainsListCollection
7070
/// An <see cref="IBindingList" /> in sync with the ObservableCollection.
7171
/// </returns>
7272
IList IListSource.GetList()
73-
=> _bindingList ?? (_bindingList = this.ToBindingList());
73+
=> _bindingList ??= this.ToBindingList();
7474
}
7575
}

src/EFCore.Abstractions/DbFunctionAttribute.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
using JetBrains.Annotations;
66
using Microsoft.EntityFrameworkCore.Utilities;
77

8-
#nullable enable
9-
108
namespace Microsoft.EntityFrameworkCore
119
{
1210
/// <summary>

src/EFCore.Abstractions/EFCore.Abstractions.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<AssemblyName>Microsoft.EntityFrameworkCore.Abstractions</AssemblyName>
88
<RootNamespace>Microsoft.EntityFrameworkCore</RootNamespace>
99
<GenerateDocumentationFile>true</GenerateDocumentationFile>
10+
<Nullable>enable</Nullable>
1011
</PropertyGroup>
1112

1213
<ItemGroup>

src/EFCore.Abstractions/EntityTypeConfigurationAttribute.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33
using System;
4+
using Microsoft.EntityFrameworkCore.Utilities;
45

56
namespace Microsoft.EntityFrameworkCore
67
{
@@ -16,6 +17,8 @@ public sealed class EntityTypeConfigurationAttribute : Attribute
1617
/// <param name="entityConfigurationType"> The IEntityTypeConfiguration&lt;&gt; type to use. </param>
1718
public EntityTypeConfigurationAttribute(Type entityConfigurationType)
1819
{
20+
Check.NotNull(entityConfigurationType, nameof(entityConfigurationType));
21+
1922
EntityTypeConfigurationType = entityConfigurationType;
2023
}
2124

src/EFCore.Abstractions/IndexAttribute.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Linq;
77
using JetBrains.Annotations;
88
using Microsoft.EntityFrameworkCore.Utilities;
9+
using CA = System.Diagnostics.CodeAnalysis;
910

1011
namespace Microsoft.EntityFrameworkCore
1112
{
@@ -16,13 +17,13 @@ namespace Microsoft.EntityFrameworkCore
1617
public sealed class IndexAttribute : Attribute
1718
{
1819
private bool? _isUnique;
19-
private string _name;
20+
private string? _name;
2021

2122
/// <summary>
2223
/// Initializes a new instance of the <see cref="IndexAttribute" /> class.
2324
/// </summary>
2425
/// <param name="propertyNames"> The properties which constitute the index, in order (there must be at least one). </param>
25-
public IndexAttribute([CanBeNull] params string[] propertyNames)
26+
public IndexAttribute([NotNull] params string[] propertyNames)
2627
{
2728
Check.NotEmpty(propertyNames, nameof(propertyNames));
2829
Check.HasNoEmptyElements(propertyNames, nameof(propertyNames));
@@ -38,7 +39,8 @@ public IndexAttribute([CanBeNull] params string[] propertyNames)
3839
/// <summary>
3940
/// The name of the index.
4041
/// </summary>
41-
public string Name
42+
[CA.DisallowNull]
43+
public string? Name
4244
{
4345
get => _name;
4446
[param: NotNull] set => _name = Check.NotNull(value, nameof(value));

src/EFCore.Abstractions/Infrastructure/ILazyLoader.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ void SetLoaded(
4040
/// </summary>
4141
/// <param name="entity"> The entity on which the navigation property is located. </param>
4242
/// <param name="navigationName"> The navigation property name. </param>
43-
void Load([NotNull] object entity, [NotNull] [CallerMemberName] string navigationName = null);
43+
void Load([NotNull] object entity, [NotNull] [CallerMemberName] string navigationName = "");
4444

4545
/// <summary>
4646
/// Loads a navigation property if it has not already been loaded.
@@ -55,6 +55,6 @@ Task LoadAsync(
5555
#pragma warning restore CA1068 // CancellationToken parameters must come last
5656
[NotNull] object entity,
5757
CancellationToken cancellationToken = default,
58-
[NotNull] [CallerMemberName] string navigationName = null);
58+
[NotNull] [CallerMemberName] string navigationName = "");
5959
}
6060
}

src/EFCore.Abstractions/Infrastructure/LazyLoaderExtensions.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,13 @@ public static class LazyLoaderExtensions
2323
/// <returns>
2424
/// The loaded navigation property value, or the navigation property value unchanged if the loader is <see langword="null" />.
2525
/// </returns>
26-
public static TRelated Load<TRelated>(
27-
[CanBeNull] this ILazyLoader loader,
26+
public static TRelated? Load<TRelated>(
27+
[CanBeNull] this ILazyLoader? loader,
2828
[NotNull] object entity,
29-
[CanBeNull] ref TRelated navigationField,
30-
// ReSharper disable once AssignNullToNotNullAttribute
31-
[NotNull] [CallerMemberName] string navigationName = null)
29+
[CanBeNull] ref TRelated? navigationField,
30+
[NotNull] [CallerMemberName] string navigationName = "")
3231
where TRelated : class
3332
{
34-
// ReSharper disable once AssignNullToNotNullAttribute
3533
loader?.Load(entity, navigationName);
3634

3735
return navigationField;

src/EFCore.Abstractions/Query/NotParameterizedAttribute.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
using System;
55

6-
#nullable enable
7-
86
namespace Microsoft.EntityFrameworkCore.Query
97
{
108
/// <summary>

0 commit comments

Comments
 (0)