Skip to content

Make extracted parameters non-nullable when possible. #76386

Merged
CyrusNajmabadi merged 6 commits intodotnet:mainfrom
CyrusNajmabadi:extractMethodNullable
Dec 13, 2024
Merged

Make extracted parameters non-nullable when possible. #76386
CyrusNajmabadi merged 6 commits intodotnet:mainfrom
CyrusNajmabadi:extractMethodNullable

Conversation

@CyrusNajmabadi
Copy link
Contributor

@CyrusNajmabadi CyrusNajmabadi commented Dec 12, 2024

Fixes #61555

Followup to #76383. That needs to go in first.

Note: this is not a generally solvable issue. Fundamentally a parameter cannot represent the same sort of 'flow' state that a local variable. It is either declared as 'nullable' and the flow-in state is 'maybe null' (which may not match the flow in state of the calling code), or it is declared as 'non nullable', but then would warn on null assignments taht would be allowed in the caller.

I too the position that if the value being passed is known to be non-null at all points within the selection, the it should be treated as a non-nullable parameter. If it ever couldbe null, then the parameter remains nullable.

@CyrusNajmabadi CyrusNajmabadi requested a review from a team as a code owner December 12, 2024 01:57
@ghost ghost added Area-IDE untriaged Issues and PRs which have not yet been triaged by a lead labels Dec 12, 2024
@CyrusNajmabadi
Copy link
Contributor Author

@JoeRobich @ToddGrun @akhera99 this is ready for review.

// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Security;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this intentional? It doesn't look to me like anything uses something from System.Security.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nope!

{|CS8602:c|}.ToString();
c = null;
c?.ToString();
return c;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why return c if the extracted selection contains the last reference to it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unclear :-). I'll have to debug through to see what's going on (note, this is unrelated to this pr).

@CyrusNajmabadi CyrusNajmabadi merged commit 80b91e9 into dotnet:main Dec 13, 2024
@dotnet-policy-service dotnet-policy-service bot added this to the Next milestone Dec 13, 2024
@CyrusNajmabadi CyrusNajmabadi deleted the extractMethodNullable branch December 13, 2024 17:48
@dibarbet dibarbet modified the milestones: Next, 17.13 P3 Jan 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area-IDE untriaged Issues and PRs which have not yet been triaged by a lead VSCode

Projects

None yet

Development

Successfully merging this pull request may close these issues.

"Extract Method" introduces an invalid nullability annotation that results in a nullability issue in the extracted code

4 participants