├── .gitattributes
├── .gitignore
├── LICENSE
├── README.md
└── src
├── .nuget
└── packages.config
├── AsyncAwaitAnalyzer-Verification.sln
├── AsyncAwaitAnalyzer.Verification.ClassLib
├── AsyncAwaitAnalyzer.Verification.ClassLib.csproj
├── Foo.cs
└── Properties
│ └── AssemblyInfo.cs
├── AsyncAwaitAnalyzer.Verification.WpfApp
├── App.config
├── App.xaml
├── App.xaml.cs
├── AsyncAwaitAnalyzer.Verification.WpfApp.csproj
├── MainWindow.xaml
├── MainWindow.xaml.cs
└── Properties
│ ├── AssemblyInfo.cs
│ ├── Resources.Designer.cs
│ ├── Resources.resx
│ ├── Settings.Designer.cs
│ └── Settings.settings
├── AsyncAwaitAnalyzer.Verification
├── App.config
├── AsyncAwaitAnalyzer.Verification.csproj
├── Program.cs
└── Properties
│ └── AssemblyInfo.cs
├── AsyncAwaitAnalyzer.sln
└── AsyncAwaitAnalyzer
├── AsyncAwaitAnalyzer.Vsix
├── AsyncAwaitAnalyzer.Vsix.csproj
└── source.extension.vsixmanifest
└── AsyncAwaitAnalyzer
├── Analyzers
├── AnalyzerHelper.cs
├── AsyncMethodsNamingAnalyzer.cs
├── AsyncVoidMethodsAnalyzer.cs
└── ConfigureAwaitAnalyzer.cs
├── AsyncAwaitAnalyzer.csproj
├── Diagnostic.nuspec
├── FixProviders
└── AsyncMethodsNamingCodeFixProvider.cs
├── Properties
└── AssemblyInfo.cs
├── ReadMe.txt
├── packages.config
└── tools
├── install.ps1
└── uninstall.ps1
/.gitattributes:
--------------------------------------------------------------------------------
1 | ###############################################################################
2 | # Set default behavior to automatically normalize line endings.
3 | ###############################################################################
4 | * text=auto
5 |
6 | ###############################################################################
7 | # Set default behavior for command prompt diff.
8 | #
9 | # This is need for earlier builds of msysgit that does not have it on by
10 | # default for csharp files.
11 | # Note: This is only used by command line
12 | ###############################################################################
13 | #*.cs diff=csharp
14 |
15 | ###############################################################################
16 | # Set the merge driver for project and solution files
17 | #
18 | # Merging from the command prompt will add diff markers to the files if there
19 | # are conflicts (Merging from VS is not affected by the settings below, in VS
20 | # the diff markers are never inserted). Diff markers may cause the following
21 | # file extensions to fail to load in VS. An alternative would be to treat
22 | # these files as binary and thus will always conflict and require user
23 | # intervention with every merge. To do so, just uncomment the entries below
24 | ###############################################################################
25 | #*.sln merge=binary
26 | #*.csproj merge=binary
27 | #*.vbproj merge=binary
28 | #*.vcxproj merge=binary
29 | #*.vcproj merge=binary
30 | #*.dbproj merge=binary
31 | #*.fsproj merge=binary
32 | #*.lsproj merge=binary
33 | #*.wixproj merge=binary
34 | #*.modelproj merge=binary
35 | #*.sqlproj merge=binary
36 | #*.wwaproj merge=binary
37 |
38 | ###############################################################################
39 | # behavior for image files
40 | #
41 | # image files are treated as binary by default.
42 | ###############################################################################
43 | #*.jpg binary
44 | #*.png binary
45 | #*.gif binary
46 |
47 | ###############################################################################
48 | # diff behavior for common document formats
49 | #
50 | # Convert binary document formats to text before diffing them. This feature
51 | # is only available from the command line. Turn it on by uncommenting the
52 | # entries below.
53 | ###############################################################################
54 | #*.doc diff=astextplain
55 | #*.DOC diff=astextplain
56 | #*.docx diff=astextplain
57 | #*.DOCX diff=astextplain
58 | #*.dot diff=astextplain
59 | #*.DOT diff=astextplain
60 | #*.pdf diff=astextplain
61 | #*.PDF diff=astextplain
62 | #*.rtf diff=astextplain
63 | #*.RTF diff=astextplain
64 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | ## Ignore Visual Studio temporary files, build results, and
2 | ## files generated by popular Visual Studio add-ons.
3 |
4 |
5 | # Ignore every NuGet Package Directory (package restore is enabled)
6 | **/packages/*
7 | **/.vs/*
8 |
9 | # User-specific files
10 | *.suo
11 | *.user
12 | *.sln.docstates
13 |
14 | # Build results
15 | [Dd]ebug/
16 | [Dd]ebugPublic/
17 | [Rr]elease/
18 | x64/
19 | build/
20 | bld/
21 | [Bb]in/
22 | [Oo]bj/
23 |
24 | # MSTest test Results
25 | [Tt]est[Rr]esult*/
26 | [Bb]uild[Ll]og.*
27 |
28 | *_i.c
29 | *_p.c
30 | *_i.h
31 | *.ilk
32 | *.meta
33 | *.obj
34 | *.pch
35 | *.pdb
36 | *.pgc
37 | *.pgd
38 | *.rsp
39 | *.sbr
40 | *.tlb
41 | *.tli
42 | *.tlh
43 | *.tmp
44 | *.tmp_proj
45 | *.log
46 | *.vspscc
47 | *.vssscc
48 | .builds
49 | *.pidb
50 | *.svclog
51 | *.scc
52 |
53 | # =========================
54 | # Windows detritus
55 | # =========================
56 |
57 | # Windows image file caches
58 | Thumbs.db
59 | ehthumbs.db
60 |
61 | # Folder config file
62 | Desktop.ini
63 |
64 |
65 | # Recycle Bin used on file shares
66 | $RECYCLE.BIN/
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2015 Oliver Lohmann
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
23 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # AsyncAwaitAnalyzer
2 | A set of Roslyn Diagnostic Analyzers and Code Fixes for Async/Await Programming in C#.
3 |
4 | ## Diagnostics
5 |
6 | * **ASYNC-0001** - Avoid async void signatures. This diagnostic verifies that async void method declarations are only used on event handlers.
7 | * **ASYNC-0002** - Use async method naming style. This diagnostic enforces a 'Async' suffix for methods doing async work.
8 | * **ASYNC-0003** - Use ConfigureAwait in library code. This diagnostic verifies that CongfigureAwait(false) is used in library code.
9 |
10 | ## Code Fixes
11 | * Fix for **ASYNC-0002**.
12 |
--------------------------------------------------------------------------------
/src/.nuget/packages.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/src/AsyncAwaitAnalyzer-Verification.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 14
4 | VisualStudioVersion = 14.0.22512.0
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{0E906932-1F80-441F-B6BB-AD94E8A3E133}"
7 | ProjectSection(SolutionItems) = preProject
8 | .nuget\packages.config = .nuget\packages.config
9 | EndProjectSection
10 | EndProject
11 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AsyncAwaitAnalyzer.Verification", "AsyncAwaitAnalyzer.Verification\AsyncAwaitAnalyzer.Verification.csproj", "{44C74072-5EE8-496C-ACCE-BA9590E6B9C4}"
12 | EndProject
13 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AsyncAwaitAnalyzer.Verification.ClassLib", "AsyncAwaitAnalyzer.Verification.ClassLib\AsyncAwaitAnalyzer.Verification.ClassLib.csproj", "{00D469D4-7B09-406A-9409-CD6748BCE818}"
14 | EndProject
15 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AsyncAwaitAnalyzer.Verification.WpfApp", "AsyncAwaitAnalyzer.Verification.WpfApp\AsyncAwaitAnalyzer.Verification.WpfApp.csproj", "{4CFEB406-32C0-4275-BCB3-2D689922C74B}"
16 | EndProject
17 | Global
18 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
19 | Debug|Any CPU = Debug|Any CPU
20 | Release|Any CPU = Release|Any CPU
21 | EndGlobalSection
22 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
23 | {44C74072-5EE8-496C-ACCE-BA9590E6B9C4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
24 | {44C74072-5EE8-496C-ACCE-BA9590E6B9C4}.Debug|Any CPU.Build.0 = Debug|Any CPU
25 | {44C74072-5EE8-496C-ACCE-BA9590E6B9C4}.Release|Any CPU.ActiveCfg = Release|Any CPU
26 | {44C74072-5EE8-496C-ACCE-BA9590E6B9C4}.Release|Any CPU.Build.0 = Release|Any CPU
27 | {00D469D4-7B09-406A-9409-CD6748BCE818}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
28 | {00D469D4-7B09-406A-9409-CD6748BCE818}.Debug|Any CPU.Build.0 = Debug|Any CPU
29 | {00D469D4-7B09-406A-9409-CD6748BCE818}.Release|Any CPU.ActiveCfg = Release|Any CPU
30 | {00D469D4-7B09-406A-9409-CD6748BCE818}.Release|Any CPU.Build.0 = Release|Any CPU
31 | {4CFEB406-32C0-4275-BCB3-2D689922C74B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
32 | {4CFEB406-32C0-4275-BCB3-2D689922C74B}.Debug|Any CPU.Build.0 = Debug|Any CPU
33 | {4CFEB406-32C0-4275-BCB3-2D689922C74B}.Release|Any CPU.ActiveCfg = Release|Any CPU
34 | {4CFEB406-32C0-4275-BCB3-2D689922C74B}.Release|Any CPU.Build.0 = Release|Any CPU
35 | EndGlobalSection
36 | GlobalSection(SolutionProperties) = preSolution
37 | HideSolutionNode = FALSE
38 | EndGlobalSection
39 | EndGlobal
40 |
--------------------------------------------------------------------------------
/src/AsyncAwaitAnalyzer.Verification.ClassLib/AsyncAwaitAnalyzer.Verification.ClassLib.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | AnyCPU
7 | {00D469D4-7B09-406A-9409-CD6748BCE818}
8 | Library
9 | Properties
10 | AsyncAwaitAnalyzer.Verification.ClassLib
11 | AsyncAwaitAnalyzer.Verification.ClassLib
12 | v4.5
13 | 512
14 |
15 |
16 | true
17 | full
18 | false
19 | bin\Debug\
20 | DEBUG;TRACE
21 | prompt
22 | 4
23 |
24 |
25 | pdbonly
26 | true
27 | bin\Release\
28 | TRACE
29 | prompt
30 | 4
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
53 |
--------------------------------------------------------------------------------
/src/AsyncAwaitAnalyzer.Verification.ClassLib/Foo.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading.Tasks;
3 |
4 | namespace AsyncAwaitAnalyzer.Verification.ClassLib
5 | {
6 | public class Foo
7 | {
8 | public async Task BarAsync()
9 | {
10 | // Bad
11 | await Task.Delay(TimeSpan.FromSeconds(1));
12 | await Task.Delay(TimeSpan.FromSeconds(1)).ConfigureAwait(true);
13 |
14 | // Good
15 | await Task.Delay(TimeSpan.FromSeconds(1)).ConfigureAwait(false);
16 | }
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/src/AsyncAwaitAnalyzer.Verification.ClassLib/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("AsyncAwaitAnalyzer.ClassLib")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("AsyncAwaitAnalyzer.ClassLib")]
13 | [assembly: AssemblyCopyright("Copyright © 2015")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("00d469d4-7b09-406a-9409-cd6748bce818")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/src/AsyncAwaitAnalyzer.Verification.WpfApp/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/src/AsyncAwaitAnalyzer.Verification.WpfApp/App.xaml:
--------------------------------------------------------------------------------
1 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/src/AsyncAwaitAnalyzer.Verification.WpfApp/App.xaml.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Configuration;
4 | using System.Data;
5 | using System.Linq;
6 | using System.Threading.Tasks;
7 | using System.Windows;
8 |
9 | namespace AsyncAwaitAnalyzer.WpfApp
10 | {
11 | ///
12 | /// Interaction logic for App.xaml
13 | ///
14 | public partial class App : Application
15 | {
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/src/AsyncAwaitAnalyzer.Verification.WpfApp/AsyncAwaitAnalyzer.Verification.WpfApp.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | AnyCPU
7 | {4CFEB406-32C0-4275-BCB3-2D689922C74B}
8 | WinExe
9 | Properties
10 | AsyncAwaitAnalyzer.Verification.WpfApp
11 | AsyncAwaitAnalyzer.Verification.WpfApp
12 | v4.5
13 | 512
14 | {60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}
15 | 4
16 |
17 |
18 | AnyCPU
19 | true
20 | full
21 | false
22 | bin\Debug\
23 | DEBUG;TRACE
24 | prompt
25 | 4
26 |
27 |
28 | AnyCPU
29 | pdbonly
30 | true
31 | bin\Release\
32 | TRACE
33 | prompt
34 | 4
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 | 4.0
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 | MSBuild:Compile
54 |
55 |
56 | MSBuild:Compile
57 | Designer
58 |
59 |
60 | App.xaml
61 | Code
62 |
63 |
64 | MainWindow.xaml
65 | Code
66 |
67 |
68 |
69 |
70 | Code
71 |
72 |
73 | True
74 | True
75 | Resources.resx
76 |
77 |
78 | True
79 | Settings.settings
80 | True
81 |
82 |
83 | ResXFileCodeGenerator
84 | Resources.Designer.cs
85 |
86 |
87 | SettingsSingleFileGenerator
88 | Settings.Designer.cs
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 | {00d469d4-7b09-406a-9409-cd6748bce818}
98 | AsyncAwaitAnalyzer.Verification.ClassLib
99 |
100 |
101 | {c82ec506-f082-4851-8425-dd6accbac328}
102 | AsyncAwaitAnalyzer
103 |
104 |
105 |
106 |
113 |
--------------------------------------------------------------------------------
/src/AsyncAwaitAnalyzer.Verification.WpfApp/MainWindow.xaml:
--------------------------------------------------------------------------------
1 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
--------------------------------------------------------------------------------
/src/AsyncAwaitAnalyzer.Verification.WpfApp/MainWindow.xaml.cs:
--------------------------------------------------------------------------------
1 | using AsyncAwaitAnalyzer.Verification.ClassLib;
2 | using System.Windows;
3 |
4 | namespace AsyncAwaitAnalyzer.WpfApp
5 | {
6 | ///
7 | /// Interaction logic for MainWindow.xaml
8 | ///
9 | public partial class MainWindow : Window
10 | {
11 | public MainWindow()
12 | {
13 | InitializeComponent();
14 | }
15 |
16 | private async void Button_Click(object sender, RoutedEventArgs e)
17 | {
18 | var foo = new Foo();
19 |
20 | // OK, no .ConfigureAwait required.
21 | await foo.BarAsync();
22 | }
23 |
24 | private async void Button_Click_1(object sender, RoutedEventArgs e)
25 | {
26 | var foo = new Foo();
27 |
28 | // OK, depends on the usage.
29 | await foo.BarAsync().ConfigureAwait(false);
30 | }
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/src/AsyncAwaitAnalyzer.Verification.WpfApp/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Resources;
3 | using System.Runtime.CompilerServices;
4 | using System.Runtime.InteropServices;
5 | using System.Windows;
6 |
7 | // General Information about an assembly is controlled through the following
8 | // set of attributes. Change these attribute values to modify the information
9 | // associated with an assembly.
10 | [assembly: AssemblyTitle("AsyncAwaitAnalyzer.WpfApp")]
11 | [assembly: AssemblyDescription("")]
12 | [assembly: AssemblyConfiguration("")]
13 | [assembly: AssemblyCompany("")]
14 | [assembly: AssemblyProduct("AsyncAwaitAnalyzer.WpfApp")]
15 | [assembly: AssemblyCopyright("Copyright © 2015")]
16 | [assembly: AssemblyTrademark("")]
17 | [assembly: AssemblyCulture("")]
18 |
19 | // Setting ComVisible to false makes the types in this assembly not visible
20 | // to COM components. If you need to access a type in this assembly from
21 | // COM, set the ComVisible attribute to true on that type.
22 | [assembly: ComVisible(false)]
23 |
24 | //In order to begin building localizable applications, set
25 | //CultureYouAreCodingWith in your .csproj file
26 | //inside a . For example, if you are using US english
27 | //in your source files, set the to en-US. Then uncomment
28 | //the NeutralResourceLanguage attribute below. Update the "en-US" in
29 | //the line below to match the UICulture setting in the project file.
30 |
31 | //[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)]
32 |
33 |
34 | [assembly: ThemeInfo(
35 | ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
36 | //(used if a resource is not found in the page,
37 | // or application resource dictionaries)
38 | ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
39 | //(used if a resource is not found in the page,
40 | // app, or any theme specific resource dictionaries)
41 | )]
42 |
43 |
44 | // Version information for an assembly consists of the following four values:
45 | //
46 | // Major Version
47 | // Minor Version
48 | // Build Number
49 | // Revision
50 | //
51 | // You can specify all the values or you can default the Build and Revision Numbers
52 | // by using the '*' as shown below:
53 | // [assembly: AssemblyVersion("1.0.*")]
54 | [assembly: AssemblyVersion("1.0.0.0")]
55 | [assembly: AssemblyFileVersion("1.0.0.0")]
56 |
--------------------------------------------------------------------------------
/src/AsyncAwaitAnalyzer.Verification.WpfApp/Properties/Resources.Designer.cs:
--------------------------------------------------------------------------------
1 | //------------------------------------------------------------------------------
2 | //
3 | // This code was generated by a tool.
4 | // Runtime Version:4.0.30319.0
5 | //
6 | // Changes to this file may cause incorrect behavior and will be lost if
7 | // the code is regenerated.
8 | //
9 | //------------------------------------------------------------------------------
10 |
11 | namespace AsyncAwaitAnalyzer.Verification.WpfApp.Properties {
12 | using System;
13 |
14 |
15 | ///
16 | /// A strongly-typed resource class, for looking up localized strings, etc.
17 | ///
18 | // This class was auto-generated by the StronglyTypedResourceBuilder
19 | // class via a tool like ResGen or Visual Studio.
20 | // To add or remove a member, edit your .ResX file then rerun ResGen
21 | // with the /str option, or rebuild your VS project.
22 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")]
23 | [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
24 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
25 | internal class Resources {
26 |
27 | private static global::System.Resources.ResourceManager resourceMan;
28 |
29 | private static global::System.Globalization.CultureInfo resourceCulture;
30 |
31 | [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
32 | internal Resources() {
33 | }
34 |
35 | ///
36 | /// Returns the cached ResourceManager instance used by this class.
37 | ///
38 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
39 | internal static global::System.Resources.ResourceManager ResourceManager {
40 | get {
41 | if (object.ReferenceEquals(resourceMan, null)) {
42 | global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("AsyncAwaitAnalyzer.Verification.WpfApp.Properties.Resources", typeof(Resources).Assembly);
43 | resourceMan = temp;
44 | }
45 | return resourceMan;
46 | }
47 | }
48 |
49 | ///
50 | /// Overrides the current thread's CurrentUICulture property for all
51 | /// resource lookups using this strongly typed resource class.
52 | ///
53 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
54 | internal static global::System.Globalization.CultureInfo Culture {
55 | get {
56 | return resourceCulture;
57 | }
58 | set {
59 | resourceCulture = value;
60 | }
61 | }
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/src/AsyncAwaitAnalyzer.Verification.WpfApp/Properties/Resources.resx:
--------------------------------------------------------------------------------
1 |
2 |
3 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 | text/microsoft-resx
107 |
108 |
109 | 2.0
110 |
111 |
112 | System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
113 |
114 |
115 | System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
116 |
117 |
--------------------------------------------------------------------------------
/src/AsyncAwaitAnalyzer.Verification.WpfApp/Properties/Settings.Designer.cs:
--------------------------------------------------------------------------------
1 | //------------------------------------------------------------------------------
2 | //
3 | // This code was generated by a tool.
4 | // Runtime Version:4.0.30319.0
5 | //
6 | // Changes to this file may cause incorrect behavior and will be lost if
7 | // the code is regenerated.
8 | //
9 | //------------------------------------------------------------------------------
10 |
11 | namespace AsyncAwaitAnalyzer.Verification.WpfApp.Properties {
12 |
13 |
14 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
15 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "14.0.0.0")]
16 | internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {
17 |
18 | private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
19 |
20 | public static Settings Default {
21 | get {
22 | return defaultInstance;
23 | }
24 | }
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/src/AsyncAwaitAnalyzer.Verification.WpfApp/Properties/Settings.settings:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/src/AsyncAwaitAnalyzer.Verification/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/src/AsyncAwaitAnalyzer.Verification/AsyncAwaitAnalyzer.Verification.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | AnyCPU
7 | {44C74072-5EE8-496C-ACCE-BA9590E6B9C4}
8 | Exe
9 | Properties
10 | AsyncAwaitAnalyzer.Verification
11 | AsyncAwaitAnalyzer.Verification
12 | v4.5
13 | 512
14 |
15 |
16 | AnyCPU
17 | true
18 | full
19 | false
20 | bin\Debug\
21 | DEBUG;TRACE
22 | prompt
23 | 4
24 |
25 |
26 | AnyCPU
27 | pdbonly
28 | true
29 | bin\Release\
30 | TRACE
31 | prompt
32 | 4
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
64 |
--------------------------------------------------------------------------------
/src/AsyncAwaitAnalyzer.Verification/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading.Tasks;
3 |
4 | namespace AsyncAwaitAnalyzer.Verification
5 | {
6 | class Program
7 | {
8 | static void Main(string[] args)
9 | {
10 | }
11 | }
12 |
13 | public class BadClass
14 | {
15 | public async void FireAndForget()
16 | {
17 | await Task.Delay(TimeSpan.FromSeconds(10));
18 | }
19 |
20 | public async Task FireAndNoForgetWithoutSuffix()
21 | {
22 | await Task.Delay(TimeSpan.FromSeconds(10));
23 | }
24 |
25 | public Task FireAndNoForgetWithoutSuffixAndKeyword()
26 | {
27 | return Task.Delay(TimeSpan.FromSeconds(10));
28 | }
29 |
30 | public void FireAndForgetLambda()
31 | {
32 | // Bad: Not returning a Task.
33 | Action fireAndForgetAction = async () => await Task.Delay(TimeSpan.FromMinutes(1));
34 | Action onMoreFireAndForgetAction = async v => await Task.Delay(TimeSpan.FromMinutes(v));
35 |
36 | Action anotherFireAndForgetAction = async () =>
37 | {
38 | await Task.Delay(TimeSpan.FromDays(1));
39 | await Task.Delay(TimeSpan.FromDays(1));
40 | };
41 | }
42 | }
43 |
44 | public class GoodClass
45 | {
46 | public GoodClass()
47 | {
48 | var eventProvider = new EventProvider();
49 | eventProvider.UpdateTriggered += EventSubscription;
50 |
51 | var wpfBtn = new System.Windows.Controls.Button();
52 | var formsBtn = new System.Windows.Forms.Button();
53 |
54 | wpfBtn.Click += WpfBtn_Click;
55 | formsBtn.Click += FormsBtn_Click;
56 |
57 | // Good: Return Func of Task
58 | Func fireAndNotForgetAction = async () => await Task.Delay(TimeSpan.FromMinutes(1));
59 | Func fireAndNotForgetAction2 = async v => await Task.Delay(TimeSpan.FromMinutes(v));
60 |
61 | wpfBtn.Click += async (s, e) => await Task.Delay(TimeSpan.FromHours(1));
62 | }
63 |
64 | private async void FormsBtn_Click(object sender, EventArgs e)
65 | {
66 | await Task.Delay(TimeSpan.FromSeconds(10));
67 | }
68 |
69 | private async void WpfBtn_Click(object sender, System.Windows.RoutedEventArgs e)
70 | {
71 | await Task.Delay(TimeSpan.FromSeconds(10));
72 | }
73 |
74 | public async Task FireAndNotForgetAsync()
75 | {
76 | await Task.Delay(TimeSpan.FromSeconds(10));
77 | }
78 |
79 | public async void EventSubscription(object sender, EventArgs args)
80 | {
81 | await Task.Delay(TimeSpan.FromSeconds(10));
82 | }
83 | }
84 |
85 | public class EventProvider
86 | {
87 | public event EventHandler UpdateTriggered;
88 |
89 | public void DoIt()
90 | {
91 | var handler = UpdateTriggered;
92 |
93 | if (handler != null)
94 | {
95 | handler(this, EventArgs.Empty);
96 | }
97 | }
98 | }
99 | }
100 |
--------------------------------------------------------------------------------
/src/AsyncAwaitAnalyzer.Verification/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("AsyncAwaitAnalyzer.Verification")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("AsyncAwaitAnalyzer.Verification")]
13 | [assembly: AssemblyCopyright("Copyright © 2015")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("44c74072-5ee8-496c-acce-ba9590e6b9c4")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/src/AsyncAwaitAnalyzer.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 14
4 | VisualStudioVersion = 14.0.22512.0
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AsyncAwaitAnalyzer", "AsyncAwaitAnalyzer\AsyncAwaitAnalyzer\AsyncAwaitAnalyzer.csproj", "{C82EC506-F082-4851-8425-DD6ACCBAC328}"
7 | EndProject
8 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{0E906932-1F80-441F-B6BB-AD94E8A3E133}"
9 | ProjectSection(SolutionItems) = preProject
10 | .nuget\packages.config = .nuget\packages.config
11 | EndProjectSection
12 | EndProject
13 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AsyncAwaitAnalyzer.Vsix", "AsyncAwaitAnalyzer\AsyncAwaitAnalyzer.Vsix\AsyncAwaitAnalyzer.Vsix.csproj", "{831136DB-21DE-47E9-B2BE-DE441BED9AFB}"
14 | EndProject
15 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "VerificationProjects", "VerificationProjects", "{FCFD7203-3686-4217-AFA1-3816557008BE}"
16 | EndProject
17 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AsyncAwaitAnalyzer.Verification", "AsyncAwaitAnalyzer.Verification\AsyncAwaitAnalyzer.Verification.csproj", "{44C74072-5EE8-496C-ACCE-BA9590E6B9C4}"
18 | EndProject
19 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AsyncAwaitAnalyzer.Verification.ClassLib", "AsyncAwaitAnalyzer.Verification.ClassLib\AsyncAwaitAnalyzer.Verification.ClassLib.csproj", "{00D469D4-7B09-406A-9409-CD6748BCE818}"
20 | EndProject
21 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AsyncAwaitAnalyzer.Verification.WpfApp", "AsyncAwaitAnalyzer.Verification.WpfApp\AsyncAwaitAnalyzer.Verification.WpfApp.csproj", "{4CFEB406-32C0-4275-BCB3-2D689922C74B}"
22 | EndProject
23 | Global
24 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
25 | Debug|Any CPU = Debug|Any CPU
26 | Release|Any CPU = Release|Any CPU
27 | EndGlobalSection
28 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
29 | {C82EC506-F082-4851-8425-DD6ACCBAC328}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
30 | {C82EC506-F082-4851-8425-DD6ACCBAC328}.Debug|Any CPU.Build.0 = Debug|Any CPU
31 | {C82EC506-F082-4851-8425-DD6ACCBAC328}.Release|Any CPU.ActiveCfg = Release|Any CPU
32 | {C82EC506-F082-4851-8425-DD6ACCBAC328}.Release|Any CPU.Build.0 = Release|Any CPU
33 | {831136DB-21DE-47E9-B2BE-DE441BED9AFB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
34 | {831136DB-21DE-47E9-B2BE-DE441BED9AFB}.Debug|Any CPU.Build.0 = Debug|Any CPU
35 | {831136DB-21DE-47E9-B2BE-DE441BED9AFB}.Release|Any CPU.ActiveCfg = Release|Any CPU
36 | {831136DB-21DE-47E9-B2BE-DE441BED9AFB}.Release|Any CPU.Build.0 = Release|Any CPU
37 | {44C74072-5EE8-496C-ACCE-BA9590E6B9C4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
38 | {44C74072-5EE8-496C-ACCE-BA9590E6B9C4}.Debug|Any CPU.Build.0 = Debug|Any CPU
39 | {44C74072-5EE8-496C-ACCE-BA9590E6B9C4}.Release|Any CPU.ActiveCfg = Release|Any CPU
40 | {44C74072-5EE8-496C-ACCE-BA9590E6B9C4}.Release|Any CPU.Build.0 = Release|Any CPU
41 | {00D469D4-7B09-406A-9409-CD6748BCE818}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
42 | {00D469D4-7B09-406A-9409-CD6748BCE818}.Debug|Any CPU.Build.0 = Debug|Any CPU
43 | {00D469D4-7B09-406A-9409-CD6748BCE818}.Release|Any CPU.ActiveCfg = Release|Any CPU
44 | {00D469D4-7B09-406A-9409-CD6748BCE818}.Release|Any CPU.Build.0 = Release|Any CPU
45 | {4CFEB406-32C0-4275-BCB3-2D689922C74B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
46 | {4CFEB406-32C0-4275-BCB3-2D689922C74B}.Debug|Any CPU.Build.0 = Debug|Any CPU
47 | {4CFEB406-32C0-4275-BCB3-2D689922C74B}.Release|Any CPU.ActiveCfg = Release|Any CPU
48 | {4CFEB406-32C0-4275-BCB3-2D689922C74B}.Release|Any CPU.Build.0 = Release|Any CPU
49 | EndGlobalSection
50 | GlobalSection(SolutionProperties) = preSolution
51 | HideSolutionNode = FALSE
52 | EndGlobalSection
53 | GlobalSection(NestedProjects) = preSolution
54 | {44C74072-5EE8-496C-ACCE-BA9590E6B9C4} = {FCFD7203-3686-4217-AFA1-3816557008BE}
55 | {00D469D4-7B09-406A-9409-CD6748BCE818} = {FCFD7203-3686-4217-AFA1-3816557008BE}
56 | {4CFEB406-32C0-4275-BCB3-2D689922C74B} = {FCFD7203-3686-4217-AFA1-3816557008BE}
57 | EndGlobalSection
58 | EndGlobal
59 |
--------------------------------------------------------------------------------
/src/AsyncAwaitAnalyzer/AsyncAwaitAnalyzer.Vsix/AsyncAwaitAnalyzer.Vsix.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | 14.0
5 | $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)
6 |
7 |
8 |
9 | Debug
10 | AnyCPU
11 | 2.0
12 | {82b43b9b-a64c-4715-b499-d71e9ca2bd60};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}
13 | {831136DB-21DE-47E9-B2BE-DE441BED9AFB}
14 | Library
15 | Properties
16 | AsyncAwaitAnalyzer
17 | AsyncAwaitAnalyzer
18 | v4.5
19 | false
20 | false
21 | false
22 | false
23 | false
24 | false
25 | Roslyn
26 |
27 |
28 | true
29 | full
30 | false
31 | bin\Debug\
32 | DEBUG;TRACE
33 | prompt
34 | 4
35 |
36 |
37 | pdbonly
38 | true
39 | bin\Release\
40 | TRACE
41 | prompt
42 | 4
43 |
44 |
45 | Program
46 | $(DevEnvDir)devenv.exe
47 | /rootsuffix Roslyn
48 |
49 |
50 |
51 | Designer
52 |
53 |
54 |
55 |
56 | {C82EC506-F082-4851-8425-DD6ACCBAC328}
57 | AsyncAwaitAnalyzer
58 |
59 |
60 |
61 |
62 |
69 |
--------------------------------------------------------------------------------
/src/AsyncAwaitAnalyzer/AsyncAwaitAnalyzer.Vsix/source.extension.vsixmanifest:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | AsyncAwaitAnalyzer.Vsix
6 | This is a sample diagnostic extension for the .NET Compiler Platform ("Roslyn").
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/src/AsyncAwaitAnalyzer/AsyncAwaitAnalyzer/Analyzers/AnalyzerHelper.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.CodeAnalysis;
2 |
3 | namespace AsyncAwaitAnalyzer.Analyzers
4 | {
5 | public static class AnalyzerHelper
6 | {
7 | ///
8 | /// Checks if the given method symbol is likely to be an event handler.
9 | ///
10 | ///
11 | /// true if it is an event handler, false otherwise.
12 | public static bool IsEventHandler(this IMethodSymbol methodSymbol)
13 | {
14 | if (methodSymbol.Parameters.Length != 2)
15 | {
16 | return false;
17 | }
18 |
19 | if (methodSymbol.Parameters[0].Type.Name != "Object")
20 | {
21 | return false;
22 | }
23 |
24 | ITypeSymbol baseType = methodSymbol.Parameters[1].Type;
25 | while (baseType.BaseType != null && baseType.BaseType.Name != "Object")
26 | {
27 | baseType = baseType.BaseType;
28 | }
29 |
30 | if (baseType.Name != "EventArgs")
31 | {
32 | return false;
33 | }
34 |
35 | return true;
36 | }
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/src/AsyncAwaitAnalyzer/AsyncAwaitAnalyzer/Analyzers/AsyncMethodsNamingAnalyzer.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Immutable;
2 | using Microsoft.CodeAnalysis;
3 | using Microsoft.CodeAnalysis.Diagnostics;
4 |
5 | namespace AsyncAwaitAnalyzer.Analyzers
6 | {
7 | [DiagnosticAnalyzer(LanguageNames.CSharp)]
8 | public class AsyncMethodsNamingAnalyzer : DiagnosticAnalyzer
9 | {
10 | public const string DiagnosticId = "ASYNC-0002";
11 |
12 | internal const string Title = "Async methods should end with Async.\n" +
13 | "Exception: Event handlers.";
14 |
15 | internal const string AsyncVoidMethodViolationMsg = "To state the async nature of the method clearly, use the suffix Async.";
16 |
17 | internal const string Category = "AsyncAwait-BestPractices";
18 |
19 | internal static DiagnosticDescriptor Rule = new DiagnosticDescriptor(
20 | DiagnosticId,
21 | Title,
22 | AsyncVoidMethodViolationMsg,
23 | Category,
24 | DiagnosticSeverity.Warning,
25 | isEnabledByDefault: true);
26 |
27 | public override ImmutableArray SupportedDiagnostics { get { return ImmutableArray.Create(Rule); } }
28 |
29 | public override void Initialize(AnalysisContext context)
30 | {
31 | context.RegisterSymbolAction(CheckForAsyncVoidViolations, SymbolKind.Method);
32 | }
33 |
34 | private void CheckForAsyncVoidViolations(SymbolAnalysisContext context)
35 | {
36 | var methodSymbol = context.Symbol as IMethodSymbol;
37 | if (methodSymbol == null)
38 | {
39 | return;
40 | }
41 |
42 | if (!methodSymbol.IsAsync)
43 | {
44 | if (!methodSymbol.ReturnType.ToDisplayString().StartsWith("System.Threading.Tasks.Task"))
45 | {
46 | return;
47 | }
48 | }
49 |
50 | if (methodSymbol.IsEventHandler())
51 | {
52 | return;
53 | }
54 |
55 | if (methodSymbol.Name.EndsWith("Async"))
56 | {
57 | return;
58 | }
59 |
60 | var diagnostic = Diagnostic.Create(Rule, methodSymbol.Locations[0]);
61 | context.ReportDiagnostic(diagnostic);
62 | }
63 |
64 | private static bool IsAsyncVoidMethod(IMethodSymbol methodSymbol)
65 | {
66 | return methodSymbol.ReturnsVoid && methodSymbol.IsAsync;
67 | }
68 | }
69 | }
70 |
--------------------------------------------------------------------------------
/src/AsyncAwaitAnalyzer/AsyncAwaitAnalyzer/Analyzers/AsyncVoidMethodsAnalyzer.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Immutable;
2 | using Microsoft.CodeAnalysis;
3 | using Microsoft.CodeAnalysis.CSharp;
4 | using Microsoft.CodeAnalysis.CSharp.Syntax;
5 | using Microsoft.CodeAnalysis.Diagnostics;
6 |
7 | namespace AsyncAwaitAnalyzer.Analyzers
8 | {
9 | [DiagnosticAnalyzer(LanguageNames.CSharp)]
10 | public class AsyncVoidMethodsAnalyzer : DiagnosticAnalyzer
11 | {
12 | public const string DiagnosticId = "ASYNC-0001";
13 |
14 | internal const string Title = "Avoid async/void methods.";
15 |
16 | internal const string AsyncVoidMethodViolationMsg = "Declaring async void methods is considered bad practice.\n" +
17 | "You should return a Task to signal your clients that your method has an async behavior.\n" +
18 | "The exception: event handlers are allowed to be implemented as async void.\n"+
19 | "For more information see http://bit.ly/async-await-best-practice.";
20 |
21 | internal const string Category = "AsyncAwait-BestPractices";
22 |
23 | internal static DiagnosticDescriptor Rule = new DiagnosticDescriptor(
24 | DiagnosticId,
25 | Title,
26 | AsyncVoidMethodViolationMsg,
27 | Category,
28 | DiagnosticSeverity.Warning,
29 | isEnabledByDefault: true);
30 |
31 | public override ImmutableArray SupportedDiagnostics { get { return ImmutableArray.Create(Rule); } }
32 |
33 | public override void Initialize(AnalysisContext context)
34 | {
35 | context.RegisterSymbolAction(CheckForAsyncVoidViolations, SymbolKind.Method);
36 | context.RegisterSyntaxNodeAction(CheckForAsyncVoidViolationsInLambdas, SyntaxKind.ParenthesizedLambdaExpression, SyntaxKind.SimpleLambdaExpression);
37 | }
38 |
39 | private void CheckForAsyncVoidViolationsInLambdas(SyntaxNodeAnalysisContext context)
40 | {
41 | var parenthesizedLambdaExpressionSyntax = context.Node as ExpressionSyntax;
42 | if (parenthesizedLambdaExpressionSyntax == null)
43 | {
44 | return;
45 | }
46 |
47 | var methodSymbol = context.SemanticModel.GetSymbolInfo(parenthesizedLambdaExpressionSyntax).Symbol as IMethodSymbol;
48 | if (!methodSymbol.IsAsync)
49 | {
50 | return;
51 | }
52 |
53 | if (!methodSymbol.ReturnsVoid)
54 | {
55 | return;
56 | }
57 |
58 | var parent = parenthesizedLambdaExpressionSyntax.Parent as AssignmentExpressionSyntax;
59 | if (parent != null)
60 | {
61 | // We tolerate event handler assignments.
62 | var isParentAddAssignmentExpression = parent.IsKind(SyntaxKind.AddAssignmentExpression);
63 | if (isParentAddAssignmentExpression)
64 | {
65 | return;
66 | }
67 | }
68 |
69 | var diagnostic = Diagnostic.Create(Rule, methodSymbol.Locations[0]);
70 | context.ReportDiagnostic(diagnostic);
71 | }
72 |
73 | private void CheckForAsyncVoidViolations(SymbolAnalysisContext context)
74 | {
75 | var methodSymbol = context.Symbol as IMethodSymbol;
76 | if (methodSymbol == null)
77 | {
78 | return;
79 | }
80 |
81 | if (!IsAsyncVoidMethod(methodSymbol))
82 | {
83 | return;
84 | }
85 |
86 | if (methodSymbol.IsEventHandler())
87 | {
88 | return;
89 | }
90 |
91 | var diagnostic = Diagnostic.Create(Rule, methodSymbol.Locations[0]);
92 | context.ReportDiagnostic(diagnostic);
93 | }
94 |
95 | private static bool IsAsyncVoidMethod(IMethodSymbol methodSymbol)
96 | {
97 | return methodSymbol.ReturnsVoid && methodSymbol.IsAsync;
98 | }
99 | }
100 |
101 |
102 | }
103 |
--------------------------------------------------------------------------------
/src/AsyncAwaitAnalyzer/AsyncAwaitAnalyzer/Analyzers/ConfigureAwaitAnalyzer.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Immutable;
2 | using System.Linq;
3 | using Microsoft.CodeAnalysis;
4 | using Microsoft.CodeAnalysis.CSharp;
5 | using Microsoft.CodeAnalysis.CSharp.Syntax;
6 | using Microsoft.CodeAnalysis.Diagnostics;
7 |
8 | namespace AsyncAwaitAnalyzer.Analyzers
9 | {
10 | [DiagnosticAnalyzer(LanguageNames.CSharp)]
11 | public class ConfigureAwaitAnalyzer : DiagnosticAnalyzer
12 | {
13 | public const string DiagnosticId = "ASYNC-003";
14 |
15 | internal const string Title = "Apply .ConfigureAwait(false) to avoid deadlocks";
16 | internal const string MessageFormat = "Apply .ConfigureAwait(false) in your library code to avoid deadlocks.\n" +
17 | "More information: http://bit.ly/async-await-sync-context.";
18 | internal const string Category = "AsyncAwait-BestPractices";
19 |
20 | internal static DiagnosticDescriptor Rule = new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, Category, DiagnosticSeverity.Warning, true);
21 |
22 | private static readonly string[] _uiNamepsaces = new[]
23 | {
24 | "System.Windows",
25 | "System.Xaml"
26 | };
27 |
28 | public override ImmutableArray SupportedDiagnostics { get { return ImmutableArray.Create(Rule); } }
29 |
30 | public override void Initialize(AnalysisContext context)
31 | {
32 | context.RegisterCompilationStartAction(OnCompilationStart);
33 | }
34 |
35 | private void OnCompilationStart(CompilationStartAnalysisContext context)
36 | {
37 | context.RegisterSyntaxNodeAction(AnalyzeSyntax, SyntaxKind.AwaitExpression);
38 | }
39 |
40 | private void AnalyzeSyntax(SyntaxNodeAnalysisContext context)
41 | {
42 | var awaitExpressionSyntax = (AwaitExpressionSyntax)context.Node;
43 | var invocationExpressionSyntax = awaitExpressionSyntax.Expression as InvocationExpressionSyntax;
44 | var awaitedExpression = (invocationExpressionSyntax?.Expression as MemberAccessExpressionSyntax);
45 |
46 | if (IsUiContext(context))
47 | {
48 | return;
49 | }
50 |
51 | // Accept: .ConfigureAwait(false)
52 | if (awaitedExpression?.Name.Identifier.Text == "ConfigureAwait")
53 | {
54 | var configureAwaitIsFalse = invocationExpressionSyntax.ArgumentList.Arguments.Single().Expression.IsKind(SyntaxKind.FalseLiteralExpression);
55 | if (configureAwaitIsFalse)
56 | {
57 | return;
58 | }
59 | }
60 |
61 | // Reject: All others.
62 | var diagnostic = Diagnostic.Create(Rule, awaitExpressionSyntax.GetLocation());
63 | context.ReportDiagnostic(diagnostic);
64 | }
65 |
66 | private static bool IsUiContext(SyntaxNodeAnalysisContext context)
67 | {
68 | // Strategy: Find usings that indicate that we are
69 | var root = context.Node.SyntaxTree.GetRoot(context.CancellationToken);
70 | var usings = root.ChildNodes()
71 | .Where(_ => _.IsKind(SyntaxKind.UsingDirective))
72 | .OfType()
73 | .Select(_ => _.GetText().ToString())
74 | .ToList();
75 |
76 | return _uiNamepsaces.Any(_ => usings.Any(u => u.Contains(_)));
77 | }
78 | }
79 | }
--------------------------------------------------------------------------------
/src/AsyncAwaitAnalyzer/AsyncAwaitAnalyzer/AsyncAwaitAnalyzer.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | 11.0
6 | Debug
7 | AnyCPU
8 | {C82EC506-F082-4851-8425-DD6ACCBAC328}
9 | Library
10 | Properties
11 | AsyncAwaitAnalyzer
12 | AsyncAwaitAnalyzer
13 | {786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}
14 | Profile7
15 | v4.5
16 |
17 |
18 | true
19 | full
20 | false
21 | bin\Debug\
22 | DEBUG;TRACE
23 | prompt
24 | 4
25 |
26 |
27 | pdbonly
28 | true
29 | bin\Release\
30 | TRACE
31 | prompt
32 | 4
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 | Designer
45 | PreserveNewest
46 |
47 |
48 |
49 | PreserveNewest
50 |
51 |
52 | PreserveNewest
53 |
54 |
55 |
56 |
57 |
58 | ..\..\packages\Microsoft.CodeAnalysis.Common.1.0.0.0-beta2\lib\portable-net45+win8\Microsoft.CodeAnalysis.dll
59 | False
60 |
61 |
62 | ..\..\packages\Microsoft.CodeAnalysis.CSharp.1.0.0.0-beta2\lib\portable-net45+win8\Microsoft.CodeAnalysis.CSharp.dll
63 | False
64 |
65 |
66 | ..\..\packages\Microsoft.CodeAnalysis.CSharp.Workspaces.1.0.0.0-beta2\lib\portable-net45+win8\Microsoft.CodeAnalysis.CSharp.Workspaces.dll
67 | False
68 |
69 |
70 | ..\..\packages\Microsoft.CodeAnalysis.Workspaces.Common.1.0.0.0-beta2\lib\portable-net45+win8\Microsoft.CodeAnalysis.Workspaces.dll
71 | False
72 |
73 |
74 | ..\..\packages\System.Collections.Immutable.1.1.32-beta\lib\portable-net45+win8+wp8+wpa81\System.Collections.Immutable.dll
75 | False
76 |
77 |
78 | ..\..\packages\Microsoft.Composition.1.0.27\lib\portable-net45+win8+wp8+wpa81\System.Composition.AttributedModel.dll
79 | False
80 |
81 |
82 | ..\..\packages\Microsoft.Composition.1.0.27\lib\portable-net45+win8+wp8+wpa81\System.Composition.Convention.dll
83 | False
84 |
85 |
86 | ..\..\packages\Microsoft.Composition.1.0.27\lib\portable-net45+win8+wp8+wpa81\System.Composition.Hosting.dll
87 | False
88 |
89 |
90 | ..\..\packages\Microsoft.Composition.1.0.27\lib\portable-net45+win8+wp8+wpa81\System.Composition.Runtime.dll
91 | False
92 |
93 |
94 | ..\..\packages\Microsoft.Composition.1.0.27\lib\portable-net45+win8+wp8+wpa81\System.Composition.TypedParts.dll
95 | False
96 |
97 |
98 | ..\..\packages\System.Reflection.Metadata.1.0.17-beta\lib\portable-net45+win8\System.Reflection.Metadata.dll
99 | False
100 |
101 |
102 |
103 |
104 |
105 | "$(SolutionDir)\packages\NuGet.CommandLine.2.8.2\tools\NuGet.exe" pack Diagnostic.nuspec -NoPackageAnalysis -OutputDirectory .
106 | OnOutputUpdated
107 |
108 |
115 |
--------------------------------------------------------------------------------
/src/AsyncAwaitAnalyzer/AsyncAwaitAnalyzer/Diagnostic.nuspec:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | AsyncAwaitAnalyzer
5 | 0.1.0.0
6 | AsyncAwaitAnalyzer
7 | Oliver Lohmann; Ricardo Wickel
8 | olohmann
9 | http://raw.githubusercontent.com/olohmann/AsyncAwaitAnalyzer/master/LICENSE
10 | http://github.com/olohmann/AsyncAwaitAnalyzer
11 | false
12 | AsyncAwaitAnalyzer
13 |
14 | Copyright 2015
15 | Roslyn Diagnostics CodeFixes
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/src/AsyncAwaitAnalyzer/AsyncAwaitAnalyzer/FixProviders/AsyncMethodsNamingCodeFixProvider.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Immutable;
2 | using System.Composition;
3 | using System.Linq;
4 | using System.Threading.Tasks;
5 | using Microsoft.CodeAnalysis;
6 | using Microsoft.CodeAnalysis.CodeFixes;
7 | using Microsoft.CodeAnalysis.CSharp;
8 | using Microsoft.CodeAnalysis.CodeActions;
9 | using System.Threading;
10 | using Microsoft.CodeAnalysis.Rename;
11 |
12 | namespace AsyncAwaitAnalyzer.Analyzers
13 | {
14 | [ExportCodeFixProvider("AsyncMethodsNamingCodeFixProvider", LanguageNames.CSharp), Shared]
15 | public class AsyncMethodsNamingCodeFixProvider : CodeFixProvider
16 | {
17 | public sealed override ImmutableArray GetFixableDiagnosticIds()
18 | {
19 | return ImmutableArray.Create(AsyncMethodsNamingAnalyzer.DiagnosticId);
20 | }
21 |
22 | public sealed override FixAllProvider GetFixAllProvider()
23 | {
24 | return WellKnownFixAllProviders.BatchFixer;
25 | }
26 |
27 | public sealed override async Task ComputeFixesAsync(CodeFixContext context)
28 | {
29 | var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
30 |
31 | var diagnostic = context.Diagnostics.First();
32 | var diagnosticSpan = diagnostic.Location.SourceSpan;
33 |
34 | SyntaxToken syntaxToken = root.FindToken(diagnosticSpan.Start);
35 | var semanticModel = await context.Document.GetSemanticModelAsync(context.CancellationToken);
36 | var methodSymbol = semanticModel.GetDeclaredSymbol(syntaxToken.Parent, context.CancellationToken) as IMethodSymbol;
37 |
38 | context.RegisterFix(CodeAction.Create("Add 'Async' suffix to method name", ctoken => FixMethodName(context.Document, methodSymbol, ctoken)), diagnostic);
39 | }
40 |
41 | private async Task FixMethodName(Document document, IMethodSymbol methodSymbol, CancellationToken cancellationToken)
42 | {
43 | var originalSolution = document.Project.Solution;
44 | var optionSet = originalSolution.Workspace.Options;
45 | var newSolution = await Renamer.RenameSymbolAsync(
46 | document.Project.Solution,
47 | methodSymbol,
48 | methodSymbol.Name + "Async",
49 | optionSet,
50 | cancellationToken).ConfigureAwait(false);
51 |
52 | return newSolution;
53 | }
54 | }
55 | }
--------------------------------------------------------------------------------
/src/AsyncAwaitAnalyzer/AsyncAwaitAnalyzer/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.InteropServices;
3 |
4 | // General Information about an assembly is controlled through the following
5 | // set of attributes. Change these attribute values to modify the information
6 | // associated with an assembly.
7 | [assembly: AssemblyTitle("AsyncAwaitAnalyzer")]
8 | [assembly: AssemblyDescription("")]
9 | [assembly: AssemblyConfiguration("")]
10 | [assembly: AssemblyCompany("")]
11 | [assembly: AssemblyProduct("AsyncAwaitAnalyzer")]
12 | [assembly: AssemblyCopyright("Copyright © 2015")]
13 | [assembly: AssemblyTrademark("")]
14 | [assembly: AssemblyCulture("")]
15 |
16 | // Setting ComVisible to false makes the types in this assembly not visible
17 | // to COM components. If you need to access a type in this assembly from
18 | // COM, set the ComVisible attribute to true on that type.
19 | [assembly: ComVisible(false)]
20 |
21 | // Version information for an assembly consists of the following four values:
22 | //
23 | // Major Version
24 | // Minor Version
25 | // Build Number
26 | // Revision
27 | //
28 | // You can specify all the values or you can default the Build and Revision Numbers
29 | // by using the '*' as shown below:
30 | [assembly: AssemblyVersion("1.0.*")]
31 | [assembly: AssemblyFileVersion("1.0.0.0")]
32 |
--------------------------------------------------------------------------------
/src/AsyncAwaitAnalyzer/AsyncAwaitAnalyzer/ReadMe.txt:
--------------------------------------------------------------------------------
1 |
2 | Building this project will produce an analyzer .dll, as well as the
3 | following two ways you may wish to package that analyzer:
4 | * A NuGet package (.nupkg file) that will add your assembly as a
5 | project-local analyzer that participates in builds.
6 | * A VSIX extension (.vsix file) that will apply your analyzer to all projects
7 | and works just in the IDE.
8 |
9 | To debug your analyzer, make sure the default project is the VSIX project and
10 | start debugging. This will deploy the analyzer as a VSIX into another instance
11 | of Visual Studio, which is useful for debugging, even if you intend to produce
12 | a NuGet package.
13 |
14 |
15 | TRYING OUT YOUR NUGET PACKAGE
16 |
17 | To try out the NuGet package:
18 | 1. Create a local NuGet feed by following the instructions here:
19 | > http://docs.nuget.org/docs/creating-packages/hosting-your-own-nuget-feeds
20 | 2. Copy the .nupkg file into that folder.
21 | 3. Open the target project in Visual Studio 2015.
22 | 4. Right-click on the project node in Solution Explorer and choose Manage
23 | NuGet Packages.
24 | 5. Select the NuGet feed you created on the left.
25 | 6. Choose your analyzer from the list and click Install.
26 |
27 | If you want to automatically deploy the .nupkg file to the local feed folder
28 | when you build this project, follow these steps:
29 | 1. Right-click on this project in Solution Explorer and choose Properties.
30 | 2. Go to the Compile tab.
31 | 3. Click the Build Events button.
32 | 4. In the "Post-build event command line" box, change the -OutputDirectory
33 | path to point to your local NuGet feed folder.
34 |
--------------------------------------------------------------------------------
/src/AsyncAwaitAnalyzer/AsyncAwaitAnalyzer/packages.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/src/AsyncAwaitAnalyzer/AsyncAwaitAnalyzer/tools/install.ps1:
--------------------------------------------------------------------------------
1 | param($installPath, $toolsPath, $package, $project)
2 |
3 | $analyzerPath = join-path $toolsPath "analyzers"
4 | $analyzerFilePath = join-path $analyzerPath "AsyncAwaitAnalyzer.dll"
5 |
6 | $project.Object.AnalyzerReferences.Add("$analyzerFilePath")
--------------------------------------------------------------------------------
/src/AsyncAwaitAnalyzer/AsyncAwaitAnalyzer/tools/uninstall.ps1:
--------------------------------------------------------------------------------
1 | param($installPath, $toolsPath, $package, $project)
2 |
3 | $analyzerPath = join-path $toolsPath "analyzers"
4 | $analyzerFilePath = join-path $analyzerPath "AsyncAwaitAnalyzer.dll"
5 |
6 | $project.Object.AnalyzerReferences.Remove("$analyzerFilePath")
--------------------------------------------------------------------------------