From 82729a1bd92d35a2f7c4688c1a508024abbd9027 Mon Sep 17 00:00:00 2001 From: Giuseppe Lippolis Date: Mon, 18 Oct 2021 14:57:58 +0200 Subject: [PATCH 01/11] fixes: some null annotation --- src/Avalonia.Base/Data/BindingValue.cs | 2 ++ .../Data/Converters/MethodToCommandConverter.cs | 2 +- src/Avalonia.Controls/Application.cs | 2 +- src/Avalonia.Controls/Primitives/Popup.cs | 8 ++++---- src/Avalonia.Controls/SplitView.cs | 10 +++++----- src/Avalonia.Input/ICommandSource.cs | 6 +++--- src/Avalonia.Styling/Styling/Setter.cs | 8 ++++++++ src/tools/MicroComGenerator/CSharpGen.Utils.cs | 2 +- 8 files changed, 25 insertions(+), 15 deletions(-) diff --git a/src/Avalonia.Base/Data/BindingValue.cs b/src/Avalonia.Base/Data/BindingValue.cs index 93948e54ee..414baf88fc 100644 --- a/src/Avalonia.Base/Data/BindingValue.cs +++ b/src/Avalonia.Base/Data/BindingValue.cs @@ -247,7 +247,9 @@ namespace Avalonia.Data UnsetValueType _ => Unset, DoNothingType _ => DoNothing, BindingNotification n => n.ToBindingValue().Cast(), +#pragma warning disable CS8600 // Converting null literal or possible null value to non-nullable type. _ => new BindingValue((T)value) +#pragma warning restore CS8600 // Converting null literal or possible null value to non-nullable type. }; } diff --git a/src/Avalonia.Base/Data/Converters/MethodToCommandConverter.cs b/src/Avalonia.Base/Data/Converters/MethodToCommandConverter.cs index 7ff0a8ceca..add2280567 100644 --- a/src/Avalonia.Base/Data/Converters/MethodToCommandConverter.cs +++ b/src/Avalonia.Base/Data/Converters/MethodToCommandConverter.cs @@ -170,7 +170,7 @@ namespace Avalonia.Data.Converters .Compile(); } - private static Expression? ConvertTarget(object? target, MethodInfo method) => + private static Expression ConvertTarget(object target, MethodInfo method) => target is null ? null : Expression.Convert(Expression.Constant(target), method.DeclaringType); internal class WeakPropertyChangedProxy diff --git a/src/Avalonia.Controls/Application.cs b/src/Avalonia.Controls/Application.cs index 157bebe02b..bd53ec0100 100644 --- a/src/Avalonia.Controls/Application.cs +++ b/src/Avalonia.Controls/Application.cs @@ -175,7 +175,7 @@ namespace Avalonia /// - /// - /// - public IApplicationLifetime ApplicationLifetime { get; set; } + public IApplicationLifetime? ApplicationLifetime { get; set; } event Action> IGlobalStyles.GlobalStylesAdded { diff --git a/src/Avalonia.Controls/Primitives/Popup.cs b/src/Avalonia.Controls/Primitives/Popup.cs index 856bcd1079..50f130ab71 100644 --- a/src/Avalonia.Controls/Primitives/Popup.cs +++ b/src/Avalonia.Controls/Primitives/Popup.cs @@ -91,8 +91,8 @@ namespace Avalonia.Controls.Primitives public static readonly StyledProperty OverlayDismissEventPassThroughProperty = AvaloniaProperty.Register(nameof(OverlayDismissEventPassThrough)); - public static readonly DirectProperty OverlayInputPassThroughElementProperty = - AvaloniaProperty.RegisterDirect( + public static readonly DirectProperty OverlayInputPassThroughElementProperty = + AvaloniaProperty.RegisterDirect( nameof(OverlayInputPassThroughElement), o => o.OverlayInputPassThroughElement, (o, v) => o.OverlayInputPassThroughElement = v); @@ -136,7 +136,7 @@ namespace Avalonia.Controls.Primitives private bool _isOpen; private bool _ignoreIsOpenChanged; private PopupOpenState? _openState; - private IInputElement _overlayInputPassThroughElement; + private IInputElement? _overlayInputPassThroughElement; private Action? _popupHostChangedHandler; /// @@ -308,7 +308,7 @@ namespace Avalonia.Controls.Primitives /// Gets or sets an element that should receive pointer input events even when underneath /// the popup's overlay. /// - public IInputElement OverlayInputPassThroughElement + public IInputElement? OverlayInputPassThroughElement { get => _overlayInputPassThroughElement; set => SetAndRaise(OverlayInputPassThroughElementProperty, ref _overlayInputPassThroughElement, value); diff --git a/src/Avalonia.Controls/SplitView.cs b/src/Avalonia.Controls/SplitView.cs index 0e35c610b2..6a0d4e2023 100644 --- a/src/Avalonia.Controls/SplitView.cs +++ b/src/Avalonia.Controls/SplitView.cs @@ -129,14 +129,14 @@ namespace Avalonia.Controls /// /// Defines the property /// - public static readonly StyledProperty PaneProperty = - AvaloniaProperty.Register(nameof(Pane)); + public static readonly StyledProperty PaneProperty = + AvaloniaProperty.Register(nameof(Pane)); /// /// Defines the property. /// - public static readonly StyledProperty PaneTemplateProperty = - AvaloniaProperty.Register(nameof(PaneTemplate)); + public static readonly StyledProperty PaneTemplateProperty = + AvaloniaProperty.Register(nameof(PaneTemplate)); /// /// Defines the property @@ -267,7 +267,7 @@ namespace Avalonia.Controls /// /// Gets or sets the data template used to display the header content of the control. /// - public IDataTemplate? PaneTemplate + public IDataTemplate PaneTemplate { get => GetValue(PaneTemplateProperty); set => SetValue(PaneTemplateProperty, value); diff --git a/src/Avalonia.Input/ICommandSource.cs b/src/Avalonia.Input/ICommandSource.cs index eed71759d5..410b3a2e47 100644 --- a/src/Avalonia.Input/ICommandSource.cs +++ b/src/Avalonia.Input/ICommandSource.cs @@ -1,5 +1,5 @@ using System.Windows.Input; - +#nullable enable namespace Avalonia.Input { /// @@ -12,13 +12,13 @@ namespace Avalonia.Input /// Classes that implement this interface should enable or disable based on the command's CanExecute return value. /// The property may be implemented as read-write if desired. /// - ICommand Command { get; } + ICommand? Command { get; } /// /// The parameter that will be passed to the command when executing the command. /// The property may be implemented as read-write if desired. /// - object CommandParameter { get; } + object? CommandParameter { get; } /// diff --git a/src/Avalonia.Styling/Styling/Setter.cs b/src/Avalonia.Styling/Styling/Setter.cs index 1f3d6335a9..9e8969c164 100644 --- a/src/Avalonia.Styling/Styling/Setter.cs +++ b/src/Avalonia.Styling/Styling/Setter.cs @@ -101,7 +101,11 @@ namespace Avalonia.Styling data.result = new PropertySetterInstance( data.target, property, +#pragma warning disable CS8600 // Converting null literal or possible null value to non-nullable type. +#pragma warning disable CS8604 // Possible null reference argument. (T)data.value); +#pragma warning restore CS8604 // Possible null reference argument. +#pragma warning restore CS8600 // Converting null literal or possible null value to non-nullable type. } } @@ -128,7 +132,11 @@ namespace Avalonia.Styling data.result = new PropertySetterInstance( data.target, property, +#pragma warning disable CS8600 // Converting null literal or possible null value to non-nullable type. +#pragma warning disable CS8604 // Possible null reference argument. (T)data.value); +#pragma warning restore CS8604 // Possible null reference argument. +#pragma warning restore CS8600 // Converting null literal or possible null value to non-nullable type. } } diff --git a/src/tools/MicroComGenerator/CSharpGen.Utils.cs b/src/tools/MicroComGenerator/CSharpGen.Utils.cs index da845b0ecd..28baaa65f8 100644 --- a/src/tools/MicroComGenerator/CSharpGen.Utils.cs +++ b/src/tools/MicroComGenerator/CSharpGen.Utils.cs @@ -40,7 +40,7 @@ namespace MicroComGenerator SyntaxToken Semicolon() => Token(SyntaxKind.SemicolonToken); static VariableDeclarationSyntax DeclareVar(string type, string name, - ExpressionSyntax? initializer = null) + ExpressionSyntax initializer = null) => VariableDeclaration(ParseTypeName(type), SingletonSeparatedList(VariableDeclarator(name) .WithInitializer(initializer == null ? null : EqualsValueClause(initializer)))); From 0309dda390b0583b07d1298c6c256ebd06b3f473 Mon Sep 17 00:00:00 2001 From: Giuseppe Lippolis Date: Tue, 19 Oct 2021 10:02:54 +0200 Subject: [PATCH 02/11] fixes: Replace CS8600 warning suppression with null-forgiving operator --- src/Avalonia.Base/Data/BindingValue.cs | 4 +--- src/Avalonia.Styling/Styling/Setter.cs | 12 ++---------- 2 files changed, 3 insertions(+), 13 deletions(-) diff --git a/src/Avalonia.Base/Data/BindingValue.cs b/src/Avalonia.Base/Data/BindingValue.cs index 414baf88fc..0e7bdfe0bf 100644 --- a/src/Avalonia.Base/Data/BindingValue.cs +++ b/src/Avalonia.Base/Data/BindingValue.cs @@ -247,9 +247,7 @@ namespace Avalonia.Data UnsetValueType _ => Unset, DoNothingType _ => DoNothing, BindingNotification n => n.ToBindingValue().Cast(), -#pragma warning disable CS8600 // Converting null literal or possible null value to non-nullable type. - _ => new BindingValue((T)value) -#pragma warning restore CS8600 // Converting null literal or possible null value to non-nullable type. + _ => new BindingValue((T)value!) }; } diff --git a/src/Avalonia.Styling/Styling/Setter.cs b/src/Avalonia.Styling/Styling/Setter.cs index 9e8969c164..168a882499 100644 --- a/src/Avalonia.Styling/Styling/Setter.cs +++ b/src/Avalonia.Styling/Styling/Setter.cs @@ -101,11 +101,7 @@ namespace Avalonia.Styling data.result = new PropertySetterInstance( data.target, property, -#pragma warning disable CS8600 // Converting null literal or possible null value to non-nullable type. -#pragma warning disable CS8604 // Possible null reference argument. - (T)data.value); -#pragma warning restore CS8604 // Possible null reference argument. -#pragma warning restore CS8600 // Converting null literal or possible null value to non-nullable type. + (T)data.value!); } } @@ -132,11 +128,7 @@ namespace Avalonia.Styling data.result = new PropertySetterInstance( data.target, property, -#pragma warning disable CS8600 // Converting null literal or possible null value to non-nullable type. -#pragma warning disable CS8604 // Possible null reference argument. - (T)data.value); -#pragma warning restore CS8604 // Possible null reference argument. -#pragma warning restore CS8600 // Converting null literal or possible null value to non-nullable type. + (T)data.value!); } } From a828e5b4f5664a4aaf50280a7cf423d59e248da7 Mon Sep 17 00:00:00 2001 From: Giuseppe Lippolis Date: Tue, 19 Oct 2021 10:12:36 +0200 Subject: [PATCH 03/11] fixes: MenuFlyout Possible null reference argument for parameter --- src/Avalonia.Controls/Flyouts/FlyoutBase.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Avalonia.Controls/Flyouts/FlyoutBase.cs b/src/Avalonia.Controls/Flyouts/FlyoutBase.cs index 4b903d056c..b874d149bf 100644 --- a/src/Avalonia.Controls/Flyouts/FlyoutBase.cs +++ b/src/Avalonia.Controls/Flyouts/FlyoutBase.cs @@ -562,8 +562,12 @@ namespace Avalonia.Controls.Primitives return eventArgs.Cancel; } - internal static void SetPresenterClasses(IControl presenter, Classes classes) + internal static void SetPresenterClasses(IControl? presenter, Classes classes) { + if(presenter is null) + { + return; + } //Remove any classes no longer in use, ignoring pseudo classes for (int i = presenter.Classes.Count - 1; i >= 0; i--) { From 895fd718d9f88a68b258cff12a545f642549f63c Mon Sep 17 00:00:00 2001 From: Giuseppe Lippolis Date: Tue, 19 Oct 2021 10:42:03 +0200 Subject: [PATCH 04/11] fixes: ConstantValueEntry Possible null reference argument for parameter --- src/Avalonia.Base/PropertyStore/ConstantValueEntry.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Avalonia.Base/PropertyStore/ConstantValueEntry.cs b/src/Avalonia.Base/PropertyStore/ConstantValueEntry.cs index dc4a1d88c1..ebcd064626 100644 --- a/src/Avalonia.Base/PropertyStore/ConstantValueEntry.cs +++ b/src/Avalonia.Base/PropertyStore/ConstantValueEntry.cs @@ -30,7 +30,7 @@ namespace Avalonia.PropertyStore IValueSink sink) { Property = property; - _value = value; + _value = value!; Priority = priority; _sink = sink; } From 22644028ca5c52843f87f45f42a2a44ed2eab7fe Mon Sep 17 00:00:00 2001 From: Giuseppe Lippolis Date: Tue, 19 Oct 2021 10:44:22 +0200 Subject: [PATCH 05/11] fixes: ValueStore Possible null reference argument for parameter --- src/Avalonia.Base/ValueStore.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Avalonia.Base/ValueStore.cs b/src/Avalonia.Base/ValueStore.cs index 495f13e1a9..151e5d1949 100644 --- a/src/Avalonia.Base/ValueStore.cs +++ b/src/Avalonia.Base/ValueStore.cs @@ -192,7 +192,7 @@ namespace Avalonia _values.SetValue(property, sentinel); } - NotifyValueChanged(property, old, default, BindingPriority.Unset); + NotifyValueChanged(property, old!, default!, BindingPriority.Unset); } } } From 97a920db72a16a89aae24e2cb4d20b53a77d2883 Mon Sep 17 00:00:00 2001 From: Giuseppe Lippolis Date: Sat, 23 Oct 2021 11:45:07 +0200 Subject: [PATCH 06/11] Review suggestions apply --- src/Avalonia.Base/Data/BindingValue.cs | 2 +- src/Avalonia.Base/PropertyStore/ConstantValueEntry.cs | 2 +- src/Avalonia.Base/ValueStore.cs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Avalonia.Base/Data/BindingValue.cs b/src/Avalonia.Base/Data/BindingValue.cs index 0e7bdfe0bf..e8c84ffb21 100644 --- a/src/Avalonia.Base/Data/BindingValue.cs +++ b/src/Avalonia.Base/Data/BindingValue.cs @@ -247,7 +247,7 @@ namespace Avalonia.Data UnsetValueType _ => Unset, DoNothingType _ => DoNothing, BindingNotification n => n.ToBindingValue().Cast(), - _ => new BindingValue((T)value!) + _ => new BindingValue((T?)value) }; } diff --git a/src/Avalonia.Base/PropertyStore/ConstantValueEntry.cs b/src/Avalonia.Base/PropertyStore/ConstantValueEntry.cs index ebcd064626..dc4a1d88c1 100644 --- a/src/Avalonia.Base/PropertyStore/ConstantValueEntry.cs +++ b/src/Avalonia.Base/PropertyStore/ConstantValueEntry.cs @@ -30,7 +30,7 @@ namespace Avalonia.PropertyStore IValueSink sink) { Property = property; - _value = value!; + _value = value; Priority = priority; _sink = sink; } diff --git a/src/Avalonia.Base/ValueStore.cs b/src/Avalonia.Base/ValueStore.cs index 151e5d1949..495f13e1a9 100644 --- a/src/Avalonia.Base/ValueStore.cs +++ b/src/Avalonia.Base/ValueStore.cs @@ -192,7 +192,7 @@ namespace Avalonia _values.SetValue(property, sentinel); } - NotifyValueChanged(property, old!, default!, BindingPriority.Unset); + NotifyValueChanged(property, old, default, BindingPriority.Unset); } } } From 44225c66146cdfb63623052a19cc87f3639b29df Mon Sep 17 00:00:00 2001 From: Giuseppe Lippolis Date: Sat, 23 Oct 2021 12:31:46 +0200 Subject: [PATCH 07/11] fixes: Revert the fix, because the fix throw error CS8627 on Linux and macOS --- src/Avalonia.Base/Data/BindingValue.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Avalonia.Base/Data/BindingValue.cs b/src/Avalonia.Base/Data/BindingValue.cs index e8c84ffb21..93948e54ee 100644 --- a/src/Avalonia.Base/Data/BindingValue.cs +++ b/src/Avalonia.Base/Data/BindingValue.cs @@ -247,7 +247,7 @@ namespace Avalonia.Data UnsetValueType _ => Unset, DoNothingType _ => DoNothing, BindingNotification n => n.ToBindingValue().Cast(), - _ => new BindingValue((T?)value) + _ => new BindingValue((T)value) }; } From 67e5962680c831fa6046ded59c66125cef3675e7 Mon Sep 17 00:00:00 2001 From: Giuseppe Lippolis Date: Thu, 28 Oct 2021 10:13:44 +0200 Subject: [PATCH 08/11] fix(TrayIcon): CommandParameter alow null --- src/Avalonia.Controls/TrayIcon.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Avalonia.Controls/TrayIcon.cs b/src/Avalonia.Controls/TrayIcon.cs index 59edb6278a..9fbc2fc8b2 100644 --- a/src/Avalonia.Controls/TrayIcon.cs +++ b/src/Avalonia.Controls/TrayIcon.cs @@ -140,7 +140,7 @@ namespace Avalonia.Controls /// Gets or sets the parameter to pass to the property of a /// . /// - public object CommandParameter + public object? CommandParameter { get { return GetValue(CommandParameterProperty); } set { SetValue(CommandParameterProperty, value); } From 94eeb7d97c33779f5f79985ab81675940993153e Mon Sep 17 00:00:00 2001 From: Giuseppe Lippolis Date: Thu, 28 Oct 2021 10:14:07 +0200 Subject: [PATCH 09/11] restore fix --- src/Avalonia.Base/Data/BindingValue.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Avalonia.Base/Data/BindingValue.cs b/src/Avalonia.Base/Data/BindingValue.cs index 93948e54ee..e8c84ffb21 100644 --- a/src/Avalonia.Base/Data/BindingValue.cs +++ b/src/Avalonia.Base/Data/BindingValue.cs @@ -247,7 +247,7 @@ namespace Avalonia.Data UnsetValueType _ => Unset, DoNothingType _ => DoNothing, BindingNotification n => n.ToBindingValue().Cast(), - _ => new BindingValue((T)value) + _ => new BindingValue((T?)value) }; } From 0624a1d45896237b24c91fda194364a4b66a10f5 Mon Sep 17 00:00:00 2001 From: Giuseppe Lippolis Date: Mon, 15 Nov 2021 18:25:42 +0100 Subject: [PATCH 10/11] fixes: Mark FocusManager and InputManager as nullable --- src/Avalonia.Controls/Application.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Avalonia.Controls/Application.cs b/src/Avalonia.Controls/Application.cs index bd53ec0100..df55d9e250 100644 --- a/src/Avalonia.Controls/Application.cs +++ b/src/Avalonia.Controls/Application.cs @@ -104,7 +104,7 @@ namespace Avalonia /// /// The application's focus manager. /// - public IFocusManager FocusManager + public IFocusManager? FocusManager { get; private set; @@ -116,7 +116,7 @@ namespace Avalonia /// /// The application's input manager. /// - public InputManager InputManager + public InputManager? InputManager { get; private set; From 72fd5f01056b2c74d9b3b181e3f8ad6f6de99696 Mon Sep 17 00:00:00 2001 From: Giuseppe Lippolis Date: Mon, 22 Nov 2021 15:46:18 +0100 Subject: [PATCH 11/11] fixes: PropertySetterInstance --- src/Avalonia.Styling/Styling/PropertySetterInstance.cs | 6 +++--- src/Avalonia.Styling/Styling/Setter.cs | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Avalonia.Styling/Styling/PropertySetterInstance.cs b/src/Avalonia.Styling/Styling/PropertySetterInstance.cs index 1c3055fed6..bb0e342df6 100644 --- a/src/Avalonia.Styling/Styling/PropertySetterInstance.cs +++ b/src/Avalonia.Styling/Styling/PropertySetterInstance.cs @@ -16,14 +16,14 @@ namespace Avalonia.Styling private readonly IStyleable _target; private readonly StyledPropertyBase? _styledProperty; private readonly DirectPropertyBase? _directProperty; - private readonly T _value; + private readonly T? _value; private IDisposable? _subscription; private bool _isActive; public PropertySetterInstance( IStyleable target, StyledPropertyBase property, - T value) + T? value) { _target = target; _styledProperty = property; @@ -57,7 +57,7 @@ namespace Avalonia.Styling { if (_styledProperty is object) { - _subscription = _target.SetValue(_styledProperty, _value, BindingPriority.Style); + _subscription = _target.SetValue(_styledProperty!, _value, BindingPriority.Style); } else { diff --git a/src/Avalonia.Styling/Styling/Setter.cs b/src/Avalonia.Styling/Styling/Setter.cs index 168a882499..4c94ea02dd 100644 --- a/src/Avalonia.Styling/Styling/Setter.cs +++ b/src/Avalonia.Styling/Styling/Setter.cs @@ -101,7 +101,7 @@ namespace Avalonia.Styling data.result = new PropertySetterInstance( data.target, property, - (T)data.value!); + (T?)data.value); } }