Skip to content

Commit d8d7bb4

Browse files
committed
WIP (Core): Yet more code cleanup -- inlining var decls, simplifying null checks, marking fields readonly, using pattern matching for type checks, etc. AND one minor fix in LispReader.
1 parent 4da3b84 commit d8d7bb4

37 files changed

+289
-339
lines changed

Clojure/Clojure.Source/clojure/samples/genclass/C.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
namespace dm
77
{
88
#pragma warning disable IDE1006 // Naming Styles
9+
#pragma warning disable IDE0051 // Remove unused private members
10+
#pragma warning disable CA1822 // mark as static
911
public class C1
1012
{
11-
protected int x = 0;
13+
protected int x;
1214
protected string y = String.Empty;
1315

1416

@@ -23,6 +25,7 @@ public class C1
2325
protected int m3(int x) { Message("m3", x.ToString()); return x + 1; }
2426

2527
protected int m4(int x) { Message("m4", x.ToString()); return x + 1; }
28+
2629
private int m5(int x) { Message("m5", x.ToString()); return x + 1; }
2730

2831
public C1(int x, string y) { Message("ctor1", x.ToString(), y); this.x = x; this.y = y; }
@@ -41,6 +44,8 @@ public interface I1
4144
int m2(string x);
4245
int m2(int x);
4346
}
44-
47+
4548
#pragma warning restore IDE1006 // Naming Styles
49+
#pragma warning restore IDE0051 // Remove unused private members
50+
#pragma warning restore CA1822 // mark as static
4651
}

Clojure/Clojure.Source/clojure/samples/interop/C.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public class C1
4040
public string m6(ref int x) { x += 111; return x.ToString(); }
4141
public string m6(ref string x) { x += "abc"; return x; }
4242

43+
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:Remove unused parameter", Justification = "<Pending>")]
4344
public void m7(string format, params object[] args) { Console.WriteLine("Count is {0}", args.Length); }
4445
}
4546

