roslyn-code-analysis

Suppressing issues from Roslyn code Analyzers

℡╲_俬逩灬. 提交于 2019-12-07 08:04:33
问题 Is there any way to suppress the issues from the Roslyn Analyzers? I use the instant analyzer project type. And I want to suppress the issues if the user wants it. Also it must be permanent. If I reopen Visual Studio, the same suppression rules must still be applied. 回答1: You can ignore warnings/errors from Roslyn analyzers in exactly the same ways as ignoring normal C# compiler warnings: #pragma disable within source code The Project Properties / Build / Errors and Warnings setting The

Using the Roslyn Semantic Model to Find Symbols in a Single .cs File

Deadly 提交于 2019-12-07 06:58:18
问题 I am using Roslyn to create an analyzer that warns users if a particular class exposes its fields in an unsynchronized manner, to help prevent race conditions. The Problem: I currently have working code that checks to make sure a field is private. I’m having trouble with the last piece of the puzzle: figuring out a way to make sure that all fields are only accessed inside a lock block, so they’re (ostensibly) synchronized. using System; using System.Collections.Generic; using System

How to see if a class has implemented the interface with Roslyn

徘徊边缘 提交于 2019-12-06 05:51:46
问题 I'm still new at Roslyn , so hopefully this isn't too stupid of a question. What I'm looking for is a way to see if a class has implemented all the methods of an interface and if not, the highlight the interface, just like the built in "Implement interface" does. So far I can see if the method name is implemented, but I haven't found a way to see if the right returntype is set on the method. 回答1: You can use ITypeSymbol.FindImplementationForInterfaceMember for this purpose. Basically what you

.NET Roslyn : runtime configuration

筅森魡賤 提交于 2019-12-06 04:39:04
I am going to develop some rules with the Roslyn code analyzer. This rule is to control the access of a namespace. Example, the DAL can use only the core. If the View use the DAL, I want a warning. I use the template "Analyzer with Code Fix (NuGet + VSIX)" in 'Visual Studio 2015 Community Edition' to generate the plugin. I have made some test and it works fine. However the rule is written at hard in the code. I don't know how configure the rule in the runtime. The best will be a configuration file in the solution or the project. Can you give me some example? Thank. Resume of solution : Add the

Opening a solution with msbuildworkspace gives diagnostics errors without details

寵の児 提交于 2019-12-06 02:30:41
I am trying to analyse a solution with Roslyn, with MSBuildWorkspace. The solution is a new solution, with 2 class library projects in them, one referencing the other. They are created in Visual Studio 2017, .Net 4.6.2. When I open the solution, I receive two generic errors in workspace.Diagnostics, both are : Msbuild failed when processing the file ' PathToProject ' There is nothing more in the diagnostics or output window, to indicate WHY it failed to process the project file. The code for opening the solution: namespace RoslynAnalyse { class Program { static void Main(string[] args) {

How to find type of the field with specific name in Roslyn

半腔热情 提交于 2019-12-05 19:41:47
Need to find TypeSyntax or essentially Type of a specific filed in class by using Roslyn. Something like this: rootSyntaxNode .DescendantNodes() .OfType<FieldDeclarationSyntax>() .First(x => x.Identifier="fieldName") .GivemeTypeSyntax() But could not get any hint about how to reach Identifier and SyntaxType in FieldDeclarationSyntax node. Any idea please? Part of the issue is that fields can contain multiple variables. You'll look at Declaration for type and Variables for identifiers. I think this is what you're looking for: var tree = CSharpSyntaxTree.ParseText(@" class MyClass { int

Add a parameter to a method with a Roslyn CodeFixProvider

狂风中的少年 提交于 2019-12-05 14:16:56
I'm writing a Roslyn Code Analyzer that I want to identify if an async method does not take a CancellationToken and then suggest a code fix that adds it: //Before Code Fix: public async Task Example(){} //After Code Fix public async Task Example(CancellationToken token){} I've wired up the DiagnosticAnalyzer to correctly report a Diagnostic by inspecting the methodDeclaration.ParameterList.Parameters , but I can't find the Roslyn API for adding a Paramater to the ParameterList inside a CodeFixProvider . This is what I've got so far: private async Task<Document>

Suppressing issues from Roslyn code Analyzers

廉价感情. 提交于 2019-12-05 12:53:11
Is there any way to suppress the issues from the Roslyn Analyzers? I use the instant analyzer project type. And I want to suppress the issues if the user wants it. Also it must be permanent. If I reopen Visual Studio, the same suppression rules must still be applied. You can ignore warnings/errors from Roslyn analyzers in exactly the same ways as ignoring normal C# compiler warnings: #pragma disable within source code The Project Properties / Build / Errors and Warnings setting The [SuppressMessage] attribute In Visual Studio 2017 you can disable Roslyn warnings (like IDE0002, IDE0003, etc.)

Find missing await in solution

谁说胖子不能爱 提交于 2019-12-05 12:52:04
I have lots of async methods in my server code, but I suspect that I have callers without await. Is there a simple way to scan the code for calls where await is missing? public async Task DomeSomethingAsync() { var result = await GetResult(); await StoreResult(result); } then somewhere I've forgot to use await; public async Task SomeBuggyCode() { await Initialize(); DoSomethingAsync(); // DOH - Forgot await } I was hoping there was a smart way to identitfy these erroronous calls. This should be shown as compiler warning CS4014 with the text: Because this call is not awaited, execution of the

Is it possible to use Rosyln or Resharper to detect possible DivideByZero cases?

核能气质少年 提交于 2019-12-05 11:57:05
I'm trying to determine if there's a programmatic way to check for possible DivideByZeroException in my codebase. My codebase includes a series of relatively simple to relatively complex formulas, approximately 1500 of them (and growing). When new formulas are written, care must be taken to ensure that division is done safely to avoid exceptions during processing of these formulas. E.g. decimal val1 = 1.1m; decimal val2 = 0m; var res = val1/val2; //bad var res = val2 == 0 ? 0 : val1/val2; //good Is there any way to use Roslyn or Resharper, or other tools to inspect my codebase and identify