Skip to content

Commit

Permalink
Analyzer Warning Mitigation (#1039)
Browse files Browse the repository at this point in the history
* Fix null analyzer complaints: CS8602, CS8618, CS8603, CS8622, IDE0031

* Hide warnings related to IDE0008 related to using `var` keyword

* Removed Coersion logic from IconSourceElementConverter, and placed in IconElement and IcounSourceElement classes instead

* Cleaning single-line warnings: SA1005

- Removed commented-out Toolbox attributes
- Replaced single-line commented-out code with multi-line

* Fix missing braces or single-lined braces

- Add stylecop supression of 1502 "element should not be on a single line"
- Wrap with braces where needed

* Fix warnings on registered dependency properties

WPF0100, WPF0005, WPF0012

* Fix documentation warnings

WPF0108, WPF0060, SA1642, SA1623

* Fix warnings about using language keywords

- Use string, double, int rather than String, Double, Int32, etc
- remove 'this' keyword when context is obvious

* Fix warnings where attributes are on a single line SA1133

* When setting DPs, favor SetCurrentValue over the CLR setter. WPF0041

* Remove unnecessary things (parenthesis, assignments, etc). SA1119, IDE0059

* Fix order of operation warnings

* Do not use regions SA1124. Clean VirtualizingPanelBase of warnings

* Turn protected control fields into protected properties. fix some documentation warnings

* Cleanup warnings in Interop classes and suppress warnings related to uper-casing and private fields

* Continue cleaning up warnings in Wpf.Ui

* Edit .editorconfig to suppress warnings related to valueconverters

* Cleanup warning related to 'var'

* Fix warnings

* Split ContextMenu style from its loader dictionary

There were some issue when having a class called ContextMenu that wasn't actually a contextmenu but  a ResourceDictionary

* Resolve "expression value is never used" IDE0058 warnings

* Fix warnings related to collection initialization.

There's still a lot of IDE0028 warnings as the auto fix really wants to use the C#12 collection expressions, which btw will trigger SA1010 warnings (DotNetAnalyzers/StyleCopAnalyzers#3687)

* Remove unused suppressions

* Supress IDE0290 (Use primary constructor). Just didn't seem necessary.

* IDE0009 should be configured through dotnet_styles

* Separate out the loader component from the Menu.xaml style.

Justification: Menu was not a control.Menu, it's more like a hack to change private api settings.

* Fix warnings on NavigationView.NavigationParent attached property

* Fix warnings related to setting refernce types as default values. for dependency properties. Also fixed some enum warnings

* Fix warnings

* Fix regression error where the ContentDialog wouldn't appear and change name of ContentPresenter to DialogHost

* Remove obsolete forceBacground parameters. Breaking changes

* Remove redundant #if DEBUG directives around Debug.WriteLine calls

* Fix some analyzer warnings
  • Loading branch information
koal44 committed Apr 14, 2024
1 parent eefd6f4 commit c0ace5b
Show file tree
Hide file tree
Showing 278 changed files with 3,198 additions and 3,673 deletions.
30 changes: 23 additions & 7 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ file_header_template = This Source Code Form is subject to the terms of the MIT
#### .NET Coding Conventions ####

# this. and Me. preferences
dotnet_style_qualification_for_event = false:warning
dotnet_style_qualification_for_field = false:warning
dotnet_style_qualification_for_method = false:warning
dotnet_style_qualification_for_property = false:warning
dotnet_style_qualification_for_event = false:silent
dotnet_style_qualification_for_field = false:silent
dotnet_style_qualification_for_method = false:silent
dotnet_style_qualification_for_property = false:silent

# Language keywords vs BCL types preferences
dotnet_style_predefined_type_for_locals_parameters_members = true:warning
Expand Down Expand Up @@ -95,8 +95,8 @@ dotnet_style_readonly_field = true:warning

# var preferences
csharp_style_var_elsewhere = false:warning
csharp_style_var_for_built_in_types = false:warning
csharp_style_var_when_type_is_apparent = false:warning
csharp_style_var_for_built_in_types = false:none
csharp_style_var_when_type_is_apparent = false:none

# Expression-bodied members
csharp_style_expression_bodied_accessors = false:silent
Expand Down Expand Up @@ -388,7 +388,7 @@ dotnet_diagnostic.SA1633.severity = none
dotnet_diagnostic.SA1634.severity = none
dotnet_diagnostic.SA1652.severity = none

dotnet_diagnostic.IDE0009.severity = none


# Additional Stylecop Analyzers
dotnet_diagnostic.SA1111.severity = none
Expand All @@ -397,3 +397,19 @@ dotnet_diagnostic.SA1204.severity = none
dotnet_diagnostic.SA1208.severity = none
dotnet_diagnostic.SA1518.severity = none
dotnet_diagnostic.SA1615.severity = none
dotnet_diagnostic.SA1502.severity = none
dotnet_diagnostic.SA1010.severity = none # Opening square brackets should not be preceded by a space
# conflicts with collection expressions and IDE0028

# Suppress some ValueConverter warnings
dotnet_diagnostic.WPF0073.severity = none # Add ValueConversion attribute (unknown types)
dotnet_diagnostic.WPF0071.severity = none # Add ValueConversion attribute
dotnet_diagnostic.WPF0070.severity = none # Add default field to converter

# Suppress some IDE warnings
dotnet_diagnostic.IDE0290.severity = none # Use primary constructor
dotnet_diagnostic.CS1591.severity = none # Missing XML comment for publicly visible type or member
# 15000+ warnings in the solution
dotnet_diagnostic.CA1510.severity = none # Use ArgumentNullException throw helper
# doesn't work with older versions of .NET

4 changes: 4 additions & 0 deletions src/Wpf.Ui.Demo.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
// Copyright (C) Leszek Pomianowski and WPF UI Contributors.
// All Rights Reserved.

using System.Diagnostics;

public static class Program
{
[STAThread]
public static void Main(string[] args)
{
Debug.WriteLine("Args: " + string.Join(", ", args));

if (Application.Current is null)
{
Console.WriteLine($"Application.Current is null.");
Expand Down
12 changes: 8 additions & 4 deletions src/Wpf.Ui.Demo.Console/Utilities/ThemeUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,27 @@ public static void ApplyTheme(this FrameworkElement frameworkElement)
{
ApplicationThemeManager.Apply(frameworkElement);

ThemeChangedEvent themeChanged = (sender, args) =>
void themeChanged(ApplicationTheme sender, Color args)
{
ApplicationThemeManager.Apply(frameworkElement);
if (frameworkElement is Window window)
{
if (window != UiApplication.Current.MainWindow)
{
WindowBackgroundManager.UpdateBackground(
window,
sender,
Wpf.Ui.Controls.WindowBackdropType.None,
true
Wpf.Ui.Controls.WindowBackdropType.None
);
}
}
};
}

if (frameworkElement.IsLoaded)
{
ApplicationThemeManager.Changed += themeChanged;
}

frameworkElement.Loaded += (s, e) =>
{
ApplicationThemeManager.Changed += themeChanged;
Expand Down Expand Up @@ -85,6 +87,7 @@ public static void ChangeTheme()
ApplicationThemeManager.Apply(applicationTheme, updateAccent: false);
}

/*
/// <summary>
/// Applies Resources in the <paramref name="frameworkElement"/>.
/// </summary>
Expand Down Expand Up @@ -128,4 +131,5 @@ private static void Apply(FrameworkElement frameworkElement)
frameworkElement.Resources[resource.Key] = resource.Value;
}
}
*/
}
4 changes: 3 additions & 1 deletion src/Wpf.Ui.Demo.Console/Views/Pages/DataPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Wpf.Ui.Demo.Console.Views.Pages;
/// </summary>
public partial class DataPage
{
public ObservableCollection<DataColor> ColorsCollection = new();
public ObservableCollection<DataColor> ColorsCollection = [];

public DataPage()
{
Expand All @@ -30,6 +30,7 @@ private void InitializeData()
var random = new Random();

for (int i = 0; i < 8192; i++)
{
ColorsCollection.Add(
new DataColor
{
Expand All @@ -43,5 +44,6 @@ private void InitializeData()
)
}
);
}
}
}
2 changes: 1 addition & 1 deletion src/Wpf.Ui.Demo.Console/Views/Pages/SettingsPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ private void OnDarkThemeRadioButtonChecked(object sender, RoutedEventArgs e)
private string GetAssemblyVersion()
{
return System.Reflection.Assembly.GetExecutingAssembly().GetName().Version?.ToString()
?? String.Empty;
?? string.Empty;
}
}
1 change: 1 addition & 0 deletions src/Wpf.Ui.Demo.Console/Wpf.Ui.Demo.Console.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<UseWPF>true</UseWPF>
<ApplicationIcon>wpfui.ico</ApplicationIcon>
<NoWarn>$(NoWarn);SA1601</NoWarn>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
</PropertyGroup>

<ItemGroup>
Expand Down
34 changes: 18 additions & 16 deletions src/Wpf.Ui.Demo.Mvvm/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// This Source Code Form is subject to the terms of the MIT License.
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file, You can obtain one at https://opensource.org/licenses/MIT.
// Copyright (C) Leszek Pomianowski and WPF UI Contributors.
// All Rights Reserved.
Expand Down Expand Up @@ -26,40 +26,42 @@ public partial class App
private static readonly IHost _host = Host.CreateDefaultBuilder()
.ConfigureAppConfiguration(c =>
{
c.SetBasePath(Path.GetDirectoryName(AppContext.BaseDirectory));
var basePath = Path.GetDirectoryName(AppContext.BaseDirectory)
?? throw new DirectoryNotFoundException("Unable to find the base directory of the application.");
_ = c.SetBasePath(basePath);
})
.ConfigureServices(
(context, services) =>
{
// App Host
services.AddHostedService<ApplicationHostService>();
_ = services.AddHostedService<ApplicationHostService>();
// Page resolver service
services.AddSingleton<IPageService, PageService>();
_ = services.AddSingleton<IPageService, PageService>();
// Theme manipulation
services.AddSingleton<IThemeService, ThemeService>();
_ = services.AddSingleton<IThemeService, ThemeService>();
// TaskBar manipulation
services.AddSingleton<ITaskBarService, TaskBarService>();
_ = services.AddSingleton<ITaskBarService, TaskBarService>();
// Service containing navigation, same as INavigationWindow... but without window
services.AddSingleton<INavigationService, NavigationService>();
_ = services.AddSingleton<INavigationService, NavigationService>();
// Main window with navigation
services.AddSingleton<INavigationWindow, Views.MainWindow>();
services.AddSingleton<ViewModels.MainWindowViewModel>();
_ = services.AddSingleton<INavigationWindow, Views.MainWindow>();
_ = services.AddSingleton<ViewModels.MainWindowViewModel>();
// Views and ViewModels
services.AddSingleton<Views.Pages.DashboardPage>();
services.AddSingleton<ViewModels.DashboardViewModel>();
services.AddSingleton<Views.Pages.DataPage>();
services.AddSingleton<ViewModels.DataViewModel>();
services.AddSingleton<Views.Pages.SettingsPage>();
services.AddSingleton<ViewModels.SettingsViewModel>();
_ = services.AddSingleton<Views.Pages.DashboardPage>();
_ = services.AddSingleton<ViewModels.DashboardViewModel>();
_ = services.AddSingleton<Views.Pages.DataPage>();
_ = services.AddSingleton<ViewModels.DataViewModel>();
_ = services.AddSingleton<Views.Pages.SettingsPage>();
_ = services.AddSingleton<ViewModels.SettingsViewModel>();
// Configuration
services.Configure<AppConfig>(context.Configuration.GetSection(nameof(AppConfig)));
_ = services.Configure<AppConfig>(context.Configuration.GetSection(nameof(AppConfig)));
}
)
.Build();
Expand Down
10 changes: 2 additions & 8 deletions src/Wpf.Ui.Demo.Mvvm/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,7 @@
// Copyright (C) Leszek Pomianowski and WPF UI Contributors.
// All Rights Reserved.

using System.Windows;

[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries)
ResourceDictionaryLocation.None, // Where theme specific resource dictionaries are located (used if a resource is not found in the page, or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly // Where the generic resource dictionary is located (used if a resource is not found in the page, app, or any theme specific resource dictionaries)
)]
12 changes: 9 additions & 3 deletions src/Wpf.Ui.Demo.Mvvm/Helpers/EnumToBooleanConverter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// This Source Code Form is subject to the terms of the MIT License.
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file, You can obtain one at https://opensource.org/licenses/MIT.
// Copyright (C) Leszek Pomianowski and WPF UI Contributors.
// All Rights Reserved.
Expand All @@ -12,11 +12,15 @@ internal class EnumToBooleanConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (parameter is not String enumString)
if (parameter is not string enumString)
{
throw new ArgumentException("ExceptionEnumToBooleanConverterParameterMustBeAnEnumName");
}

