Skip to content

Commit 7ce0df7

Browse files
authored
Use compound assignment in /Microsoft.PowerShell.Commands (PowerShell#17722)
1 parent 9fe96f3 commit 7ce0df7

22 files changed

+59
-180
lines changed

src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/cim/cimChildJobBase.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,10 +1053,7 @@ internal override void WriteObject(object outputObject)
10531053

10541054
if (this.JobContext.ShowComputerName)
10551055
{
1056-
if (pso == null)
1057-
{
1058-
pso = PSObject.AsPSObject(outputObject);
1059-
}
1056+
pso ??= PSObject.AsPSObject(outputObject);
10601057

10611058
AddShowComputerNameMarker(pso);
10621059
if (cimInstance == null)

src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/cim/cimWrapper.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -107,15 +107,12 @@ internal CimCmdletDefinitionContext CmdletDefinitionContext
107107
{
108108
get
109109
{
110-
if (_cmdletDefinitionContext == null)
111-
{
112-
_cmdletDefinitionContext = new CimCmdletDefinitionContext(
113-
this.ClassName,
114-
this.ClassVersion,
115-
this.ModuleVersion,
116-
this.Cmdlet.CommandInfo.CommandMetadata.SupportsShouldProcess,
117-
this.PrivateData);
118-
}
110+
_cmdletDefinitionContext ??= new CimCmdletDefinitionContext(
111+
this.ClassName,
112+
this.ClassVersion,
113+
this.ModuleVersion,
114+
this.Cmdlet.CommandInfo.CommandMetadata.SupportsShouldProcess,
115+
this.PrivateData);
119116

120117
return _cmdletDefinitionContext;
121118
}

src/Microsoft.PowerShell.Commands.Management/commands/management/GetComputerInfoCommand.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,11 +1130,8 @@ internal static string GetLocaleName(string locale)
11301130
culture = CultureInfo.GetCultureInfo((int)localeNum);
11311131
}
11321132

1133-
if (culture == null)
1134-
{
1135-
// If TryParse failed we'll try using the original string as culture name
1136-
culture = CultureInfo.GetCultureInfo(locale);
1137-
}
1133+
// If TryParse failed we'll try using the original string as culture name
1134+
culture ??= CultureInfo.GetCultureInfo(locale);
11381135
}
11391136
catch (Exception)
11401137
{

src/Microsoft.PowerShell.Commands.Management/commands/management/MovePropertyCommand.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,7 @@ public string[] Name
6161

6262
set
6363
{
64-
if (value == null)
65-
{
66-
value = Array.Empty<string>();
67-
}
64+
value ??= Array.Empty<string>();
6865

6966
_property = value;
7067
}

src/Microsoft.PowerShell.Commands.Management/commands/management/WriteContentCommandBase.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,7 @@ protected override void ProcessRecord()
9595

9696
// Initialize the content
9797

98-
if (_content == null)
99-
{
100-
_content = Array.Empty<object>();
101-
}
98+
_content ??= Array.Empty<object>();
10299

103100
if (_pipingPaths)
104101
{

src/Microsoft.PowerShell.Commands.Utility/commands/utility/ConvertTo-Html.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -344,10 +344,7 @@ private List<MshParameter> ProcessParameter(object[] properties)
344344
TerminatingErrorContext invocationContext = new(this);
345345
ParameterProcessor processor =
346346
new(new ConvertHTMLExpressionParameterDefinition());
347-
if (properties == null)
348-
{
349-
properties = new object[] { "*" };
350-
}
347+
properties ??= new object[] { "*" };
351348

352349
return processor.ProcessParameters(properties, invocationContext);
353350
}

src/Microsoft.PowerShell.Commands.Utility/commands/utility/EnableDisableRunspaceDebugCommand.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -275,10 +275,7 @@ protected void SetDebugPreferenceHelper(string processName, string[] appDomainNa
275275
{
276276
if (!string.IsNullOrEmpty(currentAppDomainName))
277277
{
278-
if (appDomainNames == null)
279-
{
280-
appDomainNames = new List<string>();
281-
}
278+
appDomainNames ??= new List<string>();
282279

283280
appDomainNames.Add(currentAppDomainName.ToLowerInvariant());
284281
}

src/Microsoft.PowerShell.Commands.Utility/commands/utility/FormatAndOutput/OutGridView/TableView.cs

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,8 @@ internal HeaderInfo GenerateHeaderInfo(PSObject input, TableControlBody tableBod
7272
FieldPropertyToken fpt = token as FieldPropertyToken;
7373
if (fpt != null)
7474
{
75-
if (displayName == null)
76-
{
77-
// Database does not provide a label(DisplayName) for the current property, use the expression value instead.
78-
displayName = fpt.expression.expressionValue;
79-
}
75+
// If Database does not provide a label(DisplayName) for the current property, use the expression value instead.
76+
displayName ??= fpt.expression.expressionValue;
8077

8178
if (fpt.expression.isScriptBlock)
8279
{
@@ -170,10 +167,7 @@ internal HeaderInfo GenerateHeaderInfo(PSObject input, OutGridViewCommand parent
170167
propertyName = (string)key;
171168
}
172169

173-
if (propertyName == null)
174-
{
175-
propertyName = association.ResolvedExpression.ToString();
176-
}
170+
propertyName ??= association.ResolvedExpression.ToString();
177171

178172
ColumnInfo columnInfo = new OriginalColumnInfo(propertyName, propertyName, propertyName, parentCmdlet);
179173

@@ -233,10 +227,7 @@ private List<TableRowItemDefinition> GetActiveTableRowDefinition(TableControlBod
233227
}
234228
}
235229

236-
if (matchingRowDefinition == null)
237-
{
238-
matchingRowDefinition = match.BestMatch as TableRowDefinition;
239-
}
230+
matchingRowDefinition ??= match.BestMatch as TableRowDefinition;
240231

241232
if (matchingRowDefinition == null)
242233
{
@@ -254,10 +245,7 @@ private List<TableRowItemDefinition> GetActiveTableRowDefinition(TableControlBod
254245
}
255246
}
256247

257-
if (matchingRowDefinition == null)
258-
{
259-
matchingRowDefinition = match.BestMatch as TableRowDefinition;
260-
}
248+
matchingRowDefinition ??= match.BestMatch as TableRowDefinition;
261249
}
262250
}
263251

src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetUnique.cs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,7 @@ protected override void ProcessRecord()
7272
else if (AsString)
7373
{
7474
string inputString = InputObject.ToString();
75-
if (_lastObjectAsString == null)
76-
{
77-
_lastObjectAsString = _lastObject.ToString();
78-
}
75+
_lastObjectAsString ??= _lastObject.ToString();
7976

8077
if (string.Equals(
8178
inputString,
@@ -91,13 +88,10 @@ protected override void ProcessRecord()
9188
}
9289
else // compare as objects
9390
{
94-
if (_comparer == null)
95-
{
96-
_comparer = new ObjectCommandComparer(
97-
true, // ascending (doesn't matter)
98-
CultureInfo.CurrentCulture,
99-
true); // case-sensitive
100-
}
91+
_comparer ??= new ObjectCommandComparer(
92+
true, // ascending (doesn't matter)
93+
CultureInfo.CurrentCulture,
94+
true); // case-sensitive
10195

10296
isUnique = (_comparer.Compare(InputObject, _lastObject) != 0);
10397
}

src/Microsoft.PowerShell.Commands.Utility/commands/utility/Group-Object.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -392,10 +392,7 @@ protected override void ProcessRecord()
392392

393393
if (!_hasProcessedFirstInputObject)
394394
{
395-
if (Property == null)
396-
{
397-
Property = OrderByProperty.GetDefaultKeyPropertySet(InputObject);
398-
}
395+
Property ??= OrderByProperty.GetDefaultKeyPropertySet(InputObject);
399396

400397
_orderByProperty.ProcessExpressionParameter(this, Property);
401398

0 commit comments

Comments
 (0)