Analyzer
SF0002
Flags PackageReference entries that contribute no detected symbol usage in C# source so dependency cost does not accumulate silently.
Diagnostic contract
Unused packages add restore time, transitive risk, and maintenance cost. The cleanest dependency is the one you do not carry.
A PackageReference maps to assemblies in the compilation, but no symbols from those assemblies are used in countable C# source files.
PackageReference {0} has no detected symbol usage in C# source. Remove the dependency or justify it with source usage.
/analyzers/sf0002/
When it fires
These are the concrete cases to look for in code review and IDE diagnostics.
- The analyzer reads PackageReference items from the project file and maps them to referenced assemblies.
- It scans countable source files for used symbols and treats packages with zero symbol usage as unused.
- The rule reports at compilation end so it can evaluate the full project graph instead of guessing per syntax node.
Bad / better example
Use these examples to explain the rule, not just silence it.
Before
<ItemGroup>
<PackageReference Include="Serilog" Version="4.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.0" />
</ItemGroup> If no Serilog symbols are used in the project, this reference is dead weight.
After
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.0" />
</ItemGroup> The project keeps only the package that contributes real code usage.
Code fix
The code fix removes the targeted PackageReference node from the project file without rewriting unrelated XML.
- Run a normal build after applying the fix so any non-symbol usage contracts become obvious.
- Analyzer-only, build-only, or source-generator dependencies may need a deliberate justification instead of a blind delete.
- This is the second in-repo diagnostic with an automatic code fix.
Source and follow-up links
Use these links when you need to validate behavior against the source or connect the docs back to project tracking.
Analyzer implementation
UnusedDependencyAnalyzer.cs
Code fixImplementation details
UnusedDependencyCodeFixProvider.cs
TrackingIssue #55
Documentation tracker for the analyzer pages and related integration guidance.
Guidecsproj reference
Keep analyzer installation isolated while pruning package sprawl in application projects.
FilterHalfRule
Unused dependencies are a direct HalfRule smell.