Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable IDE0020 (Use pattern matching) #70523

Merged
merged 3 commits into from
Jun 17, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion eng/CodeAnalysis.src.globalconfig
Original file line number Diff line number Diff line change
Expand Up @@ -1356,7 +1356,7 @@ dotnet_diagnostic.IDE0018.severity = suggestion
dotnet_diagnostic.IDE0019.severity = suggestion

# IDE0020: Use pattern matching to avoid is check followed by a cast (with variable)
dotnet_diagnostic.IDE0020.severity = suggestion
dotnet_diagnostic.IDE0020.severity = warning

# IDE0021: Use expression body for constructors
dotnet_diagnostic.IDE0021.severity = silent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ private RuntimeTypeHandle(IntPtr value)

public override bool Equals(object? obj)
{
if (obj is RuntimeTypeHandle)
if (obj is RuntimeTypeHandle handle)
{
RuntimeTypeHandle handle = (RuntimeTypeHandle)obj;
return Equals(handle);
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,13 @@ public static string ComputeUsefulPertainantIfPossible(object pertainant)
return type.ToDisplayStringIfAvailable(null);
}

if (pertainant is MemberInfo)
if (pertainant is MemberInfo memberInfo)
{
MemberInfo memberInfo = (MemberInfo)pertainant;

StringBuilder friendlyName = new StringBuilder(memberInfo.DeclaringType.ToDisplayStringIfAvailable(null));
friendlyName.Append('.');
friendlyName.Append(memberInfo.Name);
if (pertainant is MethodBase)
if (pertainant is MethodBase method)
{
MethodBase method = (MethodBase)pertainant;
bool first;

// write out generic parameters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,10 +266,8 @@ internal void PrepareType(TypeDesc type)

bool noExtraPreparation = false; // Set this to true for types which don't need other types to be prepared. I.e GenericTypeDefinitions

if (type is DefType)
if (type is DefType typeAsDefType)
{
DefType typeAsDefType = (DefType)type;

if (typeAsDefType.HasInstantiation)
{
if (typeAsDefType.IsTypeDefinition)
Expand Down Expand Up @@ -305,10 +303,8 @@ internal void PrepareType(TypeDesc type)
{
PrepareType(((ParameterizedType)type).ParameterType);

if (type is ArrayType)
if (type is ArrayType typeAsArrayType)
{
ArrayType typeAsArrayType = (ArrayType)type;

if (typeAsArrayType.IsSzArray && !typeAsArrayType.ElementType.IsPointer)
{
TypeDesc.ComputeTemplate(state);
Expand Down Expand Up @@ -1326,10 +1322,8 @@ private void FinishRuntimeType(TypeDesc type)

var state = type.GetTypeBuilderState();

if (type is DefType)
if (type is DefType typeAsDefType)
{
DefType typeAsDefType = (DefType)type;

if (type.HasInstantiation)
{
// Type definitions don't need any further finishing once created by the EETypeCreator
Expand Down Expand Up @@ -1367,10 +1361,8 @@ private void FinishRuntimeType(TypeDesc type)
}
else if (type is ParameterizedType)
{
if (type is ArrayType)
if (type is ArrayType typeAsSzArrayType)
{
ArrayType typeAsSzArrayType = (ArrayType)type;

state.HalfBakedRuntimeTypeHandle.SetRelatedParameterType(GetRuntimeTypeHandle(typeAsSzArrayType.ElementType));

state.HalfBakedRuntimeTypeHandle.SetComponentSize(state.ComponentSize.Value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -973,10 +973,8 @@ public int? FieldAlignment
{
return checked((ushort)((DefType)TypeBeingBuilt).InstanceFieldAlignment.AsInt);
}
else if (TypeBeingBuilt is ArrayType)
else if (TypeBeingBuilt is ArrayType arrayType)
{
ArrayType arrayType = (ArrayType)TypeBeingBuilt;

if (arrayType.ElementType is DefType)
{
return checked((ushort)((DefType)arrayType.ElementType).InstanceFieldAlignment.AsInt);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,8 @@ public RuntimeFieldHandleKey(RuntimeTypeHandle declaringType, string fieldName)

public override bool Equals(object obj)
{
if (obj is RuntimeFieldHandleKey)
if (obj is RuntimeFieldHandleKey other)
{
RuntimeFieldHandleKey other = (RuntimeFieldHandleKey)obj;
return Equals(other);
}
return false;
Expand Down Expand Up @@ -145,9 +144,8 @@ public RuntimeMethodHandleKey(RuntimeTypeHandle declaringType, string methodName

public override bool Equals(object obj)
{
if (obj is RuntimeMethodHandleKey)
if (obj is RuntimeMethodHandleKey other)
{
RuntimeMethodHandleKey other = (RuntimeMethodHandleKey)obj;
return Equals(other);
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,8 @@ internal bool RetrieveRuntimeTypeHandleIfPossible()
if (state != null && state.AttemptedAndFailedToRetrieveTypeHandle)
return false;

if (type is DefType)
if (type is DefType typeAsDefType)
{
DefType typeAsDefType = (DefType)type;

TypeDesc typeDefinition = typeAsDefType.GetTypeDefinition();
RuntimeTypeHandle typeDefHandle = typeDefinition.RuntimeTypeHandle;
if (typeDefHandle.IsNull())
Expand Down Expand Up @@ -155,10 +153,8 @@ internal bool RetrieveRuntimeTypeHandleIfPossible()
}
}
}
else if (type is ParameterizedType)
else if (type is ParameterizedType typeAsParameterType)
{
ParameterizedType typeAsParameterType = (ParameterizedType)type;

if (typeAsParameterType.ParameterType.RetrieveRuntimeTypeHandleIfPossible())
{
RuntimeTypeHandle rtth;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,10 +321,8 @@ protected override int GetValueHashCode(MethodDesc value)

protected override bool CompareKeyToValue(RuntimeMethodKey key, MethodDesc value)
{
if (value is RuntimeMethodDesc)
if (value is RuntimeMethodDesc runtimeMethod)
{
RuntimeMethodDesc runtimeMethod = (RuntimeMethodDesc)value;

if (key._unboxingStub != runtimeMethod.UnboxingStub)
return false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,8 @@ public IDictionary<string, object> GetPartMetadata(TypeInfo partType)
var partMetadata = new Dictionary<string, object>();
foreach (var attr in _attributeContext.GetDeclaredAttributes(partType.AsType(), partType))
{
if (attr is PartMetadataAttribute)
if (attr is PartMetadataAttribute ma)
{
var ma = (PartMetadataAttribute)attr;
partMetadata.Add(ma.Name, ma.Value);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,8 @@ internal void Add(Constraint constraint, bool addUniqueWhenAddingForeign)
}
AddUniqueConstraint((UniqueConstraint)constraint);
}
else if (constraint is ForeignKeyConstraint)
else if (constraint is ForeignKeyConstraint fk)
{
ForeignKeyConstraint fk = (ForeignKeyConstraint)constraint;
if (addUniqueWhenAddingForeign)
{
UniqueConstraint? key = fk.RelatedTable.Constraints.FindKeyConstraint(fk.RelatedColumnsReference);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,9 @@ public override AttributeCollection Attributes

public override Type PropertyType => Column.DataType;

public override bool Equals([NotNullWhen(true)] object? other)
{
if (other is DataColumnPropertyDescriptor)
{
DataColumnPropertyDescriptor descriptor = (DataColumnPropertyDescriptor)other;
return (descriptor.Column == Column);
}

return false;
}
public override bool Equals([NotNullWhen(true)] object? other) =>
other is DataColumnPropertyDescriptor descriptor &&
descriptor.Column == Column;

public override int GetHashCode() => Column.GetHashCode();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -796,9 +796,8 @@ internal void ValidateMultipleNestedRelations()

foreach (Constraint cs in ChildTable.Constraints)
{
if (cs is ForeignKeyConstraint)
if (cs is ForeignKeyConstraint fk)
{
ForeignKeyConstraint fk = (ForeignKeyConstraint)cs;
if (!XmlTreeGen.AutoGenerated(fk, true))
{
throw ExceptionBuilder.TableCantBeNestedInTwoTables(ChildTable.TableName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,9 @@ internal DataRelationPropertyDescriptor(DataRelation dataRelation) : base(dataRe

public override Type PropertyType => typeof(IBindingList);

public override bool Equals([NotNullWhen(true)] object? other)
{
if (other is DataRelationPropertyDescriptor)
{
DataRelationPropertyDescriptor descriptor = (DataRelationPropertyDescriptor)other;
return (descriptor.Relation == Relation);
}
return false;
}
public override bool Equals([NotNullWhen(true)] object? other) =>
other is DataRelationPropertyDescriptor descriptor &&
descriptor.Relation == Relation;

public override int GetHashCode() => Relation.GetHashCode();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,9 @@ internal DataTablePropertyDescriptor(DataTable dataTable) : base(dataTable.Table

public override Type PropertyType => typeof(IBindingList);

public override bool Equals([NotNullWhen(true)] object? other)
{
if (other is DataTablePropertyDescriptor)
{
DataTablePropertyDescriptor descriptor = (DataTablePropertyDescriptor)other;
return (descriptor.Table == Table);
}
return false;
}
public override bool Equals([NotNullWhen(true)] object? other) =>
other is DataTablePropertyDescriptor descriptor &&
descriptor.Table == Table;

public override int GetHashCode() => Table.GetHashCode();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,8 @@ internal override ExpressionNode Optimize()
if (_op == Operators.Is)
{
// only 'Is Null' or 'Is Not Null' are valid
if (_right is UnaryNode)
if (_right is UnaryNode un)
{
UnaryNode un = (UnaryNode)_right;
if (un._op != Operators.Not)
{
throw ExprException.InvalidIsSyntax();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,10 +338,8 @@ public SqlGuid ToSqlGuid()
// If object is not of same type, this method throws an ArgumentException.
public int CompareTo(object? value)
{
if (value is SqlBinary)
if (value is SqlBinary i)
{
SqlBinary i = (SqlBinary)value;

return CompareTo(i);
}
throw ADP.WrongType(value!.GetType(), typeof(SqlBinary));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -441,10 +441,8 @@ public SqlString ToSqlString()
// If object is not of same type, this method throws an ArgumentException.
public int CompareTo(object? value)
{
if (value is SqlBoolean)
if (value is SqlBoolean i)
{
SqlBoolean i = (SqlBoolean)value;

return CompareTo(i);
}
throw ADP.WrongType(value!.GetType(), typeof(SqlBoolean));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -450,10 +450,8 @@ public SqlString ToSqlString()
// If object is not of same type, this method throws an ArgumentException.
public int CompareTo(object? value)
{
if (value is SqlByte)
if (value is SqlByte i)
{
SqlByte i = (SqlByte)value;

return CompareTo(i);
}
throw ADP.WrongType(value!.GetType(), typeof(SqlByte));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -597,10 +597,8 @@ public SqlString ToSqlString()
// If object is not of same type, this method throws an ArgumentException.
public int CompareTo(object? value)
{
if (value is SqlDateTime)
if (value is SqlDateTime i)
{
SqlDateTime i = (SqlDateTime)value;

return CompareTo(i);
}
throw ADP.WrongType(value!.GetType(), typeof(SqlDateTime));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -366,10 +366,8 @@ public SqlString ToSqlString()
// If object is not of same type, this method throws an ArgumentException.
public int CompareTo(object? value)
{
if (value is SqlDouble)
if (value is SqlDouble i)
{
SqlDouble i = (SqlDouble)value;

return CompareTo(i);
}
throw ADP.WrongType(value!.GetType(), typeof(SqlDouble));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,10 +257,8 @@ public SqlBinary ToSqlBinary()
// If object is not of same type, this method throws an ArgumentException.
public int CompareTo(object? value)
{
if (value is SqlGuid)
if (value is SqlGuid i)
{
SqlGuid i = (SqlGuid)value;

return CompareTo(i);
}
throw ADP.WrongType(value!.GetType(), typeof(SqlGuid));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -451,10 +451,8 @@ public SqlString ToSqlString()
// If object is not of same type, this method throws an ArgumentException.
public int CompareTo(object? value)
{
if (value is SqlInt16)
if (value is SqlInt16 i)
{
SqlInt16 i = (SqlInt16)value;

return CompareTo(i);
}
throw ADP.WrongType(value!.GetType(), typeof(SqlInt16));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -466,10 +466,8 @@ public SqlString ToSqlString()
// If object is not of same type, this method throws an ArgumentException.
public int CompareTo(object? value)
{
if (value is SqlInt32)
if (value is SqlInt32 i)
{
SqlInt32 i = (SqlInt32)value;

return CompareTo(i);
}
throw ADP.WrongType(value!.GetType(), typeof(SqlInt32));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -525,10 +525,8 @@ public SqlString ToSqlString()
// If object is not of same type, this method throws an ArgumentException.
public int CompareTo(object? value)
{
if (value is SqlInt64)
if (value is SqlInt64 i)
{
SqlInt64 i = (SqlInt64)value;

return CompareTo(i);
}
throw ADP.WrongType(value!.GetType(), typeof(SqlInt64));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -515,10 +515,8 @@ public SqlString ToSqlString()
// If object is not of same type, this method throws an ArgumentException.
public int CompareTo(object? value)
{
if (value is SqlMoney)
if (value is SqlMoney i)
{
SqlMoney i = (SqlMoney)value;

return CompareTo(i);
}
throw ADP.WrongType(value!.GetType(), typeof(SqlMoney));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -376,10 +376,8 @@ public SqlString ToSqlString()
// If object is not of same type, this method throws an ArgumentException.
public int CompareTo(object? value)
{
if (value is SqlSingle)
if (value is SqlSingle i)
{
SqlSingle i = (SqlSingle)value;

return CompareTo(i);
}
throw ADP.WrongType(value!.GetType(), typeof(SqlSingle));
Expand Down
Loading