Skip to content

Commit 1779175

Browse files
authored
Use null-coalescing assignment in the rest of /engine (PowerShell#17729)
1 parent 6bf8a98 commit 1779175

20 files changed

+50
-163
lines changed

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -380,11 +380,8 @@ public override ReadOnlyCollection<PSTypeName> OutputType
380380
}
381381
}
382382

383-
if (provider == null)
384-
{
385-
// No path argument, so just use the current path to choose the provider.
386-
provider = Context.SessionState.Path.CurrentLocation.Provider;
387-
}
383+
// If no path argument, just use the current path to choose the provider.
384+
provider ??= Context.SessionState.Path.CurrentLocation.Provider;
388385

389386
provider.GetOutputTypes(Name, providerTypes);
390387
if (providerTypes.Count > 0)

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,7 @@ internal void BindCommandLineParametersNoValidation(Collection<CommandParameterI
270270

271271
// If this generated an exception (but we didn't have one from the non-dynamic
272272
// parameters, report on this one.
273-
if (reportedBindingException == null)
274-
reportedBindingException = currentBindingException;
273+
reportedBindingException ??= currentBindingException;
275274

276275
// If the cmdlet implements a ValueFromRemainingArguments parameter (VarArgs)
277276
// bind the unbound arguments to that parameter.
@@ -1452,8 +1451,7 @@ private bool BindParameter(
14521451

14531452
BoundObsoleteParameterNames.Add(parameter.Parameter.Name);
14541453

1455-
if (ObsoleteParameterWarningList == null)
1456-
ObsoleteParameterWarningList = new List<WarningRecord>();
1454+
ObsoleteParameterWarningList ??= new List<WarningRecord>();
14571455

14581456
ObsoleteParameterWarningList.Add(warningRecord);
14591457
}

src/System.Management.Automation/engine/ComInterop/ComEventsSink.Extended.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@ private void Initialize(object rcw, Guid iid)
1818
public void AddHandler(int dispid, object func)
1919
{
2020
ComEventsMethod method = FindMethod(dispid);
21-
if (method == null)
22-
{
23-
method = AddMethod(dispid);
24-
}
21+
method ??= AddMethod(dispid);
2522

2623
if (func is Delegate d)
2724
{

src/System.Management.Automation/engine/ComInterop/ComInvokeBinder.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,7 @@ private ParameterExpression ParamVariantsVariable
114114
{
115115
get
116116
{
117-
if (_paramVariants == null)
118-
{
119-
_paramVariants = Expression.Variable(VariantArray.GetStructType(_args.Length), "paramVariants");
120-
}
117+
_paramVariants ??= Expression.Variable(VariantArray.GetStructType(_args.Length), "paramVariants");
121118
return _paramVariants;
122119
}
123120
}
@@ -140,10 +137,7 @@ private static Type MarshalType(DynamicMetaObject mo, bool isByRef)
140137
if (isByRef)
141138
{
142139
// Null just means that null was supplied.
143-
if (marshalType == null)
144-
{
145-
marshalType = mo.Expression.Type;
146-
}
140+
marshalType ??= mo.Expression.Type;
147141
marshalType = marshalType.MakeByRefType();
148142
}
149143
return marshalType;

src/System.Management.Automation/engine/ComInterop/ComTypeClassDesc.cs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@ internal class ComTypeClassDesc : ComTypeDesc, IDynamicMetaObjectProvider
1717

1818
public object CreateInstance()
1919
{
20-
if (_typeObj == null)
21-
{
22-
_typeObj = Type.GetTypeFromCLSID(Guid);
23-
}
20+
_typeObj ??= Type.GetTypeFromCLSID(Guid);
2421
return Activator.CreateInstance(Type.GetTypeFromCLSID(Guid));
2522
}
2623

@@ -46,19 +43,12 @@ private void AddInterface(ComTypes.ITypeInfo itfTypeInfo, bool isSourceItf)
4643

4744
if (isSourceItf)
4845
{
49-
if (_sourceItfs == null)
50-
{
51-
_sourceItfs = new LinkedList<string>();
52-
}
46+
_sourceItfs ??= new LinkedList<string>();
5347
_sourceItfs.AddLast(itfName);
5448
}
5549
else
5650
{
57-
if (_itfs == null)
58-
{
59-
_itfs = new LinkedList<string>();
60-
}
61-
51+
_itfs ??= new LinkedList<string>();
6252
_itfs.AddLast(itfName);
6353
}
6454
}

src/System.Management.Automation/engine/ComInterop/IDispatchComObject.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -271,10 +271,7 @@ internal override IList<string> GetMemberNames(bool dataOnly)
271271

272272
internal override IList<KeyValuePair<string, object>> GetMembers(IEnumerable<string> names)
273273
{
274-
if (names == null)
275-
{
276-
names = GetMemberNames(true);
277-
}
274+
names ??= GetMemberNames(true);
278275

279276
Type comType = RuntimeCallableWrapper.GetType();
280277

src/System.Management.Automation/engine/ComInterop/InteropServices/ComEventsMethod.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,7 @@ private void PreProcessSignature()
8383
&& pi.ParameterType.HasElementType
8484
&& pi.ParameterType.GetElementType()!.IsEnum)
8585
{
86-
if (targetTypes == null)
87-
{
88-
targetTypes = new Type?[_expectedParamsCount];
89-
}
86+
targetTypes ??= new Type?[_expectedParamsCount];
9087

9188
targetTypes[i] = pi.ParameterType.GetElementType();
9289
}

src/System.Management.Automation/engine/ComInterop/SplatCallSite.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,7 @@ internal object Invoke(object[] args)
3030
Debug.Assert(args != null);
3131

3232
// Create a CallSite and invoke it.
33-
if (_site == null)
34-
{
35-
_site = CallSite<Func<CallSite, object, object[], object>>.Create(SplatInvokeBinder.Instance);
36-
}
33+
_site ??= CallSite<Func<CallSite, object, object[], object>>.Create(SplatInvokeBinder.Instance);
3734

3835
return _site.Target(_site, _callable, args);
3936
}

src/System.Management.Automation/engine/ComInterop/TypeUtils.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,9 @@ internal static MethodInfo GetUserDefinedCoercionMethod(Type convertFrom, Type c
102102
// try lifted conversion
103103
if (nnExprType != convertFrom || nnConvType != convertToType)
104104
{
105-
method = FindConversionOperator(eMethods, nnExprType, nnConvType, implicitOnly);
106-
if (method == null)
107-
{
108-
method = FindConversionOperator(cMethods, nnExprType, nnConvType, implicitOnly);
109-
}
105+
method =
106+
FindConversionOperator(eMethods, nnExprType, nnConvType, implicitOnly) ??
107+
FindConversionOperator(cMethods, nnExprType, nnConvType, implicitOnly);
110108
if (method != null)
111109
{
112110
return method;

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,7 @@ private CompletionContext InitializeCompletionContext(TypeInferenceContext typeI
183183
{
184184
var astContext = ExtractAstContext(_ast, _tokens, _cursorPosition);
185185

186-
if (typeInferenceContext.CurrentTypeDefinitionAst == null)
187-
{
188-
typeInferenceContext.CurrentTypeDefinitionAst = Ast.GetAncestorTypeDefinitionAst(astContext.RelatedAsts.Last());
189-
}
186+
typeInferenceContext.CurrentTypeDefinitionAst ??= Ast.GetAncestorTypeDefinitionAst(astContext.RelatedAsts.Last());
190187

191188
ExecutionContext executionContext = typeInferenceContext.ExecutionContext;
192189

@@ -1894,10 +1891,7 @@ private static List<CompletionResult> GetResultForIdentifierInConfiguration(
18941891
usageString = Microsoft.PowerShell.DesiredStateConfiguration.Internal.DscClassCache.GetDSCResourceUsageString(keyword);
18951892
}
18961893

1897-
if (results == null)
1898-
{
1899-
results = new List<CompletionResult>();
1900-
}
1894+
results ??= new List<CompletionResult>();
19011895

19021896
results.Add(new CompletionResult(
19031897
keyword.Keyword,

0 commit comments

Comments
 (0)