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

CA1806 should analyze calls to date and time methods #6489

Open
reduckted opened this issue Feb 16, 2023 · 1 comment
Open

CA1806 should analyze calls to date and time methods #6489

reduckted opened this issue Feb 16, 2023 · 1 comment

Comments

@reduckted
Copy link
Contributor

Analyzer

Diagnostic ID: CA1806: Do not ignore method results

Describe the improvement

DateTime is similar to String insofar as it's immutable and has methods for changing the value which return a new instance.

CA1806 currently checks that the return value from certain String methods are not ignored. This analyzer could also check that the return value from certain DateTime methods are not ignored.

DateTimeOffset, DateOnly, TimeOnly and TimeSpan could also be supported.

Describe suggestions on how to achieve the rule

On DateTime and DateTimeOffset, the return value of these methods should not be ignored:

  • Add(TimeSpan)
  • AddDays(Double)
  • AddHours(Double)
  • AddMicroseconds(Double)
  • AddMilliseconds(Double)
  • AddMinutes(Double)
  • AddMonths(Int32)
  • AddSeconds(Double)
  • AddTicks(Int64)
  • AddYears(Int32)
  • Subtract(DateTime)
  • Subtract(TimeSpan)
  • ToLocalTime()
  • ToUniversalTime()

On DateOnly, the return value of these methods should not be ignored:

  • AddDays(Int32)
  • AddMonths(Int32)
  • AddYears(Int32)

On TimeOnly, the return value of these methods should not be ignored:

  • Add(TimeSpan)
  • Add(TimeSpan, Int32)
  • AddHours(Double)
  • AddHours(Double, Int32)
  • AddMinutes(Double)
  • AddMinutes(Double, Int32)

On TimeSpan, the return value of these methods should not be ignored:

  • Add(TimeSpan)
  • Divide(Double)
  • Divide(TimeSpan)
  • Multiply(Double)
  • Multiply(TimeSpan)
  • Negate(Double)
  • Subtract(TimeSpan)
@reduckted
Copy link
Contributor Author

reduckted commented Feb 18, 2023

Possibly related to or superseded by dotnet/runtime#34098

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

No branches or pull requests

1 participant