Skip to content

Suggest named arguments: a Roslyn code analyzer and code-fix provider for C#

License

Notifications You must be signed in to change notification settings

mykolav/use-named-args-fs

Repository files navigation

Suggest method calls that can benefit from using named arguments

Build status

This project contains a Roslyn code analyzer and an accompanying code-fix provider that suggest using named arguments when calling a method having successive parameters of the same type.

The UseNamedArgs analyzer in action

How to use it?

Just install the nuget package. The analyzer is going to look for method invocations that can benefit from named arguments across the project.

public static void IntroduceCharacter(string name, string powerLevel) {}

// Elsewhere in your code:
// if `IntroduceCharacter` method is called with positional arguments,
// the analyzer emits a warning, as the the method has two parameters 
// of the same type following one another.
IntroduceCharacter(name: "Goku", powerLevel: "Over 9000!");

Supported method kinds

The analyzer supports suggesting named arguments for the following method kinds

  • Regular instance and static methods
  • Extension methods
  • Regular constructors
  • Attribute constructors
  • Primary constructors

Download and install

Install the UseNamedArgs nuget package. For example, run the following command in the NuGet Package Manager Console.

Install-Package UseNamedArgs

This will download all the binaries, and add necessary analyzer references to your project.

Configuration

Starting in Visual Studio 2019 version 16.3, you can configure the severity of analyzer rules, or diagnostics, in an EditorConfig file, from the light bulb menu, and the error list.

You can add the following to the [*.cs] section of your .editorconfig.

[*.cs]
dotnet_diagnostic.UseNamedArgs.severity = suggestion

The possible severity values are:

  • error
  • warning
  • suggestion
  • silent
  • none
  • default (in case of this analyzer, it's equal to warning)

Please take a look at the documentation for a detailed description.

Thank you!

License

The analyzer and code-fix provider are licensed under the MIT license.

About

Suggest named arguments: a Roslyn code analyzer and code-fix provider for C#

Topics

Resources

License

Stars

Watchers

Forks