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

Enable nullable warnings as errors on projects which enabled Nullable Reference Types #7172

Merged
merged 3 commits into from
Dec 15, 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
11 changes: 11 additions & 0 deletions build/NullableEnable.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!--
The Nullable annotations on netstandard2.0 are incomplete and incorrect in places. Ignore
nullable warnings on netstandard2.0 and make them errors on later target frameworks.
-->
<PropertyGroup>
<Nullable>enable</Nullable>
<WarningsAsErrors Condition="'$(TargetFramework)' != 'netstandard2.0'">$(WarningsAsErrors);nullable</WarningsAsErrors>
<NoWarn Condition="'$(TargetFramework)' == 'netstandard2.0'">$(NoWarn);nullable</NoWarn>
Comment on lines +8 to +9
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am curious why do we need NoWarn here? I think WarningsAsErrors wouldn't simply work for projects with netstandard2.0 tfm. Also what $(WarningsAsErrors); does here? Do we need it here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WarningsAsErrors makes all nullable warnings as errors. Enabled for net6.
NoWarn makes all nullable warning as nothing. Enabled for netstandard.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also what $(WarningsAsErrors); does here

This causes the setting listed here to be appended to the existing WarningsAsErrors value, so we don't overwrite any values already set for this property.

</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion src/Avalonia.Diagnostics/Avalonia.Diagnostics.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
<TargetFrameworks>netstandard2.0;net6.0</TargetFrameworks>
<RootNamespace>Avalonia</RootNamespace>
<PackageId>Avalonia.Diagnostics</PackageId>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<Compile Update="**\*.xaml.cs">
Expand Down Expand Up @@ -34,4 +33,5 @@
<Import Project="..\..\build\Rx.props" />
<Import Project="..\..\build\BuildTargets.targets" />
<Import Project="..\..\build\ApiDiff.props" />
<Import Project="..\..\build\NullableEnable.props" />
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public AvaloniaPropertyViewModel(AvaloniaObject o, AvaloniaProperty property)

public override System.Type Type => _type;

public override string Value
public override string? Value
{
get => ConvertToString(_value);
set
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public ClrPropertyViewModel(object o, PropertyInfo property)

public override System.Type Type => _type;

public override string Value
public override string? Value
{
get => ConvertToString(_value);
set
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ public void ApplyParentProperty()
}
}

