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

Check for nullity using "is null" or "is object" #1624

Merged
merged 1 commit into from
Sep 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public abstract class ArgumentConstraintAnalyzerBase : DiagnosticAnalyzer
{
public override void Initialize(AnalysisContext context)
{
if (context == null)
if (context is null)
{
throw new ArgumentNullException(nameof(context));
}
Expand All @@ -41,13 +41,13 @@ private static SyntaxNode GetCompleteConstraint(SyntaxNode node)
private void AnalyzeNode(SyntaxNodeAnalysisContext context)
{
var memberAccess = context.Node as MemberAccessExpressionSyntax;
if (memberAccess == null)
if (memberAccess is null)
{
return;
}

var propertySymbol = SymbolHelpers.GetAccessedPropertySymbol(memberAccess, context);
if (propertySymbol == null)
if (propertySymbol is null)
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ protected override void AnalyzeArgumentConstraintCore(string propertyFullName, S

private static bool IsInArgumentToMethodThatSupportsArgumentConstraints(SyntaxNode node, SyntaxNodeAnalysisContext context)
{
while (node != null)
while (node is object)
{
switch (node.Kind())
{
Expand All @@ -69,7 +69,7 @@ private static bool IsInArgumentToMethodThatSupportsArgumentConstraints(SyntaxNo
}

var invocation = node as InvocationExpressionSyntax;
if (invocation != null && SupportsArgumentConstraints(invocation, context))
if (invocation is object && SupportsArgumentConstraints(invocation, context))
{
return true;
}
Expand All @@ -83,7 +83,7 @@ private static bool IsInArgumentToMethodThatSupportsArgumentConstraints(SyntaxNo
private static bool SupportsArgumentConstraints(InvocationExpressionSyntax invocation, SyntaxNodeAnalysisContext context)
{
var methodSymbol = SymbolHelpers.GetCalledMethodSymbol(invocation, context, true);
if (methodSymbol == null)
if (methodSymbol is null)
{
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class ArgumentConstraintTypeMismatchAnalyzer : ArgumentConstraintAnalyzer
protected override void AnalyzeArgumentConstraintCore(string propertyFullName, SyntaxNodeAnalysisContext context, SyntaxNode completeConstraint)
{
var constraintType = context.SemanticModel.GetTypeInfo(completeConstraint).Type as INamedTypeSymbol;
if (constraintType == null)
if (constraintType is null)
{
return;
}
Expand All @@ -50,7 +50,7 @@ protected override void AnalyzeArgumentConstraintCore(string propertyFullName, S
var argumentList = argument?.Parent as ArgumentListSyntax;
#endif

if (argumentList == null)
if (argumentList is null)
{
return;
}
Expand All @@ -77,7 +77,7 @@ protected override void AnalyzeArgumentConstraintCore(string propertyFullName, S

var nonNullableParameterType = parameterType.IsNullable() ? parameterType.TypeArguments[0] : null;

if (nonNullableParameterType != null &&
if (nonNullableParameterType is object &&
constraintType.IsValueType &&
!constraintType.IsNullable() &&
constraintType.Equals(nonNullableParameterType))
Expand Down Expand Up @@ -135,11 +135,11 @@ protected override void AnalyzeArgumentConstraintCore(string propertyFullName, S
case InvocationExpressionSyntax invocation:
{
var method = SymbolHelpers.GetCalledMethodSymbol(invocation, context);
if (method == null)
if (method is null)
{
#if VISUAL_BASIC
var indexer = SymbolHelpers.GetAccessedIndexerSymbol(invocation, context);
if (indexer == null)
if (indexer is null)
{
return false;
}
Expand All @@ -160,7 +160,7 @@ protected override void AnalyzeArgumentConstraintCore(string propertyFullName, S
case ElementAccessExpressionSyntax elementAccess:
{
var indexer = SymbolHelpers.GetAccessedIndexerSymbol(elementAccess, context);
if (indexer == null)
if (indexer is null)
{
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class ArgumentConstraintTypeMismatchCodeFixProvider : CodeFixProvider
public override Task RegisterCodeFixesAsync(CodeFixContext context)
{
var diagnostic = context.Diagnostics.FirstOrDefault();
if (diagnostic == null)
if (diagnostic is null)
{
return CompletedTask;
}
Expand Down Expand Up @@ -91,7 +91,7 @@ private static async Task<Document> MakeConstraintNullableAsync(CodeFixContext c

// The T type
var constraintType = aType?.TypeArgumentList.Arguments.FirstOrDefault();
if (constraintType != null)
if (constraintType is object)
{
// The T? type
var nullableConstraintType = SyntaxFactory.NullableType(constraintType);
Expand Down Expand Up @@ -147,7 +147,7 @@ private static async Task<Document> ChangeConstraintTypeAsync(CodeFixContext con

// The T type
var constraintType = GetConstraintType(diagnostic, root);
if (constraintType != null)
if (constraintType is object)
{
var parameterTypeName = diagnostic.Properties[ArgumentConstraintTypeMismatchAnalyzer.ParameterTypeKey];
var parameterType = SyntaxFactory.ParseName(parameterTypeName);
Expand Down
6 changes: 3 additions & 3 deletions src/FakeItEasy.Analyzer.Shared/NonVirtualSetupAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ internal class NonVirtualSetupAnalyzer : DiagnosticAnalyzer

public override void Initialize(AnalysisContext context)
{
if (context == null)
if (context is null)
{
throw new ArgumentNullException(nameof(context));
}
Expand Down Expand Up @@ -111,7 +111,7 @@ private static void AnalyzeVBIndexer(SyntaxNodeAnalysisContext context)

var invocationParent = FindInvocationInHierarchy(invocationExpression);

if (invocationParent == null || !IsSetupInvocation(context, invocationParent))
if (invocationParent is null || !IsSetupInvocation(context, invocationParent))
{
return;
}
Expand Down Expand Up @@ -139,7 +139,7 @@ private static bool IsVirtual(SymbolInfo symbolInfo)
private static bool IsSetupInvocation(SyntaxNodeAnalysisContext context, InvocationExpressionSyntax parent)
{
var methodSymbol = SymbolHelpers.GetCalledMethodSymbol(parent, context);
return methodSymbol != null && CallSpecMethods.Contains(methodSymbol.GetFullName());
return methodSymbol is object && CallSpecMethods.Contains(methodSymbol.GetFullName());
}

private static bool IsProperty(SymbolInfo symbolInfo)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class RepeatedAssertionAnalyzer : DiagnosticAnalyzer

public override void Initialize(AnalysisContext context)
{
if (context == null)
if (context is null)
{
throw new ArgumentNullException(nameof(context));
}
Expand Down
4 changes: 2 additions & 2 deletions src/FakeItEasy.Analyzer.Shared/SymbolExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ internal static class SymbolExtensions
/// <returns>The full name of the type.</returns>
public static string GetFullName(this INamedTypeSymbol type)
{
if (type == null)
if (type is null)
{
throw new ArgumentNullException(nameof(type));
}

var nameParts = new Stack<string>();
nameParts.Push(type.GetDecoratedName());
var containingType = type.ContainingType;
while (containingType != null)
while (containingType is object)
{
nameParts.Push(containingType.GetDecoratedName());
containingType = containingType.ContainingType;
Expand Down
2 changes: 1 addition & 1 deletion src/FakeItEasy.Analyzer.Shared/SymbolHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ internal static IMethodSymbol GetCalledMethodSymbol(InvocationExpressionSyntax c
{
var symbolInfo = context.SemanticModel.GetSymbolInfo(call);
var symbol = symbolInfo.Symbol;
if (symbol == null && useFirstCandidateIfNotResolved)
if (symbol is null && useFirstCandidateIfNotResolved)
{
symbol = symbolInfo.CandidateSymbols.FirstOrDefault();
}
Expand Down
6 changes: 3 additions & 3 deletions src/FakeItEasy.Analyzer.Shared/UnusedReturnValueAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class UnusedReturnValueAnalyzer : DiagnosticAnalyzer

public override void Initialize(AnalysisContext context)
{
if (context == null)
if (context is null)
{
throw new ArgumentNullException(nameof(context));
}
Expand All @@ -53,13 +53,13 @@ public override void Initialize(AnalysisContext context)
private static void AnalyzeCall(SyntaxNodeAnalysisContext context)
{
var call = context.Node as InvocationExpressionSyntax;
if (call == null)
if (call is null)
{
return;
}

var methodSymbol = SymbolHelpers.GetCalledMethodSymbol(call, context);
if (methodSymbol == null ||
if (methodSymbol is null ||
!CallSpecMethods.Contains(methodSymbol.GetFullName()))
{
return;
Expand Down
10 changes: 5 additions & 5 deletions src/FakeItEasy/ArgumentConstraintManagerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static class ArgumentConstraintManagerExtensions
{
Guard.AgainstNull(manager, nameof(manager));

return manager.Matches(x => x == null, x => x.Write("NULL"));
return manager.Matches(x => x is null, x => x.Write("NULL"));
}

/// <summary>
Expand All @@ -39,7 +39,7 @@ public static class ArgumentConstraintManagerExtensions
{
Guard.AgainstNull(manager, nameof(manager));

return manager.Matches(x => x == null, x => x.Write("NULL"));
return manager.Matches(x => x is null, x => x.Write("NULL"));
}

/// <summary>
Expand All @@ -52,7 +52,7 @@ public static class ArgumentConstraintManagerExtensions
{
Guard.AgainstNull(manager, nameof(manager));

return manager.Matches(x => x != null, x => x.Write("NOT NULL"));
return manager.Matches(x => x is object, x => x.Write("NOT NULL"));
}

/// <summary>
Expand All @@ -66,7 +66,7 @@ public static class ArgumentConstraintManagerExtensions
{
Guard.AgainstNull(manager, nameof(manager));

return manager.Matches(x => x != null, x => x.Write("NOT NULL"));
return manager.Matches(x => x is object, x => x.Write("NOT NULL"));
}

/// <summary>
Expand Down Expand Up @@ -317,7 +317,7 @@ public static T NullCheckedMatches<T>(this IArgumentConstraintManager<T> manager
Guard.AgainstNull(manager, nameof(manager));

return manager.Matches(
x => !ReferenceEquals(x, null) && predicate(x),
x => x is object && predicate(x),
descriptionWriter);
}

Expand Down
4 changes: 2 additions & 2 deletions src/FakeItEasy/Configuration/AnyCallCallRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public AnyCallCallRule()

public override void DescribeCallOn(IOutputWriter writer)
{
if (this.ApplicableToMembersWithReturnType != null)
if (this.ApplicableToMembersWithReturnType is object)
{
if (this.ApplicableToMembersWithReturnType == typeof(void))
{
Expand Down Expand Up @@ -59,7 +59,7 @@ protected override bool OnIsApplicableTo(IFakeObjectCall fakeObjectCall)
throw new UserCallbackException(ExceptionMessages.UserCallbackThrewAnException("Arguments predicate"), ex);
}

if (this.ApplicableToMembersWithReturnType != null)
if (this.ApplicableToMembersWithReturnType is object)
{
return this.ApplicableToMembersWithReturnType == fakeObjectCall.Method.ReturnType;
}
Expand Down
2 changes: 1 addition & 1 deletion src/FakeItEasy/Configuration/BuildableCallRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ private static ICollection<int> GetIndexesOfOutAndRefParameters(IInterceptedFake

private void ApplyOutAndRefParametersValueProducer(IInterceptedFakeObjectCall fakeObjectCall)
{
if (this.OutAndRefParametersValueProducer == null)
if (this.OutAndRefParametersValueProducer is null)
{
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/FakeItEasy/Configuration/FakeConfigurationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public IPropertySetterAnyValueConfiguration<TValue> CallToSet<TValue>(Expression

private static void GuardAgainstNonFake(object target)
{
if (target != null)
if (target is object)
{
Fake.GetFakeManager(target);
}
Expand Down
4 changes: 2 additions & 2 deletions src/FakeItEasy/Configuration/PropertyExpressionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ internal static class PropertyExpressionHelper
ParsedCallExpression parsedCallExpression)
{
var propertyName = GetPropertyName(parsedCallExpression);
if (propertyName == null)
if (propertyName is null)
{
var expressionDescription = GetExpressionDescription(parsedCallExpression);
throw new ArgumentException("Expression '" + expressionDescription +
Expand All @@ -36,7 +36,7 @@ internal static class PropertyExpressionHelper
var setPropertyName = "set_" + propertyName;
var indexerSetterInfo = callTargetType.GetMethod(setPropertyName, parameterTypes);

if (indexerSetterInfo == null)
if (indexerSetterInfo is null)
{
if (parsedCallExpression.ArgumentsExpressions.Any())
{
Expand Down
2 changes: 1 addition & 1 deletion src/FakeItEasy/Configuration/RuleBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ private void AddRuleIfNeeded()
{
if (!this.wasRuleAdded)
{
if (this.PreviousRule != null)
if (this.PreviousRule is object)
{
this.manager.AddRuleAfter(this.PreviousRule, this.RuleBeingBuilt);
}
Expand Down
2 changes: 1 addition & 1 deletion src/FakeItEasy/Core/ArgumentConstraintTrap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ internal class ArgumentConstraintTrap
[SuppressMessage("Microsoft.Naming", "CA2204:Literals should be spelled correctly", MessageId = "CallTo", Justification = "It's an identifier")]
public static void ReportTrappedConstraint(IArgumentConstraint constraint)
{
if (trappedConstraints == null)
if (trappedConstraints is null)
{
throw new InvalidOperationException("A<T>.Ignored, A<T>._, and A<T>.That can only be used in the context of a call specification with A.CallTo()");
}
Expand Down
4 changes: 2 additions & 2 deletions src/FakeItEasy/Core/ArgumentValueFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public ArgumentValueFormatter(IEnumerable<IArgumentValueFormatter> typeFormatter

public virtual string GetArgumentValueAsString(object argumentValue)
{
if (argumentValue == null)
if (argumentValue is null)
{
return "NULL";
}
Expand Down Expand Up @@ -63,7 +63,7 @@ private static int GetDistanceFromKnownType(Type comparedType, Type knownType)

var distance = 2;
var currentType = knownType.GetTypeInfo().BaseType;
while (currentType != null)
while (currentType is object)
{
if (currentType == comparedType)
{
Expand Down
2 changes: 1 addition & 1 deletion src/FakeItEasy/Core/CallRuleMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ private CallRuleMetadata(IFakeObjectCallRule rule, int calledNumberOfTimes)
/// <returns>True if the rule has not been called the number of times specified.</returns>
public bool HasNotBeenCalledSpecifiedNumberOfTimes()
{
return this.Rule.NumberOfTimesToCall == null || this.CalledNumberOfTimes < this.Rule.NumberOfTimesToCall.Value;
return this.Rule.NumberOfTimesToCall is null || this.CalledNumberOfTimes < this.Rule.NumberOfTimesToCall.Value;
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/FakeItEasy/Core/DefaultArgumentConstraintManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ bool IArgumentConstraint.IsValid(object argument)

private static bool IsValueValidForType(object argument)
{
if (argument == null)
if (argument is null)
{
return IsNullable;
}
Expand Down
2 changes: 1 addition & 1 deletion src/FakeItEasy/Core/DefaultFakeManagerAccessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public FakeManager GetFakeManager(object proxy)

FakeManager result = this.TryGetFakeManager(proxy);

if (result == null)
if (result is null)
{
throw new ArgumentException(ExceptionMessages.NotRecognizedAsAFake(proxy, proxy.GetType()));
}
Expand Down
2 changes: 1 addition & 1 deletion src/FakeItEasy/Core/DynamicDummyFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public bool TryCreateDummyObject(Type typeOfDummy, out object fakeObject)
typeOfDummy,
type => this.allDummyFactories.FirstOrDefault(factory => factory.CanCreate(type)));

if (dummyFactory == null)
if (dummyFactory is null)
{
fakeObject = null;
return false;
Expand Down