Skip to content

Commit

Permalink
Add UsageOfImplicitConversion_InLinqWithVariables_ProducesWarningMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
joeriddles committed Jan 9, 2024
1 parent 9f3ce70 commit 25c0a0c
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 35 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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<Pair> 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<Pair> list = new(){ new(DateTimeOffset.Now, DateTime.Now) };
_ = list.Where(pair => pair.DateTimeOffset < pair.DateTime);
}
}
}";

VerifyCSharpDiagnostic(
source,
Expand All @@ -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)]
}
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
<NoWarn>CA2007,CA1815,CA1303,CA1707,CA1305</NoWarn>
</PropertyGroup>

<ItemGroup>
<Compile Remove="Data\**" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers">
<PrivateAssets>all</PrivateAssets>
Expand Down

0 comments on commit 25c0a0c

Please sign in to comment.