Coder Social home page Coder Social logo

csharpfunctionalextensions.analyzers's Introduction

Nuget

CSharpFunctionalExtensions.Analyzers

Unofficial code analyzers for CSharpFunctionalExtensions. This project aims to provide code analyzers to identify potential issues or misuse of the Result object from the CSharpFunctionalExtensions library.

Features

  • Identifies scenarios where the IsSuccess property is not checked before accessing the Value.
  • Verifies that logic is terminated when IsFailure is true before before accessing the Value.
  • Supports a variety of control flow structures, including if statements, ternary operators, and now C# switch expressions.

Getting Started

To install the analyzers, you can add the NuGet package to your project:

Install-Package CSharpFunctionalExtensions.Analyzers

Examples

Here are some examples to show what this analyzer can catch.

Example 1: Accessing Value without checking IsSuccess

public void DoSomething(Result<int> result)
{
    var x = result.Value;  // Analyzer will report a warning here
}

Example 2: Using IsSuccess in Switch Expressions

public IActionResult ProcessResult(Result<int> result)
{
    return result switch
    {
        { Error: var err } when err == Error.NotFound => NotFound(),
        _ => Ok(result.Value)  // Analyzer will report a warning here
    };
}

Example 3: Not breaking out when IsFailure is true

public void DoSomething(Result<int> result)
{
    if (result.IsFailure)
    {
        // logic here
    }
    var x= result.Value; // Analyzer will report a warning here
}

Will be fixed if you return ;

public void DoSomething(Result<int> result)
{
   if (result.IsFailure)
   {
       return
   }
   var x= result.Value; // Analyzer will report a warning here
}

Changelog

See changelog

Contributing

Contributions are welcome! Please feel free to open an issue or submit a pull request.

License

MIT

csharpfunctionalextensions.analyzers's People

Contributors

almaraubel avatar dependabot[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

csharpfunctionalextensions.analyzers's Issues

False postive warning of accessing value without checking with ternary operator

When you use the ternary operator like string resultMessage = result.IsSuccess ? $"{result.Value} success." : "Failed."; you get warning CFE0001 "Accessing Value without checking IsSuccess or IsFailure can result in an unexpected Errors". This is incorrect because the ternary operator is checking the value. Converting the ternary operator into a if else statement removes the warning.

Pattern matching

Checks on isSuccess with pattern matching are not recognized as correct check for isSuccess

 return result switch
        {
            { IsSuccess: true } => Ok(result.Value),
            { Error: var err } when err == Foo.Error.NotFound
                => NotFound(),
            _ => InternalServerError(result.Error.UserFriendlyName)
        };

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.