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
7
votes
0 answers

Cannot suppress warnings automatically in .Net Core Console project

Steps to replicate on VS 2017 Enterprise: Create new .Net Core solution with a Console App project Add the following NuGet package: Microsoft.CodeAnalysis.FxCopAnalyzers v2.3.0-beta1 Run code analysis with the Error List filtered to "Build…
7
votes
3 answers

Visual Studio 2017 - measuring "Lines of code"

I'm unable to find a good option for my Visual Studio 2017. I tried right clicking the projects, but there wasn't an option for "Analyze". Just a few hours ago, I installed "Roslyn Code Analysis" using Nuget package manager. It's installed as…
Vivek Jaiswal
  • 97
  • 1
  • 1
  • 3
7
votes
2 answers

Appveyor nuget analyser

I'm trying to invent good way to pack my Roslyn analyzer projects into NuGet. It requires specific NuGet package structure with .dll put into \analyzer\, not \lib\. For this to work, I have to call nuget pack .nuspec, not nuget pack…
Leotsarev
  • 1,040
  • 7
  • 23
7
votes
1 answer

Determining if two SyntaxTokens are the same

Given the following piece of code as an example: public class Thing { public int Item { get; } public Thing(int item) { Item = Item; // note incorrect assignment: rhs should be item, the passed-in arg, hence analyzer should…
Ian Kemp
  • 28,293
  • 19
  • 112
  • 138
7
votes
2 answers

Can I use a roslyn analyzer from within the solution it is defined in?

As I understand it there are two ways to distribute and consume roslyn analyzers:- As a Visual Studio plugin As a Nuget package I frequently find myself wanting to enforce certain domain-specific restrictions, along with convenient code-fixes.…
Iain Galloway
  • 18,669
  • 6
  • 52
  • 73
7
votes
1 answer

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

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.…
logitropicity
  • 105
  • 1
  • 7
7
votes
1 answer

Roslyn load project documents failing

In a Visual Studio Extension (VSIX) solution, I'm using Roslyn to load a specific project from my current solution: Project myProject = this.CurrentComponentModel.GetService() .CurrentSolution.Projects …
6
votes
1 answer

C# Roslyn Analyzer via ProjectReference leads to not found error after opening Visual Studio

I've written an Roslyn Analyzer for a specific C# Solution and want to add the analyer to all projects in the solution via ProjectReference: 8.0
Eike S
  • 300
  • 1
  • 11
6
votes
1 answer

what is correct format for multiple discard variables in c# method

I have a method that is called by an external api that is expecting the method to have these specific parameters public static bool DoSomething( int x, int y ) => true; Rosyln code analysis is…
James Bradt
  • 564
  • 5
  • 11
6
votes
1 answer

Generate Report out of FxCop Code Analyzer(Roslyn) in Azure Build Pipeline

I want to generate code analysis report from the Build Pipeline. I have recently moved from Fxcopcmd.exe to Roslyn based analyzers. I am able to see warnings in the build log, but I am not able to get a seperate report that can be shared with…
6
votes
1 answer

Visual Studio: Suppress warnings for all files in a namespace

I have following namespaces in my project. I want to disable a specific warning on a specific namespace (lets say Project.ViewModels). I can disable a warning on one files by doing this in the GlobalSuppression.cs [assembly:…
fhnaseer
  • 7,159
  • 16
  • 60
  • 112
6
votes
1 answer

FindReferencesAsync doesn't return references across projects

I am trying to find all the references of a type symbol using FindReferencesAsync. It works well for references within a C# project; however, cross-project references don't appear. Here is the code that I am using. var model =…
Tushar
  • 790
  • 7
  • 12
6
votes
1 answer

Compile-time only Nuget dependency (FxCop)

I am creating a .Net Standard 2.0 Nuget package and I want to perform static code analysis for the build. However, when I add the Microsoft.CodeAnalysis.FxCopAnalyzers package it becomes a runtime dependency, so any projects that reference my Nuget…
scottrudy
  • 1,633
  • 1
  • 14
  • 24
6
votes
1 answer

Constructing NameOf expression via SyntaxFactory (Roslyn)

Trying to construct nameof expression from scratch using C# SyntaxFactory. Roslyn fails to recognize my InvocationExpressionSyntax as a contextual nameof keyword and throws error diagnostics upon Emit command. Tried to give Roslyn valid code to…
InflexCZE
  • 722
  • 1
  • 14
  • 30
6
votes
2 answers

Adding new field declaration to class with Roslyn

There is some way to add a member to a class with roslyn? I want to add : public int number {get;set;} UPDATE I used this code: PropertyDeclarationSyntax o = SyntaxFactory.PropertyDeclaration(SyntaxFactory.ParseTypeName("public …
blinkettaro
  • 341
  • 6
  • 18