diff --git a/IntelliTect.Analyzer/IntelliTect.Analyzer.Test/Data/DateTime_ImplicitConversion_InLinq.cs b/IntelliTect.Analyzer/IntelliTect.Analyzer.Test/Data/DateTime_ImplicitConversion_InLinq.cs deleted file mode 100644 index 933c9008..00000000 --- a/IntelliTect.Analyzer/IntelliTect.Analyzer.Test/Data/DateTime_ImplicitConversion_InLinq.cs +++ /dev/null @@ -1,27 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; - -namespace ConsoleApp1 -{ - internal class Pair - { - public DateTimeOffset DateTimeOffset { get; init; } - public DateTime DateTime { get; init; } - - public Pair(DateTimeOffset dateTimeOffset, DateTime dateTime) - { - DateTimeOffset = dateTimeOffset; - DateTime = dateTime; - } - } - - internal class Program - { - static void Main(string[] args) - { - List list = new(){ new(DateTimeOffset.Now, DateTime.Now) }; - _ = list.Where(pair => pair.DateTimeOffset < pair.DateTime); - } - } -} diff --git a/IntelliTect.Analyzer/IntelliTect.Analyzer.Test/DateTimeConversionTests.cs b/IntelliTect.Analyzer/IntelliTect.Analyzer.Test/DateTimeConversionTests.cs index 4523d52a..4d9d785f 100644 --- a/IntelliTect.Analyzer/IntelliTect.Analyzer.Test/DateTimeConversionTests.cs +++ b/IntelliTect.Analyzer/IntelliTect.Analyzer.Test/DateTimeConversionTests.cs @@ -133,12 +133,65 @@ static void Main(string[] args) } + [TestMethod] + public void UsageOfImplicitConversion_InLinqWithVariables_ProducesWarningMessage() + { + string source = @" +using System; +using System.Collections.Generic; +using System.Linq; + +namespace ConsoleApp1 +{ + internal class Pair(DateTimeOffset DateTimeOffset, DateTime DateTime); + + internal class Program + { + static void Main(string[] args) + { + List list = new(){ new(DateTimeOffset.Now, DateTime.Now) }; + _ = list.Where(pair => { + DateTimeOffset first = pair.DateTimeOffset; + DateTime second = pair.DateTime; + return first < second; + }); + } + } +}"; + + VerifyCSharpDiagnostic( + source, + new DiagnosticResult + { + Id = "INTL0202", + Severity = DiagnosticSeverity.Warning, + Message = "Using the symbol 'DateTimeOffset.implicit operator DateTimeOffset(DateTime)' can result in unpredictable behavior", + Locations = [new DiagnosticResultLocation("Test0.cs", 18, 32)] + } + ); + } + [TestMethod] public void UsageOfImplicitConversion_InLinq_ProducesWarningMessage() { - string dir = Directory.GetParent(Environment.CurrentDirectory).Parent.Parent.FullName; - string path = Path.Combine(dir, "Data", "DateTime_ImplicitConversion_InLinq.cs"); - string source = File.ReadAllText(path); + string source = @" +using System; +using System.Collections.Generic; +using System.Linq; + +namespace ConsoleApp1 +{ + internal class Pair(DateTimeOffset DateTimeOffset, DateTime DateTime); + + internal class Program + { + static void Main(string[] args) + { + List list = new(){ new(DateTimeOffset.Now, DateTime.Now) }; + _ = list.Where(pair => pair.DateTimeOffset < pair.DateTime); + } + } +}"; VerifyCSharpDiagnostic( source, @@ -147,7 +200,7 @@ public void UsageOfImplicitConversion_InLinq_ProducesWarningMessage() Id = "INTL0202", Severity = DiagnosticSeverity.Warning, Message = "Using the symbol 'DateTimeOffset.implicit operator DateTimeOffset(DateTime)' can result in unpredictable behavior", - Locations = [new DiagnosticResultLocation("Test0.cs", 17, 25)] + Locations = [new DiagnosticResultLocation("Test0.cs", 15, 36)] } ); } diff --git a/IntelliTect.Analyzer/IntelliTect.Analyzer.Test/IntelliTect.Analyzer.Tests.csproj b/IntelliTect.Analyzer/IntelliTect.Analyzer.Test/IntelliTect.Analyzer.Tests.csproj index 29364a45..0a3961f6 100644 --- a/IntelliTect.Analyzer/IntelliTect.Analyzer.Test/IntelliTect.Analyzer.Tests.csproj +++ b/IntelliTect.Analyzer/IntelliTect.Analyzer.Test/IntelliTect.Analyzer.Tests.csproj @@ -5,10 +5,6 @@ CA2007,CA1815,CA1303,CA1707,CA1305 - - - - all