CommunityToolkit / CommunityToolkit/dotnet

Add analyzer to check Guard clauses used for non-arguments

Open
#1,106 0 comments 0 reactions 0 assignees View on GitHub
feature request :mailbox_with_mail:
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

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.