Commit b340e18
INTL0202: Flag DateTimeOffset(DateTime) constructor alongside implicit conversion (#368)
## Plan for INTL0202 - Also flag DateTimeOffset(DateTime) constructor
- [x] Understand current INTL0202 analyzer implementation
- [x] Review BanImplicitDateTimeToDateTimeOffsetConversion.cs
- [x] Review existing tests in DateTimeConversionTests.cs
- [x] Confirm Microsoft documentation on DateTimeOffset constructors
- [x] Extend analyzer to detect DateTimeOffset(DateTime) constructor
usage
- [x] Register operation action for ObjectCreation operations
- [x] Check if the created type is DateTimeOffset
- [x] Check if constructor has exactly 1 parameter of type DateTime
- [x] Report diagnostic when match is found
- [x] Add tests for the new functionality
- [x] Test that flags `new DateTimeOffset(DateTime.Now)`
- [x] Test that flags `new DateTimeOffset(new DateTime(...))`
- [x] Test that does NOT flag `new DateTimeOffset(DateTime.Now,
TimeSpan.Zero)`
- [x] Test that does NOT flag other DateTimeOffset constructors
- [x] Run existing tests to ensure no regression
- [x] Build and test the solution
- [x] Request code review
- [x] Address code review feedback (removed trailing whitespace)
- [x] Address PR review feedback (throw instead of return on null types)
- [x] 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).
<!-- START COPILOT ORIGINAL PROMPT -->
<details>
<summary>Original prompt</summary>
>
> ----
>
> *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)
>
> <comments>
> </comments>
>
</details>
<!-- START COPILOT CODING AGENT SUFFIX -->
- Fixes #302
<!-- START COPILOT CODING AGENT TIPS -->
---
✨ Let Copilot coding agent [set things up for
you](https://github.com/IntelliTect/CodingGuidelines/issues/new?title=✨+Set+up+Copilot+instructions&body=Configure%20instructions%20for%20this%20repository%20as%20documented%20in%20%5BBest%20practices%20for%20Copilot%20coding%20agent%20in%20your%20repository%5D%28https://gh.io/copilot-coding-agent-tips%29%2E%0A%0A%3COnboard%20this%20repo%3E&assignees=copilot)
— coding agent works faster and does higher quality work when set up for
your repo.
---------
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: BenjaminMichaelis <22186029+BenjaminMichaelis@users.noreply.github.com>
Co-authored-by: Benjamin Michaelis <git@relay.benjamin.michaelis.net>1 parent 175e98a commit b340e18
2 files changed
Lines changed: 137 additions & 3 deletions
File tree
- IntelliTect.Analyzer
- IntelliTect.Analyzer.Test
- IntelliTect.Analyzer/Analyzers
Lines changed: 106 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
33 | | - | |
| 33 | + | |
34 | 34 | | |
35 | 35 | | |
36 | 36 | | |
| |||
71 | 71 | | |
72 | 72 | | |
73 | 73 | | |
74 | | - | |
| 74 | + | |
75 | 75 | | |
76 | 76 | | |
77 | 77 | | |
| |||
101 | 101 | | |
102 | 102 | | |
103 | 103 | | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
104 | 208 | | |
105 | 209 | | |
106 | 210 | | |
| |||
Lines changed: 31 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
29 | 29 | | |
30 | 30 | | |
31 | 31 | | |
| 32 | + | |
32 | 33 | | |
33 | 34 | | |
34 | 35 | | |
| |||
51 | 52 | | |
52 | 53 | | |
53 | 54 | | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
54 | 84 | | |
55 | 85 | | |
56 | 86 | | |
57 | 87 | | |
58 | | - | |
| 88 | + | |
59 | 89 | | |
60 | 90 | | |
61 | 91 | | |
| |||
0 commit comments