Skip to content

Commit 4982e8e

Browse files
authored
build: C# 11, Warnings as errors, and NetAnalyzers (#334)
1 parent 48a95ba commit 4982e8e

File tree

55 files changed

+316
-190
lines changed

Some content is hidden

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

55 files changed

+316
-190
lines changed

.editorconfig

+5-1
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ indent_style = space
1818
[*.md]
1919
indent_style = space
2020

21-
[{*.cs, *.xaml, *.xml, *.targets, *.props}]
21+
[*.{cs,xaml,xml,targets,props}]
2222
indent_style = tab
2323

24+
[*.cs]
25+
2426
# Code Style
2527
dotnet_style_operator_placement_when_wrapping=beginning_of_line
2628
csharp_new_line_before_open_brace=all
@@ -40,3 +42,5 @@ csharp_space_before_semicolon_in_for_statement=false
4042
csharp_space_around_binary_operators=before_and_after
4143
csharp_preserve_single_line_blocks=true
4244
csharp_preserve_single_line_statements=true
45+
46+
dotnet_diagnostic.CA1848.severity = none

Directory.Build.targets

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<Project ToolsVersion="15.0">
2+
3+
<ItemGroup>
4+
<PackageReference Include="Microsoft.Net.Compilers.Toolset" PrivateAssets="all" />
5+
</ItemGroup>
6+
7+
</Project>

samples/Directory.Packages.props

+2
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,7 @@
5151
<PackageVersion Include="Xamarin.Jetbrains.Annotations" Version="23.0.0.4" />
5252
<PackageVersion Include="Xamarin.TestCloud.Agent" Version="0.23.0" />
5353

54+
<PackageVersion Include="Microsoft.Net.Compilers.Toolset" Version="4.4.0" />
55+
<PackageVersion Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="7.0.0-preview1.22518.1" />
5456
</ItemGroup>
5557
</Project>

samples/Uno.Toolkit.WinUI.Samples/Uno.Toolkit.WinUI.Samples.Wasm/Uno.Toolkit.WinUI.Samples.Wasm.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<OutputType>Exe</OutputType>
55
<TargetFramework>net5.0</TargetFramework>
66
<NoWarn>NU1701</NoWarn>
7-
7+
<WasmShellIncludeWindowsCompatibility>false</WasmShellIncludeWindowsCompatibility>
88
</PropertyGroup>
99
<PropertyGroup Condition="'$(Configuration)'=='Debug'">
1010
<MonoRuntimeDebuggerEnabled>true</MonoRuntimeDebuggerEnabled>

src/Directory.Build.props

+19-1
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,19 @@
1616

1717
<Product>$(AssemblyName) ($(TargetFramework))</Product>
1818
<DefaultLanguage>en-US</DefaultLanguage>
19-
<LangVersion>latest</LangVersion>
19+
<LangVersion>11</LangVersion>
2020
<Nullable>enable</Nullable>
2121

2222
<NoNFloatUsing>true</NoNFloatUsing>
23+
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
24+
<!-- NU5048: The 'PackageIconUrl'/'iconUrl' element is deprecated. Consider using the 'PackageIcon'/'icon' element instead. Learn more at https://aka.ms/deprecateIconUrl -->
25+
<NoWarn>$(NoWarn);NU5048</NoWarn>
26+
<!-- Uno0001: Uno type or member is not implemented -->
27+
<NoWarn>$(NoWarn);Uno0001</NoWarn>
28+
<!-- This should be enabled back once https://github.com/microsoft/microsoft-ui-xaml/issues/4187 is fixed. -->
29+
<NoWarn>$(NoWarn);CS8305</NoWarn>
30+
<!-- TODO: Enable once https://github.com/xamarin/xamarin-macios/pull/16513 is merged and we have it -->
31+
<NoWarn>$(NoWarn);CA1416</NoWarn>
2332
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
2433
</PropertyGroup>
2534

@@ -64,6 +73,15 @@
6473
</When>
6574
</Choose>
6675

76+
<PropertyGroup Condition="'$(IsTestProject)'=='false'">
77+
<!-- Enable performance CA rules from 'Microsoft.CodeAnalysis.NetAnalyzers' as build warnings by default. Specific rules are disabled or downgraded in the repo's editorconfig. -->
78+
<AnalysisLevel>latest</AnalysisLevel>
79+
<AnalysisModePerformance>AllEnabledByDefault</AnalysisModePerformance>
80+
</PropertyGroup>
81+
<ItemGroup Condition="'$(IsTestProject)'=='false'">
82+
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" PrivateAssets="All" />
83+
</ItemGroup>
84+
6785
<!--
6886
Adjust the output paths for runtime project in order for those
6987
projects to stay in the same folder as the original reference one.

