Examples
The following examples show the source-code before (Non-Compliant) and after, containing the EagleRepair's fix (Compliant).
R1 DisposePattern
Non-Compliant
using System;
namespace N
{
public class C : IDisposable
{
public void Dispose()
{
// cleanup
}
}
}
Compliant
using System;
namespace N
{
public class C : IDisposable
{
// To detect redundant calls
private bool _disposed = false;
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (_disposed)
{
return;
}
if (disposing)
{
// cleanup
}
_disposed = true;
}
}
}
R2 MergeSequentialChecks
Non-Compliant
public void M(B b)
{
if (b == null || b.Parent is null)
{
// do something
}
}
Compliant
public void M(B b)
{
if (b?.Parent is null)
{
// do something
}
}
R3 NullChecksShouldNotBeUsedWithIs
Non-Compliant
public void M(object s)
{
if (s != null && s is string)
{
// do something
}
}
Compliant
public void M(object s)
{
if (s is string)
{
// do something
}
}
R4 SimplifyLinq
Non-Compliant
public void M()
{
var list = new List<int>();
var result1 = list.Where(i => i is not null && i.ToString().Equals("foo")).Count();
var result2 = list.Where(i => i > 0).First();
var result3 = list.Where(i => i > 0).FirstOrDefault();
var result4 = list.Where(i => i > 0).Single();
var result5 = list.Where(i => i > 0).SingleOrDefault();
var result6 = list.Where(i => i is not null && i.ToString().Equals("foo")).Last();
var result7 = list.Where(element => element is T).Select(e => e as T);
}
Compliant
public void M()
{
var list = new List<int>();
var result1 = list.Count(i => i is not null);
var result2 = list.First(i => i > 0);
var result3 = list.FirstOrDefault(i => i > 0);
var result4 = list.Single(i => i > 0);
var result5 = list.SingleOrDefault(i => i > 0);
var result6 = list.Last(i => i is not null);
var result7 = list.OfType<T>();
}
R5 TypeCheckAndCast
Non-Compliant
if (o is string)
{
var str = (string) o;
var length = str.Length.GetHashCode();
return length > 2;
}
Compliant
if (o is string str)
{
var length = str.Length.GetHashCode();
return length > 2;
}
R6 UseMethodAny
Non-Compliant
if (list.Count() > 0)
{
Console.WriteLine("List is not empty.");
}
Compliant
if (list.Any())
{
Console.WriteLine("List is not empty.");
}
R7 UseNullPropagation
Non-Compliant
public void M(Car c)
{
if (c != null)
{
c.Fly(42);
}
}
Compliant
public void M(Car c)
{
c?.Fly(42);
}
R8 UsePatternMatching
Non-Compliant
public void M(object o)
{
var s = o as string;
// Check if string is not null
if (s != null) {
Console.WriteLine($"Hi. {s}");
}
}
Compliant
public void M(object o)
{
// Check if string is not null
if (o is string s) {
Console.WriteLine($"Hi. {s}");
}
}
R9 UseStringInterpolation
Non-Compliant
public IList<string> M()
{
const string msg = "Value is:";
return new List<string>
{
string.Format("{0} {1}", msg, _utils.GetValue())
};
}
}
Compliant
public IList<string> M()
{
const string msg = "Value is:";
return new List<string>
{
$"{msg} {_utils.GetValue()}"
};
}
}
R10 UseStringIsNullOrEmpty
Non-Compliant
public void M(string s)
{
if(s != null && s.Length > 0) {
// do something
}
}
Compliant
public void M(string s)
{
if(!string.IsNullOrEmpty(s)) {
// do something
}
}
R11 UseCountProperty (COMING SOON)
Non-Compliant
public int M(ICollection<int> collection)
{
return collection.Count();
}
Compliant
public int M(ICollection<int> collection)
{
return collection.Count;
}