Skip to content

Commit f1715bb

Browse files
committed
Add failing test
1 parent ab114e8 commit f1715bb

File tree

3 files changed

+127
-9
lines changed

3 files changed

+127
-9
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
namespace ConsoleApp1
6+
{
7+
internal class Pair
8+
{
9+
public DateTimeOffset DateTimeOffset { get; init; }
10+
public DateTime DateTime { get; init; }
11+
12+
public Pair(DateTimeOffset dateTimeOffset, DateTime dateTime)
13+
{
14+
DateTimeOffset = dateTimeOffset;
15+
DateTime = dateTime;
16+
}
17+
}
18+
19+
internal class Program
20+
{
21+
static void Main(string[] args)
22+
{
23+
List<Pair> list = new(){ new(DateTimeOffset.Now, DateTime.Now) };
24+
_ = list.Where(pair => pair.DateTimeOffset < pair.DateTime);
25+
}
26+
}
27+
}

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

Lines changed: 96 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using Microsoft.CodeAnalysis;
1+
using System;
2+
using System.IO;
3+
using Microsoft.CodeAnalysis;
24
using Microsoft.CodeAnalysis.Diagnostics;
35
using Microsoft.VisualStudio.TestTools.UnitTesting;
46
using TestHelper;
@@ -40,7 +42,7 @@ static void Main(string[] args)
4042
}
4143

4244
[TestMethod]
43-
public void UsageOfImplicitConversionInComparison_ProducesWarningMessage()
45+
public void UsageOfImplicitConversionInComparison_DateTimeToDateTimeOffset_ProducesWarningMessage()
4446
{
4547
string source = @"
4648
using System;
@@ -53,15 +55,42 @@ internal class Program
5355
static void Main(string[] args)
5456
{
5557
DateTime first = DateTime.Now;
58+
DateTimeOffset second = DateTimeOffset.Now;
59+
_ = first < second
60+
}
61+
}
62+
}";
5663

57-
Thread.Sleep(10);
64+
VerifyCSharpDiagnostic(source,
65+
new DiagnosticResult
66+
{
67+
Id = "INTL0202",
68+
Severity = DiagnosticSeverity.Warning,
69+
Message = "Using the symbol 'DateTimeOffset.implicit operator DateTimeOffset(DateTime)' can result in unpredictable behavior",
70+
Locations =
71+
new[] {
72+
new DiagnosticResultLocation("Test0.cs", 13, 17)
73+
}
74+
});
5875

59-
DateTimeOffset second = DateTimeOffset.Now;
76+
}
6077

61-
if (first < second)
62-
{
63-
Console.WriteLine(""Time has passed..."");
64-
}
78+
[TestMethod]
79+
public void UsageOfImplicitConversionInComparison_DateTimeOffsetToDateTime_ProducesWarningMessage()
80+
{
81+
string source = @"
82+
using System;
83+
using System.Threading;
84+
85+
namespace ConsoleApp1
86+
{
87+
internal class Program
88+
{
89+
static void Main(string[] args)
90+
{
91+
DateTimeOffset first = DateTimeOffset.Now;
92+
DateTime second = DateTime.Now;
93+
_ = first < second
6594
}
6695
}
6796
}";
@@ -74,12 +103,70 @@ static void Main(string[] args)
74103
Message = "Using the symbol 'DateTimeOffset.implicit operator DateTimeOffset(DateTime)' can result in unpredictable behavior",
75104
Locations =
76105
new[] {
77-
new DiagnosticResultLocation("Test0.cs", 17, 17)
106+
new DiagnosticResultLocation("Test0.cs", 13, 25)
78107
}
79108
});
80109

81110
}
82111

112+
[TestMethod]
113+
public void UsageOfImplicitConversionInComparison_NullableDateTimeOffsetToDateTime_ProducesWarningMessage()
114+
{
115+
string source = @"
116+
using System;
117+
using System.Threading;
118+
119+
namespace ConsoleApp1
120+
{
121+
internal class Program
122+
{
123+
static void Main(string[] args)
124+
{
125+
DateTimeOffset? first = DateTimeOffset.Now;
126+
DateTime second = DateTime.Now;
127+
_ = first < second
128+
}
129+
}
130+
}";
131+
132+
VerifyCSharpDiagnostic(
133+
source,
134+
new DiagnosticResult
135+
{
136+
Id = "INTL0202",
137+
Severity = DiagnosticSeverity.Warning,
138+
Message = "Using the symbol 'DateTimeOffset.implicit operator DateTimeOffset(DateTime)' can result in unpredictable behavior",
139+
Locations = new[] {
140+
new DiagnosticResultLocation("Test0.cs", 13, 25)
141+
}
142+
}
143+
);
144+
145+
}
146+
147+
[TestMethod]
148+
public void UsageOfImplicitConversion_InLinq_ProducesWarningMessage()
149+
{
150+
string dir = Directory.GetParent(Environment.CurrentDirectory).Parent.Parent.FullName;
151+
string path = Path.Combine(dir, "Data", "DateTime_ImplicitConversion_InLinq.cs");
152+
string source = File.ReadAllText(path);
153+
154+
VerifyCSharpDiagnostic(
155+
source,
156+
new DiagnosticResult
157+
{
158+
Id = "INTL0202",
159+
Severity = DiagnosticSeverity.Warning,
160+
Message = "Using the symbol 'DateTimeOffset.implicit operator DateTimeOffset(DateTime)' can result in unpredictable behavior",
161+
Locations =
162+
new[] {
163+
new DiagnosticResultLocation("Test0.cs", 17, 25)
164+
}
165+
}
166+
);
167+
}
168+
169+
83170
[TestMethod]
84171
public void UsageOfExplicitConversion_ProducesNothing()
85172
{

IntelliTect.Analyzer/IntelliTect.Analyzer.Test/IntelliTect.Analyzer.Tests.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
<NoWarn>CA2007,CA1815,CA1303,CA1707,CA1305</NoWarn>
66
</PropertyGroup>
77

8+
<ItemGroup>
9+
<Compile Remove="Data\**" />
10+
</ItemGroup>
11+
812
<ItemGroup>
913
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4-beta1.22559.1">
1014
<PrivateAssets>all</PrivateAssets>

0 commit comments

Comments
 (0)