812 questions
0
votes
2
answers
94
views
Access .editorconfig File from Roslyn CodeFixProvider
I am working on a Roslyn Diagnostic Analyzer and Code Fix Provider. Part of this entails an attribute, which takes a parameter of a method name, to override some validation logic. One of the ...
0
votes
0
answers
75
views
Source code generator fails to match attribute declared on class against the actual attribute definition
Use Case
Given a class with a specific attribute, during compilation, look for classes with that attribute, evaluate all the properties for the class, add [Display(Name = "some value")] to ...
1
vote
1
answer
125
views
.NET analyzers not detecting wrong usage of async APIs
I was surprised that no analyzer found the wrong usage of async APIs like that:
public void NoAnalyzerFindsThatIssue()
{
// does not trigger https://learn.microsoft.com/en-us/dotnet/csharp/...
1
vote
1
answer
64
views
Validation when one constructor calls another with enum in C#
A simple class with a copy constructor, which calls another constructor:
public class Foo<TEnum> where TEnum : Enum
{
public Foo(TEnum myEnum)
{
MyEnum = myEnum;
}
public Foo(Foo<...
0
votes
1
answer
26
views
Change all comments with ReplaceTrivia
CodeGeneration. Problems calling ReplaceTrivia method on some comments.
The issue arises when attempting to replace all comments in a method using the ReplaceTrivia method from the Roslyn API. ...
2
votes
0
answers
111
views
Roslynator fail with C# source generator
I have a project specific roslyn source generator. It is a project reference in a library (which is also a project reference) in my application.
<ProjectReference Include="$(RepoRoot)\...
0
votes
1
answer
77
views
Roslyn Diagnostic Analzyer - Underline Only An Attribute Parameter
I have created a diagnostic analyzer which reports various issues with custom attributes for properties. However, I am having trouble get the location of only the attribute and/or attribute arguments ...
0
votes
0
answers
35
views
VS 2022 Diagnostic Analyzer Stops Working When Trying to Get Type Information of Custom Class
I am trying to learn how to create a Roslyn diagnostic analyzer and decided to try to develop it against a library I recently created; specifically, for one of the attributes. The attribute is called ...
0
votes
0
answers
48
views
Downgraded package reference on build
I have an analyzer (targeting netstandard2.0) relying on Microsoft.CodeAnalysis v 4.12.0, which in turn has reference to System.Memory v 4.6.0. And I have no direct reference to any other version of ...
1
vote
0
answers
90
views
Roslyn Analyzer: Check values of readonly fields
I have a bit of a peculiar problem at work: In this project all of the ErrorCodes are defined in the code base. They look somewhat like this:
public readonly ErrorMessageCode 00001 = new ...
2
votes
0
answers
111
views
Using [MemberNotNull] in a virtual override that is called in BaseClass constructor
I have the following code Try it here
using System;
using System.Diagnostics.CodeAnalysis;
public class Base{
protected Base(){
Initialize();
}
protected virtual void ...
0
votes
0
answers
81
views
Better way to search for generic Task<T> expression
I am on a roslyn analyzer that can handle async Task<T> boxing to an object variable like:
int GetValue() => ...
void Print(object value) => Console.WriteLine(value);
void Main() {
...
15
votes
1
answer
2k
views
Changing semantics in C# analyser "Collection initialization can be simplified" IDE0028
In C# 12 there's the "Collection initialization can be simplified" analyser IDE0028.
I've been wondering how the fixer decides when to fix or not fix initialisers such as new(), new List<...
1
vote
2
answers
144
views
Is there a way to highlight certain class usage as a warning?
Is there a way to highlight certain class usage as a warning? For example I want to restrict usage of System.Math in my project. Can I somehow configure Rider or Roslyn to make it underline all ...
1
vote
1
answer
131
views
Roslyn Analyzer should use more than one code file at once
I am writing some Roslyn Analyzers at the moment and some of them have to use information from several classes (usualy located in different files) in order to fullfill their job.
For example, I need ...
0
votes
2
answers
178
views
How to add new line in object initializer with Roslyn?
I'm trying to format code automatically to improve readability using Roslyn.
This is what my method looks like:
public SyntaxNode GetFormatedNode(SyntaxNode node)
{
var workspace = new ...
0
votes
1
answer
157
views
How to suppress warning for all derived types?
I have an ITemplate interface in my project. All interfaces derived from it have names that do not start with I. This causes the InconsistentNaming warning to appear.
I want to suppress this warning ...
0
votes
1
answer
78
views
How to address null reference assignment warning when nullable check is performed and handled
Is there a way to tell the compiler that a following check will ensure that Item is not null and it is safe in the rest of the code and the CS8601 warning is not needed?
The warning, which technically ...
0
votes
1
answer
754
views
Intellisense not seeing source generated from a local C# source generator
I have a solution which contains 4 projects. One of them is a Roslyn source generator, one is a class library which uses the source generator, one is a testing project which also uses the source ...
0
votes
0
answers
30
views
I have a project where I allow users to run 'macros' against the data. This used to work fine, but I am now getting StackOverflowException errors
My project is a complex data modelling tool. To save me having to write bespoke reports for it, I added the facility for users to write macros to extract the data they need (in reality it is just me ...
1
vote
0
answers
52
views
Why does Visual Studio's popup sometimes display non-nullable reference types as nullable? [duplicate]
I've noticed an oddity in the way Visual Studio's popup (when you hover over a variable) displays non-nullable reference types, for example:
clipData.GetFormats() returns a string[] - a non-nullable ...
3
votes
2
answers
257
views
How to generate compiler warning when using a C# enum's ToString method
We have some code which names infrastructure constructs based on a few criteria, including the environment name. Here's a simplified example:
enum AppEnvironment
{
Dev,
Staging,
Production
...
0
votes
0
answers
205
views
Roslyn diagnostics reported through CompilationAnalysisContext.ReportDiagnostic do not show up in Visual Studio
I have created a Roslyn analyzer using the Analyzer with Code Fix template in Visual Studio 2022. Everything works in unit tests, but no diagnostics are shown up in Visual Studio.
A minimal example ...
4
votes
1
answer
465
views
Unit test C# source generator to emulate a user changing source code?
I'm writing a source generator (IIncrementalGenerator to be specific) and I want to make sure that the caching is correctly responding to changes in the source code.
Run the source generator
Assert ...
2
votes
2
answers
3k
views
Visual Studio 2022: Impossible to install a VSIX extension from 'Analyzer with Code Fix (.NET Standard)' project template
I'm currently trying to create a new code analysis rule for Visual Studio 2022 so i followed this tutorial.
When you follow the tutorial, you have to create a new Analyzer with Code Fix (.NET Standard)...