Coder Social home page Coder Social logo

asyncusageanalyzers's Introduction

DotNetAnalyzers

This project is for the community to make a common code base of .NET code analyzers using the new VS14 code analyzer functionality. Overview

asyncusageanalyzers's People

Contributors

abergs avatar dersteve avatar gitter-badger avatar sharwell avatar tmaczynski avatar tristal avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

asyncusageanalyzers's Issues

The non-async method for subclass return a ValueTask report AvoidAsyncSuffix

I have a method need implement from the interface, and this implement is not async. So I write the following signature:
public ValueTask<SongInfo> FindAsync()
But it report AvoidAsyncSuffix. It work fine with:
public Task<SongInfo> FindAsync()

I think the ValueTask should be support.

Addition: I see the recent update, but the package in nuget is really out of date. Could you update the nuget package?

Refactor issue for ignoring a rule

I am assuming that the alt + enter suppression is a feature by this analyzer.

When I have a method like this:

/// <summary>
/// some text
/// </summary>
public async void MyMethod()
{
}

And want to supress the warning, it puts the pragma above the documentation:

#pragma warning disable AvoidAsyncVoid // Avoid async void
/// <summary>
/// some text
/// </summary>
public async void MyMethod()
#pragma warning restore AvoidAsyncVoid // Avoid async void
{
}

I think it should be put above the method call:

/// <summary>
/// some text
/// </summary>
#pragma warning disable AvoidAsyncVoid // Avoid async void
public async void MyMethod()
#pragma warning restore AvoidAsyncVoid // Avoid async void
{
}

Define the behavior of AvoidAsyncSuffix for Event-based Asynchronous Pattern

The current implementation of the AvoidAsyncSuffix analyzer only targets the Task-based Asynchronous Pattern (TAP). The Event-based Asynchronous Pattern (EAP) also uses the Async suffix for certain methods, but the signatures are different from the TAP.

The behavior of the AvoidAsyncSuffix analyzer should be fully defined for this case, and tests included to detect regressions.

Task<T> class Properties shouldn't require `Async` suffix

UseAsyncSuffixAnalyzer enforces the Async suffix on property names. Is that something this rule should enforce -- it seems like that should be the exception to the rule?

Errors:

public class MyClass
{
    Task<string> MyString { get; set; }
}

New rule proposal: Async methods should have a CancellationToken parameter

This doesn't apply to all asynchronous methods, but I think it might be a useful rule to have in the toolbox (rulebox?) provided it is disabled by default.

I write a lot of methods like this:

public Task<Response> GetResponse(
    string href,
    CancellationToken cancellationToken = default(CancellationToken))
{ 
    //... 
}

...which is a pattern modeled after the TAP guidelines and Mongo's excellent C# driver.

It's easy to forget the CancellationToken parameter, though, so in my own project I have a short unit test that uses reflection to make sure I haven't forgotten something. That made me think that an analyzer rule might be a better tool for the job! ๐Ÿ˜„

Non-fatal error while installing package

While installing the package with the Package Manager Console, the following is reported:

