From 3363b248a08ce3412bbf6fa1577f079be9d386f5 Mon Sep 17 00:00:00 2001 From: Sergei Vorobev Date: Thu, 17 Nov 2016 11:20:04 -0800 Subject: [PATCH] Fix code errors found by PVS-Studio (#2695) * Fix errors found by PSV-studio analyzer http://www.viva64.com/en/b/0447/ * Remove old redundent comment errorAsts is passed by ref --- .../common/DisplayDatabase/displayDescriptionData_List.cs | 2 +- .../common/DisplayDatabase/displayDescriptionData_Wide.cs | 2 +- .../FormatAndOutput/common/FormatViewGenerator_Complex.cs | 3 +-- .../engine/CommandMetadata.cs | 7 ++----- src/System.Management.Automation/engine/ErrorPackage.cs | 2 +- .../engine/Modules/ImportModuleCommand.cs | 2 +- .../engine/SessionStateProviderAPIs.cs | 2 +- .../engine/hostifaces/RunspacePoolInternal.cs | 8 ++++---- src/System.Management.Automation/engine/parser/Parser.cs | 3 +-- .../engine/remoting/client/Job.cs | 6 +++--- .../engine/remoting/server/serverremotesession.cs | 2 +- .../namespaces/FileSystemProvider.cs | 2 +- .../utils/PowerShellETWTracer.cs | 2 +- 13 files changed, 19 insertions(+), 24 deletions(-) diff --git a/src/System.Management.Automation/commands/utility/FormatAndOutput/common/DisplayDatabase/displayDescriptionData_List.cs b/src/System.Management.Automation/commands/utility/FormatAndOutput/common/DisplayDatabase/displayDescriptionData_List.cs index 436ba7b894..e62c649aa8 100644 --- a/src/System.Management.Automation/commands/utility/FormatAndOutput/common/DisplayDatabase/displayDescriptionData_List.cs +++ b/src/System.Management.Automation/commands/utility/FormatAndOutput/common/DisplayDatabase/displayDescriptionData_List.cs @@ -349,7 +349,7 @@ namespace System.Management.Automation internal bool SafeForExport() { return DisplayEntry.SafeForExport() && - ItemSelectionCondition == null || ItemSelectionCondition.SafeForExport(); + (ItemSelectionCondition == null || ItemSelectionCondition.SafeForExport()); } internal bool CompatibleWithOldPowerShell() diff --git a/src/System.Management.Automation/commands/utility/FormatAndOutput/common/DisplayDatabase/displayDescriptionData_Wide.cs b/src/System.Management.Automation/commands/utility/FormatAndOutput/common/DisplayDatabase/displayDescriptionData_Wide.cs index 660271fbcb..4e25bb280a 100644 --- a/src/System.Management.Automation/commands/utility/FormatAndOutput/common/DisplayDatabase/displayDescriptionData_Wide.cs +++ b/src/System.Management.Automation/commands/utility/FormatAndOutput/common/DisplayDatabase/displayDescriptionData_Wide.cs @@ -244,7 +244,7 @@ namespace System.Management.Automation internal bool SafeForExport() { - return DisplayEntry.SafeForExport() && EntrySelectedBy == null || EntrySelectedBy.SafeForExport(); + return DisplayEntry.SafeForExport() && (EntrySelectedBy == null || EntrySelectedBy.SafeForExport()); } internal bool CompatibleWithOldPowerShell() diff --git a/src/System.Management.Automation/commands/utility/FormatAndOutput/common/FormatViewGenerator_Complex.cs b/src/System.Management.Automation/commands/utility/FormatAndOutput/common/FormatViewGenerator_Complex.cs index 81ce6cea20..c01c363db1 100644 --- a/src/System.Management.Automation/commands/utility/FormatAndOutput/common/FormatViewGenerator_Complex.cs +++ b/src/System.Management.Automation/commands/utility/FormatAndOutput/common/FormatViewGenerator_Complex.cs @@ -433,8 +433,7 @@ namespace Microsoft.PowerShell.Commands.Internal.Format TraversalInfo level = new TraversalInfo(0, maxDepth); List mshParameterList = null; - if (inputParameters != null) - mshParameterList = inputParameters.mshParameterList; + mshParameterList = inputParameters.mshParameterList; // create a top level entry as root of the tree ComplexViewEntry cve = new ComplexViewEntry(); diff --git a/src/System.Management.Automation/engine/CommandMetadata.cs b/src/System.Management.Automation/engine/CommandMetadata.cs index b3502b598d..fc57bf4755 100644 --- a/src/System.Management.Automation/engine/CommandMetadata.cs +++ b/src/System.Management.Automation/engine/CommandMetadata.cs @@ -189,12 +189,9 @@ namespace System.Management.Automation _parameters = new Dictionary(other.Parameters.Count, StringComparer.OrdinalIgnoreCase); // deep copy - if (other.Parameters != null) + foreach (KeyValuePair entry in other.Parameters) { - foreach (KeyValuePair entry in other.Parameters) - { - _parameters.Add(entry.Key, new ParameterMetadata(entry.Value)); - } + _parameters.Add(entry.Key, new ParameterMetadata(entry.Value)); } // deep copy of the collection, collection items (Attributes) copied by reference diff --git a/src/System.Management.Automation/engine/ErrorPackage.cs b/src/System.Management.Automation/engine/ErrorPackage.cs index f78eb8ee66..b8edf4f060 100644 --- a/src/System.Management.Automation/engine/ErrorPackage.cs +++ b/src/System.Management.Automation/engine/ErrorPackage.cs @@ -1129,7 +1129,7 @@ namespace System.Management.Automation { PopulateProperties(exception, targetObject, fullyQualifiedErrorId, errorCategory, errorCategory_Activity, errorCategory_Reason, errorCategory_TargetName, errorCategory_TargetType, - errorDetails_Message, errorDetails_Message, errorDetails_RecommendedAction, null); + errorCategory_Message, errorDetails_Message, errorDetails_RecommendedAction, null); } private void PopulateProperties(Exception exception, diff --git a/src/System.Management.Automation/engine/Modules/ImportModuleCommand.cs b/src/System.Management.Automation/engine/Modules/ImportModuleCommand.cs index acabd4f007..0382232a37 100644 --- a/src/System.Management.Automation/engine/Modules/ImportModuleCommand.cs +++ b/src/System.Management.Automation/engine/Modules/ImportModuleCommand.cs @@ -1660,7 +1660,7 @@ namespace Microsoft.PowerShell.Commands /// protected override void ProcessRecord() { - if (BaseMaximumVersion != null && BaseMaximumVersion != null && BaseMaximumVersion < BaseMinimumVersion) + if (BaseMaximumVersion != null && BaseMinimumVersion != null && BaseMaximumVersion < BaseMinimumVersion) { string message = StringUtil.Format(Modules.MinimumVersionAndMaximumVersionInvalidRange, BaseMinimumVersion, BaseMaximumVersion); throw new PSArgumentOutOfRangeException(message); diff --git a/src/System.Management.Automation/engine/SessionStateProviderAPIs.cs b/src/System.Management.Automation/engine/SessionStateProviderAPIs.cs index 3ef80291a7..2196458f4b 100644 --- a/src/System.Management.Automation/engine/SessionStateProviderAPIs.cs +++ b/src/System.Management.Automation/engine/SessionStateProviderAPIs.cs @@ -1001,7 +1001,7 @@ namespace System.Management.Automation { ProviderNotFoundException e = new ProviderNotFoundException( - providerName.ToString(), + "null", SessionStateCategory.CmdletProvider, "ProviderNotFound", SessionStateStrings.ProviderNotFound); diff --git a/src/System.Management.Automation/engine/hostifaces/RunspacePoolInternal.cs b/src/System.Management.Automation/engine/hostifaces/RunspacePoolInternal.cs index c332583fe8..fd65d9f59c 100644 --- a/src/System.Management.Automation/engine/hostifaces/RunspacePoolInternal.cs +++ b/src/System.Management.Automation/engine/hostifaces/RunspacePoolInternal.cs @@ -574,14 +574,14 @@ namespace System.Management.Automation.Runspaces.Internal int unUsedCapacity = (maxPoolSz - totalRunspaces) < 0 ? 0 : (maxPoolSz - totalRunspaces); return (pool.Count + unUsedCapacity); } - else if (stateInfo.State != RunspacePoolState.BeforeOpen && stateInfo.State != RunspacePoolState.Opening) - { - throw new InvalidOperationException(HostInterfaceExceptionsStrings.RunspacePoolNotOpened); - } else if (stateInfo.State == RunspacePoolState.Disconnected) { throw new InvalidOperationException(RunspacePoolStrings.CannotWhileDisconnected); } + else if (stateInfo.State != RunspacePoolState.BeforeOpen && stateInfo.State != RunspacePoolState.Opening) + { + throw new InvalidOperationException(HostInterfaceExceptionsStrings.RunspacePoolNotOpened); + } else { return maxPoolSz; diff --git a/src/System.Management.Automation/engine/parser/Parser.cs b/src/System.Management.Automation/engine/parser/Parser.cs index 7ebedd8cc7..65a45bcf68 100644 --- a/src/System.Management.Automation/engine/parser/Parser.cs +++ b/src/System.Management.Automation/engine/parser/Parser.cs @@ -4970,9 +4970,8 @@ namespace System.Management.Automation.Language } else { - errorAsts.Concat(exceptionTypes); + errorAsts.AddRange(exceptionTypes); } - // REVIEW: seems like some code is missing here, errorAsts isn't used } return null; } diff --git a/src/System.Management.Automation/engine/remoting/client/Job.cs b/src/System.Management.Automation/engine/remoting/client/Job.cs index ff3162e9d6..913d8587c7 100644 --- a/src/System.Management.Automation/engine/remoting/client/Job.cs +++ b/src/System.Management.Automation/engine/remoting/client/Job.cs @@ -1873,7 +1873,7 @@ namespace System.Management.Automation foreach (Job j in ChildJobs) { PSRemotingChildJob child = j as PSRemotingChildJob; - if (j == null) continue; + if (child == null) continue; if (String.Equals(child.Runspace.ConnectionInfo.ComputerName, computerName, StringComparison.OrdinalIgnoreCase)) { @@ -1897,7 +1897,7 @@ namespace System.Management.Automation foreach (Job j in ChildJobs) { PSRemotingChildJob child = j as PSRemotingChildJob; - if (j == null) continue; + if (child == null) continue; if (child.Runspace.InstanceId.Equals(runspace.InstanceId)) { returnJobList.Add(child); @@ -1920,7 +1920,7 @@ namespace System.Management.Automation foreach (Job j in ChildJobs) { PSRemotingChildJob child = j as PSRemotingChildJob; - if (j == null) continue; + if (child == null) continue; if (child.Helper.Equals(helper)) { returnJobList.Add(child); diff --git a/src/System.Management.Automation/engine/remoting/server/serverremotesession.cs b/src/System.Management.Automation/engine/remoting/server/serverremotesession.cs index 25fc3d3233..9157455b35 100644 --- a/src/System.Management.Automation/engine/remoting/server/serverremotesession.cs +++ b/src/System.Management.Automation/engine/remoting/server/serverremotesession.cs @@ -630,7 +630,7 @@ namespace System.Management.Automation.Remoting int clientRequestedMinRunspaces = -1; int clientRequestedMaxRunspaces = -1; bool clientRequestedRunspaceCount = false; - if (connectRunspacePoolObject.Data.Properties[RemoteDataNameStrings.MinRunspaces] != null && connectRunspacePoolObject.Data.Properties[RemoteDataNameStrings.MinRunspaces] != null) + if (connectRunspacePoolObject.Data.Properties[RemoteDataNameStrings.MinRunspaces] != null && connectRunspacePoolObject.Data.Properties[RemoteDataNameStrings.MaxRunspaces] != null) { try { diff --git a/src/System.Management.Automation/namespaces/FileSystemProvider.cs b/src/System.Management.Automation/namespaces/FileSystemProvider.cs index 8e94801295..607ef6e47c 100644 --- a/src/System.Management.Automation/namespaces/FileSystemProvider.cs +++ b/src/System.Management.Automation/namespaces/FileSystemProvider.cs @@ -4125,7 +4125,7 @@ namespace Microsoft.PowerShell.Commands { // Check if the remote source file has any alternate data streams ArrayList remoteFileStreams = GetRemoteSourceAlternateStreams(ps, sourceFileFullName); - if ((remoteFileStreams.Count > 0) && (remoteFileStreams != null)) + if ((remoteFileStreams != null) && (remoteFileStreams.Count > 0)) { foreach (string streamName in remoteFileStreams) { diff --git a/src/System.Management.Automation/utils/PowerShellETWTracer.cs b/src/System.Management.Automation/utils/PowerShellETWTracer.cs index b853d751d4..ad27026a80 100644 --- a/src/System.Management.Automation/utils/PowerShellETWTracer.cs +++ b/src/System.Management.Automation/utils/PowerShellETWTracer.cs @@ -1085,7 +1085,7 @@ namespace System.Management.Automation.Tracing { return DebugChannel.TraceDebug(PowerShellTraceEvent.Job, PowerShellTraceOperationCode.Method, PowerShellTraceTask.None, - job.Id.ToString(CultureInfo.InvariantCulture), job.InstanceId.ToString(), "NULL job"); + "", "", "NULL job"); } }