Skip to content

Commit d204d29

Browse files
authored
Use null propagation operator in System.Management.Automation (PowerShell#17792)
1 parent 0d52375 commit d204d29

File tree

15 files changed

+42
-155
lines changed

15 files changed

+42
-155
lines changed

src/System.Management.Automation/DscSupport/CimDSCParser.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -970,10 +970,7 @@ public static List<CimClass> ImportClasses(string path, Tuple<string, Version> m
970970
{
971971
// Ignore modules with invalid schemas.
972972
s_tracer.WriteLine("DSC ClassCache: Error importing file '{0}', with error '{1}'. Skipping file.", path, e);
973-
if (errors != null)
974-
{
975-
errors.Add(e);
976-
}
973+
errors?.Add(e);
977974
}
978975

979976
if (classes != null)
@@ -1000,10 +997,7 @@ public static List<CimClass> ImportClasses(string path, Tuple<string, Version> m
1000997
ParserStrings.DuplicateCimClassDefinition, className, path, files);
1001998

1002999
e.SetErrorId("DuplicateCimClassDefinition");
1003-
if (errors != null)
1004-
{
1005-
errors.Add(e);
1006-
}
1000+
errors?.Add(e);
10071001
}
10081002
}
10091003

src/System.Management.Automation/FormatAndOutput/common/ComplexWriter.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,10 +239,7 @@ internal IndentationStackFrame(IndentationManager mgr)
239239

240240
public void Dispose()
241241
{
242-
if (_mgr != null)
243-
{
244-
_mgr.RemoveStackFrame();
245-
}
242+
_mgr?.RemoveStackFrame();
246243
}
247244

248245
private readonly IndentationManager _mgr;

src/System.Management.Automation/FormatAndOutput/common/FormattingObjectsDeserializer.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -325,9 +325,7 @@ internal WriteStreamType DeserializeWriteStreamTypeMemberVariable(PSObject so)
325325
internal FormatInfoData DeserializeObject(PSObject so)
326326
{
327327
FormatInfoData fid = FormatInfoDataClassFactory.CreateInstance(so, this);
328-
329-
if (fid != null)
330-
fid.Deserialize(so, this);
328+
fid?.Deserialize(so, this);
331329
return fid;
332330
}
333331

src/System.Management.Automation/FormatAndOutput/common/OutputManager.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,19 +92,14 @@ internal override void ProcessRecord()
9292
internal override void EndProcessing()
9393
{
9494
// shut down only if we ever processed a pipeline object
95-
if (_mgr != null)
96-
_mgr.ShutDown();
95+
_mgr?.ShutDown();
9796
}
9897

9998
internal override void StopProcessing()
10099
{
101100
lock (_syncRoot)
102101
{
103-
if (_lo != null)
104-
{
105-
_lo.StopProcessing();
106-
}
107-
102+
_lo?.StopProcessing();
108103
_isStopped = true;
109104
}
110105
}

src/System.Management.Automation/cimSupport/cmdletization/ObjectModelWrapper.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,7 @@ internal void Initialize(PSCmdlet cmdlet, string className, string classVersion,
5050
delegate
5151
{
5252
var disposable = this as IDisposable;
53-
if (disposable != null)
54-
{
55-
disposable.Dispose();
56-
}
53+
disposable?.Dispose();
5754
};
5855
}
5956
}

src/System.Management.Automation/help/CabinetNativeApi.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,7 @@ protected override void Dispose(bool disposing)
7070
}
7171

7272
// Free managed objects within 'if (disposing)' if needed
73-
if (fdiContext != null)
74-
{
75-
fdiContext.Dispose();
76-
}
73+
fdiContext?.Dispose();
7774
// Free unmanaged objects here
7875
this.CleanUpDelegates();
7976

src/System.Management.Automation/help/SaveHelpCommand.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -260,10 +260,7 @@ internal override bool ProcessModuleWithCulture(UpdatableHelpModuleInfo module,
260260
}
261261
finally
262262
{
263-
if (helpInfoDrive != null)
264-
{
265-
helpInfoDrive.Dispose();
266-
}
263+
helpInfoDrive?.Dispose();
267264
}
268265
}
269266

@@ -407,10 +404,7 @@ internal override bool ProcessModuleWithCulture(UpdatableHelpModuleInfo module,
407404
}
408405
finally
409406
{
410-
if (helpContentDrive != null)
411-
{
412-
helpContentDrive.Dispose();
413-
}
407+
helpContentDrive?.Dispose();
414408
}
415409
}
416410
}

