Enable Static Analysis on programs plugin (#5847)

* Removing unused ProgramSettings constructor paramaters.

* Fix for:

Severity Code Description Project File Line Suppression State
Error CA1829 Use the "Count" property instead of Enumerable.Count(). Microsoft.Plugin.Program C:\Repos\PowerToys\src\modules\launcher\Plugins\Microsoft.Plugin.Program\Views\ProgramSetting.xaml.cs 182 Active

Severity Code Description Project File Line Suppression State
Error CA1827 Count() is used where Any() could be used instead to improve performance. Microsoft.Plugin.Program C:\Repos\PowerToys\src\modules\launcher\Plugins\Microsoft.Plugin.Program\Views\ProgramSetting.xaml.cs 287 Active

* Fixes for CA1031:
https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1031?view=vs-2019

* More fixes(Suppression) for CA1031 (Catching base exception)
https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1031?view=vs-2019

* Fix for nested types being externally visible.  CA1034
https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1034?view=vs-2019

* Fix for  CA1028 If possible, make the underlying type of Hresult System.Int32 instead of uint. Microsoft.Plugin.Program C:\Repos\PowerToys\src\modules\launcher\Plugins\Microsoft.Plugin.Program\Programs\UWP.cs 236 Active

* Fix for CA2227 Collection properties should be readonly.
https://docs.microsoft.com/en-us/visualstudio/code-quality/ca2227?view=vs-2019

Fix rror CA2211 Non-constant fields should not be visible
https://docs.microsoft.com/en-us/visualstudio/code-quality/ca2211?view=vs-2019

* CA2208: Instantiate argument exceptions correctly
https://docs.microsoft.com/en-us/visualstudio/code-quality/ca2208?view=vs-2019

* Win32.cs Static analysis fixes:

CA2200: Rethrow to preserve stack details
https://docs.microsoft.com/en-us/visualstudio/code-quality/ca2200?view=vs-2019

CA1825: Avoid zero-length array allocations
https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1825?view=vs-2019

CA2211: Non-constant fields should not be visible
https://docs.microsoft.com/en-us/visualstudio/code-quality/ca2211?view=vs-2019

* More Win32.cs static analysis fixes:

CA1307: Specify StringComparison
https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1307?view=vs-2019

Verifying query is not null before using it.
CA1062: Validate arguments of public methods
https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1062?view=vs-2019

CA1305: Specify IFormatProvider
https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1305?view=vs-2019

* UWPApplication.

CA2007: Do not directly await a Task
https://docs.microsoft.com/en-us/visualstudio/code-quality/ca2007?view=vs-2019

* Error CA2010 Consume the hresult returned by method 'Microsoft.Plugin.Program.Programs.AppxPackageHelper.IAppxManifestApplication.GetStringValue(string, out string)' and call Marshal.ThrowExceptionForHR.

Note:  CA2010 has been removed from future FXCop versions as it can provide incorrect advice for methods that don't return hresult (possibly when [PreserveSig] has been incorrectly labeled.)  I've verified the methods in question do still return hresults.

* More fixes for
Error CA2010 Consume the hresult returned by method and call Marshal.ThrowExceptionForHR.

* Fixes for:
Error CA2000 Call System.IDisposable.Dispose on object created before all references to it are out of scope.

* Suppress CA1031:Do not catch general exception types

* Fixes for
Error CA1034 Do not nest type ...

* Fixing Unit tests that were broken as per a previous fix.

* Fix for: CA1034: Nested types should not be visible:
https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1034?view=vs-2019

* Suppressing CA1707 for native/com types.
CA1707:Identifiers should not contain underscores

* Fix for Error CA1307 The behavior of 'string.Replace(string, string?)' could vary based on the current user's locale settings.

* Fixes for Error CA1825 Avoid unnecessary zero-length array allocations.  Use Array.Empty<string>() instead.

* Fix for: Error CA1823 Unused field 'IndexLock'.

* Fixes for CA1822: Mark members as static:
https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1822?view=vs-2019

* Fix for Error CA1819 Properties should not return arrays

* Fix for Error CA1806 ContextMenus calls Trim but does not use the new string instance that the method returns. Pass the instance as an argument to another method, assign the instance to a variable, or remove the call if it is unnecessary.

* Fix for Error CA1801 Parameter settings of method UpdateSettings is never used. Remove the parameter or use it in the method body.

* Fix for Error CA1724 The type name Settings conflicts in whole or in part with the namespace name 'Microsoft.PowerToys.Settings'.

* Includes FxCop for static analysis

* Fix for Error CA1717 Only FlagsAttribute enums should have plural names

* Suppress Stgm flags: Error CA1714 Flags enums should have plural names Microsoft.Plugin.Program

* Rename Win32 to Win32Program

Fix for: Error CA1724 The type name Win32 conflicts in whole or in part with the namespace name 'Microsoft.Win32' defined in the .NET Framework. Rename the type to eliminate the conflict.

* Fixes for Error CA1507 Use nameof in place of string literal

* Using ordinal instead of invariant culture when not symbolic comparison

* CA1308: Normalize strings to uppercase
https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1308?view=vs-2019

* CA1304: Specify CultureInfo
CA1305: Specify IFormatProvider

https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1305?view=vs-2019
https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1304?view=vs-2019

* CA1062: Validate arguments of public methods
https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1062?view=vs-2019

* CA1056: URI properties should not be strings
https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1056?view=vs-2019

Fix:   Making LogoURi private, as it is only used internally and operated on as a string

* Warning CA1052 Type 'AppxPackageHelper' is a static holder type but is neither static nor NotInheritable

* CA1060: Move P/Invokes to NativeMethods class
https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1060?view=vs-2019

* Programs Plugin - Treat warnings as errors true

* CA1031:Do not catch general exception types
Missed release only compile issue.  Making debug and release behave the same.

* Renaming 'Create' to 'CreateWin32Program' based on PR feedback.
This commit is contained in:
ryanbodrug-microsoft
2020-08-11 09:08:44 -07:00
committed by GitHub
parent 83de1686c0
commit ac10c988b9
24 changed files with 985 additions and 884 deletions

View File

@@ -14,15 +14,15 @@ namespace Microsoft.Plugin.Program.Storage
/// A repository for storing packaged applications such as UWP apps or appx packaged desktop apps.
/// This repository will also monitor for changes to the PackageCatelog and update the repository accordingly
/// </summary>
internal class PackageRepository : ListRepository<UWP.Application>, IProgramRepository
internal class PackageRepository : ListRepository<UWPApplication>, IProgramRepository
{
private IStorage<IList<UWP.Application>> _storage;
private IStorage<IList<UWPApplication>> _storage;
private IPackageCatalog _packageCatalog;
public PackageRepository(IPackageCatalog packageCatalog, IStorage<IList<UWP.Application>> storage)
public PackageRepository(IPackageCatalog packageCatalog, IStorage<IList<UWPApplication>> storage)
{
_storage = storage ?? throw new ArgumentNullException("storage", "StorageRepository requires an initialized storage interface");
_packageCatalog = packageCatalog ?? throw new ArgumentNullException("packageCatalog", "PackageRepository expects an interface to be able to subscribe to package events");
_storage = storage ?? throw new ArgumentNullException(nameof(storage), "StorageRepository requires an initialized storage interface");
_packageCatalog = packageCatalog ?? throw new ArgumentNullException(nameof(packageCatalog), "PackageRepository expects an interface to be able to subscribe to package events");
_packageCatalog.PackageInstalling += OnPackageInstalling;
_packageCatalog.PackageUninstalling += OnPackageUninstalling;
}
@@ -76,7 +76,7 @@ namespace Microsoft.Plugin.Program.Storage
var windows10 = new Version(10, 0);
var support = Environment.OSVersion.Version.Major >= windows10.Major;
var applications = support ? Programs.UWP.All() : new Programs.UWP.Application[] { };
var applications = support ? Programs.UWP.All() : Array.Empty<UWPApplication>();
Set(applications);
}
@@ -87,7 +87,7 @@ namespace Microsoft.Plugin.Program.Storage
public void Load()
{
var items = _storage.TryLoad(new Programs.UWP.Application[] { });
var items = _storage.TryLoad(Array.Empty<UWPApplication>());
Set(items);
}
}

