dotnet icon indicating copy to clipboard operation
dotnet copied to clipboard

Add analyzer to check Guard clauses used for non-arguments

Open prlcutting opened this issue 7 months ago • 0 comments

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:

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

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

prlcutting avatar Jul 08 '25 18:07 prlcutting