Skip to content

Commit

Permalink
Fix Possible 'System.NullReferenceException' warning for Types assert…
Browse files Browse the repository at this point in the history
…ions
  • Loading branch information
lg2de authored and jnyrup committed Aug 27, 2023
1 parent e332aff commit bc49f11
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
4 changes: 2 additions & 2 deletions Src/FluentAssertions/Types/MethodBaseAssertions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ protected MethodBaseAssertions(TSubject subject)
.ForCondition(accessModifier == subjectAccessModifier)
.BecauseOf(because, becauseArgs)
.FailWith(
$"Expected method {Subject.Name} to be {accessModifier}{{reason}}, but it is {subjectAccessModifier}.");
$"Expected method {Subject!.Name} to be {accessModifier}{{reason}}, but it is {subjectAccessModifier}.");
}

return new AndConstraint<TAssertions>((TAssertions)this);
Expand Down Expand Up @@ -88,7 +88,7 @@ protected MethodBaseAssertions(TSubject subject)
Execute.Assertion
.ForCondition(accessModifier != subjectAccessModifier)
.BecauseOf(because, becauseArgs)
.FailWith($"Expected method {Subject.Name} not to be {accessModifier}{{reason}}, but it is.");
.FailWith($"Expected method {Subject!.Name} not to be {accessModifier}{{reason}}, but it is.");
}

return new AndConstraint<TAssertions>((TAssertions)this);
Expand Down
8 changes: 4 additions & 4 deletions Src/FluentAssertions/Types/MethodInfoAssertions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public AndConstraint<MethodInfoAssertions> NotBeAsync(string because = "", param
if (success)
{
Execute.Assertion
.ForCondition(typeof(void) == Subject.ReturnType)
.ForCondition(typeof(void) == Subject!.ReturnType)
.BecauseOf(because, becauseArgs)
.FailWith("Expected the return type of method " + Subject.Name + " to be void{reason}, but it is {0}.",
Subject.ReturnType.FullName);
Expand Down Expand Up @@ -184,7 +184,7 @@ public AndConstraint<MethodInfoAssertions> NotBeAsync(string because = "", param
if (success)
{
Execute.Assertion
.ForCondition(returnType == Subject.ReturnType)
.ForCondition(returnType == Subject!.ReturnType)
.BecauseOf(because, becauseArgs)
.FailWith("Expected the return type of method " + Subject.Name + " to be {0}{reason}, but it is {1}.",
returnType, Subject.ReturnType.FullName);
Expand Down Expand Up @@ -231,7 +231,7 @@ public AndConstraint<MethodInfoAssertions> NotBeAsync(string because = "", param
if (success)
{
Execute.Assertion
.ForCondition(typeof(void) != Subject.ReturnType)
.ForCondition(typeof(void) != Subject!.ReturnType)
.BecauseOf(because, becauseArgs)
.FailWith("Expected the return type of method " + Subject.Name + " not to be void{reason}, but it is.");
}
Expand Down Expand Up @@ -265,7 +265,7 @@ public AndConstraint<MethodInfoAssertions> NotBeAsync(string because = "", param
if (success)
{
Execute.Assertion
.ForCondition(returnType != Subject.ReturnType)
.ForCondition(returnType != Subject!.ReturnType)
.BecauseOf(because, becauseArgs)
.FailWith(
"Expected the return type of method " + Subject.Name + " not to be {0}{reason}, but it is.", returnType);
Expand Down
16 changes: 8 additions & 8 deletions Src/FluentAssertions/Types/PropertyInfoAssertions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public AndConstraint<PropertyInfoAssertions> NotBeVirtual(string because = "", p
if (success)
{
Execute.Assertion
.ForCondition(Subject.CanWrite)
.ForCondition(Subject!.CanWrite)
.BecauseOf(because, becauseArgs)
.FailWith(
"Expected {context:property} {0} to have a setter{reason}.",
Expand Down Expand Up @@ -132,7 +132,7 @@ public AndConstraint<PropertyInfoAssertions> NotBeVirtual(string because = "", p
{
Subject.Should().BeWritable(because, becauseArgs);

Subject.GetSetMethod(nonPublic: true).Should().HaveAccessModifier(accessModifier, because, becauseArgs);
Subject!.GetSetMethod(nonPublic: true).Should().HaveAccessModifier(accessModifier, because, becauseArgs);
}

return new AndConstraint<PropertyInfoAssertions>(this);
Expand All @@ -159,7 +159,7 @@ public AndConstraint<PropertyInfoAssertions> NotBeVirtual(string because = "", p
if (success)
{
Execute.Assertion
.ForCondition(!Subject.CanWrite)
.ForCondition(!Subject!.CanWrite)
.BecauseOf(because, becauseArgs)
.FailWith(
"Expected {context:property} {0} not to have a setter{reason}.",
Expand Down Expand Up @@ -188,7 +188,7 @@ public AndConstraint<PropertyInfoAssertions> BeReadable(string because = "", par

if (success)
{
Execute.Assertion.ForCondition(Subject.CanRead)
Execute.Assertion.ForCondition(Subject!.CanRead)
.BecauseOf(because, becauseArgs)
.FailWith("Expected property " + Subject.Name + " to have a getter{reason}, but it does not.");
}
Expand Down Expand Up @@ -223,7 +223,7 @@ public AndConstraint<PropertyInfoAssertions> BeReadable(string because = "", par
{
Subject.Should().BeReadable(because, becauseArgs);

Subject.GetGetMethod(nonPublic: true).Should().HaveAccessModifier(accessModifier, because, becauseArgs);
Subject!.GetGetMethod(nonPublic: true).Should().HaveAccessModifier(accessModifier, because, becauseArgs);
}

return new AndConstraint<PropertyInfoAssertions>(this);
Expand All @@ -250,7 +250,7 @@ public AndConstraint<PropertyInfoAssertions> BeReadable(string because = "", par
if (success)
{
Execute.Assertion
.ForCondition(!Subject.CanRead)
.ForCondition(!Subject!.CanRead)
.BecauseOf(because, becauseArgs)
.FailWith(
"Expected {context:property} {0} not to have a getter{reason}.",
Expand Down Expand Up @@ -284,7 +284,7 @@ public AndConstraint<PropertyInfoAssertions> BeReadable(string because = "", par

if (success)
{
Execute.Assertion.ForCondition(Subject.PropertyType == propertyType)
Execute.Assertion.ForCondition(Subject!.PropertyType == propertyType)
.BecauseOf(because, becauseArgs)
.FailWith("Expected Type of property " + Subject.Name + " to be {0}{reason}, but it is {1}.",
propertyType, Subject.PropertyType);
Expand Down Expand Up @@ -333,7 +333,7 @@ public AndConstraint<PropertyInfoAssertions> NotReturn(Type propertyType, string
if (success)
{
Execute.Assertion
.ForCondition(Subject.PropertyType != propertyType)
.ForCondition(Subject!.PropertyType != propertyType)
.BecauseOf(because, becauseArgs)
.FailWith("Expected Type of property " + Subject.Name + " not to be {0}{reason}, but it is.", propertyType);
}
Expand Down

0 comments on commit bc49f11

Please sign in to comment.