Skip to content

Commit 4b4f1d1

Browse files
authored
Use null propagation operator in /engine (PowerShell#17789)
1 parent 5eb9136 commit 4b4f1d1

36 files changed

+137
-479
lines changed

src/System.Management.Automation/engine/COM/ComTypeInfo.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,7 @@ private void AddProperty(string strName, COM.FUNCDESC funcdesc, int index)
183183
_properties[strName] = prop;
184184
}
185185

186-
if (prop != null)
187-
{
188-
prop.UpdateFuncDesc(funcdesc, index);
189-
}
186+
prop?.UpdateFuncDesc(funcdesc, index);
190187
}
191188

192189
private void AddMethod(string strName, int index)
@@ -198,10 +195,7 @@ private void AddMethod(string strName, int index)
198195
_methods[strName] = method;
199196
}
200197

201-
if (method != null)
202-
{
203-
method.AddFuncDesc(index);
204-
}
198+
method?.AddFuncDesc(index);
205199
}
206200

207201
/// <summary>

src/System.Management.Automation/engine/CmdletParameterBinderController.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -202,10 +202,7 @@ internal void BindCommandLineParameters(Collection<CommandParameterInternal> arg
202202
internal void BindCommandLineParametersNoValidation(Collection<CommandParameterInternal> arguments)
203203
{
204204
var psCompiledScriptCmdlet = this.Command as PSScriptCmdlet;
205-
if (psCompiledScriptCmdlet != null)
206-
{
207-
psCompiledScriptCmdlet.PrepareForBinding(this.CommandLineParameters);
208-
}
205+
psCompiledScriptCmdlet?.PrepareForBinding(this.CommandLineParameters);
209206

210207
InitUnboundArguments(arguments);
211208
CommandMetadata cmdletMetadata = _commandMetadata;
@@ -1044,10 +1041,7 @@ private bool RestoreParameter(CommandParameterInternal argumentToBind, MergedCom
10441041
_commandMetadata.ImplementsDynamicParameters,
10451042
"The metadata for the dynamic parameters should only be available if the command supports IDynamicParameters");
10461043

1047-
if (_dynamicParameterBinder != null)
1048-
{
1049-
_dynamicParameterBinder.BindParameter(argumentToBind.ParameterName, argumentToBind.ArgumentValue, parameter.Parameter);
1050-
}
1044+
_dynamicParameterBinder?.BindParameter(argumentToBind.ParameterName, argumentToBind.ArgumentValue, parameter.Parameter);
10511045

10521046
break;
10531047
}

src/System.Management.Automation/engine/CommandCompletion/PseudoParameterBinder.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1222,10 +1222,7 @@ private bool PrepareCommandElements(ExecutionContext context)
12221222
var parameter = _commandElements[commandIndex] as CommandParameterAst;
12231223
if (parameter != null)
12241224
{
1225-
if (argumentsToGetDynamicParameters != null)
1226-
{
1227-
argumentsToGetDynamicParameters.Add(parameter.Extent.Text);
1228-
}
1225+
argumentsToGetDynamicParameters?.Add(parameter.Extent.Text);
12291226

12301227
AstPair parameterArg = parameter.Argument != null
12311228
? new AstPair(parameter)

src/System.Management.Automation/engine/CommandInfo.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -467,11 +467,8 @@ private MergedCommandParameterMetadata GetMergedCommandParameterMetadataSafely()
467467
processInCurrentThread: true,
468468
waitForCompletionInCurrentThread: true);
469469

470-
if (eventArgs.Exception != null)
471-
{
472-
// An exception happened on a different thread, rethrow it here on the correct thread.
473-
eventArgs.Exception.Throw();
474-
}
470+
// An exception happened on a different thread, rethrow it here on the correct thread.
471+
eventArgs.Exception?.Throw();
475472

476473
return eventArgs.Result;
477474
}

src/System.Management.Automation/engine/EventManager.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -862,10 +862,7 @@ private void UnsubscribeEvent(PSEventSubscriber subscriber, bool skipDraining)
862862
}
863863

864864
// Stop the job
865-
if (subscriber.Action != null)
866-
{
867-
subscriber.Action.NotifyJobStopped();
868-
}
865+
subscriber.Action?.NotifyJobStopped();
869866