protected void NavigateToProperty(object o, string entityName)
protected void NavigateToProperty(object o, string? entityName)
{
var oldSelectedEntity = SelectedEntity;
if (oldSelectedEntity is IAvaloniaObject ao1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ internal abstract class PropertyViewModel : ViewModelBase
public abstract string Group { get; }
public abstract Type Type { get; }
public abstract Type? DeclaringType { get; }
public abstract string Value { get; set; }
public abstract string? Value { get; set; }
public abstract string Priority { get; }
public abstract bool? IsAttached { get; }
public abstract void Update();

protected static string ConvertToString(object? value)
protected static string? ConvertToString(object? value)
{
if (value is null)
{
Expand Down Expand Up @@ -59,8 +59,13 @@ protected static string ConvertToString(object? value)
throw new InvalidCastException("Unable to convert value.");
}

protected static object? ConvertFromString(string s, Type targetType)
protected static object? ConvertFromString(string? s, Type targetType)
{
if (s is null)
{
return null;
}

var converter = TypeDescriptor.GetConverter(targetType);

if (converter.CanConvertFrom(typeof(string)))
Expand Down
6 changes: 3 additions & 3 deletions src/Avalonia.Diagnostics/Diagnostics/Views/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ namespace Avalonia.Diagnostics.Views
{
internal class MainWindow : Window, IStyleHost
{
private readonly IDisposable _keySubscription;
private readonly IDisposable? _keySubscription;
private readonly Dictionary<Popup, IDisposable> _frozenPopupStates;
private TopLevel? _root;

public MainWindow()
{
InitializeComponent();

_keySubscription = InputManager.Instance.Process
_keySubscription = InputManager.Instance?.Process
.OfType<RawKeyEventArgs>()
.Where(x => x.Type == RawKeyEventType.KeyDown)
.Subscribe(RawKeyDown);
Expand Down Expand Up @@ -82,7 +82,7 @@ public TopLevel? Root
protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);
_keySubscription.Dispose();
_keySubscription?.Dispose();

foreach (var state in _frozenPopupStates)
{
Expand Down
10 changes: 5 additions & 5 deletions src/Avalonia.Input/AccessKeyHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public void Unregister(IInputElement element)
/// </summary>
/// <param name="sender">The event sender.</param>
/// <param name="e">The event args.</param>
protected virtual void OnPreviewKeyDown(object sender, KeyEventArgs e)
protected virtual void OnPreviewKeyDown(object? sender, KeyEventArgs e)
{
if (e.Key == Key.LeftAlt || e.Key == Key.RightAlt)
{
Expand Down Expand Up @@ -172,7 +172,7 @@ protected virtual void OnPreviewKeyDown(object sender, KeyEventArgs e)
/// </summary>
/// <param name="sender">The event sender.</param>
/// <param name="e">The event args.</param>
protected virtual void OnKeyDown(object sender, KeyEventArgs e)
protected virtual void OnKeyDown(object? sender, KeyEventArgs e)
{
bool menuIsOpen = MainMenu?.IsOpen == true;

Expand Down Expand Up @@ -207,7 +207,7 @@ protected virtual void OnKeyDown(object sender, KeyEventArgs e)
/// </summary>
/// <param name="sender">The event sender.</param>
/// <param name="e">The event args.</param>
protected virtual void OnPreviewKeyUp(object sender, KeyEventArgs e)
protected virtual void OnPreviewKeyUp(object? sender, KeyEventArgs e)
{
switch (e.Key)
{
Expand All @@ -234,7 +234,7 @@ protected virtual void OnPreviewKeyUp(object sender, KeyEventArgs e)
/// </summary>
/// <param name="sender">The event sender.</param>
/// <param name="e">The event args.</param>
protected virtual void OnPreviewPointerPressed(object sender, PointerEventArgs e)
protected virtual void OnPreviewPointerPressed(object? sender, PointerEventArgs e)
{
if (_showingAccessKeys)
{
Expand All @@ -251,7 +251,7 @@ private void CloseMenu()
_owner!.ShowAccessKeys = _showingAccessKeys = false;
}

private void MainMenuClosed(object sender, EventArgs e)
private void MainMenuClosed(object? sender, EventArgs e)
{
_owner!.ShowAccessKeys = false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Avalonia.Input/Avalonia.Input.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;net6.0</TargetFrameworks>
<Nullable>Enable</Nullable>
</PropertyGroup>
<ItemGroup>
<Compile Include="..\Avalonia.Base\Metadata\NullableAttributes.cs" Link="NullableAttributes.cs" />
Expand All @@ -15,4 +14,5 @@
</ItemGroup>
<Import Project="..\..\build\Rx.props" />
<Import Project="..\..\build\ApiDiff.props" />
<Import Project="..\..\build\NullableEnable.props" />
</Project>
5 changes: 4 additions & 1 deletion src/Avalonia.Input/FocusManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,11 @@ private static IEnumerable<IFocusScope> GetFocusScopeAncestors(IInputElement con
/// </summary>
/// <param name="sender">The event sender.</param>
/// <param name="e">The event args.</param>
private static void OnPreviewPointerPressed(object sender, RoutedEventArgs e)
private static void OnPreviewPointerPressed(object? sender, RoutedEventArgs e)
{
if (sender is null)
return;

var ev = (PointerPressedEventArgs)e;
var visual = (IVisual)sender;

Expand Down
2 changes: 1 addition & 1 deletion src/Avalonia.Input/KeyboardNavigationHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public void Move(
/// </summary>
/// <param name="sender">The event sender.</param>
/// <param name="e">The event args.</param>
protected virtual void OnKeyDown(object sender, KeyEventArgs e)
protected virtual void OnKeyDown(object? sender, KeyEventArgs e)
{
var current = FocusManager.Instance.Current;

Expand Down
13 changes: 7 additions & 6 deletions src/Avalonia.Input/MouseDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -298,10 +298,10 @@ private bool MouseUp(IMouseDevice device, ulong timestamp, IInputRoot root, Poin
root = root ?? throw new ArgumentNullException(nameof(root));

var hit = HitTest(root, p);
var source = GetSource(hit);

if (hit != null)
if (source is not null)
{
var source = GetSource(hit);
var e = new PointerReleasedEventArgs(source, _pointer, root, p, timestamp, props, inputModifiers,
_lastMouseDownButton);

Expand All @@ -321,10 +321,10 @@ private bool MouseWheel(IMouseDevice device, ulong timestamp, IInputRoot root, P
root = root ?? throw new ArgumentNullException(nameof(root));

var hit = HitTest(root, p);
var source = GetSource(hit);

if (hit != null)
if (source is not null)
{
var source = GetSource(hit);
var e = new PointerWheelEventArgs(source, _pointer, root, p, timestamp, props, inputModifiers, delta);

source?.RaiseEvent(e);
Expand All @@ -334,9 +334,10 @@ private bool MouseWheel(IMouseDevice device, ulong timestamp, IInputRoot root, P
return false;
}

private IInteractive GetSource(IVisual hit)
private IInteractive? GetSource(IVisual? hit)
{
hit = hit ?? throw new ArgumentNullException(nameof(hit));
if (hit is null)
return null;

return _pointer.Captured ??
(hit as IInteractive) ??
Expand Down
2 changes: 1 addition & 1 deletion src/Avalonia.Input/Pointer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ IInputElement GetNextCapture(IVisual parent)
return parent as IInputElement ?? parent.FindAncestorOfType<IInputElement>();
}

private void OnCaptureDetached(object sender, VisualTreeAttachmentEventArgs e)
private void OnCaptureDetached(object? sender, VisualTreeAttachmentEventArgs e)
{
Capture(GetNextCapture(e.Parent));
}
Expand Down
5 changes: 2 additions & 3 deletions src/Avalonia.Input/TextInput/InputMethodManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ internal class TextInputMethodManager
private ITextInputMethodImpl? _im;
private IInputElement? _focusedElement;
private ITextInputMethodClient? _client;
private IDisposable? _subscribeDisposable;
private readonly TransformTrackingHelper _transformTracker = new TransformTrackingHelper();

public TextInputMethodManager()
Expand Down Expand Up @@ -64,7 +63,7 @@ private void OnIsInputMethodEnabledChanged(AvaloniaPropertyChangedEventArgs<bool
}
}

private void OnTextViewVisualChanged(object sender, EventArgs e)
private void OnTextViewVisualChanged(object? sender, EventArgs e)
=> _transformTracker.SetVisual(_client?.TextViewVisual);

private void UpdateCursorRect()
Expand All @@ -79,7 +78,7 @@ private void UpdateCursorRect()
_im.SetCursorRect(_client.CursorRectangle.TransformToAABB(transform.Value));
}

private void OnCursorRectangleChanged(object sender, EventArgs e)
private void OnCursorRectangleChanged(object? sender, EventArgs e)
{
if (sender == _client)
UpdateCursorRect();
Expand Down
6 changes: 3 additions & 3 deletions src/Avalonia.Input/TextInput/TransformTrackingHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ void UpdateMatrix()
}
}

private void OnAttachedToVisualTree(object sender, VisualTreeAttachmentEventArgs visualTreeAttachmentEventArgs)
private void OnAttachedToVisualTree(object? sender, VisualTreeAttachmentEventArgs visualTreeAttachmentEventArgs)
{
SubscribeToParents();
UpdateMatrix();
Expand All @@ -94,13 +94,13 @@ private void EnqueueForUpdate()
Dispatcher.UIThread.Post(UpdateMatrix, DispatcherPriority.Render);
}

private void PropertyChangedHandler(object sender, AvaloniaPropertyChangedEventArgs e)
private void PropertyChangedHandler(object? sender, AvaloniaPropertyChangedEventArgs e)
{
if (e.IsEffectiveValueChange && e.Property == Visual.BoundsProperty)
EnqueueForUpdate();
}

private void OnDetachedFromVisualTree(object sender, VisualTreeAttachmentEventArgs visualTreeAttachmentEventArgs)
private void OnDetachedFromVisualTree(object? sender, VisualTreeAttachmentEventArgs visualTreeAttachmentEventArgs)
{
UnsubscribeFromParents();
UpdateMatrix();
Expand Down
3 changes: 1 addition & 2 deletions src/Avalonia.Interactivity/Avalonia.Interactivity.csproj
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;net6.0</TargetFrameworks>
<Nullable>Enable</Nullable>
<WarningsAsErrors>CS8600;CS8602;CS8603</WarningsAsErrors>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Avalonia.Animation\Avalonia.Animation.csproj" />
Expand All @@ -12,4 +10,5 @@
</ItemGroup>
<Import Project="..\..\build\Rx.props" />
<Import Project="..\..\build\ApiDiff.props" />
<Import Project="..\..\build\NullableEnable.props" />
</Project>
4 changes: 2 additions & 2 deletions src/Avalonia.Interactivity/RoutedEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public IDisposable AddClassHandler<TTarget>(
bool handledEventsToo = false)
where TTarget : class, IInteractive
{
void Adapter(object sender, RoutedEventArgs e)
void Adapter(object? sender, RoutedEventArgs e)
{
if (sender is TTarget target && e is TEventArgs args)
{
Expand All @@ -136,7 +136,7 @@ public IDisposable AddClassHandler<TTarget>(
RoutingStrategies routes = RoutingStrategies.Direct | RoutingStrategies.Bubble,
bool handledEventsToo = false) where TTarget : class, IInteractive
{
void Adapter(object sender, RoutedEventArgs e)
void Adapter(object? sender, RoutedEventArgs e)
{
if (sender is TTarget target && e is TEventArgs args)
{
Expand Down
3 changes: 1 addition & 2 deletions src/Avalonia.ReactiveUI/Avalonia.ReactiveUI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
<TargetFrameworks>netstandard2.0;net6.0</TargetFrameworks>
<PackageId>Avalonia.ReactiveUI</PackageId>
<SignAssembly>false</SignAssembly>
<Nullable>enable</Nullable>
<WarningsAsErrors>nullable</WarningsAsErrors>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\packages\Avalonia\Avalonia.csproj" />
</ItemGroup>
<Import Project="..\..\build\Rx.props" />
<Import Project="..\..\build\ReactiveUI.props" />
<Import Project="..\..\build\ApiDiff.props" />
<Import Project="..\..\build\NullableEnable.props" />
</Project>
3 changes: 1 addition & 2 deletions src/Markup/Avalonia.Markup/Avalonia.Markup.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
<PropertyGroup>
<TargetFrameworks>netstandard2.0;net6.0</TargetFrameworks>
<RootNamespace>Avalonia</RootNamespace>
<Nullable>Enable</Nullable>
<WarningsAsErrors>CS8600;CS8602;CS8603</WarningsAsErrors>
</PropertyGroup>
<ItemGroup>
<None Remove="Markup\Parsers\Nodes\ExpressionGrammer" />
Expand All @@ -19,4 +17,5 @@
<Import Project="..\..\..\build\Rx.props" />
<Import Project="..\..\..\build\System.Memory.props" />
<Import Project="..\..\..\build\ApiDiff.props" />
<Import Project="..\..\..\build\NullableEnable.props" />
</Project>
2 changes: 1 addition & 1 deletion src/Markup/Avalonia.Markup/Data/BindingBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ protected override void Unsubscribed()
_target.PropertyChanged -= PropertyChanged;
}

private void PropertyChanged(object sender, AvaloniaPropertyChangedEventArgs e)
private void PropertyChanged(object? sender, AvaloniaPropertyChangedEventArgs e)
{
if (e.Property == _property)
{
Expand Down
4 changes: 2 additions & 2 deletions src/Markup/Avalonia.Markup/Data/TemplateBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ private void TemplatedParentChanged()
PublishValue();
}

private void TargetPropertyChanged(object sender, AvaloniaPropertyChangedEventArgs e)
private void TargetPropertyChanged(object? sender, AvaloniaPropertyChangedEventArgs e)
{
if (e.Property == StyledElement.TemplatedParentProperty)
{
Expand All @@ -173,7 +173,7 @@ private void TargetPropertyChanged(object sender, AvaloniaPropertyChangedEventAr
}
}

private void TemplatedParentPropertyChanged(object sender, AvaloniaPropertyChangedEventArgs e)
private void TemplatedParentPropertyChanged(object? sender, AvaloniaPropertyChangedEventArgs e)
{
if (e.Property == Property)
{
Expand Down
4 changes: 2 additions & 2 deletions src/Markup/Avalonia.Markup/Markup/Data/DelayedBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ public static void ApplyBindings(IStyledElement control)
}
}

private static void ApplyBindings(object sender, EventArgs e)
private static void ApplyBindings(object? sender, EventArgs e)
{
var target = (IStyledElement)sender;
var target = (IStyledElement)sender!;
ApplyBindings(target);
target.Initialized -= ApplyBindings;
}
Expand Down
Loading