Executing script file '...\StyleCopAnalyzers\packages\AsyncUsageAnalyzers.1.0.0-alpha001\tools\install.ps1'
Get-ChildItem : Cannot find path '...\StyleCopAnalyzers\packages\AsyncUsageAnalyzers.1.0.0-alpha001\tools\analyzers\C#' because it does not exist.
At ...\StyleCopAnalyzers\packages\AsyncUsageAnalyzers.1.0.0-alpha001\tools\install.ps1:18 char:31
+ foreach ($analyzerFilePath in Get-ChildItem $languageAnalyzersPath -Filter *.dll ...
+                               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (...ls\analyzers\C#:String) [Get-ChildItem], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand

New rule proposal: Propagate cancellation tokens

This diagnostic would be reported when CancellationToken.None is explicitly provided in a call, but another cancellation token is available in the current context (see #36 and #32 for comments describing the current context).

The code fix for this would replace the use of CancellationToken.None with the "best" cancellation token as described in the code fix for #36.

Update to Roslyn 1.0

The AsyncUsageAnalyzers has to be updated to Roslyn 1.0 to be supported in visual studio 2015

UseConfigureAwait rule inappropriate in many contexts

The default behavior of await is appropriate for app development. In some libraries, it may be appropriate to add .ConfigureAwait(false) at the end of an awaitable expression, but there are plenty of contexts where the default behavior is appropriate.

Yes, I know rules can be disabled. But perhaps the default should be to disable the rule and let users enable it when they know they're working on a shared library project for which no requirement to abide by the SynchronizationContext rules.

Analyzers require mscorlib 2.0.5.0 because of JSON.NET

I'm getting this warning:

MissingAnalyzerReference Analyzer assembly 'packages\StyleCop.Analyzers.1.0.0-beta009\analyzers\dotnet\cs\Newtonsoft.Json.dll' depends on 'mscorlib, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e' but it was not found. Analyzers may not run correctly

Update IncludeCancellationParameter analyzer to check overloads

Currently the analyzer does not check for an overload that takes a cancellation token. For example, the following interface would result in a diagnostic being reported, but it shouldn't.

interface InterfaceName
{
  Task MethodAsync();
  Task MethodAsync(CancellationToken cancellationToken);
}

New rule proposal: Provide CancellationToken in calls

This diagnostic would be reported if a method M is called without providing a CancellationToken, and either M or one of its overloads has a CancellationToken parameter.

The code fix for this would attempt to fill in the CancellationToken as follows:

  1. If a local variable or parameter provides a CancellationToken (with consideration for context objects as described in #32), the innermost visible CancellationToken is passed.
  2. Otherwise, pass CancellationToken.None.

โš ๏ธ Separate equivalence keys should be used for the code fix for the case where the updated call still calls the same method, and the case where the updated call is to a different overload of the method.

Support Supression attribute

At the moment there is only support for "pragma" for supression of a rule.

It would be good to be consistent and support SupressionAttribute as well.

Method name analyzers should not run run for override/implementing methods

Currently the UseAsyncSuffix diagnostic is reported for cases where a method overrides a method from a base class and/or implements an interface method. In these cases, the name is set by the base definition, so no naming warnings should be reported at the site of an overriding or implementing method.

New rule proposal: Methods returning a Task should end with Async

Category: Naming
Rule: Methods returning a Task should end with Async
Description: The manner in which asynchronous methods are consumed in code differs substantially from non-asynchronous methods. The Task-based Asynchronous Pattern (TAP) documentation, along with the .NET Framework itself, uses a specific naming convention to identify methods following one particular pattern for asynchronous method calls. This rule ensures other methods following this pattern do the same.

I was originally planning to file this as a new rule proposal for StyleCopAnalyzers. However, I decided to post it here since it likely applies to a wider range of code than StyleCopAnalyzers supports (e.g. VB code).

New rule proposal: Use ConfigureAwait(false)

Category: Usage
Rule: Use ConfigureAwait(false)
Severity: Hidden
Description: This analyzer identifies code which uses await on a Task, and does not use ConfigureAwait(false). By default it uses Hidden severity, which enables Ctrl+. on the line containing the call, but libraries that don't interact with the UI can reset the severity to Warning or Error in order to enforce this rule across an entire project.

Add special rule for async void eventhandlers.

private async void OnChanged(object sender, EventArgs e)
{
    ...
}

What about adding a rule AvoidAsyncVoidInEventhandlers?
The reason for this would be to be able to tune severity separately for them.
Heuristics could be: method with two arguments of type object and assignable to EventArgs or maybe it can be figured out from climbing around the syntax tree that the method is only used for subscribing to an event.

Perhaps there could be another analyzer checking that there is try-catch surrounding all awaits.

Await inside usings.

using (var webClient = new WebClient())
{
    return webClient.DownloadStringTaskAsync(address);
}

We talked about this in chat, this can lead to situations where it is undefined if the IDisposable is disposed or not.

Use Task.FromResult to return already computed results

I'm not sure how well to enforce this, because the anti-patterns are very diverse, and subtle.

During coded reviews I've seen code (often in mocks for tests) that use some form of task construction and execution (like Task.Run()) to produce a result when Task.FromResult() would have been much cleaner.

Proposal: Properties should not be of type Task or Task<T>

Inspired by #21:

Properties should not be of type Task or Task<T>.
Most people perceive properties as near instantaneous (synchronous) operations.
Task and Task<T> are associated with asynchronous operations and should therefore not be used as property types, as this does not match with the expectations of the majority of developers.

avoid async void methods

Async methods should not return void (unless mandated by an interface or event handler contract)

We should even suggest a clear "Fire and Forget" idiom to catch and log exceptions in the case of exceptions when async void must be used.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.