This is a C# code analyzer to warn you about unused variables.
You can add it to your project, run dotnet build and see the warnings.
This project was created specifically because the default behavior of the compiler skips over unused variables.
You might say that that's not true, that you've seen unused variable warnings before.
Yes, it exists, but unfortunately it's pretty limited. Specifically, the right hand side must be a compile time constant.
So:
public void Foo()
{
var x = "unused"; // this is detected
var y = new System.Text.StringBuilder(); // this is NOT detected
}So basically it only catches the simplest case. In other words: it's useless.
This project is meant to fix this.
There are 3 ways of installing and using this analyzer, depending on what type of project you're working on:
- the simple, single project one
- central package management with
.propsfiles - vendor in the package and link it with
ProjectRefence
choose the approach that suits you best.
This project is on NuGet, so you can add it through dotnet CLI:
dotnet add package MissingAnalyzersor add this to your .csproj file:
<ItemGroup>
<PackageReference Include="MissingAnalyzers" Version="0.6.0" />
</ItemGroup>If you're using "central package version management"
you can add this to the Directory.Packages.props file
<ItemGroup>
<PackageVersion Include="MissingAnalyzers" Version="0.6.0" />
</ItemGroup>and this to Directory.Build.props file
<ItemGroup>
<PackageReference Include="MissingAnalyzers" />
</ItemGroup>You can clone this project, modify it if you need to, and use it as a project reference locally. To do this, first clone the repo
git pull https://github.com/bartoszluka/cs-analyzers MissingAnalyzersthen add it to the .csproj:
<ItemGroup>
<ProjectReference
Include="/path/to/MissingAnalyzers/MissingAnalyzers.csproj"
OutputItemType="Analyzer"
ReferenceOutputAssembly="false"
/>
</ItemGroup>- catch unused variables and report a compiler warning
- publish the package on NuGet
- fill
AnalyzerReleases.Shipped.md(whatever those are) - maybe support versions older than
net8.0?netstandard2.0supported - fix a bug with shadowing names in lambdas
- fix support for top-level statements
- fix a bug in identity lambdas
x => x - fix a bug where the explicitly ignored parameter
_gets reported - add tests
- add another analyzer if I see another problem