Skip to content

Applies Roslyn code fixes automatically during build

License

Notifications You must be signed in to change notification settings

kzu/AutoCodeFix

Repository files navigation

Icon AutoCodeFix

Version Downloads Build Status

Applies Roslyn code fixes automatically during build for the chosen "auto codefix" diagnostics, fixing the code for you automatically, instead of having to manually apply code fixes in the IDE.

In the project file, specify the diagnostic identifiers you want to apply code fixes automatically to. This example uses two diagnostics from the StyleCop.Analyzers package:

<ItemGroup>
    <PackageReference Include="StyleCop.Analyzers" Version="1.1.1-rc*" />
    <PackageReference Include="AutoCodeFix" Version="*" />
</ItemGroup>

<ItemGroup>
    <!-- System usings should go first --> 
    <AutoCodeFix Include="SA1208" />
    <!-- Field names should not begin with underscore -->
    <AutoCodeFix Include="SA1309" />
</ItemGroup>

The analyzers and code fixes available during build are the same used during design time in the IDE, added to the project via the <Analyzer Include="..." /> item group. Analyzers distributed via nuget packages already add those automatically to your project (such as the StyleCop.Analyzers , RefactoringEssentials, Roslynator.Analyzers and Roslynator.CodeFixes, etc).

It's important to note that by default, the compiler has to emit the diagnostics you want them fixed automatically. For diagnostics that are of Info severity by default (i.e. RCS1003: Add braces to if-else) you can bump its severity to Warning so that AutoCodeFix can properly process them automatically on the next build.

You configure analyzers using the built-in editor in VS, which for the example above, would result in a rule set like the following:

<RuleSet Name="MyRules" ToolsVersion="15.0">
  <Rules AnalyzerId="Roslynator.CSharp.Analyzers" RuleNamespace="Roslynator.CSharp.Analyzers">
    <Rule Id="RCS1003" Action="Warning" />
  </Rules>
</RuleSet>

With that configuration in place, you can add the AutoCodeFix just like before:

<ItemGroup>
    <AutoCodeFix Include="RCS1003" />
</ItemGroup>

When no fixable diagnostics are emitted by the compiler, the impact of AutoCodeFix on build times is virtually none.

NOTE: the main use case for AutoCodeFix is to fix the code as you go, on every build. Therefore, it performs best when warnings to fix are few and introduced in between builds. Although it can be used to apply fixes to entire code bases for normalization/compliance purposes as a one-time fixup, that can take some time, even if a Fix All provider exists for the diagnostics. Run time is also impacted by the complexity of the code fix itself. As an example, the StyleCop code fix for usings sorting can fix ~60 instances per second on my Dell XPS 13 9370.

Icon Gear by Putra Theoo, from The Noun Project

About

Applies Roslyn code fixes automatically during build

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published