@@ -122,26 +122,53 @@ private static bool IsValidatedNotNull(string possibleNullReference, BlockSyntax
122122 }
123123 }
124124
125- // Check if this is Assert.NotNull or Assert.IsNotNull for the same symbol
125+ // Check if this is an Assert for the same symbol
126126 if ( AssertHelper . IsAssert ( expressionStatement . Expression , out string member , out ArgumentListSyntax ? argumentList ) )
127127 {
128- if ( member == NUnitFrameworkConstants . NameOfAssertNotNull ||
129- member == NUnitFrameworkConstants . NameOfAssertIsNotNull ||
130- member == NUnitFrameworkConstants . NameOfAssertThat )
128+ string firstArgument = argumentList . Arguments . First ( ) . Expression . ToString ( ) ;
129+
130+ if ( member == NUnitFrameworkConstants . NameOfAssertThat )
131131 {
132- if ( member == NUnitFrameworkConstants . NameOfAssertThat )
132+ string ? secondArgument =
133+ argumentList . Arguments . ElementAtOrDefault ( 1 ) ? . Expression . ToString ( ) ;
134+
135+ // If test is on <nullable>.HasValue
136+ if ( IsHasValue ( firstArgument , possibleNullReference ) )
133137 {
134- // We must check the 2nd argument for anything but "Is.Null"
135- // E.g.: Is.Not.Null.And.Not.Empty.
136- ArgumentSyntax ? secondArgument = argumentList . Arguments . ElementAtOrDefault ( 1 ) ;
137- if ( secondArgument ? . ToString ( ) == "Is.Null" )
138+ // Could be:
139+ // Assert.That(<nullable>.HasValue)
140+ // Assert.That(<nullable>.HasValue, "Ensure Value Set")
141+ // Assert.That(<nullable>.HasValue, Is.True)
142+ if ( secondArgument != "Is.False" )
138143 {
139- continue ;
144+ return true ;
140145 }
141146 }
142-
143- ArgumentSyntax firstArgument = argumentList . Arguments . First ( ) ;
144- if ( CoveredBy ( firstArgument . Expression . ToString ( ) , possibleNullReference ) )
147+ else
148+ {
149+ // Null check, could be Is.Not.Null or more complex
150+ // like Is.Not.Null.And.Not.Empty.
151+ if ( secondArgument != "Is.Null" )
152+ {
153+ if ( CoveredBy ( firstArgument , possibleNullReference ) )
154+ {
155+ return true ;
156+ }
157+ }
158+ }
159+ }
160+ else if ( member == NUnitFrameworkConstants . NameOfAssertNotNull ||
161+ member == NUnitFrameworkConstants . NameOfAssertIsNotNull )
162+ {
163+ if ( CoveredBy ( firstArgument , possibleNullReference ) )
164+ {
165+ return true ;
166+ }
167+ }
168+ else if ( member == NUnitFrameworkConstants . NameOfAssertIsTrue ||
169+ member == NUnitFrameworkConstants . NameOfAssertTrue )
170+ {
171+ if ( IsHasValue ( firstArgument , possibleNullReference ) )
145172 {
146173 return true ;
147174 }
@@ -224,6 +251,11 @@ private static bool CoveredBy(string assertedNotNull, string possibleNullReferen
224251 return false ;
225252 }
226253
254+ private static bool IsHasValue ( string argument , string possibleNullReference )
255+ {
256+ return argument == possibleNullReference + ".HasValue" ;
257+ }
258+
227259 private static ImmutableDictionary < string , SuppressionDescriptor > CreateSuppressionDescriptors ( params string [ ] suppressionDiagnosticsIds )
228260 {
229261 var builder = new Dictionary < string , SuppressionDescriptor > ( ) ;
0 commit comments