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

Add workaround and missing [MemberNotNullWhen] attributes #1879

Merged
merged 4 commits into from
Oct 17, 2024

Conversation

sguryev
Copy link
Contributor

@sguryev sguryev commented Oct 14, 2024

What kind of change does this PR introduce?
This is the second attempt to add [MemberNotNullWhen] attribute for Content property of IApiResponse<T>.
Issue #1671

What is the current behavior?
Analyzer doesn't know that Content is not null if IsSuccessStatusCode is true.
image

What is the new behavior?
Analyzer sees new attributes and doesn't show warning\error about possible null ref.
image

What might this PR break?
I hope nothing.

  • I had to add 3 new properties to the derived interface otherwise Rider shows the waring and analyzer ignores attribute for property, declared in the base interface.
    image
  • Explicit base property implementation may lead to the unexpected behavior based on the resolved interface (sharplab)
    image
    But we are not going to add explicit implementation and ApiResponse<T> class is sealed so nobody will be able to make a derived class with explicit base implementation.
  • I didn't introduce any breaking changes (I hope) like I did last time (change inheritance hierarchy with IApiResponseBase)
  • We have 3 new properties in the IApiResponse<T> interface (with summary and [SuppressMessage]) just for making [MemberNotNullWhen] work. But they are wrapped with #if NET6_0_OR_GREATER directive so will be invisible for older frameworks.
  • The new behavior for interface is safer than for class so I can't imagine any other problems we may have.

Please check if the PR fulfills these requirements
not applicable - [ ] Tests for the changes have been added (for bug fixes / features)
not applicable - [ ] Docs have been added / updated (for bug fixes / features)

Other information:
I hope we finally can get rid of this annoying additional check we have everywhere now
image

@TimothyMakkison
Copy link
Contributor

TimothyMakkison commented Oct 15, 2024

Nice change, rarely see the new keyword used like that.

This should be a separate PR/issue but perhaps Refit should polyfill MemberNotNullWhen. Don't know if it would break anything

@ChrisPulman ChrisPulman enabled auto-merge (squash) October 17, 2024 23:26
@ChrisPulman ChrisPulman merged commit b13cbf2 into reactiveui:main Oct 17, 2024
1 check passed
Copy link

codecov bot commented Oct 17, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 83.80%. Comparing base (6ebeda5) to head (2a85c2c).
Report is 140 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1879      +/-   ##
==========================================
- Coverage   87.73%   83.80%   -3.93%     
==========================================
  Files          33       36       +3     
  Lines        2348     2476     +128     
  Branches      294      347      +53     
==========================================
+ Hits         2060     2075      +15     
- Misses        208      318     +110     
- Partials       80       83       +3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@sguryev sguryev deleted the serj/main-member-not-null-when branch October 18, 2024 09:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants