This visual studio extension adds a new diagnostic and fix for 'var' type declaration misuses with special rules.
In this case the variable's type is not obvious:
var shouldReload = list.Take(1);
After code fix variable declaraiton would look like:
IEnumerable<string> shouldReload = list.Take(1);
There is also a solution wide fixer available.
Which would diagnose all 'var' issues throught the entire solution and present a list of fixes for them.
Supported fixer scopes can be determined via GetSupportedFixAllScopes
method result list.
Available scopes:
- FixAllScope.Solution
- FixAllScope.Project
- FixAllScope.Document
There are only exclusion rules, meaning that provided cases below will be excluded, otherwise a 'var' declaration will be replaced with an actual type. Please follow this example on how 'ShouldBeExplicit' method works:
ShouldBeExplicit(string variableDeclaration, string typeDeclaration)
variableDeclaration: var shouldReload = list.Take(1);
typeDeclaration: IEnumerable<string>
variableDeclaration.StartsWith(typeDeclaration)
variableDeclaration.Contains($"<{typeDeclaration}>")
variableDeclaration.Contains($"new {typeDeclaration}")
variableDeclaration.Contains($" ({typeDeclaration})")
typeDeclaration.Contains("<anonymous type:")
variableDeclaration.Contains($"as {typeDeclaration}")
This section explains how a code diagnostic extension and fixer are working inside Visual Studio.
- Visual Studio loads the extension and scans open documents with
VariableCodeAnalyzer
- Diagnostic information will be displayed in Error List window. (with Information severity)
- You press Alt + Enter or hover over a suggestion in Visual Studio
- Diagnostic object is retrieved from
CodeFixContext context
VariableCodeFixProvider
code fix executed, that is registered inRegisterCodeFixesAsync
methodASCodeFixService.ApplyDiagnosticFixAsync
is called in order to provide a code change (returns whole content of .cs with changed variable declaration)- You click on the solution or press enter in a popup window
- Variable declaration is replaced with an actual type
If you selected 'Solution' to fix all diagnosed issues throught the solution:
- Whole solution will be scanned using
VariableCodeAnalyzer
- For each problem there will be an instance of Diagnostic created
ASVariableFixAllProvider
code fix executed, that is registered inGetFixAsync
method- For each document (csharp file) in the solution a code fixer will be executed
ASCodeFixService.ApplyDiagnosticFixesAsync
- Code fixer will calculate required changes to the documents, based on the diagnostics list
- A modified solution instance will be created as a result of
ChangeSolution
method execution (with modified documents) - A window with a list of changes will be presented to you
There is a helpful tool, that allows you to see the syntax tree objects live.
Use it in order to figure out the code structure that needs changes.
You can find it in Visual Studio: View -> Syntax Visualizer
- Visual Studio custom extension
- FixAllProvider example
- Custom FixAllProvider
- Custom code cleanup fixer
- Custom code fix extension
- Code diagnostic
- Code analyzer
- C#