src/Directory.Build.targets

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
<Project ToolsVersion="15.0">
22

3-
<ItemGroup>
4-
</ItemGroup>
5-
63
<Import Project="nuget_override.props" Condition="Exists('nuget_override.props')" />
74

85
<Target Name="_UnoOverrideNuget"
@@ -30,7 +27,5 @@
3027
DestinationFiles="@(_OutputRefFiles->'$(_TargetNugetRefFolder)\%(RecursiveDir)%(Filename)%(Extension)')" />
3128
</Target>
3229

33-
34-
35-
30+
<Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.targets', '$(MSBuildThisFileDirectory)../'))" />
3631
</Project>

src/Directory.Packages.props

+3-1
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,7 @@
2626
<PackageVersion Include="Uno.UITest.Xamarin" Version="1.1.0-dev.24" />
2727
<PackageVersion Include="Xamarin.UITest" Version="3.2.9" />
2828
<PackageVersion Include="MSTest.TestFramework" Version="2.1.2" />
29+
<PackageVersion Include="Microsoft.Net.Compilers.Toolset" Version="4.4.0" />
30+
<PackageVersion Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="7.0.0-preview1.22518.1" />
2931
</ItemGroup>
30-
</Project>
32+
</Project>

src/Uno.Toolkit.UI/Behaviors/CommandExtensions.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public static class CommandExtensions
6969
new PropertyMetadata(default(object?)));
7070

7171
[DynamicDependency(nameof(SetCommandParameter))]
72-
public static object? GetCommandParameter(DependencyObject? obj) => obj.GetValue(CommandParameterProperty);
72+
public static object? GetCommandParameter(DependencyObject obj) => obj.GetValue(CommandParameterProperty);
7373

7474
[DynamicDependency(nameof(GetCommandParameter))]
7575
public static void SetCommandParameter(DependencyObject obj, object? value) => obj.SetValue(CommandParameterProperty, value);

src/Uno.Toolkit.UI/Behaviors/StatusBar.iOS.cs

+26-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#if __IOS__
22
using System;
3+
using System.Linq;
34
using System.Collections.Generic;
4-
using System.Text;
5+
using CoreGraphics;
56
using UIKit;
67

78
using XamlColor = Windows.UI.Color;
@@ -21,15 +22,37 @@ static partial void SetBackgroundCore(XamlColor value)
2122
// random unique tag to avoid recreating the view
2223
const int StatusBarViewTag = 38482;
2324

24-
foreach (var window in UIApplication.SharedApplication.Windows)
25+
var (windows, statusBarFrame) = GetWindowsAndStatusBarFrame();
26+
foreach (var window in windows)
2527
{
26-
var sbar = window.ViewWithTag(StatusBarViewTag) ?? new UIView(UIApplication.SharedApplication.StatusBarFrame) { Tag = StatusBarViewTag };
28+
var sbar = window.ViewWithTag(StatusBarViewTag) ?? new UIView(statusBarFrame) { Tag = StatusBarViewTag };
2729
sbar.BackgroundColor = value;
2830
sbar.TintColor = value;
2931

3032
window.AddSubview(sbar);
3133
}
3234
}
35+
36+
private static (UIWindow[] Windows, CGRect StatusBarFrame) GetWindowsAndStatusBarFrame()
37+
{
38+
if (UIDevice.CurrentDevice.CheckSystemVersion(13, 0))
39+
{
40+
IEnumerable<UIScene> scenes = UIApplication.SharedApplication.ConnectedScenes;
41+
var currentScene = scenes.FirstOrDefault(n => n.ActivationState == UISceneActivationState.ForegroundActive);
42+
43+
if (currentScene is not UIWindowScene uiWindowScene)
44+
throw new InvalidOperationException("Unable to find current window scene.");
45+
46+
if (uiWindowScene.StatusBarManager is not { } statusBarManager)
47+
throw new InvalidOperationException("Unable to find a status bar manager.");
48+
49+
return (uiWindowScene.Windows, statusBarManager.StatusBarFrame);
50+
}
51+
else
52+
{
53+
return (UIApplication.SharedApplication.Windows, UIApplication.SharedApplication.StatusBarFrame);
54+
}
55+
}
3356
}
3457
}
3558
#endif

src/Uno.Toolkit.UI/Behaviors/TabBarSelectorBehaviorState.Android.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ private void OnFlipViewSizeChanged(object sender, SizeChangedEventArgs args)
6363
}
6464
}
6565

