Coder Social home page Coder Social logo

meziantou / meziantou.analyzer Goto Github PK

View Code? Open in Web Editor NEW
852.0 852.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 Issues

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>

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: 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

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: 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: `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; }
  }
}

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.