Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom roslyn analyzer to override nullability analysis? #32042

Closed
adamfk opened this issue Dec 28, 2018 · 6 comments
Closed

Custom roslyn analyzer to override nullability analysis? #32042

adamfk opened this issue Dec 28, 2018 · 6 comments
Labels
Area-Analyzers Feature - Nullable Reference Types Nullable Reference Types Resolution-Duplicate The described behavior is tracked in another issue

Comments

@adamfk
Copy link

adamfk commented Dec 28, 2018

I understand that a bunch of annotations to inform nullability analysis is already in the works #26761, but I'd like to suggest that it would be useful to allow writing custom roslyn analyzers that can override the compiler's nullability warnings.

Mads mentioned in this video that a mini language of attributes is being developed to inform nullability analysis to cover 98% of cases. I think that custom analyzers might be able to cover the remaining 2% of cases.

The use cases that I'm currently thinking of are initializing fields of an object via reflection during construction.

@jaredpar
Copy link
Member

jaredpar commented Jan 2, 2019

Moving to @dotnet/roslyn-ide as this appears to be more of a feature request for analyzers.

@sharwell
Copy link
Member

sharwell commented Jan 2, 2019

This would be covered under #20242 if that API moves forward.

@sharwell
Copy link
Member

sharwell commented Jan 3, 2019

Duplicate of #20242

@sharwell sharwell marked this as a duplicate of #20242 Jan 3, 2019
@sharwell sharwell closed this as completed Jan 3, 2019
@sharwell sharwell added the Resolution-Duplicate The described behavior is tracked in another issue label Jan 3, 2019
@Dreamescaper
Copy link
Contributor

Dreamescaper commented Oct 10, 2019

@sharwell
Can we reopen this one? I don't think that's same as #20242. Problem is that with such approach I need to re-implement nullability logic that is already present in Roslyn.

Let me give an example from NUnit test.

SomeType? obj = GetSomeObjectWithNullableProperties();
Assert.That(obj?.SomeProperty?.NestedProperty, Is.Not.Null);  <---this line

Suppose I'd like to support nullability changes for Is.Not.Null asserts by analyzer. From that line I need know that:
obj is not null
obj.SomeProperty is not null
obj.SomeProperty.NestedProperty is not null

And to implement that in my suppression analyzer.
But I don't want to do that - Roslyn already knows how to handle that, ideally I'd like to tell Roslyn - consider this expression as not null, and that's it.

@sharwell
Copy link
Member

@jaredpar #32042 (comment) sounds like a compiler request

@mavasani
Copy link
Contributor

mavasani commented Oct 10, 2019

ideally I'd like to tell Roslyn - consider this expression as not null, and that's it.

Currently no part of the compiler's analysis depends on any third party analyzer's input/output. Introducing any such feedback loop can potentially lead to unwanted deadlocks and/or infinite recursion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Analyzers Feature - Nullable Reference Types Nullable Reference Types Resolution-Duplicate The described behavior is tracked in another issue
Projects
None yet
Development

No branches or pull requests

6 participants