66-
private void OnPageScrolled(object sender, ViewPager.PageScrolledEventArgs e)
66+
private void OnPageScrolled(object? sender, ViewPager.PageScrolledEventArgs e)
6767
{
6868
UpdateOffset(e.Position, e.PositionOffset, GetOffset(e));
6969
}

src/Uno.Toolkit.UI/Behaviors/TabBarSelectorBehaviorState.Uwp.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ partial void DisconnectPartial()
5656
_viewChangedRevoker.Disposable = null;
5757
}
5858

59-
private void OnScrolViewerViewChanged(object sender, ScrollViewerViewChangedEventArgs e)
59+
private void OnScrolViewerViewChanged(object? sender, ScrollViewerViewChangedEventArgs e)
6060
{
6161
var scrollViewer = sender as ScrollViewer;
6262

src/Uno.Toolkit.UI/Behaviors/TabBarSelectorBehaviorState.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ internal partial class TabBarSelectorBehaviorState
2626
public TabBar TabBar { get; }
2727

2828
private bool _isSynchronizing;
29-
private double _lastOffsetX = 0;
29+
private double _lastOffsetX;
3030

3131
public TabBarSelectorBehaviorState(Selector selector, TabBar tabBar)
3232
{

src/Uno.Toolkit.UI/Behaviors/TabBarSelectorBehaviorState.iOS.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ private void OnFlipViewLoaded(object sender, RoutedEventArgs e)
4545
&& flipView.FindFirstChild<NativeFlipView>() is { } nativeFlipView)
4646
{
4747
scrollView.Delegate = new ScrollViewDelegate(this, nativeFlipView.Source as FlipViewSource);
48-
_scrolledRevoker.Disposable = Disposable.Create(() => scrollView.Delegate = null);
48+
_scrolledRevoker.Disposable = Disposable.Create(() => scrollView.Delegate = null!);
4949
}
5050

5151
_flipViewLoadedRevoker.Disposable = null;

src/Uno.Toolkit.UI/Controls/Card/Card.Properties.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,11 @@ public DataTemplate IconsContentTemplate
143143
#endregion
144144

145145
#region Elevation
146-
public double Elevation
146+
public
147+
#if __ANDROID__
148+
new
149+
#endif
150+
double Elevation
147151
{
148152
get { return (double)GetValue(ElevationProperty); }
149153
set { SetValue(ElevationProperty, value); }

src/Uno.Toolkit.UI/Controls/CardContentControl/CardContentControl.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ public CardContentControl()
3535
}
3636

3737
#region Elevation
38-
public double Elevation
38+
public
39+
#if __ANDROID__
40+
new
41+
#endif
42+
double Elevation
3943
{
4044
get => (double)GetValue(ElevationProperty);
4145
set => SetValue(ElevationProperty, value);

src/Uno.Toolkit.UI/Controls/Chips/Chip.Members.cs

+10-6
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@ public bool CanRemove
4444
/// <summary>
4545
/// Gets or sets the elevation of the Chip.
4646
/// </summary>
47-
public double Elevation
47+
public
48+
#if __ANDROID__
49+
new
50+
#endif
51+
double Elevation
4852
{
4953
get => (double)GetValue(ElevationProperty);
5054
set => SetValue(ElevationProperty, value);
@@ -63,7 +67,7 @@ public double Elevation
6367
/// <summary>
6468
/// Gets or sets the icon of the Chip.
6569
/// </summary>
66-
public object Icon
70+
public object? Icon
6771
{
6872
get => (object)GetValue(IconProperty);
6973
set => SetValue(IconProperty, value);
@@ -82,7 +86,7 @@ public object Icon
8286
/// <summary>
8387
/// Gets or sets the data template that is used to display the icon of the Chip.
8488
/// </summary>
85-
public DataTemplate IconTemplate
89+
public DataTemplate? IconTemplate
8690
{
8791
get => (DataTemplate)GetValue(IconTemplateProperty);
8892
set => SetValue(IconTemplateProperty, value);
@@ -159,19 +163,19 @@ public partial class Chip // Events
159163
/// <remarks>
160164
/// This event is bypassed when set with <see cref="SetIsCheckedSilently(bool?)"/>
161165
/// </remarks>
162-
internal event RoutedEventHandler IsCheckedChanged;
166+
internal event RoutedEventHandler? IsCheckedChanged;
163167

164168
/// <summary>
165169
/// Occurs when the remove button is pressed.
166170
/// </summary>
167171
/// <remarks>
168172
/// When used outside of a <see cref="ChipGroup"/>, this event does not cause the Chip to be removed from the view.
169173
/// </remarks>
170-
public event RoutedEventHandler Removed;
174+
public event RoutedEventHandler? Removed;
171175

172176
/// <summary>
173177
/// Occurs when the remove button is pressed, but before <see cref="Removed" /> event allowing for cancellation.
174178
/// </summary>
175-
public event ChipRemovingEventHandler Removing;
179+
public event ChipRemovingEventHandler? Removing;
176180
}
177181
}

src/Uno.Toolkit.UI/Controls/Chips/Chip.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public partial class Chip : ToggleButton
2020
{
2121
private const string RemoveButtonName = "PART_RemoveButton";
2222

23-
private bool _isMuted = false;
23+
private bool _isMuted;
2424

2525
public Chip()
2626
{

src/Uno.Toolkit.UI/Controls/Chips/ChipGroup.Members.cs

+11-10
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public bool CanRemove
3939
typeof(DataTemplate),
4040
typeof(ChipGroup),
4141
new PropertyMetadata(null, (s, e) => (s as ChipGroup)?.ApplyIconTemplate()));
42-
42+
4343
/// <summary>
4444
/// Gets or sets the value of each <see cref="Chip.IconTemplate"/>.
4545
/// </summary>
@@ -65,7 +65,7 @@ public DataTemplate IconTemplate
6565
/// <remarks>
6666
/// This property only works for <see cref="ChipSelectionMode.Single"/>.
6767
/// </remarks>
68-
public object SelectedItem
68+
public object? SelectedItem
6969
{
7070
get => (object)GetValue(SelectedItemProperty);
7171
set => SetValue(SelectedItemProperty, value);
@@ -80,15 +80,15 @@ public object SelectedItem
8080
typeof(IList),
8181
typeof(ChipGroup),
8282
new PropertyMetadata(default, (s, e) => (s as ChipGroup)?.OnSelectedItemsChanged(e)));
83-
83+
8484
/// <summary>
8585
/// Gets or sets the selected items.
8686
/// </summary>
8787
/// <remarks>
8888
/// The value will be null if the selection is empty.
8989
/// This property only works for <see cref="ChipSelectionMode.Multiple"/>.
9090
/// </remarks>
91-
public IList SelectedItems
91+
public IList? SelectedItems
9292
{
9393
get => (IList)GetValue(SelectedItemsProperty);
9494
set => SetValue(SelectedItemsProperty, value);
@@ -103,10 +103,11 @@ public IList SelectedItems
103103
typeof(string),
104104
typeof(ChipGroup),
105105
new PropertyMetadata(default, (s, e) => (s as ChipGroup)?.OnSelectionMemberPathChanged(e)));
106-
106+
#pragma warning disable CS1574 // XML comment has cref attribute 'IsChecked' that could not be resolved
107107
/// <summary>
108108
/// Gets or sets the path which each <see cref="Chip.IsChecked"/> is data-bind to.
109109
/// </summary>
110+
#pragma warning restore CS1574
110111
public string SelectionMemberPath
111112
{
112113
get => (string)GetValue(SelectionMemberPathProperty);
@@ -143,26 +144,26 @@ public partial class ChipGroup // Events
143144
/// <summary>
144145
/// Occurs when a <see cref="Chip"/> item is pressed.
145146
/// </summary>
146-
public event ChipItemEventHandler ItemClick;
147+
public event ChipItemEventHandler? ItemClick;
147148

148149
/// <summary>
149150
/// Occurs when a <see cref="Chip"/> item is checked.
150151
/// </summary>
151-
public event ChipItemEventHandler ItemChecked;
152+
public event ChipItemEventHandler? ItemChecked;
152153

153154
/// <summary>
154155
/// Occurs when a <see cref="Chip"/> item is unchecked.
155156
/// </summary>
156-
public event ChipItemEventHandler ItemUnchecked;
157+
public event ChipItemEventHandler? ItemUnchecked;
157158

158159
/// <summary>
159160
/// Occurs when a <see cref="Chip"/> item is removed.
160161
/// </summary>
161-
public event ChipItemEventHandler ItemRemoved;
162+
public event ChipItemEventHandler? ItemRemoved;
162163

163164
/// <summary>
164165
/// Occurs when a <see cref="Chip"/> item is about to be removed.
165166
/// </summary>
166-
public event ChipItemRemovingEventHandler ItemRemoving;
167+
public event ChipItemRemovingEventHandler? ItemRemoving;
167168
}
168169
}

0 commit comments

Comments
 (0)