Coder Social home page Coder Social logo

meziantou / meziantou.analyzer Goto Github PK

View Code? Open in Web Editor NEW
844.0 844.0 46.0 2.28 MB

A Roslyn analyzer to enforce some good practices in C#.

License: MIT License

C# 99.91% PowerShell 0.09% Batchfile 0.01%
analyzer csharp dotnet hacktoberfest roslyn roslyn-analyzer static-analysis vsix

meziantou.analyzer's Introduction

Hi there 👋

My name is Gérald Barré (@meziantou). I'm an experienced software developer with more than 10 years of experience in the industry. I currently work as a Software Engineer, where I'm part of a team that is responsible for developing and maintaining tools to help developers adopt InnerSource and increase code sharing in the company.

I have a strong background in C#, .NET, and web development, as well as a deep understanding of software architecture and design patterns. I'm also proficient in many other technologies, such as SQL Server, PowerShell, Docker, and Azure. I'm an expert at writing clean code that is easy to maintain and understand. I'm passionate about building high-quality software solutions and take pride in my work. I'm always eager to learn new technologies and love to experiment with different technologies to find the best solution for each problem.

In addition to my professional work, I'm highly involved in the tech community. I'm a recognized Microsoft MVP for my contributions to the developer community, which include speaking at tech events, writing technical blog posts and contributing to open-source. I'm also a maintainer of various open-source projects, such as Meziantou.Analyzer or Meziantou.Framework. I'm passionate about sharing my knowledge and expertise with others through my blog, which can be found at https://www.meziantou.net.

Linkedin Badge Twitter Badge

📗 Recent blog posts

Checkout out my complete list of blog entries!

💹 Stats

Github stats

meziantou.analyzer's People

Contributors

abatishchev avatar davidelettieri avatar dependabot-preview[bot] avatar dependabot[bot] avatar dittodhole avatar drieseng avatar exg906 avatar f3 avatar jackalvrus avatar jannesrsa avatar jontdelorme avatar juchom avatar k3min avatar louis-z avatar meziantou avatar micke3rd avatar mokarchi avatar pretasoc avatar quixoticaxis avatar renovate[bot] avatar rsking avatar xtqqczze avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

meziantou.analyzer's Issues

New rule: The length returned from Stream.Read should be used

public void DoSomething(string fileName)
{
  using (var stream = File.Open(fileName, FileMode.Open))
  {
    var result = new byte[stream.Length];
    stream.Read(result, 0, (int)stream.Length); // Noncompliant, should be readLength = stream.Read(...) and readLength should be used
  }
}

Analyzer 'Meziantou.Analyzer.Rules.ReturnTaskFromResultInsteadOfReturningNullAnalyzer' threw an exception of type 'System.NullReferenceException'

SyntaxNode: ref coll.FindAsync(filter) [LocalFunctionStatementSyntax]@[6688..6714) (172,12)-(172,38)

System.NullReferenceException: Object reference not set to an instance of an object.
   at Meziantou.Analyzer.Rules.ReturnTaskFromResultInsteadOfReturningNullAnalyzer.IsTaskType(Compilation compilation, ITypeSymbol typeSyntax)
   at Meziantou.Analyzer.Rules.ReturnTaskFromResultInsteadOfReturningNullAnalyzer.AnalyzeLocalFunction(SyntaxNodeAnalysisContext context)
   at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.<>c__44`1.<ExecuteSyntaxNodeAction>b__44_0(ValueTuple`2 data)
   at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.ExecuteAndCatchIfThrows_NoLock[TArg](DiagnosticAnalyzer analyzer, Action`1 analyze, TArg argument, Nullable`1 info)

New rule: Constructors should only call non-overridable methods

Calling an overridable method from a constructor could result in failures or strange behaviors when instantiating a subclass which overrides the method.

public class Foo
{
  public Foo()
  {
    VirtualMethod();  // Noncompliant
  }

  public virtual void VirtualMethod()
  {
    ...
  }
}

Skip the validation if the class is sealed

New rule: `ConstructorArgument` parameters should exist in constructors

using System;

namespace myLibrary
{
  public class MyExtension : MarkupExtension
  {
    public MyExtension() { }

    public MyExtension(object value1)
    {
      Value1 = value1;
    }

    [ConstructorArgument("value2")] // Error, value2 is not a parameter of the constructor
    public object Value1 { get; set; }
  }
}

New rule: Use task result instead of task itself

interface Repository 
{
    Task FindProduct(string name);
}
public async Task<int> GetId(string name)
{
    await Task.Yield(); //Some stuff

    var product = _repository.FindProduct(name);

    return product.Id;
}

Should be

public async Task<int> GetId(string name)
{
    await Task.Yield(); //Some stuff

    var product = await _repository.FindProduct(name);

    return product.Id; // Or product.Result.Id
}

This one is hard to see/debug 😖😭😆

Analyzer 'Meziantou.Analyzer.Rules.DoNotCallVirtualMethodInConstructorAnalyzer' threw an exception of type 'System.InvalidOperationException' with message 'Sequence contains more than one element'.

Analyzer 'Meziantou.Analyzer.Rules.DoNotCallVirtualMethodInConstructorAnalyzer' threw the following exception:'
Exception occurred with following context:
Compilation: xxx
SyntaxTree: D:\xxx\QueryResultSchema.cs
SyntaxNode: public QueryResultSchema(string ... [ConstructorDeclarationSyntax]@[480..866) (19,8)-(29,9)
System.InvalidOperationException: Sequence contains more than one element
   at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)
   at Meziantou.Analyzer.Rules.DoNotCallVirtualMethodInConstructorAnalyzer.AnalyzeConstructorSyntax(SyntaxNodeAnalysisContext context)
   at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.<>c__44`1.<ExecuteSyntaxNodeAction>b__44_0(ValueTuple`2 data)
   at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.ExecuteAndCatchIfThrows_NoLock[TArg](DiagnosticAnalyzer analyzer, Action`1 analyze, TArg argument, Nullable`1 info)-----'.
--

New rule: Local variables should not shadow class fields

It avoid problems such as

public class Sample
{
    private string field;
    public Sample(string field)
    {
        field = field; // Assign parameter instead of field
    }
}

BTW, it should also detect assignment of the same variable

var a = 10;
a = a; // Error

MA0001 should detect more methods

  • Enumerable.Contains<string>
  • Enumerable.Distinct<string>
  • Enumerable.Except<string>
  • Enumerable.GroupBy<string>
  • Enumerable.GroupJoin<string>
  • Enumerable.Intersect<string>
  • Enumerable.Join<string>
  • Enumerable.OrderBy<string>
  • Enumerable.OrderByDescending<string>
  • Enumerable.SequenceEqual<string>
  • Enumerable.ToDictionary<string>
  • Enumerable.ToLookup<string>
  • Enumerable.Union<string>

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.