-
-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New rule: MA0133 - Use DateTimeOffset instead of DateTime to avoid an…
… implicit conversion (#506)
- Loading branch information
Showing
7 changed files
with
72 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# MA0133 - Use DateTimeOffset instead of relying on the implicit conversion | ||
|
||
Replace `DateTime.UtcNow` or `DateTime.Now` with `DateTimeOffset.UtcNow` or `DateTimeOffset.Now` to avoid an implicit conversion. | ||
|
||
````c# | ||
Sample(DateTime.UtcNow); // non-compliant | ||
Sample(DateTimeOffset.UtcNow); // ok | ||
void Sample(DateTimeOffset datetime) { } | ||
```` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
...ziantou.Analyzer.Test/Rules/DoNotCompareDateTimeWithDateTimeOffsetAnalyzerTests_MA0133.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using System.Threading.Tasks; | ||
using Meziantou.Analyzer.Rules; | ||
using Microsoft.CodeAnalysis; | ||
using TestHelper; | ||
using Xunit; | ||
|
||
namespace Meziantou.Analyzer.Test.Rules; | ||
|
||
public class DoNotCompareDateTimeWithDateTimeOffsetAnalyzerTests_MA0133 | ||
{ | ||
private static ProjectBuilder CreateProjectBuilder() | ||
{ | ||
return new ProjectBuilder() | ||
.WithOutputKind(OutputKind.ConsoleApplication) | ||
.WithAnalyzer<DoNotImplicitlyConvertDateTimeToDateTimeOffsetAnalyzer>(id: "MA0133"); | ||
} | ||
|
||
[Fact] | ||
public async Task ImplicitConversion_BinaryOperation_Subtract_UtcNow() | ||
{ | ||
await CreateProjectBuilder() | ||
.WithSourceCode(""" | ||
using System; | ||
_ = [|DateTime.UtcNow|] - DateTimeOffset.UtcNow; | ||
""") | ||
.ValidateAsync(); | ||
} | ||
} |