IntelliTect Guidelines

IntelliTect's guidelines for coding and architecture. Documentation for IntelliTect's .NET Compiler Platform (Roslyn) Analyzers.


Project maintained by IntelliTect Hosted on GitHub Pages — Theme by mattgraham

O2XX Block - Reliability

INTL0201

Async Void

Async methods must return either Task or Task<T>.

Allowed

public async void SomeTopLevelMethod(object sender, EventArgs e)
{
    // some code
}

public async Task SomeMethod()
{
    // some code
}

Disallowed

public async void SomeMethod()
{
    // some code
}

When to suppress

The only acceptable use of async void is in a top level methods, such as event handlers. For these cases it is safe to suppress this warning. In all other cases, this warning should NOT be suppressed.

INTL0202 - Do not implicitly convert from DateTime to DateTimeOffset.

Implicit conversion of DateTime to DateTimeOffset determines timezone offset based on the DateTime.Kind value, and for DateTimeKind.Unspecified it assumes DateTimeKind.Local, which may lead to differing behavior between running locally and on a server. Code relying on certain behaviors may function correctly when run in tests locally because all code will be running in the same timezone. This same code will fail or have different behavior when some of it is running in a hosted environment like Azure where the time zone is often set to UTC.

See the feature proposal to remove this from the dotnet corelib.

Misleading usecase is that TimeZoneInfo.ConvertTime has 2 overloads for DateTime and DateTimeOffset. When result of the first one is assigned to DateTimeOffset typed variable, DateTimeOffset record with local time zone offset is created. This is unclear for common code reader there’s something unintended behaviour may take a place ((hey, I’ve supplied date, time and timezone to this function, and expect it to return date&time object for this timezone)), because types of either DateTime or DateTimeOffset that comes to ConvertTime argument may be masked by complex computational expression.

When to suppress

Do not suppress a warning from this rule.