Skip to content

Conversation

hvitved
Copy link
Contributor

@hvitved hvitved commented Dec 11, 2019

Apply a conservative approach by filtering out results for accesses to captured nullable values, when there is an (implicit) call to the capturing callable which is null-guarded. For example:

bool M(int? i, IEnumerable<int> @is)
{
    if (i.HasValue)
        return @is.Any(j => j == i.Value); // GOOD
    return false;
}

Addresses #2485. I will run a dist-comparison and report the results.

@hvitved hvitved added the C# label Dec 11, 2019
@hvitved hvitved requested a review from a team as a code owner December 11, 2019 14:23
calumgrant
calumgrant previously approved these changes Dec 19, 2019
Copy link
Contributor

@calumgrant calumgrant left a comment

Choose a reason for hiding this comment

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

LGTM

@hvitved hvitved force-pushed the csharp/null-maybe-capture branch from 2fea704 to 12194a1 Compare April 23, 2020 07:46
@hvitved hvitved linked an issue Apr 23, 2020 that may be closed by this pull request
@adityasharad adityasharad changed the base branch from master to main August 14, 2020 18:35
@adityasharad adityasharad requested a review from a team as a code owner August 14, 2020 18:35
@hvitved hvitved force-pushed the csharp/null-maybe-capture branch from 12194a1 to d5e212c Compare August 20, 2020 08:34
Apply a conservative approach by filtering out results for accesses to
captured nullable values, when there is an (implicit) call to the capturing
callable which is `null`-guarded. For example:

```
bool M(int? i, IEnumerable<int> @is)
{
    if (i.HasValue)
        return @is.Any(j => j == i.Value); // GOOD
    return false;
}
```
@hvitved hvitved force-pushed the csharp/null-maybe-capture branch from d5e212c to 984e01e Compare February 8, 2022 13:02
Copy link
Contributor

@michaelnebel michaelnebel left a comment

Choose a reason for hiding this comment

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

Looks plausible to me!

static bool Ex43(int? i, IEnumerable<int> @is)
{
if (i.HasValue)
return @is.Any(j => j == i.Value); // GOOD
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice to get this covered!
I think that C#10 also has some improvements related to definite assignment in terms of compiler warnings. Not sure, if there is also some support for nullable values.

@hvitved hvitved merged commit 0f60401 into github:main Feb 11, 2022
@hvitved hvitved deleted the csharp/null-maybe-capture branch February 11, 2022 09:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

LGTM.com - false positive - (C#) Dereferenced variable may be null
3 participants