Skip to content

Add analyzer to check Guard clauses used for non-arguments #1106

@prlcutting

Description

@prlcutting

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature request 📬A request for new changes to improve functionality

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions