Questions tagged [roslyn-code-analysis]

For questions regarding code analysis for C# and VB based on the Roslyn .NET compiler platform API.

The Roslyn compiler platform provides an API to implement code analysis, code fixes, and refactoring tools for C# and VB.

The Roslyn Wiki also contains an introduction for "How To Write a C# Analyzer and Code Fix" and for Visual Basic.

769 questions
0
votes
2 answers

Roslyn Analyzer - Access to ITextBuffer

Is it somehow possible to have access to the ITextBuffer instance of the document that is being analyzed by a Roslyn diagnostic analyzer in Visual Studio. Or at least the filename, so that I can retrieve the ITextBuffer myself? For more information…
TWT
  • 2,511
  • 1
  • 23
  • 37
0
votes
0 answers

Are Roslyn Code Analysers suitable (and complete enough) for VB.NET .NET Framework solutions?

I'm looking at static code analysis for our C# and VB.NET .NET Framework solutions and trying to decide whether to upgrade to Roslyn code analysers, and, if so, which ones to use? We are on VS2019. I've…
JamesB
  • 165
  • 1
  • 12
0
votes
1 answer

Adding System.Web.Ui.WebControls to analyzer test project

TL:DR -- How do I add System.Web.Ui.WebControls to my AdhocWorkspace? Long version... My unit test for an analyzer is failing because the test compilation environment doesn't have a reference to System.Web.Ui.WebControls. I need to get a semantic…
jmoreno
  • 12,752
  • 4
  • 60
  • 91
0
votes
1 answer

Can you get the current project from an Analyzer?

I’d like to write a Analyzer that adds either Option Strict Off or Option Strict On, which seems like it would be easy to do ... if the Analyzer can get access to a files project.
jmoreno
  • 12,752
  • 4
  • 60
  • 91
0
votes
1 answer

How to get the class identifier from object instantiation using Roslyn

I'm trying to create an analyzer that will find where each method invocation comes from especially the class where the method is defined. let's assume we have the following code: Movie myMovie = new Movie(); myMovie.Rent(); my analyzer till now…
Hamza
  • 440
  • 7
  • 20
0
votes
2 answers

Detect external api calls using roslyn

I would like to know how detect if for example InvocationExpressionSyntax is from my code or from external code. I tried to find how to do it, but with no success. Example.: public async Task MyMethodAsync1() { await MyMethodAsync2(); // How to…
Peter M.
  • 1,028
  • 2
  • 10
  • 27
0
votes
1 answer

In a DiagnosticAnalyzer's action callback, how do I get the Document or Project from which the SyntaxNode was derived?

I'm writing a DiagnosticAnalyzer, and register a SyntaxNode action for SyntaxKind.Attribute. The attribute names some other file in the project. For example, the code being analyzed may include [RelatedFile("otherFileName.foo")] interface…
Jim Lyon
  • 21
  • 4
0
votes
0 answers

GetChangedDocumentAsync returns null but should have a value

My CodeFix returns a Document and I can trace it through Roslyn until GetChangeSolutionAsync. When I view in debugger ChangedDocument has a Document with the correct text. But the next line compares ChangedDocument == null and it is null and exits…
Paul Cohen
  • 241
  • 4
  • 14
0
votes
0 answers

Testing Roslyn codeAction with confusing results

I have a CodeFix that gets applied "randomly" in Unit Testing. I have 46 unit tests for a single code fix and 20 fail. What is strange is that when I look at the Document I return in every case it is correct at the end of my CodeFix. but when I look…
Paul Cohen
  • 241
  • 4
  • 14
0
votes
1 answer

EF Core Analyzer RawSqlStringInjectionDiagnosticAnalyzer error

I am trying to port everything from Entity Framework 6.3 to Entity Framework Core 2.1.2 and this is my first experience with EF Core. I have 2 projects one is my core or infrastructure project with all the entity models, DB Context and repositories…
0
votes
1 answer

Is there a way to check parameter potential value with Roslyn?

I have a enum Color: enum Color { red, blue, noColor // This will be deprecated. } And a function PrintColor: public void PrintColor(Color color) { Console.WriteLine(color); } Because I want to noColor to be deprecated, so I…
Caesium
  • 789
  • 3
  • 7
  • 24
0
votes
1 answer

Roslyn Diagnostic Analyzer RegisterSymbolAction not being called

I created a C# diagnostic project for VB, and am having trouble with it. public override void Initialize(AnalysisContext context) { context.RegisterSymbolAction(CheckExpression, SyntaxKind.InvocationExpression); } But…
jmoreno
  • 12,752
  • 4
  • 60
  • 91
0
votes
1 answer

C# How can i detect EvenHandler subscription in Roslyn

I'm writing a custom analyzer rule using Roslyn. I want to find a method which is a handler for some event (via subscription). Like this: public class Counter { public event EventHandler ThresholdReached; } public class TestEvent { public…
0
votes
2 answers

How do I calculate the code-to-comment ratio of a C# project?

Note: I am not asking what the golden code-to-comment ratio is, nor am I attempting to affix a particular ratio to our team. Instead, we would like to improve how well our codebase is documented (we started with a "code should document itself"…
TheHans255
  • 2,059
  • 1
  • 19
  • 36
0
votes
1 answer

C# - Test that all calls to .Single(), .First(), etc. conform to coding standards

We use some extension methods, which allows us to write something like: .Single(e => $"{nameof(SomeParameter)}: {SomeParameter}, ...") instead of just .Single() where e is an exception. This greatly increases logging experience because the upper…