src/System.Management.Automation/help/UpdateHelpCommand.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -322,10 +322,7 @@ internal override bool ProcessModuleWithCulture(UpdatableHelpModuleInfo module,
322322
}
323323
finally
324324
{
325-
if (helpInfoDrive != null)
326-
{
327-
helpInfoDrive.Dispose();
328-
}
325+
helpInfoDrive?.Dispose();
329326
}
330327
}
331328
else

src/System.Management.Automation/namespaces/AliasProvider.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -194,11 +194,7 @@ internal override void SetSessionStateItem(string name, object value, bool write
194194
if (dynamicParametersSpecified)
195195
{
196196
item = (AliasInfo)GetSessionStateItem(name);
197-
198-
if (item != null)
199-
{
200-
item.SetOptions(dynamicParameters.Options, Force);
201-
}
197+
item?.SetOptions(dynamicParameters.Options, Force);
202198
}
203199
else
204200
{

src/System.Management.Automation/namespaces/CoreCommandContext.cs

Lines changed: 12 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -390,13 +390,8 @@ private void CopyFilters(CmdletProviderContext context)
390390
Filter = context.Filter;
391391
}
392392

393-
internal void RemoveStopReferral()
394-
{
395-
if (_copiedContext != null)
396-
{
397-
_copiedContext.StopReferrals.Remove(this);
398-
}
399-
}
393+
internal void RemoveStopReferral() => _copiedContext?.StopReferrals.Remove(this);
394+
400395
#endregion Internal properties
401396

402397
#region Public properties
@@ -774,65 +769,29 @@ internal bool ShouldContinue(
774769
/// <param name="text">
775770
/// The string that needs to be written.
776771
/// </param>
777-
internal void WriteVerbose(string text)
778-
{
779-
if (_command != null)
780-
{
781-
_command.WriteVerbose(text);
782-
}
783-
}
772+
internal void WriteVerbose(string text) => _command?.WriteVerbose(text);
784773

785774
/// <summary>
786775
/// Writes the object to the Warning pipe.
787776
/// </summary>
788777
/// <param name="text">
789778
/// The string that needs to be written.
790779
/// </param>
791-
internal void WriteWarning(string text)
792-
{
793-
if (_command != null)
794-
{
795-
_command.WriteWarning(text);
796-
}
797-
}
780+
internal void WriteWarning(string text) => _command?.WriteWarning(text);
798781

799-
internal void WriteProgress(ProgressRecord record)
800-
{
801-
if (_command != null)
802-
{
803-
_command.WriteProgress(record);
804-
}
805-
}
782+
internal void WriteProgress(ProgressRecord record) => _command?.WriteProgress(record);
806783

807784
/// <summary>
808785
/// Writes a debug string.
809786
/// </summary>
810787
/// <param name="text">
811788
/// The String that needs to be written.
812789
/// </param>
813-
internal void WriteDebug(string text)
814-
{
815-
if (_command != null)
816-
{
817-
_command.WriteDebug(text);
818-
}
819-
}
790+
internal void WriteDebug(string text) => _command?.WriteDebug(text);
820791

821-
internal void WriteInformation(InformationRecord record)
822-
{
823-
if (_command != null)
824-
{
825-
_command.WriteInformation(record);
826-
}
827-
}
792+
internal void WriteInformation(InformationRecord record) => _command?.WriteInformation(record);
828793

829-
internal void WriteInformation(object messageData, string[] tags)
830-
{
831-
if (_command != null)
832-
{
833-
_command.WriteInformation(messageData, tags);
834-
}
835-
}
794+
internal void WriteInformation(object messageData, string[] tags) => _command?.WriteInformation(messageData, tags);
836795

837796
#endregion User feedback mechanisms
838797

@@ -1154,14 +1113,10 @@ internal void StopProcessing()
11541113
{
11551114
Stopping = true;
11561115

1157-
if (_providerInstance != null)
1158-
{
1159-
// We don't need to catch any of the exceptions here because
1160-
// we are terminating the pipeline and any exception will
1161-
// be caught by the engine.
1162-
1163-
_providerInstance.StopProcessing();
1164-
}
1116+
// We don't need to catch any of the exceptions here because
1117+
// we are terminating the pipeline and any exception will
1118+
// be caught by the engine.
1119+
_providerInstance?.StopProcessing();
11651120

11661121
// Call the stop referrals if any
11671122

0 commit comments

Comments
 (0)