if (!Enum.IsDefined(typeof(Wpf.Ui.Appearance.ApplicationTheme), value))
{
throw new ArgumentException("ExceptionEnumToBooleanConverterValueMustBeAnEnum");
}

var enumValue = Enum.Parse(typeof(Wpf.Ui.Appearance.ApplicationTheme), enumString);

Expand All @@ -25,8 +29,10 @@ public object Convert(object value, Type targetType, object parameter, CultureIn

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (parameter is not String enumString)
if (parameter is not string enumString)
{
throw new ArgumentException("ExceptionEnumToBooleanConverterParameterMustBeAnEnumName");
}

return Enum.Parse(typeof(Wpf.Ui.Appearance.ApplicationTheme), enumString);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Wpf.Ui.Demo.Mvvm/Models/AppConfig.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// This Source Code Form is subject to the terms of the MIT License.
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file, You can obtain one at https://opensource.org/licenses/MIT.
// Copyright (C) Leszek Pomianowski and WPF UI Contributors.
// All Rights Reserved.
Expand All @@ -7,7 +7,7 @@ namespace Wpf.Ui.Demo.Mvvm.Models;

public class AppConfig
{
public string ConfigurationsFolder { get; set; }
public string? ConfigurationsFolder { get; set; }

public string AppPropertiesFileName { get; set; }
public string? AppPropertiesFileName { get; set; }
}
6 changes: 3 additions & 3 deletions src/Wpf.Ui.Demo.Mvvm/Services/ApplicationHostService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// This Source Code Form is subject to the terms of the MIT License.
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file, You can obtain one at https://opensource.org/licenses/MIT.
// Copyright (C) Leszek Pomianowski and WPF UI Contributors.
// All Rights Reserved.
Expand All @@ -14,7 +14,7 @@ namespace Wpf.Ui.Demo.Mvvm.Services;
public class ApplicationHostService : IHostedService
{
private readonly IServiceProvider _serviceProvider;
private INavigationWindow _navigationWindow;
private INavigationWindow? _navigationWindow;

public ApplicationHostService(IServiceProvider serviceProvider)
{
Expand Down Expand Up @@ -53,7 +53,7 @@ private async Task HandleActivationAsync()
)!;
_navigationWindow!.ShowWindow();

_navigationWindow.Navigate(typeof(Views.Pages.DashboardPage));
_ = _navigationWindow.Navigate(typeof(Views.Pages.DashboardPage));
}

await Task.CompletedTask;
Expand Down
8 changes: 6 additions & 2 deletions src/Wpf.Ui.Demo.Mvvm/Services/PageService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// This Source Code Form is subject to the terms of the MIT License.
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file, You can obtain one at https://opensource.org/licenses/MIT.
// Copyright (C) Leszek Pomianowski and WPF UI Contributors.
// All Rights Reserved.
Expand All @@ -16,7 +16,7 @@ public class PageService : IPageService
private readonly IServiceProvider _serviceProvider;

/// <summary>
/// Creates new instance and attaches the <see cref="IServiceProvider"/>.
/// Initializes a new instance of the <see cref="PageService"/> class and attaches the <see cref="IServiceProvider"/>.
/// </summary>
public PageService(IServiceProvider serviceProvider)
{
Expand All @@ -28,7 +28,9 @@ public PageService(IServiceProvider serviceProvider)
where T : class
{
if (!typeof(FrameworkElement).IsAssignableFrom(typeof(T)))
{
throw new InvalidOperationException("The page should be a WPF control.");
}

return (T?)_serviceProvider.GetService(typeof(T));
}
Expand All @@ -37,7 +39,9 @@ public PageService(IServiceProvider serviceProvider)
public FrameworkElement? GetPage(Type pageType)
{
if (!typeof(FrameworkElement).IsAssignableFrom(pageType))
{
throw new InvalidOperationException("The page should be a WPF control.");
}

return _serviceProvider.GetService(pageType) as FrameworkElement;
}
Expand Down
14 changes: 8 additions & 6 deletions src/Wpf.Ui.Demo.Mvvm/ViewModels/DataViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// This Source Code Form is subject to the terms of the MIT License.
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file, You can obtain one at https://opensource.org/licenses/MIT.
// Copyright (C) Leszek Pomianowski and WPF UI Contributors.
// All Rights Reserved.
Expand All @@ -14,23 +14,26 @@ public partial class DataViewModel : ObservableObject, INavigationAware
private bool _isInitialized = false;

[ObservableProperty]
private IEnumerable<DataColor> _colors;
private List<DataColor> _colors = [];

public void OnNavigatedTo()
{
if (!_isInitialized)
{
InitializeViewModel();
}
}

public void OnNavigatedFrom() { }

private void InitializeViewModel()
{
var random = new Random();
var colorCollection = new List<DataColor>();
Colors.Clear();

for (int i = 0; i < 8192; i++)
colorCollection.Add(
{
Colors.Add(
new DataColor
{
Color = new SolidColorBrush(
Expand All @@ -43,8 +46,7 @@ private void InitializeViewModel()
)
}
);

Colors = colorCollection;
}

_isInitialized = true;
}
Expand Down
Loading

0 comments on commit c0ace5b

Please sign in to comment.