View File

@@ -19,7 +19,7 @@ namespace Microsoft.Plugin.Program.Storage
}
// Returns an array of paths to be watched
private string[] GetPathsToWatch()
private static string[] GetPathsToWatch()
{
string[] paths = new string[]
{

View File

@@ -6,14 +6,15 @@ using Wox.Infrastructure.Logger;
using Wox.Infrastructure.Storage;
using System.IO;
using System.Linq;
using System.Globalization;
namespace Microsoft.Plugin.Program.Storage
{
using Win32 = Programs.Win32;
internal class Win32ProgramRepository : ListRepository<Programs.Win32>, IProgramRepository
using Win32Program = Programs.Win32Program;
internal class Win32ProgramRepository : ListRepository<Programs.Win32Program>, IProgramRepository
{
private IStorage<IList<Programs.Win32>> _storage;
private Settings _settings;
private IStorage<IList<Programs.Win32Program>> _storage;
private ProgramPluginSettings _settings;
private IList<IFileSystemWatcherWrapper> _fileSystemWatcherHelpers;
private string[] _pathsToWatch;
private int _numberOfPathsToWatch;
@@ -21,13 +22,13 @@ namespace Microsoft.Plugin.Program.Storage
private readonly string lnkExtension = ".lnk";
private readonly string urlExtension = ".url";
public Win32ProgramRepository(IList<IFileSystemWatcherWrapper> fileSystemWatcherHelpers, IStorage<IList<Win32>> storage, Settings settings, string[] pathsToWatch)
public Win32ProgramRepository(IList<IFileSystemWatcherWrapper> fileSystemWatcherHelpers, IStorage<IList<Win32Program>> storage, ProgramPluginSettings settings, string[] pathsToWatch)
{
this._fileSystemWatcherHelpers = fileSystemWatcherHelpers;
this._storage = storage ?? throw new ArgumentNullException("storage", "Win32ProgramRepository requires an initialized storage interface");
this._settings = settings ?? throw new ArgumentNullException("settings", "Win32ProgramRepository requires an initialized settings object");
this._storage = storage ?? throw new ArgumentNullException(nameof(storage), "Win32ProgramRepository requires an initialized storage interface");
this._settings = settings ?? throw new ArgumentNullException(nameof(settings), "Win32ProgramRepository requires an initialized settings object");
this._pathsToWatch = pathsToWatch;
this._numberOfPathsToWatch = pathsToWatch.Count();
this._numberOfPathsToWatch = pathsToWatch.Length;
InitializeFileSystemWatchers();
}
@@ -56,16 +57,17 @@ namespace Microsoft.Plugin.Program.Storage
// Enable it to search in sub folders as well
_fileSystemWatcherHelpers[index].IncludeSubdirectories = true;
}
}
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "Intentially keeping the process alive>")]
private void OnAppRenamed(object sender, RenamedEventArgs e)
{
string oldPath = e.OldFullPath;
string newPath = e.FullPath;
string extension = Path.GetExtension(newPath);
Programs.Win32 newApp = Programs.Win32.GetAppFromPath(newPath);
Programs.Win32 oldApp = null;
Programs.Win32Program newApp = Programs.Win32Program.GetAppFromPath(newPath);
Programs.Win32Program oldApp = null;
// Once the shortcut application is renamed, the old app does not exist and therefore when we try to get the FullPath we get the lnk path instead of the exe path
// This changes the hashCode() of the old application.
@@ -75,15 +77,15 @@ namespace Microsoft.Plugin.Program.Storage
{
if (extension.Equals(lnkExtension, StringComparison.OrdinalIgnoreCase))
{
oldApp = new Win32() { Name = Path.GetFileNameWithoutExtension(e.OldName), ExecutableName = newApp.ExecutableName, FullPath = newApp.FullPath };
oldApp = new Win32Program() { Name = Path.GetFileNameWithoutExtension(e.OldName), ExecutableName = newApp.ExecutableName, FullPath = newApp.FullPath };
}
else if (extension.Equals(urlExtension, StringComparison.OrdinalIgnoreCase))
{
oldApp = new Win32() { Name = Path.GetFileNameWithoutExtension(e.OldName), ExecutableName = Path.GetFileName(e.OldName), FullPath = newApp.FullPath };
oldApp = new Win32Program() { Name = Path.GetFileNameWithoutExtension(e.OldName), ExecutableName = Path.GetFileName(e.OldName), FullPath = newApp.FullPath };
}
else
{
oldApp = Win32.GetAppFromPath(oldPath);
oldApp = Win32Program.GetAppFromPath(oldPath);
}
}
catch (Exception ex)
@@ -102,13 +104,14 @@ namespace Microsoft.Plugin.Program.Storage
{
Add(newApp);
}
}
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "Intentionally keeping the process alive")]
private void OnAppDeleted(object sender, FileSystemEventArgs e)
{
string path = e.FullPath;
string extension = Path.GetExtension(path);
Programs.Win32 app = null;
Programs.Win32Program app = null;
try
{
@@ -123,7 +126,7 @@ namespace Microsoft.Plugin.Program.Storage
}
else
{
app = Programs.Win32.GetAppFromPath(path);
app = Programs.Win32Program.GetAppFromPath(path);
}
}
catch (Exception ex)
@@ -138,11 +141,11 @@ namespace Microsoft.Plugin.Program.Storage
}
// When a URL application is deleted, we can no longer get the HashCode directly from the path because the FullPath a Url app is the URL obtained from reading the file
private Win32 GetAppWithSameNameAndExecutable(string name, string executableName)
private Win32Program GetAppWithSameNameAndExecutable(string name, string executableName)
{
foreach (Win32 app in Items)
foreach (Win32Program app in Items)
{
if (name.Equals(app.Name, StringComparison.OrdinalIgnoreCase) && executableName.Equals(app.ExecutableName, StringComparison.OrdinalIgnoreCase))
if (name.Equals(app.Name, StringComparison.CurrentCultureIgnoreCase) && executableName.Equals(app.ExecutableName, StringComparison.CurrentCultureIgnoreCase))
{
return app;
}
@@ -152,11 +155,11 @@ namespace Microsoft.Plugin.Program.Storage
// To mitigate the issue faced (as stated above) when a shortcut application is renamed, the Exe FullPath and executable name must be obtained.
// Unlike the rename event args, since we do not have a newPath, we iterate through all the programs and find the one with the same LnkResolved path.
private Programs.Win32 GetAppWithSameLnkResolvedPath(string lnkResolvedPath)
private Programs.Win32Program GetAppWithSameLnkResolvedPath(string lnkResolvedPath)
{
foreach (Programs.Win32 app in Items)
foreach (Programs.Win32Program app in Items)
{
if (lnkResolvedPath.ToLower().Equals(app.LnkResolvedPath))
if (lnkResolvedPath.ToLower(CultureInfo.CurrentCulture).Equals(app.LnkResolvedPath, StringComparison.CurrentCultureIgnoreCase))
{
return app;
}
@@ -167,9 +170,9 @@ namespace Microsoft.Plugin.Program.Storage
private void OnAppCreated(object sender, FileSystemEventArgs e)
{
string path = e.FullPath;
if (!Path.GetExtension(path).Equals(urlExtension))
if (!Path.GetExtension(path).Equals(urlExtension, StringComparison.CurrentCultureIgnoreCase))
{
Programs.Win32 app = Programs.Win32.GetAppFromPath(path);
Programs.Win32Program app = Programs.Win32Program.GetAppFromPath(path);
if (app != null)
{
Add(app);
@@ -180,9 +183,9 @@ namespace Microsoft.Plugin.Program.Storage
private void OnAppChanged(object sender, FileSystemEventArgs e)
{
string path = e.FullPath;
if (Path.GetExtension(path).Equals(urlExtension))
if (Path.GetExtension(path).Equals(urlExtension, StringComparison.CurrentCultureIgnoreCase))
{
Programs.Win32 app = Programs.Win32.GetAppFromPath(path);
Programs.Win32Program app = Programs.Win32Program.GetAppFromPath(path);
if (app != null)
{
Add(app);
@@ -192,7 +195,7 @@ namespace Microsoft.Plugin.Program.Storage
public void IndexPrograms()
{
var applications = Programs.Win32.All(_settings);
var applications = Programs.Win32Program.All(_settings);
Set(applications);
}
@@ -203,7 +206,7 @@ namespace Microsoft.Plugin.Program.Storage
public void Load()
{
var items = _storage.TryLoad(new Programs.Win32[] { });
var items = _storage.TryLoad(Array.Empty<Win32Program>());
Set(items);
}