Enable nullables and nullable warnings as errors globally

This commit is contained in:
Arlo Godfrey 2022-03-21 12:22:43 -05:00
Родитель 2596fa36bd
Коммит 7580d12edb
13 изменённых файлов: 50 добавлений и 53 удалений

Просмотреть файл

@ -83,7 +83,7 @@ namespace {containingClassSymbol.ContainingNamespace}
{{
public partial class {containingClassSymbol.Name} : {nameof(System.ComponentModel.INotifyPropertyChanged)}
{{
public event PropertyChangedEventHandler PropertyChanged;
public event PropertyChangedEventHandler? PropertyChanged;
}}
}}
";
@ -141,7 +141,7 @@ namespace {containingClassSymbol.ContainingNamespace}
{{
public {typeName} {propertyName}
{{
get => (({typeName})(({viewModelType.FullName})GeneratedPropertyMetadata!.First(x => x.Name == ""{propertyName}""))!.Value);
get => (({typeName})(({viewModelType.FullName})GeneratedPropertyMetadata!.First(x => x.Name == ""{propertyName}""))!.Value!)!;
set
{{
if (GeneratedPropertyMetadata?.FirstOrDefault(x => x.Name == nameof({propertyName})) is {viewModelType.FullName} metadata)

Просмотреть файл

@ -56,7 +56,7 @@ namespace CommunityToolkit.Labs.Shared
/// <param name="e">Details about the launch request and process.</param>
protected override void OnLaunched(LaunchActivatedEventArgs e)
{
Frame rootFrame = null;
Frame? rootFrame = null;
#if WINAPPSDK
var window = new Window();
@ -88,6 +88,9 @@ namespace CommunityToolkit.Labs.Shared
#else
if (e.PrelaunchActivated == false)
{
if (rootFrame is null)
throw new InvalidOperationException("Cannot display app content, root frame is missing.");
if (rootFrame.Content == null)
{
rootFrame.Navigate(typeof(AppLoadingView), e.Arguments);

Просмотреть файл

@ -115,7 +115,7 @@ namespace CommunityToolkit.Labs.Shared
}
// Needed because Frame.Navigate doesn't work inside of the OnNavigatedTo override.
private void ScheduleNavigate(Type type, object param = null)
private void ScheduleNavigate(Type type, object? param = null)
{
DispatcherQueue.GetForCurrentThread().TryEnqueue(() =>
{

Просмотреть файл

@ -21,6 +21,7 @@
<Compile Include="$(MSBuildThisFileDirectory)AppLoadingView.xaml.cs">
<DependentUpon>AppLoadingView.xaml</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)Polyfills\System.Runtime.CompilerServices\IsExternalInit.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Renderers\GeneratedSampleOptionsRenderer.xaml.cs">
<DependentUpon>GeneratedSampleOptionsRenderer.xaml</DependentUpon>
</Compile>

Просмотреть файл

@ -61,7 +61,7 @@ namespace CommunityToolkit.Labs.Shared
NavigationViewItems.Add(item);
}
base.OnNavigatedTo(e);
base.OnNavigatedTo(e);
}
private void OnSelectionChanged(NavigationView sender, NavigationViewSelectionChangedEventArgs e)
@ -83,12 +83,12 @@ namespace CommunityToolkit.Labs.Shared
foreach (var navData in categoryData)
{
// Make subcategories
var subcategoryData = GenerateSubcategoryNavItems(navData.SampleMetadata);
var subcategoryData = GenerateSubcategoryNavItems(navData.SampleMetadata ?? Enumerable.Empty<ToolkitSampleMetadata>());
foreach (var subcategoryItemData in subcategoryData)
{
// Make samples
var sampleNavigationItems = GenerateSampleNavItems(subcategoryItemData.SampleMetadata);
var sampleNavigationItems = GenerateSampleNavItems(subcategoryItemData.SampleMetadata ?? Enumerable.Empty<ToolkitSampleMetadata>());
foreach (var item in sampleNavigationItems)
{
@ -123,14 +123,10 @@ namespace CommunityToolkit.Labs.Shared
foreach (var subcategoryGroup in samplesBySubcategory)
{
yield return new GroupNavigationItemData
yield return new GroupNavigationItemData(new NavigationViewItem
{
NavItem = new NavigationViewItem
{
Content = subcategoryGroup.Key,
},
SampleMetadata = subcategoryGroup.ToArray(),
};
Content = subcategoryGroup.Key,
}, subcategoryGroup.ToArray());
}
}
@ -140,28 +136,15 @@ namespace CommunityToolkit.Labs.Shared
foreach (var categoryGroup in samplesByCategory)
{
yield return new GroupNavigationItemData()
yield return new GroupNavigationItemData(new NavigationViewItem
{
NavItem = new NavigationViewItem()
{
Content = categoryGroup.Key,
},
SampleMetadata = categoryGroup.ToArray(),
};
Content = categoryGroup.Key,
}, categoryGroup.ToArray());
}
}
private record GroupNavigationItemData
{
/// <summary>
/// A navigation item to contain items in this group.
/// </summary>
public NavigationViewItem NavItem { get; set; }
/// <summary>
/// The samples that belong under <see cref="NavItem"/>.
/// </summary>
public IEnumerable<ToolkitSampleMetadata> SampleMetadata { get; set; }
};
/// <param name="NavItem">A navigation item to contain items in this group.</param>
/// <param name="SampleMetadata">The samples that belong under <see cref="NavItem"/>.</param>
private record GroupNavigationItemData(NavigationViewItem NavItem, IEnumerable<ToolkitSampleMetadata> SampleMetadata);
}
}

Просмотреть файл

@ -0,0 +1,16 @@
#if !NET6_0_OR_GREATER
using System.ComponentModel;
namespace System.Runtime.CompilerServices;
/// <summary>
/// Reserved to be used by the compiler for tracking metadata.
/// This class should not be used by developers in source code.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
internal static class IsExternalInit
{
}
#endif

Просмотреть файл

@ -129,7 +129,7 @@ namespace CommunityToolkit.Labs.Shared.Renderers
var sampleControlInstance = (UIElement)Metadata.SampleControlFactory();
// Custom control-based sample options.
if (Metadata.SampleOptionsPaneType is not null)
if (Metadata.SampleOptionsPaneType is not null && Metadata.SampleOptionsPaneFactory is not null)
{
SampleOptionsPaneInstance = (UIElement)Metadata.SampleOptionsPaneFactory(sampleControlInstance);
}
@ -182,6 +182,12 @@ namespace CommunityToolkit.Labs.Shared.Renderers
var simpleAssemblyName = type.Assembly.GetName().Name;
var typeNamespace = type.Namespace;
if (string.IsNullOrWhiteSpace(simpleAssemblyName))
throw new ArgumentException($"Unable to find assembly name for provided type {type}.", nameof(simpleAssemblyName));
if (string.IsNullOrWhiteSpace(typeNamespace))
throw new ArgumentException($"Unable to find namespace for provided type {type}.", nameof(typeNamespace));
var folderPath = typeNamespace.Replace(simpleAssemblyName, "").Trim('.').Replace('.', '/');
return $"ms-appx:///{simpleAssemblyName}/{folderPath}/{type.Name}";

Просмотреть файл

@ -3,7 +3,7 @@
<RepositoryDirectory>$(MSBuildThisFileDirectory)</RepositoryDirectory>
</PropertyGroup>
<Import Project="$(RepositoryDirectory)Windows.Toolkit.Common.props" />
<Import Project="Windows.Toolkit.Common.props" />
<PropertyGroup>
<DebugType>Embedded</DebugType>

Просмотреть файл

@ -60,6 +60,6 @@ namespace CanvasLayout.Sample.SampleThree
public class CanvasItem : CanvasLayoutItem
{
public string Text { get; set; }
public string Text { get; set; } = string.Empty;
}
}

Просмотреть файл

@ -6,7 +6,7 @@ namespace CanvasLayout.Wasm
{
public class Program
{
private static App _app;
private static App? _app;
static int Main(string[] args)
{

Просмотреть файл

@ -104,20 +104,7 @@ namespace CommunityToolkit.Labs.WinUI
/// List of layout bounds for items starting with the
/// FirstRealizedIndex.
/// </summary>
public List<Rect> LayoutRects
{
get
{
if (_layoutRects == null)
{
_layoutRects = new List<Rect>();
}
return _layoutRects;
}
}
private List<Rect> _layoutRects;
public List<Rect> LayoutRects { get; } = new List<Rect>();
}
// TODO: Make DP? Can we do this with property mapping instead?

Просмотреть файл

@ -6,7 +6,7 @@ namespace CommunityToolkit.Labs.Wasm
{
public class Program
{
private static App _app;
private static App? _app;
static int Main(string[] args)
{

Просмотреть файл

@ -15,7 +15,8 @@
<PropertyGroup>
<Features>Strict</Features>
<Nullable>Disable</Nullable>
<Nullable>enable</Nullable>
<WarningsAsErrors>nullable</WarningsAsErrors>
<LangVersion>Latest</LangVersion>
<DefaultLanguage>en-US</DefaultLanguage>
<NoPackageAnalysis>true</NoPackageAnalysis>