Skip to content

Commit c414301

Browse files
Add missing nullable DateTime comparison tests for INTL0202
Add test coverage for DateTime? compared against DateTimeOffset and DateTimeOffset? in binary comparison expressions, which were previously untested edge cases. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 283256e commit c414301

1 file changed

Lines changed: 80 additions & 0 deletions

File tree

IntelliTect.Analyzer/IntelliTect.Analyzer.Test/DateTimeConversionTests.cs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,86 @@ static void Main(string[] args)
339339
});
340340
}
341341

342+
[TestMethod]
343+
public void NullableDateTimeToDateTimeOffsetComparison_ProducesWarningMessage()
344+
{
345+
string source = @"
346+
using System;
347+
using System.Threading;
348+
349+
namespace ConsoleApp1
350+
{
351+
internal class Program
352+
{
353+
static void Main(string[] args)
354+
{
355+
DateTime? first = DateTime.Now;
356+
357+
Thread.Sleep(10);
358+
359+
DateTimeOffset second = DateTimeOffset.Now;
360+
361+
if (first < second)
362+
{
363+
Console.WriteLine(""Time has passed..."");
364+
}
365+
}
366+
}
367+
}";
368+
369+
VerifyCSharpDiagnostic(source,
370+
new DiagnosticResult
371+
{
372+
Id = "INTL0202",
373+
Severity = DiagnosticSeverity.Warning,
374+
Message = "Using 'DateTimeOffset.implicit operator DateTimeOffset(DateTime)' or 'new DateTimeOffset(DateTime)' can result in unpredictable behavior",
375+
Locations =
376+
[
377+
new DiagnosticResultLocation("Test0.cs", 17, 17)
378+
]
379+
});
380+
}
381+
382+
[TestMethod]
383+
public void NullableDateTimeToNullableDateTimeOffsetComparison_ProducesWarningMessage()
384+
{
385+
string source = @"
386+
using System;
387+
using System.Threading;
388+
389+
namespace ConsoleApp1
390+
{
391+
internal class Program
392+
{
393+
static void Main(string[] args)
394+
{
395+
DateTime? first = DateTime.Now;
396+
397+
Thread.Sleep(10);
398+
399+
DateTimeOffset? second = DateTimeOffset.Now;
400+
401+
if (first < second)
402+
{
403+
Console.WriteLine(""Time has passed..."");
404+
}
405+
}
406+
}
407+
}";
408+
409+
VerifyCSharpDiagnostic(source,
410+
new DiagnosticResult
411+
{
412+
Id = "INTL0202",
413+
Severity = DiagnosticSeverity.Warning,
414+
Message = "Using 'DateTimeOffset.implicit operator DateTimeOffset(DateTime)' or 'new DateTimeOffset(DateTime)' can result in unpredictable behavior",
415+
Locations =
416+
[
417+
new DiagnosticResultLocation("Test0.cs", 17, 17)
418+
]
419+
});
420+
}
421+
342422
protected override DiagnosticAnalyzer GetCSharpDiagnosticAnalyzer()
343423
{
344424
return new Analyzers.BanImplicitDateTimeToDateTimeOffsetConversion();

0 commit comments

Comments
 (0)