Skip to content

Commit 27f1a7a

Browse files
teinarsspchote
authored andcommitted
Use out var syntax
1 parent d52e479 commit 27f1a7a

File tree

193 files changed

+395
-826
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

193 files changed

+395
-826
lines changed

OpenRA.Game/Actor.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -560,8 +560,7 @@ public int GrantCondition(string condition)
560560
/// <returns>The invalid token ID.</returns>
561561
public int RevokeCondition(int token)
562562
{
563-
string condition;
564-
if (!conditionTokens.TryGetValue(token, out condition))
563+
if (!conditionTokens.TryGetValue(token, out var condition))
565564
throw new InvalidOperationException("Attempting to revoke condition with invalid token {0} for {1}.".F(token, this));
566565

567566
conditionTokens.Remove(token);
@@ -594,8 +593,7 @@ public void OnScriptBind(ScriptContext context)
594593

595594
public LuaValue Equals(LuaRuntime runtime, LuaValue left, LuaValue right)
596595
{
597-
Actor a, b;
598-
if (!left.TryGetClrValue(out a) || !right.TryGetClrValue(out b))
596+
if (!left.TryGetClrValue(out Actor a) || !right.TryGetClrValue(out Actor b))
599597
return false;
600598

601599
return a == b;

OpenRA.Game/CPos.cs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -89,31 +89,26 @@ public MPos ToMPos(MapGridType gridType)
8989

9090
public LuaValue Add(LuaRuntime runtime, LuaValue left, LuaValue right)
9191
{
92-
CPos a;
93-
CVec b;
94-
if (!left.TryGetClrValue(out a) || !right.TryGetClrValue(out b))
92+
if (!left.TryGetClrValue(out CPos a) || !right.TryGetClrValue(out CVec b))
9593
throw new LuaException("Attempted to call CPos.Add(CPos, CVec) with invalid arguments ({0}, {1})".F(left.WrappedClrType().Name, right.WrappedClrType().Name));
9694

9795
return new LuaCustomClrObject(a + b);
9896
}
9997

10098
public LuaValue Subtract(LuaRuntime runtime, LuaValue left, LuaValue right)
10199
{
102-
CPos a;
103100
var rightType = right.WrappedClrType();
104-
if (!left.TryGetClrValue(out a))
101+
if (!left.TryGetClrValue(out CPos a))
105102
throw new LuaException("Attempted to call CPos.Subtract(CPos, (CPos|CVec)) with invalid arguments ({0}, {1})".F(left.WrappedClrType().Name, rightType.Name));
106103

107104
if (rightType == typeof(CPos))
108105
{
109-
CPos b;
110-
right.TryGetClrValue(out b);
106+
right.TryGetClrValue(out CPos b);
111107
return new LuaCustomClrObject(a - b);
112108
}
113109
else if (rightType == typeof(CVec))
114110
{
115-
CVec b;
116-
right.TryGetClrValue(out b);
111+
right.TryGetClrValue(out CVec b);
117112
return new LuaCustomClrObject(a - b);
118113
}
119114

@@ -122,8 +117,7 @@ public LuaValue Subtract(LuaRuntime runtime, LuaValue left, LuaValue right)
122117

123118
public LuaValue Equals(LuaRuntime runtime, LuaValue left, LuaValue right)
124119
{
125-
CPos a, b;
126-
if (!left.TryGetClrValue(out a) || !right.TryGetClrValue(out b))
120+
if (!left.TryGetClrValue(out CPos a) || !right.TryGetClrValue(out CPos b))
127121
return false;
128122

129123
return a == b;

OpenRA.Game/CVec.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,17 +75,15 @@ public CVec Clamp(Rectangle r)
7575

7676
public LuaValue Add(LuaRuntime runtime, LuaValue left, LuaValue right)
7777
{
78-
CVec a, b;
79-
if (!left.TryGetClrValue(out a) || !right.TryGetClrValue(out b))
78+
if (!left.TryGetClrValue(out CVec a) || !right.TryGetClrValue(out CVec b))
8079
throw new LuaException("Attempted to call CVec.Add(CVec, CVec) with invalid arguments ({0}, {1})".F(left.WrappedClrType().Name, right.WrappedClrType().Name));
8180

8281
return new LuaCustomClrObject(a + b);
8382
}
8483

8584
public LuaValue Subtract(LuaRuntime runtime, LuaValue left, LuaValue right)
8685
{
87-
CVec a, b;
88-
if (!left.TryGetClrValue(out a) || !right.TryGetClrValue(out b))
86+
if (!left.TryGetClrValue(out CVec a) || !right.TryGetClrValue(out CVec b))
8987
throw new LuaException("Attempted to call CVec.Subtract(CVec, CVec) with invalid arguments ({0}, {1})".F(left.WrappedClrType().Name, right.WrappedClrType().Name));
9088

9189
return new LuaCustomClrObject(a - b);
@@ -98,8 +96,7 @@ public LuaValue Minus(LuaRuntime runtime)
9896

9997
public LuaValue Equals(LuaRuntime runtime, LuaValue left, LuaValue right)
10098
{
101-
CVec a, b;
102-
if (!left.TryGetClrValue(out a) || !right.TryGetClrValue(out b))
99+
if (!left.TryGetClrValue(out CVec a) || !right.TryGetClrValue(out CVec b))
103100
return false;
104101

105102
return a == b;

OpenRA.Game/Exts.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,7 @@ public static V GetOrAdd<K, V>(this Dictionary<K, V> d, K k)
125125

126126
public static V GetOrAdd<K, V>(this Dictionary<K, V> d, K k, Func<K, V> createFn)
127127
{
128-
V ret;
129-
if (!d.TryGetValue(k, out ret))
128+
if (!d.TryGetValue(k, out var ret))
130129
d.Add(k, ret = createFn(k));
131130
return ret;
132131
}
@@ -353,8 +352,7 @@ public static ulong ISqrt(ulong number, ISqrtRoundMode round = ISqrtRoundMode.Fl
353352

354353
public static int IntegerDivisionRoundingAwayFromZero(int dividend, int divisor)
355354
{
356-
int remainder;
357-
var quotient = Math.DivRem(dividend, divisor, out remainder);
355+
var quotient = Math.DivRem(dividend, divisor, out var remainder);
358356
if (remainder == 0)
359357
return quotient;
360358
return quotient + (Math.Sign(dividend) == Math.Sign(divisor) ? 1 : -1);
@@ -405,8 +403,7 @@ public static Dictionary<TKey, TElement> ToDictionaryWithConflictLog<TSource, TK
405403
// Check for a key conflict:
406404
if (d.ContainsKey(key))
407405
{
408-
List<string> dupKeyMessages;
409-
if (!dupKeys.TryGetValue(key, out dupKeyMessages))
406+
if (!dupKeys.TryGetValue(key, out var dupKeyMessages))
410407
{
411408
// Log the initial conflicting value already inserted:
412409
dupKeyMessages = new List<string>();

OpenRA.Game/FieldLoader.cs

Lines changed: 22 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,7 @@ static bool TryGetValueFromYaml(string yamlName, FieldInfo field, Dictionary<str
121121
{
122122
ret = null;
123123

124-
MiniYaml yaml;
125-
if (!md.TryGetValue(yamlName, out yaml))
124+
if (!md.TryGetValue(yamlName, out var yaml))
126125
return false;
127126

128127
ret = GetValue(field.Name, field.FieldType, yaml, field);
@@ -185,37 +184,32 @@ public static object GetValue(string fieldName, Type fieldType, MiniYaml yaml, M
185184

186185
if (fieldType == typeof(int))
187186
{
188-
int res;
189-
if (Exts.TryParseIntegerInvariant(value, out res))
187+
if (Exts.TryParseIntegerInvariant(value, out var res))
190188
return res;
191189
return InvalidValueAction(value, fieldType, fieldName);
192190
}
193191
else if (fieldType == typeof(ushort))
194192
{
195-
ushort res;
196-
if (ushort.TryParse(value, NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out res))
193+
if (ushort.TryParse(value, NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out var res))
197194
return res;
198195
return InvalidValueAction(value, fieldType, fieldName);
199196
}
200197

201198
if (fieldType == typeof(long))
202199
{
203-
long res;
204-
if (long.TryParse(value, NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out res))
200+
if (long.TryParse(value, NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out var res))
205201
return res;
206202
return InvalidValueAction(value, fieldType, fieldName);
207203
}
208204
else if (fieldType == typeof(float))
209205
{
210-
float res;
211-
if (value != null && float.TryParse(value.Replace("%", ""), NumberStyles.Float, NumberFormatInfo.InvariantInfo, out res))
206+
if (value != null && float.TryParse(value.Replace("%", ""), NumberStyles.Float, NumberFormatInfo.InvariantInfo, out var res))
212207
return res * (value.Contains('%') ? 0.01f : 1f);
213208
return InvalidValueAction(value, fieldType, fieldName);
214209
}
215210
else if (fieldType == typeof(decimal))
216211
{
217-
decimal res;
218-
if (value != null && decimal.TryParse(value.Replace("%", ""), NumberStyles.Float, NumberFormatInfo.InvariantInfo, out res))
212+
if (value != null && decimal.TryParse(value.Replace("%", ""), NumberStyles.Float, NumberFormatInfo.InvariantInfo, out var res))
219213
return res * (value.Contains('%') ? 0.01m : 1m);
220214
return InvalidValueAction(value, fieldType, fieldName);
221215
}
@@ -227,16 +221,14 @@ public static object GetValue(string fieldName, Type fieldType, MiniYaml yaml, M
227221
}
228222
else if (fieldType == typeof(Color))
229223
{
230-
Color color;
231-
if (value != null && Color.TryParse(value, out color))
224+
if (value != null && Color.TryParse(value, out var color))
232225
return color;
233226

234227
return InvalidValueAction(value, fieldType, fieldName);
235228
}
236229
else if (fieldType == typeof(Hotkey))
237230
{
238-
Hotkey res;
239-
if (Hotkey.TryParse(value, out res))
231+
if (Hotkey.TryParse(value, out var res))
240232
return res;
241233

242234
return InvalidValueAction(value, fieldType, fieldName);
@@ -247,8 +239,7 @@ public static object GetValue(string fieldName, Type fieldType, MiniYaml yaml, M
247239
}
248240
else if (fieldType == typeof(WDist))
249241
{
250-
WDist res;
251-
if (WDist.TryParse(value, out res))
242+
if (WDist.TryParse(value, out var res))
252243
return res;
253244

254245
return InvalidValueAction(value, fieldType, fieldName);
@@ -260,8 +251,7 @@ public static object GetValue(string fieldName, Type fieldType, MiniYaml yaml, M
260251
var parts = value.Split(',');
261252
if (parts.Length == 3)
262253
{
263-
WDist rx, ry, rz;
264-
if (WDist.TryParse(parts[0], out rx) && WDist.TryParse(parts[1], out ry) && WDist.TryParse(parts[2], out rz))
254+
if (WDist.TryParse(parts[0], out var rx) && WDist.TryParse(parts[1], out var ry) && WDist.TryParse(parts[2], out var rz))
265255
return new WVec(rx, ry, rz);
266256
}
267257
}
@@ -281,8 +271,7 @@ public static object GetValue(string fieldName, Type fieldType, MiniYaml yaml, M
281271

282272
for (var i = 0; i < vecs.Length; ++i)
283273
{
284-
WDist rx, ry, rz;
285-
if (WDist.TryParse(parts[3 * i], out rx) && WDist.TryParse(parts[3 * i + 1], out ry) && WDist.TryParse(parts[3 * i + 2], out rz))
274+
if (WDist.TryParse(parts[3 * i], out var rx) && WDist.TryParse(parts[3 * i + 1], out var ry) && WDist.TryParse(parts[3 * i + 2], out var rz))
286275
vecs[i] = new WVec(rx, ry, rz);
287276
}
288277

@@ -298,8 +287,7 @@ public static object GetValue(string fieldName, Type fieldType, MiniYaml yaml, M
298287
var parts = value.Split(',');
299288
if (parts.Length == 3)
300289
{
301-
WDist rx, ry, rz;
302-
if (WDist.TryParse(parts[0], out rx) && WDist.TryParse(parts[1], out ry) && WDist.TryParse(parts[2], out rz))
290+
if (WDist.TryParse(parts[0], out var rx) && WDist.TryParse(parts[1], out var ry) && WDist.TryParse(parts[2], out var rz))
303291
return new WPos(rx, ry, rz);
304292
}
305293
}
@@ -308,8 +296,7 @@ public static object GetValue(string fieldName, Type fieldType, MiniYaml yaml, M
308296
}
309297
else if (fieldType == typeof(WAngle))
310298
{
311-
int res;
312-
if (Exts.TryParseIntegerInvariant(value, out res))
299+
if (Exts.TryParseIntegerInvariant(value, out var res))
313300
return new WAngle(res);
314301
return InvalidValueAction(value, fieldType, fieldName);
315302
}
@@ -320,8 +307,7 @@ public static object GetValue(string fieldName, Type fieldType, MiniYaml yaml, M
320307
var parts = value.Split(',');
321308
if (parts.Length == 3)
322309
{
323-
int rr, rp, ry;
324-
if (Exts.TryParseIntegerInvariant(parts[0], out rr) && Exts.TryParseIntegerInvariant(parts[1], out rp) && Exts.TryParseIntegerInvariant(parts[2], out ry))
310+
if (Exts.TryParseIntegerInvariant(parts[0], out var rr) && Exts.TryParseIntegerInvariant(parts[1], out var rp) && Exts.TryParseIntegerInvariant(parts[2], out var ry))
325311
return new WRot(new WAngle(rr), new WAngle(rp), new WAngle(ry));
326312
}
327313
}
@@ -360,8 +346,7 @@ public static object GetValue(string fieldName, Type fieldType, MiniYaml yaml, M
360346
var vecs = new CVec[parts.Length / 2];
361347
for (var i = 0; i < vecs.Length; i++)
362348
{
363-
int rx, ry;
364-
if (int.TryParse(parts[2 * i], out rx) && int.TryParse(parts[2 * i + 1], out ry))
349+
if (int.TryParse(parts[2 * i], out var rx) && int.TryParse(parts[2 * i + 1], out var ry))
365350
vecs[i] = new CVec(rx, ry);
366351
}
367352

@@ -415,8 +400,7 @@ public static object GetValue(string fieldName, Type fieldType, MiniYaml yaml, M
415400
}
416401
else if (fieldType == typeof(bool))
417402
{
418-
bool result;
419-
if (bool.TryParse(value.ToLowerInvariant(), out result))
403+
if (bool.TryParse(value.ToLowerInvariant(), out var result))
420404
return result;
421405

422406
return InvalidValueAction(value, fieldType, fieldName);
@@ -509,8 +493,7 @@ public static object GetValue(string fieldName, Type fieldType, MiniYaml yaml, M
509493
var parts = value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
510494
float xx = 0;
511495
float yy = 0;
512-
float res;
513-
if (float.TryParse(parts[0].Replace("%", ""), NumberStyles.Float, NumberFormatInfo.InvariantInfo, out res))
496+
if (float.TryParse(parts[0].Replace("%", ""), NumberStyles.Float, NumberFormatInfo.InvariantInfo, out var res))
514497
xx = res * (parts[0].Contains('%') ? 0.01f : 1f);
515498
if (float.TryParse(parts[1].Replace("%", ""), NumberStyles.Float, NumberFormatInfo.InvariantInfo, out res))
516499
yy = res * (parts[1].Contains('%') ? 0.01f : 1f);
@@ -524,13 +507,11 @@ public static object GetValue(string fieldName, Type fieldType, MiniYaml yaml, M
524507
if (value != null)
525508
{
526509
var parts = value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
527-
float x = 0;
528-
float y = 0;
529-
float z = 0;
530-
float.TryParse(parts[0], NumberStyles.Float, NumberFormatInfo.InvariantInfo, out x);
531-
float.TryParse(parts[1], NumberStyles.Float, NumberFormatInfo.InvariantInfo, out y);
510+
float.TryParse(parts[0], NumberStyles.Float, NumberFormatInfo.InvariantInfo, out var x);
511+
float.TryParse(parts[1], NumberStyles.Float, NumberFormatInfo.InvariantInfo, out var y);
532512

533513
// z component is optional for compatibility with older float2 definitions
514+
float z = 0;
534515
if (parts.Length > 2)
535516
float.TryParse(parts[2], NumberStyles.Float, NumberFormatInfo.InvariantInfo, out z);
536517

@@ -575,8 +556,7 @@ public static object GetValue(string fieldName, Type fieldType, MiniYaml yaml, M
575556
}
576557
else if (fieldType == typeof(DateTime))
577558
{
578-
DateTime dt;
579-
if (DateTime.TryParseExact(value, "yyyy-MM-dd HH-mm-ss", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal, out dt))
559+
if (DateTime.TryParseExact(value, "yyyy-MM-dd HH-mm-ss", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal, out var dt))
580560
return dt;
581561
return InvalidValueAction(value, fieldType, fieldName);
582562
}
@@ -726,8 +706,7 @@ public static string Translate(string key)
726706
if (translations == null)
727707
return key;
728708

729-
string value;
730-
if (!translations.TryGetValue(key, out value))
709+
if (!translations.TryGetValue(key, out var value))
731710
return key;
732711

733712
return value;

0 commit comments

Comments
 (0)