From 6ab5179ba89bd1e32def6d632b89d0537b0a142b Mon Sep 17 00:00:00 2001 From: xtqqczze Date: Mon, 27 Jul 2020 12:34:37 +0100 Subject: [PATCH 1/4] Autofix RCS1172: Use 'is' operator instead of 'as' operator https://github.com/JosefPihrt/Roslynator/blob/master/docs/analyzers/RCS1172.md --- .../commands/utility/ShowCommand/ShowCommandCommandInfo.cs | 2 +- .../utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs | 2 +- .../remoting/commands/NewPSSessionConfigurationFile.cs | 6 +++--- .../engine/remoting/fanin/InitialSessionStateProvider.cs | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ShowCommand/ShowCommandCommandInfo.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ShowCommand/ShowCommandCommandInfo.cs index c0226471bdd..e0358bd5b52 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ShowCommand/ShowCommandCommandInfo.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ShowCommand/ShowCommandCommandInfo.cs @@ -92,7 +92,7 @@ public ShowCommandCommandInfo(PSObject other) var parameterSets = (other.Members["ParameterSets"].Value as PSObject).BaseObject as System.Collections.ArrayList; this.ParameterSets = GetObjectEnumerable(parameterSets).Cast().Select(x => new ShowCommandParameterSetInfo(x)).ToList().AsReadOnly(); - if (other.Members["Module"] != null && other.Members["Module"].Value as PSObject != null) + if (other.Members["Module"] != null && other.Members["Module"].Value is PSObject) { this.Module = new ShowCommandModuleInfo(other.Members["Module"].Value as PSObject); } diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs index 68c69d982a7..d2fb4d4b845 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs @@ -1737,7 +1737,7 @@ internal long SetRequestContent(HttpRequestMessage request, XmlNode xmlNode) byte[] bytes = null; XmlDocument doc = xmlNode as XmlDocument; - if (doc != null && (doc.FirstChild as XmlDeclaration) != null) + if (doc != null && doc.FirstChild is XmlDeclaration) { XmlDeclaration decl = doc.FirstChild as XmlDeclaration; Encoding encoding = Encoding.GetEncoding(decl.Encoding); diff --git a/src/System.Management.Automation/engine/remoting/commands/NewPSSessionConfigurationFile.cs b/src/System.Management.Automation/engine/remoting/commands/NewPSSessionConfigurationFile.cs index 8a7a9b88b88..66cf770109c 100644 --- a/src/System.Management.Automation/engine/remoting/commands/NewPSSessionConfigurationFile.cs +++ b/src/System.Management.Automation/engine/remoting/commands/NewPSSessionConfigurationFile.cs @@ -967,7 +967,7 @@ protected override void ProcessRecord() ThrowTerminatingError(e.ErrorRecord); } - if ((hashtable[ConfigFileConstants.FunctionValueToken] as ScriptBlock) == null) + if (!(hashtable[ConfigFileConstants.FunctionValueToken] is ScriptBlock)) { PSArgumentException e = new PSArgumentException(StringUtil.Format(RemotingErrorIdStrings.DISCKeyMustBeScriptBlock, ConfigFileConstants.FunctionValueToken, ConfigFileConstants.FunctionDefinitions, _path)); @@ -1714,7 +1714,7 @@ protected override void ProcessRecord() ThrowTerminatingError(e.ErrorRecord); } - if ((hashtable[ConfigFileConstants.FunctionValueToken] as ScriptBlock) == null) + if (!(hashtable[ConfigFileConstants.FunctionValueToken] is ScriptBlock)) { PSArgumentException e = new PSArgumentException(StringUtil.Format(RemotingErrorIdStrings.DISCKeyMustBeScriptBlock, ConfigFileConstants.FunctionValueToken, ConfigFileConstants.FunctionDefinitions, _path)); @@ -1951,7 +1951,7 @@ internal static string CombineHashtable(IDictionary table, StreamWriter writer, sb.AppendFormat("{0," + (4 * (indent + 1)) + "}", string.Empty); sb.Append(QuoteName(key)); sb.Append(" = "); - if ((table[key] as ScriptBlock) != null) + if (table[key] is ScriptBlock) { sb.Append(WrapScriptBlock(table[key].ToString())); continue; diff --git a/src/System.Management.Automation/engine/remoting/fanin/InitialSessionStateProvider.cs b/src/System.Management.Automation/engine/remoting/fanin/InitialSessionStateProvider.cs index 4f84dfadbdf..9b64f70616d 100644 --- a/src/System.Management.Automation/engine/remoting/fanin/InitialSessionStateProvider.cs +++ b/src/System.Management.Automation/engine/remoting/fanin/InitialSessionStateProvider.cs @@ -1209,7 +1209,7 @@ private static bool FunctionDefinitionsTypeValidationCallback(string key, object return false; } - if ((hashtable[FunctionValueToken] as ScriptBlock) == null) + if (!(hashtable[FunctionValueToken] is ScriptBlock)) { cmdlet.WriteVerbose(StringUtil.Format(RemotingErrorIdStrings.DISCKeyMustBeScriptBlock, FunctionValueToken, key, path)); return false; @@ -2221,7 +2221,7 @@ public override InitialSessionState GetInitialSessionState(PSSenderInfo senderIn foreach (Hashtable variable in variables) { if (variable.ContainsKey(ConfigFileConstants.VariableValueToken) && - ((variable[ConfigFileConstants.VariableValueToken] as ScriptBlock) != null)) + (variable[ConfigFileConstants.VariableValueToken] is ScriptBlock)) { iss.DynamicVariablesToDefine.Add(variable); continue; From abaf6e13e99fb495736a506f1ad5b2f3c214d9e7 Mon Sep 17 00:00:00 2001 From: Aditya Patwardhan Date: Thu, 30 Jul 2020 12:15:07 -0700 Subject: [PATCH 2/4] Apply suggestions from code review Co-authored-by: Ilya --- .../engine/remoting/commands/NewPSSessionConfigurationFile.cs | 4 ++-- .../engine/remoting/fanin/InitialSessionStateProvider.cs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/System.Management.Automation/engine/remoting/commands/NewPSSessionConfigurationFile.cs b/src/System.Management.Automation/engine/remoting/commands/NewPSSessionConfigurationFile.cs index 66cf770109c..4fe43f53cee 100644 --- a/src/System.Management.Automation/engine/remoting/commands/NewPSSessionConfigurationFile.cs +++ b/src/System.Management.Automation/engine/remoting/commands/NewPSSessionConfigurationFile.cs @@ -967,7 +967,7 @@ protected override void ProcessRecord() ThrowTerminatingError(e.ErrorRecord); } - if (!(hashtable[ConfigFileConstants.FunctionValueToken] is ScriptBlock)) + if (hashtable[ConfigFileConstants.FunctionValueToken] is not ScriptBlock) { PSArgumentException e = new PSArgumentException(StringUtil.Format(RemotingErrorIdStrings.DISCKeyMustBeScriptBlock, ConfigFileConstants.FunctionValueToken, ConfigFileConstants.FunctionDefinitions, _path)); @@ -1714,7 +1714,7 @@ protected override void ProcessRecord() ThrowTerminatingError(e.ErrorRecord); } - if (!(hashtable[ConfigFileConstants.FunctionValueToken] is ScriptBlock)) + if (hashtable[ConfigFileConstants.FunctionValueToken] is not ScriptBlock) { PSArgumentException e = new PSArgumentException(StringUtil.Format(RemotingErrorIdStrings.DISCKeyMustBeScriptBlock, ConfigFileConstants.FunctionValueToken, ConfigFileConstants.FunctionDefinitions, _path)); diff --git a/src/System.Management.Automation/engine/remoting/fanin/InitialSessionStateProvider.cs b/src/System.Management.Automation/engine/remoting/fanin/InitialSessionStateProvider.cs index 9b64f70616d..e2c8ab5cf04 100644 --- a/src/System.Management.Automation/engine/remoting/fanin/InitialSessionStateProvider.cs +++ b/src/System.Management.Automation/engine/remoting/fanin/InitialSessionStateProvider.cs @@ -1209,7 +1209,7 @@ private static bool FunctionDefinitionsTypeValidationCallback(string key, object return false; } - if (!(hashtable[FunctionValueToken] is ScriptBlock)) + if (hashtable[FunctionValueToken] is not ScriptBlock) { cmdlet.WriteVerbose(StringUtil.Format(RemotingErrorIdStrings.DISCKeyMustBeScriptBlock, FunctionValueToken, key, path)); return false; From 2c699e62196d8eeead017bce59bc78f81a7d8da8 Mon Sep 17 00:00:00 2001 From: xtqqczze <45661989+xtqqczze@users.noreply.github.com> Date: Fri, 31 Jul 2020 01:00:28 +0100 Subject: [PATCH 3/4] Apply suggestions from code review Co-authored-by: Joel Sallow (/u/ta11ow) <32407840+vexx32@users.noreply.github.com> --- .../commands/utility/ShowCommand/ShowCommandCommandInfo.cs | 2 +- .../utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ShowCommand/ShowCommandCommandInfo.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ShowCommand/ShowCommandCommandInfo.cs index e0358bd5b52..8c46dc9d259 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ShowCommand/ShowCommandCommandInfo.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ShowCommand/ShowCommandCommandInfo.cs @@ -92,7 +92,7 @@ public ShowCommandCommandInfo(PSObject other) var parameterSets = (other.Members["ParameterSets"].Value as PSObject).BaseObject as System.Collections.ArrayList; this.ParameterSets = GetObjectEnumerable(parameterSets).Cast().Select(x => new ShowCommandParameterSetInfo(x)).ToList().AsReadOnly(); - if (other.Members["Module"] != null && other.Members["Module"].Value is PSObject) + if (other.Members["Module"]?.Value is PSObject) { this.Module = new ShowCommandModuleInfo(other.Members["Module"].Value as PSObject); } diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs index d2fb4d4b845..94dde3c00fb 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs @@ -1737,7 +1737,7 @@ internal long SetRequestContent(HttpRequestMessage request, XmlNode xmlNode) byte[] bytes = null; XmlDocument doc = xmlNode as XmlDocument; - if (doc != null && doc.FirstChild is XmlDeclaration) + if (doc?.FirstChild is XmlDeclaration) { XmlDeclaration decl = doc.FirstChild as XmlDeclaration; Encoding encoding = Encoding.GetEncoding(decl.Encoding); From 7ed0391da4be6faaa273375faf6c1057a1b27472 Mon Sep 17 00:00:00 2001 From: xtqqczze Date: Fri, 31 Jul 2020 01:04:38 +0100 Subject: [PATCH 4/4] Remove unecessary parenthesis --- .../engine/remoting/fanin/InitialSessionStateProvider.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/System.Management.Automation/engine/remoting/fanin/InitialSessionStateProvider.cs b/src/System.Management.Automation/engine/remoting/fanin/InitialSessionStateProvider.cs index e2c8ab5cf04..07597fd79ec 100644 --- a/src/System.Management.Automation/engine/remoting/fanin/InitialSessionStateProvider.cs +++ b/src/System.Management.Automation/engine/remoting/fanin/InitialSessionStateProvider.cs @@ -2221,7 +2221,7 @@ public override InitialSessionState GetInitialSessionState(PSSenderInfo senderIn foreach (Hashtable variable in variables) { if (variable.ContainsKey(ConfigFileConstants.VariableValueToken) && - (variable[ConfigFileConstants.VariableValueToken] is ScriptBlock)) + variable[ConfigFileConstants.VariableValueToken] is ScriptBlock) { iss.DynamicVariablesToDefine.Add(variable); continue;