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 93948e54ee9..414baf88fc4 100644 --- a/src/Avalonia.Base/Data/BindingValue.cs +++ b/src/Avalonia.Base/Data/BindingValue.cs @@ -247,7 +247,9 @@ public static BindingValue FromUntyped(object? value) 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 7ff0a8ceca8..add22805675 100644 --- a/src/Avalonia.Base/Data/Converters/MethodToCommandConverter.cs +++ b/src/Avalonia.Base/Data/Converters/MethodToCommandConverter.cs @@ -170,7 +170,7 @@ static Func CreateCanExecute(object target .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 157bebe02b0..bd53ec0100a 100644 --- a/src/Avalonia.Controls/Application.cs +++ b/src/Avalonia.Controls/Application.cs @@ -175,7 +175,7 @@ public IResourceDictionary Resources /// - /// - /// - 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 856bcd10791..50f130ab71b 100644 --- a/src/Avalonia.Controls/Primitives/Popup.cs +++ b/src/Avalonia.Controls/Primitives/Popup.cs @@ -91,8 +91,8 @@ public class Popup : Control, IVisualTreeHost, IPopupHostProvider 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 @@ public class Popup : Control, IVisualTreeHost, IPopupHostProvider private bool _isOpen; private bool _ignoreIsOpenChanged; private PopupOpenState? _openState; - private IInputElement _overlayInputPassThroughElement; + private IInputElement? _overlayInputPassThroughElement; private Action? _popupHostChangedHandler; /// @@ -308,7 +308,7 @@ public bool OverlayDismissEventPassThrough /// 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 0e35c610b2b..6a0d4e20230 100644 --- a/src/Avalonia.Controls/SplitView.cs +++ b/src/Avalonia.Controls/SplitView.cs @@ -129,14 +129,14 @@ Pseudo classes & combos /// /// 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 @@ public object Pane /// /// 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 eed71759d5d..410b3a2e47d 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 @@ public interface ICommandSource /// 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 1f3d6335a9b..9e8969c1647 100644 --- a/src/Avalonia.Styling/Styling/Setter.cs +++ b/src/Avalonia.Styling/Styling/Setter.cs @@ -101,7 +101,11 @@ void IAvaloniaPropertyVisitor.Visit( 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 @@ void IAvaloniaPropertyVisitor.Visit( 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 da845b0ecd7..28baaa65f83 100644 --- a/src/tools/MicroComGenerator/CSharpGen.Utils.cs +++ b/src/tools/MicroComGenerator/CSharpGen.Utils.cs @@ -40,7 +40,7 @@ string Format(CompilationUnitSyntax unit) 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 414baf88fc4..0e7bdfe0bfb 100644 --- a/src/Avalonia.Base/Data/BindingValue.cs +++ b/src/Avalonia.Base/Data/BindingValue.cs @@ -247,9 +247,7 @@ public static BindingValue FromUntyped(object? value) 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 9e8969c1647..168a8824994 100644 --- a/src/Avalonia.Styling/Styling/Setter.cs +++ b/src/Avalonia.Styling/Styling/Setter.cs @@ -101,11 +101,7 @@ void IAvaloniaPropertyVisitor.Visit( 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 @@ void IAvaloniaPropertyVisitor.Visit( 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 4b903d056c8..b874d149bfd 100644 --- a/src/Avalonia.Controls/Flyouts/FlyoutBase.cs +++ b/src/Avalonia.Controls/Flyouts/FlyoutBase.cs @@ -562,8 +562,12 @@ private bool CancelOpening() 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 dc4a1d88c1c..ebcd0646269 100644 --- a/src/Avalonia.Base/PropertyStore/ConstantValueEntry.cs +++ b/src/Avalonia.Base/PropertyStore/ConstantValueEntry.cs @@ -30,7 +30,7 @@ public ConstantValueEntry( 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 495f13e1a9a..151e5d1949d 100644 --- a/src/Avalonia.Base/ValueStore.cs +++ b/src/Avalonia.Base/ValueStore.cs @@ -192,7 +192,7 @@ public void ClearLocalValue(StyledPropertyBase property) _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 0e7bdfe0bfb..e8c84ffb214 100644 --- a/src/Avalonia.Base/Data/BindingValue.cs +++ b/src/Avalonia.Base/Data/BindingValue.cs @@ -247,7 +247,7 @@ public static BindingValue FromUntyped(object? value) 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 ebcd0646269..dc4a1d88c1c 100644 --- a/src/Avalonia.Base/PropertyStore/ConstantValueEntry.cs +++ b/src/Avalonia.Base/PropertyStore/ConstantValueEntry.cs @@ -30,7 +30,7 @@ public ConstantValueEntry( 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 151e5d1949d..495f13e1a9a 100644 --- a/src/Avalonia.Base/ValueStore.cs +++ b/src/Avalonia.Base/ValueStore.cs @@ -192,7 +192,7 @@ public void ClearLocalValue(StyledPropertyBase property) _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 e8c84ffb214..93948e54ee9 100644 --- a/src/Avalonia.Base/Data/BindingValue.cs +++ b/src/Avalonia.Base/Data/BindingValue.cs @@ -247,7 +247,7 @@ public static BindingValue FromUntyped(object? value) 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 59edb6278a3..9fbc2fc8b27 100644 --- a/src/Avalonia.Controls/TrayIcon.cs +++ b/src/Avalonia.Controls/TrayIcon.cs @@ -140,7 +140,7 @@ public ICommand? Command /// 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 93948e54ee9..e8c84ffb214 100644 --- a/src/Avalonia.Base/Data/BindingValue.cs +++ b/src/Avalonia.Base/Data/BindingValue.cs @@ -247,7 +247,7 @@ public static BindingValue FromUntyped(object? value) 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 bd53ec0100a..df55d9e2502 100644 --- a/src/Avalonia.Controls/Application.cs +++ b/src/Avalonia.Controls/Application.cs @@ -104,7 +104,7 @@ public static Application Current /// /// The application's focus manager. /// - public IFocusManager FocusManager + public IFocusManager? FocusManager { get; private set; @@ -116,7 +116,7 @@ public IFocusManager FocusManager /// /// 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 1c3055fed6b..bb0e342df6f 100644 --- a/src/Avalonia.Styling/Styling/PropertySetterInstance.cs +++ b/src/Avalonia.Styling/Styling/PropertySetterInstance.cs @@ -16,14 +16,14 @@ internal class PropertySetterInstance : SingleSubscriberObservableBase? _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 @@ public void Start(bool hasActivator) { 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 168a8824994..4c94ea02dd7 100644 --- a/src/Avalonia.Styling/Styling/Setter.cs +++ b/src/Avalonia.Styling/Styling/Setter.cs @@ -101,7 +101,7 @@ void IAvaloniaPropertyVisitor.Visit( data.result = new PropertySetterInstance( data.target, property, - (T)data.value!); + (T?)data.value); } }