870867
lock (_eventSubscribers)
871868
{
@@ -1532,10 +1529,7 @@ public void Dispose(bool disposing)
15321529
{
15331530
lock (_eventSubscribers)
15341531
{
1535-
if (_timer != null)
1536-
{
1537-
_timer.Dispose();
1538-
}
1532+
_timer?.Dispose();
15391533

15401534
foreach (PSEventSubscriber currentSubscriber in _eventSubscribers.Keys.ToArray())
15411535
{

src/System.Management.Automation/engine/ExecutionContext.cs

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,12 @@ internal ScriptDebugger Debugger
5353
/// </summary>
5454
internal void ResetManagers()
5555
{
56-
if (_debugger != null)
57-
{
58-
_debugger.ResetDebugger();
59-
}
60-
61-
if (Events != null)
62-
{
63-
Events.Dispose();
64-
}
56+
_debugger?.ResetDebugger();
6557

58+
Events?.Dispose();
6659
Events = new PSLocalEventManager(this);
67-
if (this.transactionManager != null)
68-
{
69-
this.transactionManager.Dispose();
70-
}
7160

61+
this.transactionManager?.Dispose();
7262
this.transactionManager = new PSTransactionManager();
7363
}
7464
/// <summary>
@@ -1155,22 +1145,12 @@ internal void RunspaceClosingNotification()
11551145
{
11561146
EngineSessionState.RunspaceClosingNotification();
11571147

1158-
if (_debugger != null)
1159-
{
1160-
_debugger.Dispose();
1161-
}
1162-
1163-
if (Events != null)
1164-
{
1165-
Events.Dispose();
1166-
}
1148+
_debugger?.Dispose();
11671149

1150+
Events?.Dispose();
11681151
Events = null;
1169-
if (this.transactionManager != null)
1170-
{
1171-
this.transactionManager.Dispose();
1172-
}
11731152

1153+
this.transactionManager?.Dispose();
11741154
this.transactionManager = null;
11751155
}
11761156

src/System.Management.Automation/engine/InternalCommands.cs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -919,17 +919,14 @@ private void ProcessScriptBlockParameterSet()
919919
// because it allows you to parameterize a command - for example you might allow
920920
// for actions before and after the main processing script. They could be null
921921
// by default and therefore ignored then filled in later...
922-
if (_scripts[i] != null)
923-
{
924-
_scripts[i].InvokeUsingCmdlet(
925-
contextCmdlet: this,
926-
useLocalScope: false,
927-
errorHandlingBehavior: ScriptBlock.ErrorHandlingBehavior.WriteToCurrentErrorPipe,
928-
dollarUnder: InputObject,
929-
input: new object[] { InputObject },
930-
scriptThis: AutomationNull.Value,
931-
args: Array.Empty<object>());
932-
}
922+
_scripts[i]?.InvokeUsingCmdlet(
923+
contextCmdlet: this,
924+
useLocalScope: false,
925+
errorHandlingBehavior: ScriptBlock.ErrorHandlingBehavior.WriteToCurrentErrorPipe,
926+
dollarUnder: InputObject,
927+
input: new object[] { InputObject },
928+
scriptThis: AutomationNull.Value,
929+
args: Array.Empty<object>());
933930
}
934931
}
935932

src/System.Management.Automation/engine/Modules/ModuleCmdletBase.cs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5013,17 +5013,14 @@ internal void RemoveModule(PSModuleInfo module, string moduleNameInRemoveModuleC
50135013
if (Context.Modules.ModuleTable.ContainsKey(module.Path))
50145014
{
50155015
// We should try to run OnRemove as the very first thing
5016-
if (module.OnRemove != null)
5017-
{
5018-
module.OnRemove.InvokeUsingCmdlet(
5019-
contextCmdlet: this,
5020-
useLocalScope: true,
5021-
errorHandlingBehavior: ScriptBlock.ErrorHandlingBehavior.WriteToCurrentErrorPipe,
5022-
dollarUnder: AutomationNull.Value,
5023-
input: AutomationNull.Value,
5024-
scriptThis: AutomationNull.Value,
5025-
args: new object[] { module });
5026-
}
5016+
module.OnRemove?.InvokeUsingCmdlet(
5017+
contextCmdlet: this,
5018+
useLocalScope: true,
5019+
errorHandlingBehavior: ScriptBlock.ErrorHandlingBehavior.WriteToCurrentErrorPipe,
5020+
dollarUnder: AutomationNull.Value,
5021+
input: AutomationNull.Value,
5022+
scriptThis: AutomationNull.Value,
5023+
args: new object[] { module });
50275024

50285025
if (module.ImplementingAssembly != null && !module.ImplementingAssembly.IsDynamic)
50295026
{
@@ -6893,10 +6890,7 @@ internal static void AddModuleToModuleTables(ExecutionContext context, SessionSt
68936890
targetSessionState.ModuleTableKeys.Add(moduleTableKey);
68946891
}
68956892

6896-
if (targetSessionState.Module != null)
6897-
{
6898-
targetSessionState.Module.AddNestedModule(module);
6899-
}
6893+
targetSessionState.Module?.AddNestedModule(module);
69006894
}
69016895

69026896
/// <summary>

src/System.Management.Automation/engine/MshCmdlet.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,7 @@ public bool HasErrors
283283
/// </exception>
284284
public string ExpandString(string source)
285285
{
286-
if (_cmdlet != null)
287-
_cmdlet.ThrowIfStopping();
286+
_cmdlet?.ThrowIfStopping();
288287
return _context.Engine.Expand(source);
289288
}
290289

@@ -818,8 +817,7 @@ private Collection<PSObject> InvokeScript(
818817
IList input,
819818
params object[] args)
820819
{
821-
if (_cmdlet != null)
822-
_cmdlet.ThrowIfStopping();
820+
_cmdlet?.ThrowIfStopping();
823821

824822
Cmdlet cmdletToUse = null;
825823
ScriptBlock.ErrorHandlingBehavior errorHandlingBehavior = ScriptBlock.ErrorHandlingBehavior.WriteToExternalErrorPipe;
@@ -910,8 +908,7 @@ private Collection<PSObject> InvokeScript(
910908
/// <exception cref="ParseException"></exception>
911909
public ScriptBlock NewScriptBlock(string scriptText)
912910
{
913-
if (_commandRuntime != null)
914-
_commandRuntime.ThrowIfStopping();
911+
_commandRuntime?.ThrowIfStopping();
915912

916913
ScriptBlock result = ScriptBlock.Create(_context, scriptText);
917914
return result;

src/System.Management.Automation/engine/MshCommandRuntime.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2395,10 +2395,7 @@ public Exception ManageException(Exception e)
23952395
if (e == null)
23962396
throw PSTraceSource.NewArgumentNullException(nameof(e));
23972397

2398-
if (PipelineProcessor != null)
2399-
{
2400-
PipelineProcessor.RecordFailure(e, _thisCommand);
2401-
}
2398+
PipelineProcessor?.RecordFailure(e, _thisCommand);
24022399

24032400
// 1021203-2005/05/09-JonN
24042401
// HaltCommandException will cause the command

0 commit comments

Comments
 (0)