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 null check" quickfix should support generating ArgumentNullException.ThrowIfNull() calls #61181

Closed
Eli-Black-Work opened this issue May 9, 2022 · 6 comments
Labels
Area-IDE Feature Request untriaged Issues and PRs which have not yet been triaged by a lead

Comments

@Eli-Black-Work
Copy link

The "Add null check" quickfix in Visual Studio doesn't have an option for generating a null check using ArgumentNullException.ThrowIfNull().

Consider the following code:

public string Example(object name)
{
    return name.ToString();
}

The "Add null check" quickfix looks like this:
add null check quickfix

I'd like for the quickfix to also have an option for generating a call to ArgumentNullException.ThrowIfNull(), so that the resulting code looks like this:

public string Example(object name)
{
    ArgumentNullException.ThrowIfNull(name);

    return name.ToString();
}
@dotnet-issue-labeler dotnet-issue-labeler bot added the untriaged Issues and PRs which have not yet been triaged by a lead label May 9, 2022
@Eli-Black-Work
Copy link
Author

This would need to be only available on projects using .NET 6 or above.

@Neme12
Copy link
Contributor

Neme12 commented May 9, 2022

@Bosch-Eli-Black I wouldn't check for the target framework, but rather whether the method exists and has the correct signature.

@Eli-Black-Work
Copy link
Author

Eli-Black-Work commented May 10, 2022

It's also be sweet if there were a quickfix to convert this:

if (name == null)
        throw new ArgumentNullException(nameof(name));

to this:

ArgumentNullException.ThrowIfNull(name);

(Preferably a quickfix that could be run across all .cs files in the solution 🙂)

@davidwengier
Copy link
Contributor

dotnet/runtime#68326

@Eli-Black-Work
Copy link
Author

@davidwengier Oooh, thanks! Guess I was in the wrong repo 🙂 Let me comment over there.

Thanks again! 🙂

@Eli-Black-Work
Copy link
Author

@Neme12 Ah, yes, good point; that's probably the proper way to do that 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-IDE Feature Request untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
Development

No branches or pull requests

3 participants