1

I want to modify how some code in Visual Studio 22 is generated.

For instance, in methods I can generate null check on using the Add null check (MS docs link) for arguments in the following way.

public void MyMethod(object parameter)
{
    if (object is null)
    {
        throw new ArgumentNullException(nameof(parameter));
    }
    // rest of the method
    // ...
}

What I would like to be generated is this:

public void MyMethod(object parameter)
{
    if (object is null) throw new ArgumentNullException(nameof(parameter));
    
    // rest of the method
    // ...
}

I was looking at code snippet settings and refactoring settings but I can not find the Add null check option anywhere. Let alone change it.

The best option I came up with is to create a code snippet (MS docs link) but that does not serve my purpose fully.

Is there an option I can modify to say how is the code generated?

Jan Kleprlík
  • 320
  • 3
  • 12
  • Look for the refactoring snippets folder eg. C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC#\Snippets\1033\Refactoring – Lee Mar 25 '22 at 14:46
  • 1
    @Lee I went by hand through all of the snippets in the *1033* folder but sadly none of them generate the *null check*. – Jan Kleprlík Mar 25 '22 at 15:25
  • No, that's not generated by a snippet. You might be able to set up your code formatting rules in .editorconfig to omit braces and newlines, but then that will apply *across the board*, and not just to null checks. To be honest, with [`!!`](https://stackoverflow.com/questions/71063730/what-is-double-exclamation-mark-in-c/71063828) just around the corner, you might as well put up with this for now, and move to `!!` when it becomes available – canton7 Mar 25 '22 at 15:53
  • @canton7 As you say, setting the rules in .editorconfig would apply *across the board* which could cause more harm than good. I am also aware of the upcoming `!!` however, that neither answers the question if said kind of code generation is modifiable. It seems like there is not any way I could edit such code generation, which surprises me. – Jan Kleprlík Mar 25 '22 at 16:09
  • 1
    Generated code is formatted according to the rules in your .editorconfig (or a set of defaults if not specified). It's not that VS is generating that exact snippet: it's that it's semantically generating an `if` followed by a `throw`, and then formatting it according to your preferences. There's no separate code style preference for "Null checks at the start of methods must not use braces" – canton7 Mar 25 '22 at 16:13
  • @canton7 Oh I see now. That explains it pretty well, thank you for that. Out of interest do you know if this `if`, `throw` sequence that is generated could be modified? Let's say I would want to use something else than *exceptions* for reporting nulls (ie. some error library). – Jan Kleprlík Mar 25 '22 at 16:35
  • 1
    It's [defined here](https://github.com/dotnet/roslyn/blob/52bdc01db337967e9d71080b548a5419a44ed996/src/Features/Core/Portable/InitializeParameter/AbstractAddParameterCheckCodeRefactoringProvider.cs#L351), then [see here](https://github.com/dotnet/roslyn/blob/52bdc01db337967e9d71080b548a5419a44ed996/src/Features/Core/Portable/InitializeParameter/AbstractAddParameterCheckCodeRefactoringProvider.cs#L406). This uses the standard analyzers infrastructure, so you could happily create your own roslyn analyzer and add a code fix provider to it, to do whatever you wanted – canton7 Mar 25 '22 at 16:47

1 Answers1

2

Rewriting an answer by canton7 from the comments:

The Add null check is not generated by any code snippet. Rather it is based on the standard analyzer infrastructure (defined here and seen here). This means it is just a semantically generated if statement with a throw inside of it. Then based on your .editorconfig (or defaults if not specified) it formates the code accordingly.

One is left with the following options:

  • Edit the .editorconfig file to make the code format as pleased. Note that this approach applies globally.
  • Wait for the C# 11 !! syntax.
  • Write your own analyzer and code fix (tutorial).
  • Edit it by hand :).
Jan Kleprlík
  • 320
  • 3
  • 12