Skip to content

INTL0202: Flag DateTimeOffset(DateTime) constructor alongside implicit conversion#368

Merged
BenjaminMichaelis merged 5 commits intomainfrom
copilot/flag-datetimeoffset-constructor
Feb 3, 2026
Merged

INTL0202: Flag DateTimeOffset(DateTime) constructor alongside implicit conversion#368
BenjaminMichaelis merged 5 commits intomainfrom
copilot/flag-datetimeoffset-constructor

Conversation

Copy link
Contributor

Copilot AI commented Feb 2, 2026

Plan for INTL0202 - Also flag DateTimeOffset(DateTime) constructor

  • Understand current INTL0202 analyzer implementation
    • Review BanImplicitDateTimeToDateTimeOffsetConversion.cs
    • Review existing tests in DateTimeConversionTests.cs
    • Confirm Microsoft documentation on DateTimeOffset constructors
  • Extend analyzer to detect DateTimeOffset(DateTime) constructor usage
    • Register operation action for ObjectCreation operations
    • Check if the created type is DateTimeOffset
    • Check if constructor has exactly 1 parameter of type DateTime
    • Report diagnostic when match is found
  • Add tests for the new functionality
    • Test that flags new DateTimeOffset(DateTime.Now)
    • Test that flags new DateTimeOffset(new DateTime(...))
    • Test that does NOT flag new DateTimeOffset(DateTime.Now, TimeSpan.Zero)
    • Test that does NOT flag other DateTimeOffset constructors
  • Run existing tests to ensure no regression
  • Build and test the solution
  • Request code review
    • Address code review feedback (removed trailing whitespace)
    • Address PR review feedback (throw instead of return on null types)
  • Run CodeQL security checks

Summary

Successfully extended the INTL0202 analyzer to detect and flag usage of the DateTimeOffset(DateTime) constructor. Updated implementation to throw exceptions when fundamental types cannot be found, per code review feedback. All tests pass (7 DateTimeConversion tests, 78 total tests passing).

Original prompt

This section details on the original issue you should resolve

<issue_title>INTL0202 - Also flag when the DateTime only Constructor of DateTimeOffset is used</issue_title>
<issue_description>Right now it flags on the implicit conversion of DT to DTO but you could mistakingly get around this by doing DateTimeOffset dto = new DateTimeOffset(new DateTime(2,2,2022)); and not including a timespan in your constructor when this is doing the same thing.</issue_description>

<agent_instructions>https://learn.microsoft.com/en-us/visualstudio/code-quality/roslyn-analyzers-overview?view=vs-2022

https://github.com/dotnet/roslyn/blob/main/docs/wiki/How-To-Write-a-C%23-Analyzer-and-Code-Fix.md

and any other docs you can find in microsoft learn about writing analyzers</agent_instructions>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 2 commits February 3, 2026 00:02
Co-authored-by: BenjaminMichaelis <22186029+BenjaminMichaelis@users.noreply.github.com>
…ion.cs

Co-authored-by: BenjaminMichaelis <22186029+BenjaminMichaelis@users.noreply.github.com>
Copilot AI changed the title [WIP] Add flag for DateTime only constructor of DateTimeOffset INTL0202: Flag DateTimeOffset(DateTime) constructor alongside implicit conversion Feb 3, 2026
@BenjaminMichaelis BenjaminMichaelis marked this pull request as ready for review February 3, 2026 00:32
@BenjaminMichaelis
Copy link
Member

@copilot Review the PR comments

Co-authored-by: BenjaminMichaelis <22186029+BenjaminMichaelis@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Feb 3, 2026

Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • pe3vsblobprodcus354.vsblob.vsassets.io
    • Triggering command: /usr/share/dotnet/dotnet dotnet restore --no-dependencies /home/REDACTED/work/CodingGuidelines/CodingGuidelines/Tools/XMLtoMD/GuidelineXmlToMD.sln --packages /home/REDACTED/work/CodingGuidelines/.codeql-scratch/dbs/csharp/working/packages /p:DisableImplicitNuGetFallbackFolder=true --verbosity normal /p:TargetFrameworkRootPath=/home/REDACTED/work/CodingGuidelines/.codeql-scratch/dbs/csharp/working/emptyFakeDotnetRoot /p:NetCoreTargetingPackRoot=/home/REDACTED/work/CodingGuidelines/.codeql-scratch/dbs/csharp/working/emptyFakeDotnetRoot /p:AllowMissingPrunePackageData=true (dns block)
    • Triggering command: /usr/share/dotnet/dotnet dotnet restore --no-dependencies /home/REDACTED/work/CodingGuidelines/CodingGuidelines/Tools/XMLtoMD/GuidelineXmlToMD/GuidelineXmlToMD.csproj --packages /home/REDACTED/work/CodingGuidelines/.codeql-scratch/dbs/csharp/working/packages /p:DisableImplicitNuGetFallbackFolder=true --verbosity normal /p:TargetFrameworkRootPath=/home/REDACTED/work/CodingGuidelines/.codeql-scratch/dbs/csharp/working/emptyFakeDotnetRoot /p:NetCoreTargetingPackRoot=/home/REDACTED/work/CodingGuidelines/.codeql-scratch/dbs/csharp/working/emptyFakeDotnetRoot /p:AllowMissingPrunePackageData=true (dns block)
    • Triggering command: /usr/share/dotnet/dotnet dotnet restore --no-dependencies /home/REDACTED/work/CodingGuidelines/CodingGuidelines/Tools/XMLtoMD/GuidelineXmlToMD.Test/GuidelineXmlToMD.Test.csproj --packages /home/REDACTED/work/CodingGuidelines/.codeql-scratch/dbs/csharp/working/packages /p:DisableImplicitNuGetFallbackFolder=true --verbosity normal /p:TargetFrameworkRootPath=/home/REDACTED/work/CodingGuidelines/.codeql-scratch/dbs/csharp/working/emptyFakeDotnetRoot /p:NetCoreTargetingPackRoot=/home/REDACTED/work/CodingGuidelines/.codeql-scratch/dbs/csharp/working/emptyFakeDotnetRoot /p:AllowMissingPrunePackageData=true (dns block)
  • s8mvsblobprodcus38.vsblob.vsassets.io
    • Triggering command: /usr/share/dotnet/dotnet dotnet restore --no-dependencies /home/REDACTED/work/CodingGuidelines/.codeql-scratch/dbs/csharp/working/152969D1C94CACFB2DD8DDFC3D3B91A8/missingpackages_workingdir --packages /home/REDACTED/work/CodingGuidelines/.codeql-scratch/dbs/csharp/working/missingpackages /p:DisableImplicitNuGetFallbackFolder=true --verbosity normal --configfile /home/REDACTED/work/CodingGuidelines/.codeql-scratch/dbs/csharp/working/nugetconfig/nuget.config --force (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

@BenjaminMichaelis BenjaminMichaelis merged commit b340e18 into main Feb 3, 2026
2 checks passed
@BenjaminMichaelis BenjaminMichaelis deleted the copilot/flag-datetimeoffset-constructor branch February 3, 2026 16:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

INTL0202 - Also flag when the DateTime only Constructor of DateTimeOffset is used

3 participants