@@ -65,8 +66,12 @@ public class C4
6566
// For playing with c-tors
6667
public class C5
6768
{
69+
#pragma warning disable IDE0044 // Add readonly modifier
70+
#pragma warning disable IDE0052 // Remove unread private members
6871
string _msg;
6972
object _data;
73+
#pragma warning restore IDE0052 // Remove unread private members
74+
#pragma warning restore IDE0044 // Add readonly modifier
7075

7176
public override string ToString()
7277
{

Clojure/Clojure.Tests/ReflectorTryCatchFixture.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public static void fail(double y)
3737

3838
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE1006:Naming Styles", Justification = "ClojureJVM name match")]
3939
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:Remove unused parameter", Justification = "Part of API")]
40-
public void failWithCause(Double y)
40+
public static void failWithCause(Double y)
4141
{
4242
throw new Cookies("Wrapped", new Cookies("Cause"));
4343
}

Clojure/Clojure/CljCompiler/Ast/DynInitHelper.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ class DynInitHelper
3131
{
3232
#region Data
3333

34-
int _id = 0;
35-
readonly AssemblyGen _assemblyGen = null;
36-
TypeBuilder _typeBuilder = null;
37-
TypeGen _typeGen = null;
34+
int _id;
35+
readonly AssemblyGen _assemblyGen;
36+
TypeBuilder _typeBuilder;
37+
TypeGen _typeGen;
3838

3939
readonly string _typeName;
4040

4141

42-
List<SiteInfo> _siteInfos = null;
42+
List<SiteInfo> _siteInfos;
4343

4444
public class SiteInfo
4545
{
@@ -225,14 +225,14 @@ internal Type MakeDelegateType(string name, Type[] parameters, Type returnType)
225225
//private int _index;
226226
internal TypeBuilder DefineType(string name, Type parent, TypeAttributes attr, bool preserveName)
227227
{
228-
ContractUtils.RequiresNotNull(name, "name");
229-
ContractUtils.RequiresNotNull(parent, "parent");
228+
ContractUtils.RequiresNotNull(name, nameof(name));
229+
ContractUtils.RequiresNotNull(parent, nameof(parent));
230230

231231
StringBuilder sb = new StringBuilder(name);
232232
if (!preserveName)
233233
{
234234
int index = RT.nextID(); //Interlocked.Increment(ref _index);
235-
sb.Append("$");
235+
sb.Append('$');
236236
sb.Append(index);
237237
}
238238

Clojure/Clojure/Lib/BigInteger.cs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public class BigInteger: IComparable, IConvertible, IEquatable<BigInteger>
6060
/// </remarks>
6161
private readonly uint[] _data;
6262

63-
private static readonly BigInteger _zero = new BigInteger(0, new uint[] { });
63+
private static readonly BigInteger _zero = new BigInteger(0, Array.Empty<uint>());
6464
private static readonly BigInteger _one = new BigInteger(1, new uint[] { 1 });
6565
private static readonly BigInteger _two = new BigInteger(1, new uint[] { 2 });
6666
private static readonly BigInteger _five = new BigInteger(1, new uint[] { 5 });
@@ -472,7 +472,7 @@ public string ToString(uint radix)
472472
StringBuilder sb = new StringBuilder(rems.Count * RadixDigitsPerDigit[radix] + 1);
473473

474474
if (_sign < 0)
475-
sb.Append("-");
475+
sb.Append('-');
476476

477477
char[] charBuf = new char[RadixDigitsPerDigit[radix]];
478478

@@ -1575,10 +1575,7 @@ public int CompareTo(object obj)
15751575
return 1;
15761576

15771577

1578-
if (!(obj is BigInteger o))
1579-
throw new ArgumentException("Expected a BigInteger to compare against");
1580-
1581-
return Compare(this, o);
1578+
return obj is BigInteger o ? Compare(this, o) : throw new ArgumentException("Expected a BigInteger to compare against");
15821579
}
15831580

15841581
#endregion
@@ -2009,7 +2006,7 @@ public BigInteger Abs()
20092006
public BigInteger Power(int exp)
20102007
{
20112008
if (exp < 0)
2012-
throw new ArgumentOutOfRangeException("exp","Exponent must be non-negative");
2009+
throw new ArgumentOutOfRangeException(nameof(exp),"Exponent must be non-negative");
20132010

20142011
if (exp == 0)
20152012
return One;
@@ -2043,7 +2040,7 @@ public BigInteger ModPow(BigInteger power, BigInteger mod)
20432040
{
20442041
// TODO: Look at Java implementation for a more efficient version
20452042
if (power < 0)
2046-
throw new ArgumentOutOfRangeException("power","must be non-negative");
2043+
throw new ArgumentOutOfRangeException(nameof(power),"must be non-negative");
20472044

20482045
if (power._sign == 0)
20492046
return One;
@@ -3014,8 +3011,8 @@ private static void DivMod(uint[] x, uint[] y, out uint[] q, out uint[] r)
30143011
// Special case: dividend == 0
30153012
if (xlen == 0)
30163013
{
3017-
q = new uint[0];
3018-
r = new uint[0];
3014+
q = Array.Empty<uint>();
3015+
r = Array.Empty<uint>();
30193016
return;
30203017
}
30213018

@@ -3032,7 +3029,7 @@ private static void DivMod(uint[] x, uint[] y, out uint[] q, out uint[] r)
30323029
// Special case: dividend < divisor
30333030
if (cmp < 0 )
30343031
{
3035-
q = new uint[0];
3032+
q = Array.Empty<uint>();
30363033
r = (uint[])x.Clone();
30373034
return;
30383035
}

Clojure/Clojure/Lib/EdnReader.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,9 @@ public static object read(PushbackTextReader r,
165165
if (isRecursive)
166166
throw;
167167

168-
if (!(r is LineNumberingTextReader lntr))
169-
throw;
170-
171-
throw new ReaderException(lntr.LineNumber, lntr.ColumnNumber, e);
168+
if (r is LineNumberingTextReader lntr)
169+
throw new ReaderException(lntr.LineNumber, lntr.ColumnNumber, e);
170+
throw;
172171
}
173172
}
174173

@@ -1158,7 +1157,7 @@ public override void GetObjectData(SerializationInfo info, StreamingContext cont
11581157
{
11591158
if (info == null)
11601159
{
1161-
throw new ArgumentNullException("info");
1160+
throw new ArgumentNullException(nameof(info));
11621161
}
11631162
base.GetObjectData(info, context);
11641163
info.AddValue("Line", this._line, typeof(int));

Clojure/Clojure/Lib/ExceptionInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public ExceptionInfo(String s, IPersistentMap data, Exception innerException)
5454
if (data != null)
5555
this.data = data;
5656
else
57-
throw new ArgumentException("Additional data must be non-nil.", "data");
57+
throw new ArgumentException("Additional data must be non-nil.", nameof(data));
5858
}
5959

6060
public ExceptionInfo()
@@ -86,7 +86,7 @@ public override void GetObjectData(SerializationInfo info, StreamingContext cont
8686
{
8787
if (info == null)
8888
{
89-
throw new ArgumentNullException("info");
89+
throw new ArgumentNullException(nameof(info));
9090
}
9191
base.GetObjectData(info, context);
9292
info.AddValue("data", this.data, typeof(IPersistentMap));

Clojure/Clojure/Lib/Future.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class Future : IDeref, IBlockingDeref, IPending, IDisposable
3131
object _value;
3232
Exception _error;
3333
bool _cancelled;
34-
bool _disposed = false;
34+
bool _disposed;
3535

3636
#endregion
3737

Clojure/Clojure/Lib/Keyword.cs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,7 @@ public override bool Equals(object obj)
149149
if ( ReferenceEquals(this,obj) )
150150
return true;
151151

152-
if (!(obj is Keyword keyword))
153-
return false;
154-
155-
return _sym.Equals(keyword.Symbol);
152+
return obj is Keyword keyword && _sym.Equals(keyword.Symbol);
156153
}
157154

158155
/// <summary>
@@ -252,10 +249,9 @@ public sealed override object invoke(object arg1, object notFound)
252249
/// <returns>neg,zero,pos for &lt; = &gt;</returns>
253250
public int CompareTo(object obj)
254251
{
255-
if (!(obj is Keyword k))
256-
throw new ArgumentException("Cannot compare to null or non-Keyword", "obj");
257-
258-
return _sym.CompareTo(k._sym);
252+
return obj is Keyword k
253+
? _sym.CompareTo(k._sym)
254+
: throw new ArgumentException("Cannot compare to null or non-Keyword", nameof(obj));
259255
}
260256

261257
#endregion
@@ -290,7 +286,7 @@ public int CompareTo(object obj)
290286
return false;
291287

292288
if (k1 is null)
293-
throw new ArgumentNullException("k1");
289+
throw new ArgumentNullException(nameof(k1));
294290

295291
return k1.CompareTo(k2) < 0;
296292
}
@@ -301,7 +297,7 @@ public int CompareTo(object obj)
301297
return false;
302298

303299
if (k1 is null)
304-
throw new ArgumentNullException("k1");
300+
throw new ArgumentNullException(nameof(k1));
305301

306302
return k1.CompareTo(k2) > 0;
307303
}

Clojure/Clojure/Lib/LazySeq.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -259,13 +259,13 @@ public object this[int index]
259259
get
260260
{
261261
if (index < 0)
262-
throw new ArgumentOutOfRangeException("index","Index must be non-negative.");
262+
throw new ArgumentOutOfRangeException(nameof(index),"Index must be non-negative.");
263263

264264
ISeq s = seq();
265265
for (int i = 0; s != null; s = s.next(), i++)
266266
if (i == index)
267267
return s.first();
268-
throw new ArgumentOutOfRangeException("index", "Index past end of sequence.");
268+
throw new ArgumentOutOfRangeException(nameof(index), "Index past end of sequence.");
269269
}
270270
set
271271
{
@@ -280,13 +280,13 @@ public object this[int index]
280280
public void CopyTo(object[] array, int arrayIndex)
281281
{
282282
if (array == null)
283-
throw new ArgumentNullException("array");
283+
throw new ArgumentNullException(nameof(array));
284284
if (arrayIndex < 0)
285-
throw new ArgumentOutOfRangeException("arrayIndex", "must be non-negative.");
285+
throw new ArgumentOutOfRangeException(nameof(arrayIndex), "must be non-negative.");
286286
if (array.Rank > 1)
287-
throw new ArgumentException("must not be multidimensional","array" );
287+
throw new ArgumentException("must not be multidimensional",nameof(array));
288288
if (arrayIndex >= array.Length)
289-
throw new ArgumentException("must be less than the length", "arrayIndex");
289+
throw new ArgumentException("must be less than the length", nameof(arrayIndex));
290290
if (count() > array.Length - arrayIndex)
291291
throw new InvalidOperationException("Not enough available space from index to end of the array.");
292292

@@ -298,13 +298,13 @@ public void CopyTo(object[] array, int arrayIndex)
298298
public void CopyTo(Array array, int index)
299299
{
300300
if (array == null)
301-
throw new ArgumentNullException("array");
301+
throw new ArgumentNullException(nameof(array));
302302
if (index < 0)
303-
throw new ArgumentOutOfRangeException("index","must be non-negative.");
303+
throw new ArgumentOutOfRangeException(nameof(index),"must be non-negative.");
304304
if (array.Rank > 1)
305-
throw new ArgumentException("must not be multidimensional.", "array");
305+
throw new ArgumentException("must not be multidimensional.", nameof(array));
306306
if (index >= array.Length)
307-
throw new ArgumentException("must be less than the length", "index");
307+
throw new ArgumentException("must be less than the length", nameof(index));
308308
if (count() > array.Length - index)
309309
throw new InvalidOperationException("Not enough available space from index to end of the array.");
310310

0 commit comments

Comments
 (0)