From 2762484822bd2aa47f6e3e2415113d5148c4d4bf Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Thu, 5 Dec 2024 07:16:44 +1100 Subject: [PATCH] Remove some redundant casts (#6167) --- src/NuGet.Clients/NuGet.CommandLine/MsBuildToolset.cs | 2 +- src/NuGet.Clients/NuGet.CommandLine/MsBuildUtility.cs | 7 +------ .../NuGet.Console/WpfConsole/WpfConsole.cs | 10 +++++----- .../Options/AddMappingDialog.xaml.cs | 6 +++--- .../OptionsPageActivator.cs | 4 ++-- .../PackageFeeds/RecommenderPackageFeed.cs | 6 +++--- .../NuGet.Tools/PackageManagerLaunchService.cs | 4 ++-- .../VsProjectJsonToPackageReferenceMigrator.cs | 4 +--- .../Utility/ProjectPackagesPrintUtility.cs | 2 +- .../Settings/Items/TrustedSignerItem.cs | 2 +- .../NuGet.PackageManagement/NuGetPackageManager.cs | 4 ++-- .../Projects/ProjectJsonNuGetProject.cs | 2 +- .../Rules/DependenciesGroupsForEachTFMRule.cs | 2 +- .../Verification/AllowListVerificationProvider.cs | 2 +- .../JsonPackageSpecReader.Utf8JsonStreamReader.cs | 6 +++--- .../NuGet.ProjectModel/Utf8JsonStreamReader.cs | 4 ++-- 16 files changed, 30 insertions(+), 37 deletions(-) diff --git a/src/NuGet.Clients/NuGet.CommandLine/MsBuildToolset.cs b/src/NuGet.Clients/NuGet.CommandLine/MsBuildToolset.cs index 546e4d09084..92041cd0204 100644 --- a/src/NuGet.Clients/NuGet.CommandLine/MsBuildToolset.cs +++ b/src/NuGet.Clients/NuGet.CommandLine/MsBuildToolset.cs @@ -96,7 +96,7 @@ private static DateTime ConvertFILETIMEToDateTime(FILETIME time) { long highBits = time.dwHighDateTime; highBits = highBits << 32; - return DateTime.FromFileTimeUtc(highBits | (long)(uint)time.dwLowDateTime); + return DateTime.FromFileTimeUtc(highBits | (uint)time.dwLowDateTime); } public static string GetMsBuildDirFromVsDir(string vsDir) diff --git a/src/NuGet.Clients/NuGet.CommandLine/MsBuildUtility.cs b/src/NuGet.Clients/NuGet.CommandLine/MsBuildUtility.cs index a2f8ace2b0a..39e84ee714f 100644 --- a/src/NuGet.Clients/NuGet.CommandLine/MsBuildUtility.cs +++ b/src/NuGet.Clients/NuGet.CommandLine/MsBuildUtility.cs @@ -928,18 +928,13 @@ private static List GetInstalledSxsToolsets() ISetupConfiguration configuration; try { - configuration = new SetupConfiguration() as ISetupConfiguration2; + configuration = new SetupConfiguration(); } catch (Exception) { return null; // No COM class } - if (configuration == null) - { - return null; - } - var enumerator = configuration.EnumInstances(); if (enumerator == null) { diff --git a/src/NuGet.Clients/NuGet.Console/WpfConsole/WpfConsole.cs b/src/NuGet.Clients/NuGet.Console/WpfConsole/WpfConsole.cs index 81e8af6ad8d..41073ea6c2d 100644 --- a/src/NuGet.Clients/NuGet.Console/WpfConsole/WpfConsole.cs +++ b/src/NuGet.Clients/NuGet.Console/WpfConsole/WpfConsole.cs @@ -637,7 +637,7 @@ private async Task WriteProgressAsync(string operation, int percentComplete) 1 /* in progress */, operation, (uint)percentComplete, - (uint)100); + 100); } } @@ -648,8 +648,8 @@ private void HideProgress() ref _pdwCookieForStatusBar, 0 /* completed */, string.Empty, - (uint)100, - (uint)100); + 100, + 100); } private async Task HideProgressAsync() @@ -660,8 +660,8 @@ private async Task HideProgressAsync() ref _pdwCookieForStatusBar, 0 /* completed */, string.Empty, - (uint)100, - (uint)100); + 100, + 100); } public void SetExecutionMode(bool isExecuting) diff --git a/src/NuGet.Clients/NuGet.PackageManagement.UI/Options/AddMappingDialog.xaml.cs b/src/NuGet.Clients/NuGet.PackageManagement.UI/Options/AddMappingDialog.xaml.cs index ef92f677e08..1732ec4f8d4 100644 --- a/src/NuGet.Clients/NuGet.PackageManagement.UI/Options/AddMappingDialog.xaml.cs +++ b/src/NuGet.Clients/NuGet.PackageManagement.UI/Options/AddMappingDialog.xaml.cs @@ -92,9 +92,9 @@ private void ExecuteAddMapping(object parameter) var viewModel = new SourceMappingViewModel(packageId, packageSources); _parent.SourceMappingsCollection.Add(viewModel); - (_parent.ShowAddDialogCommand as DelegateCommand).RaiseCanExecuteChanged(); - (_parent.RemoveMappingCommand as DelegateCommand).RaiseCanExecuteChanged(); - (_parent.RemoveAllMappingsCommand as DelegateCommand).RaiseCanExecuteChanged(); + _parent.ShowAddDialogCommand.RaiseCanExecuteChanged(); + _parent.RemoveMappingCommand.RaiseCanExecuteChanged(); + _parent.RemoveAllMappingsCommand.RaiseCanExecuteChanged(); bool isGlobbing = packageId.Contains("*"); var evt = NavigatedTelemetryEvent.CreateWithAddPackageSourceMapping( diff --git a/src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/OptionsPageActivator.cs b/src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/OptionsPageActivator.cs index 63cecbf22f9..d240c0ac469 100644 --- a/src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/OptionsPageActivator.cs +++ b/src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/OptionsPageActivator.cs @@ -100,8 +100,8 @@ private async Task ShowOptionsPageAsync(string optionsPageGuid) IVsUIShell vsUIShell = await _vsUIShell.GetValueAsync(); vsUIShell.PostExecCommand( ref toolsGroupGuid, - (uint)VSConstants.cmdidToolsOptions, - (uint)0, + VSConstants.cmdidToolsOptions, + 0, ref targetGuid); } } diff --git a/src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/PackageFeeds/RecommenderPackageFeed.cs b/src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/PackageFeeds/RecommenderPackageFeed.cs index b80847b0fe4..6ac84b20c16 100644 --- a/src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/PackageFeeds/RecommenderPackageFeed.cs +++ b/src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/PackageFeeds/RecommenderPackageFeed.cs @@ -33,7 +33,7 @@ public class RecommenderPackageFeed : IPackageFeed private readonly IPackageMetadataProvider _metadataProvider; private readonly Common.ILogger _logger; - public (string modelVersion, string vsixVersion) VersionInfo { get; set; } = (modelVersion: (string)null, vsixVersion: (string)null); + public (string modelVersion, string vsixVersion) VersionInfo { get; set; } = (modelVersion: null, vsixVersion: null); private readonly AsyncLazyNuGetRecommender _nuGetRecommender; @@ -137,8 +137,8 @@ private async Task> RecommendPackagesAsync( if (!(NuGetRecommender is null)) { var VersionDict = NuGetRecommender.GetVersionInfo(); - VersionInfo = (modelVersion: VersionDict.ContainsKey("Model") ? VersionDict["Model"] : (string)null, - vsixVersion: VersionDict.ContainsKey("Vsix") ? VersionDict["Vsix"] : (string)null); + VersionInfo = (modelVersion: VersionDict.ContainsKey("Model") ? VersionDict["Model"] : null, + vsixVersion: VersionDict.ContainsKey("Vsix") ? VersionDict["Vsix"] : null); } } diff --git a/src/NuGet.Clients/NuGet.Tools/PackageManagerLaunchService.cs b/src/NuGet.Clients/NuGet.Tools/PackageManagerLaunchService.cs index 7a15ac2a525..72eeb0a2262 100644 --- a/src/NuGet.Clients/NuGet.Tools/PackageManagerLaunchService.cs +++ b/src/NuGet.Clients/NuGet.Tools/PackageManagerLaunchService.cs @@ -28,8 +28,8 @@ public void LaunchSolutionPackageManager() var guidNuGetDialog = GuidList.guidNuGetDialogCmdSet; vsUIShell.PostExecCommand( ref guidNuGetDialog, - (uint)PkgCmdIDList.cmdidAddPackageDialogForSolution, - (uint)0, + PkgCmdIDList.cmdidAddPackageDialogForSolution, + 0, ref targetGuid); }).PostOnFailure(nameof(PackageManagerLaunchService)); } diff --git a/src/NuGet.Clients/NuGet.VisualStudio.Implementation/Extensibility/VsProjectJsonToPackageReferenceMigrator.cs b/src/NuGet.Clients/NuGet.VisualStudio.Implementation/Extensibility/VsProjectJsonToPackageReferenceMigrator.cs index ce37fe32bb2..3b8ff6b495e 100644 --- a/src/NuGet.Clients/NuGet.VisualStudio.Implementation/Extensibility/VsProjectJsonToPackageReferenceMigrator.cs +++ b/src/NuGet.Clients/NuGet.VisualStudio.Implementation/Extensibility/VsProjectJsonToPackageReferenceMigrator.cs @@ -9,7 +9,6 @@ using System.Threading.Tasks; using Microsoft; using NuGet.PackageManagement.VisualStudio; -using NuGet.ProjectManagement.Projects; using NuGet.VisualStudio.Etw; using NuGet.VisualStudio.Implementation.Resources; using NuGet.VisualStudio.Telemetry; @@ -93,8 +92,7 @@ private async Task MigrateProjectToPackageRefAsync(string projectUniqueN project, optionalContext: null); Assumes.Present(legacyPackageRefBasedProject); - await ProjectJsonToPackageRefMigrator.MigrateAsync( - legacyPackageRefBasedProject as BuildIntegratedNuGetProject); + await ProjectJsonToPackageRefMigrator.MigrateAsync(legacyPackageRefBasedProject); var result = new VsProjectJsonToPackageReferenceMigrateResult(success: true, errorMessage: null); await nuGetProject.SaveAsync(CancellationToken.None); await _solutionManager.Value.UpgradeProjectToPackageReferenceAsync(nuGetProject); diff --git a/src/NuGet.Core/NuGet.CommandLine.XPlat/Utility/ProjectPackagesPrintUtility.cs b/src/NuGet.Core/NuGet.CommandLine.XPlat/Utility/ProjectPackagesPrintUtility.cs index e3ca6401a27..21b0c0b98a2 100644 --- a/src/NuGet.Core/NuGet.CommandLine.XPlat/Utility/ProjectPackagesPrintUtility.cs +++ b/src/NuGet.Core/NuGet.CommandLine.XPlat/Utility/ProjectPackagesPrintUtility.cs @@ -122,7 +122,7 @@ internal static IEnumerable BuildPackagesTable( // Include "Requested" version column for top level package list if (!printingTransitive) { - valueSelectors.Add(p => new FormattedCell((p as ListReportPackage)?.RequestedVersion)); + valueSelectors.Add(p => new FormattedCell(p?.RequestedVersion)); } // "Resolved" version diff --git a/src/NuGet.Core/NuGet.Configuration/Settings/Items/TrustedSignerItem.cs b/src/NuGet.Core/NuGet.Configuration/Settings/Items/TrustedSignerItem.cs index a15ef051671..0a24d574fa2 100644 --- a/src/NuGet.Core/NuGet.Configuration/Settings/Items/TrustedSignerItem.cs +++ b/src/NuGet.Core/NuGet.Configuration/Settings/Items/TrustedSignerItem.cs @@ -134,7 +134,7 @@ internal override void Update(SettingItem other) } else if (cert is SettingItem item) { - item.Update(otherChild as SettingItem); + item.Update(otherChild); } } diff --git a/src/NuGet.Core/NuGet.PackageManagement/NuGetPackageManager.cs b/src/NuGet.Core/NuGet.PackageManagement/NuGetPackageManager.cs index a0a795c2cc1..c188457c536 100644 --- a/src/NuGet.Core/NuGet.PackageManagement/NuGetPackageManager.cs +++ b/src/NuGet.Core/NuGet.PackageManagement/NuGetPackageManager.cs @@ -1031,7 +1031,7 @@ private async Task> PreviewUpdatePackagesForClas if (packageIdentity.HasVersion) { preferredVersions[packageIdentity.Id] = packageIdentity; - ((List)primaryTargets).Add(packageIdentity); + primaryTargets.Add(packageIdentity); } // Otherwise we just have the Id and so we wil explicitly not prefer the one currently installed else @@ -3477,7 +3477,7 @@ await BuildIntegratedRestoreUtility.ExecuteInitPs1ScriptsAsync( if (dgSpecForParents.Restore.Count > 0) { // Restore and commit the lock file to disk regardless of the result - // This will restore all parents in a single restore + // This will restore all parents in a single restore await DependencyGraphRestoreUtility.RestoreAsync( dgSpecForParents, referenceContext, diff --git a/src/NuGet.Core/NuGet.PackageManagement/Projects/ProjectJsonNuGetProject.cs b/src/NuGet.Core/NuGet.PackageManagement/Projects/ProjectJsonNuGetProject.cs index d59b3306373..aef27747d33 100644 --- a/src/NuGet.Core/NuGet.PackageManagement/Projects/ProjectJsonNuGetProject.cs +++ b/src/NuGet.Core/NuGet.PackageManagement/Projects/ProjectJsonNuGetProject.cs @@ -369,7 +369,7 @@ private async Task UpdateFrameworkAsync(JObject json) { // project.json can have only one target framework JsonConfigUtility.ClearFrameworks(json); - JsonConfigUtility.AddFramework(json, newTargetFramework as NuGetFramework); + JsonConfigUtility.AddFramework(json, newTargetFramework); } } } diff --git a/src/NuGet.Core/NuGet.Packaging/Rules/DependenciesGroupsForEachTFMRule.cs b/src/NuGet.Core/NuGet.Packaging/Rules/DependenciesGroupsForEachTFMRule.cs index a6f9d7aa91a..06ede31ceed 100644 --- a/src/NuGet.Core/NuGet.Packaging/Rules/DependenciesGroupsForEachTFMRule.cs +++ b/src/NuGet.Core/NuGet.Packaging/Rules/DependenciesGroupsForEachTFMRule.cs @@ -35,7 +35,7 @@ public IEnumerable Validate(PackageArchiveReader package) internal IEnumerable GenerateWarnings(HashSet compatNotExactMatches, HashSet noExactMatchesFromFile, HashSet noExactMatchesFromNuspec) { - (string noExactMatchString, string compatMatchString) = GenerateWarningString(noExactMatchesFromFile, noExactMatchesFromNuspec, (ICollection)compatNotExactMatches); + (string noExactMatchString, string compatMatchString) = GenerateWarningString(noExactMatchesFromFile, noExactMatchesFromNuspec, compatNotExactMatches); var issues = new List(); diff --git a/src/NuGet.Core/NuGet.Packaging/Signing/Verification/AllowListVerificationProvider.cs b/src/NuGet.Core/NuGet.Packaging/Signing/Verification/AllowListVerificationProvider.cs index 95ba4c6948c..dee44fd373e 100644 --- a/src/NuGet.Core/NuGet.Packaging/Signing/Verification/AllowListVerificationProvider.cs +++ b/src/NuGet.Core/NuGet.Packaging/Signing/Verification/AllowListVerificationProvider.cs @@ -114,7 +114,7 @@ private bool IsSignatureAllowed( if (IsSignatureTargeted(certificateHashEntry.Target, repositoryCountersignature.Value) && StringComparer.OrdinalIgnoreCase.Equals(certificateHashEntry.Fingerprint, countersignatureCertificateFingerprint)) { - if (ShouldVerifyOwners(certificateHashEntry as TrustedSignerAllowListEntry, repositoryCountersignature.Value as IRepositorySignature, out var allowedOwners, out var actualOwners)) + if (ShouldVerifyOwners(certificateHashEntry as TrustedSignerAllowListEntry, repositoryCountersignature.Value, out var allowedOwners, out var actualOwners)) { if (allowedOwners.Intersect(actualOwners).Any()) { diff --git a/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.Utf8JsonStreamReader.cs b/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.Utf8JsonStreamReader.cs index 8d9a7eb9409..378e949fca5 100644 --- a/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.Utf8JsonStreamReader.cs +++ b/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.Utf8JsonStreamReader.cs @@ -189,7 +189,7 @@ internal static PackageSpec GetPackageSpec(ref Utf8JsonStreamReader jsonReader, { ReadScripts(ref jsonReader, packageSpec); } -#pragma warning restore CS0612 // Type or member is +#pragma warning restore CS0612 // Type or member is else if (jsonReader.ValueTextEquals(DependenciesPropertyName)) { ReadDependencies( @@ -881,7 +881,7 @@ private static void ReadMappings(ref Utf8JsonStreamReader jsonReader, string map { var files = new IncludeExcludeFiles() { - Include = new[] { (string)jsonReader.GetString() } + Include = new[] { jsonReader.GetString() } }; mappings.Add(mappingKey, files); @@ -1671,7 +1671,7 @@ private static void ReadScripts(ref Utf8JsonStreamReader jsonReader, PackageSpec { if (jsonReader.TokenType == JsonTokenType.String) { - packageSpec.Scripts[propertyName] = new string[] { (string)jsonReader.GetString() }; + packageSpec.Scripts[propertyName] = new string[] { jsonReader.GetString() }; } else if (jsonReader.TokenType == JsonTokenType.StartArray) { diff --git a/src/NuGet.Core/NuGet.ProjectModel/Utf8JsonStreamReader.cs b/src/NuGet.Core/NuGet.ProjectModel/Utf8JsonStreamReader.cs index f328fcca429..3e59619263a 100644 --- a/src/NuGet.Core/NuGet.ProjectModel/Utf8JsonStreamReader.cs +++ b/src/NuGet.Core/NuGet.ProjectModel/Utf8JsonStreamReader.cs @@ -250,7 +250,7 @@ internal IReadOnlyList ReadNextStringOrArrayOfStringsAsReadOnlyList() switch (_reader.TokenType) { case JsonTokenType.String: - return new[] { (string)_reader.GetString() }; + return new[] { _reader.GetString() }; case JsonTokenType.StartArray: return ReadStringArrayAsReadOnlyListFromArrayStart(); @@ -312,7 +312,7 @@ private void GetMoreBytesFromStream() } /// - /// Loops through the stream and reads it into the buffer until the buffer is full or the stream is empty, creates the Utf8JsonReader. + /// Loops through the stream and reads it into the buffer until the buffer is full or the stream is empty, creates the Utf8JsonReader. /// private void ReadStreamIntoBuffer(JsonReaderState jsonReaderState) {