CommunityToolkit / CommunityToolkit/dotnet
Add analyzer to check Guard clauses used for non-arguments
- Dominant language
- C#
- Stars
- 3.8k
- Forks
- 400
- PR merge metrics
- No merged PRs in 30d
Description
### Overview
I've found guard clauses being abused and used to validate non-arguments. It would be great to have an analyzer that checks for incorrect usage where one of the `Guard` clauses is used for something that is _not_ an argument to a method, property, etc.
Here is an example of something that I would like the analyzer to catch:
```csharp
class C
{
string P { get; }
void M(string s)
{
Guard.IsNotNull(P); // <-- P is not an argument!
}
}
```
### API breakdown
N/A for an analyzer.
### Usage example
```csharp
class C
{
string P { get; }
void M(string s)
{
Guard.IsNotNull(P); // <-- The analyzer should report a warning, squiggle, etc.
}
}
```
### Breaking change?
No
### Alternatives
Anyone could write an analyzer to do this outside of the toolkit, but it would obviously be best and most natural to have it included as part of the toolkit itself.
### Additional context
This is predicated on my understanding that the `Guard` clauses provided by the toolkit are specifically intended for arguments.
### Help us help you
No, just wanted to propose this
Contributor guide
Assessment
This issue has not been assessed yet.