Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix S6580 FP/FN: Do not raise when DateTimeStyles.RoundtripKind is specified #8004

Open
gregory-paidis-sonarsource opened this issue Sep 13, 2023 · 1 comment
Labels
Area: C# C# rules related issues. Area: VB.NET VB.NET rules related issues. Type: False Positive Rule IS triggered when it shouldn't be.

Comments

@gregory-paidis-sonarsource
Copy link
Contributor

Description

When using one of the following methods with the overload that accepts a DateTimeStyles argument and DateTimeStyles.RoundtripKind is used, a format provider does not need to be specified.

Repro steps

using System.Globalization;

var before = new DateTimeOffset(2008, 4, 12, 9, 30, 0, new TimeSpan(-8, 0, 0));
// converted to string with the "o" ISO round-trip-style. DateTimeKind is "baked" in the string.
var stringified = before.ToString("o"); 
// RoundTripKind is enough to parse this.
var after = DateTimeOffset.Parse(stringified, null, DateTimeStyles.RoundtripKind); // Noncompliant FP
Console.WriteLine("Round-tripped {0} to {1}.", before, after); // before == after

You can actually see this on Microsoft's msdn code snippet about the "o", ISO-roundtrip style format (click me)

Applies to overloads of these methods:

DateTime.Parse
DateTimeOffset.Parse
DateTime.TryParse
DateTimeOffset.TryParse
DateTime.ParseExact
DateTimeOffset.ParseExact
DateTime.TryParseExact
DateTimeOffset.TryParseExact

@gregory-paidis-sonarsource gregory-paidis-sonarsource added Type: False Positive Rule IS triggered when it shouldn't be. Area: VB.NET VB.NET rules related issues. Area: C# C# rules related issues. labels Sep 13, 2023
@BennieCopeland
Copy link

In addition to this, I don't think it should raise when I provide the expected format/s:

var before = new DateOnly(2008, 4, 12);
var stringified = before.ToString("o");
// Unfortunately, RoundTripKind doesn't work with DateOnly
var dt = DateOnly.ParseExact(stringified, "yyyy-MM-dd")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: C# C# rules related issues. Area: VB.NET VB.NET rules related issues. Type: False Positive Rule IS triggered when it shouldn't be.
Projects
None yet
Development

No branches or pull requests

2 participants