Git Product home page Git Product logo

meziantou.analyzer's Introduction

Meziantou.Analyzer

Meziantou.Analyzer on NuGet Meziantou.Analyzer on NuGet

A Roslyn analyzer to enforce some good practices in C# in terms of design, usage, security, performance, and style.

Installation

Install the NuGet package https://www.nuget.org/packages/Meziantou.Analyzer/

Rules

If you are already using other analyzers, you can check which rules are duplicated with well-known analyzers

Id Category Description Severity Is enabled Code fix
MA0001 Usage StringComparison is missing ℹ️ ✔️ ✔️
MA0002 Usage IEqualityComparer<string> or IComparer<string> is missing ⚠️ ✔️ ✔️
MA0003 Style Add parameter name to improve readability ℹ️ ✔️ ✔️
MA0004 Usage Use Task.ConfigureAwait ⚠️ ✔️ ✔️
MA0005 Performance Use Array.Empty<T>() ⚠️ ✔️ ✔️
MA0006 Usage Use String.Equals instead of equality operator ⚠️ ✔️ ✔️
MA0007 Style Add a comma after the last value ℹ️ ✔️ ✔️
MA0008 Performance Add StructLayoutAttribute ⚠️ ✔️ ✔️
MA0009 Security Add regex evaluation timeout ⚠️ ✔️
MA0010 Design Mark attributes with AttributeUsageAttribute ⚠️ ✔️ ✔️
MA0011 Usage IFormatProvider is missing ⚠️ ✔️
MA0012 Design Do not raise reserved exception type ⚠️ ✔️
MA0013 Design Types should not extend System.ApplicationException ⚠️ ✔️
MA0014 Design Do not raise System.ApplicationException type ⚠️ ✔️
MA0015 Usage Specify the parameter name in ArgumentException ⚠️ ✔️
MA0016 Design Prefer using collection abstraction instead of implementation ⚠️ ✔️
MA0017 Design Abstract types should not have public or internal constructors ⚠️ ✔️ ✔️
MA0018 Design Do not declare static members on generic types (deprecated; use CA1000 instead) ℹ️ ✔️
MA0019 Usage Use EventArgs.Empty ⚠️ ✔️ ✔️
MA0020 Performance Use direct methods instead of LINQ methods ℹ️ ✔️ ✔️
MA0021 Usage Use StringComparer.GetHashCode instead of string.GetHashCode ⚠️ ✔️ ✔️
MA0022 Design Return Task.FromResult instead of returning null ⚠️ ✔️ ✔️
MA0023 Performance Add RegexOptions.ExplicitCapture ⚠️ ✔️
MA0024 Usage Use an explicit StringComparer when possible ⚠️ ✔️ ✔️
MA0025 Design Implement the functionality instead of throwing NotImplementedException ⚠️ ✔️
MA0026 Design Fix TODO comment ⚠️ ✔️
MA0027 Usage Prefer rethrowing an exception implicitly ⚠️ ✔️ ✔️
MA0028 Performance Optimize StringBuilder usage ℹ️ ✔️ ✔️
MA0029 Performance Combine LINQ methods ℹ️ ✔️ ✔️
MA0030 Performance Remove useless OrderBy call ⚠️ ✔️ ✔️
MA0031 Performance Optimize Enumerable.Count() usage ℹ️ ✔️ ✔️
MA0032 Usage Use an overload with a CancellationToken argument ℹ️
MA0033 Design Do not tag instance fields with ThreadStaticAttribute ⚠️ ✔️
MA0035 Usage Do not use dangerous threading methods ⚠️ ✔️
MA0036 Design Make class static ℹ️ ✔️ ✔️
MA0037 Usage Remove empty statement ✔️ ✔️
MA0038 Design Make method static (deprecated, use CA1822 instead) ℹ️ ✔️ ✔️
MA0039 Security Do not write your own certificate validation method ✔️
MA0040 Usage Forward the CancellationToken parameter to methods that take one ℹ️ ✔️ ✔️
MA0041 Design Make property static (deprecated, use CA1822 instead) ℹ️ ✔️ ✔️
MA0042 Design Do not use blocking calls in an async method ℹ️ ✔️ ✔️
MA0043 Usage Use nameof operator in ArgumentException ℹ️ ✔️ ✔️
MA0044 Performance Remove useless ToString call ℹ️ ✔️ ✔️
MA0045 Design Do not use blocking calls in a sync method (need to make calling method async) ℹ️ ✔️
MA0046 Design Use EventHandler<T> to declare events ⚠️ ✔️
MA0047 Design Declare types in namespaces ⚠️ ✔️
MA0048 Design File name must match type name ⚠️ ✔️
MA0049 Design Type name should not match containing namespace ✔️
MA0050 Design Validate arguments correctly in iterator methods ℹ️ ✔️ ✔️
MA0051 Design Method is too long ⚠️ ✔️
MA0052 Performance Replace constant Enum.ToString with nameof ℹ️ ✔️ ✔️
MA0053 Design Make class sealed ℹ️ ✔️ ✔️
MA0054 Design Embed the caught exception as innerException ⚠️ ✔️
MA0055 Design Do not use finalizer ⚠️ ✔️
MA0056 Design Do not call overridable members in constructor ⚠️ ✔️
MA0057 Naming Class name should end with 'Attribute' ℹ️ ✔️
MA0058 Naming Class name should end with 'Exception' ℹ️ ✔️
MA0059 Naming Class name should end with 'EventArgs' ℹ️ ✔️
MA0060 Design The value returned by Stream.Read/Stream.ReadAsync is not used ⚠️ ✔️
MA0061 Design Method overrides should not change default values ⚠️ ✔️ ✔️
MA0062 Design Non-flags enums should not be marked with "FlagsAttribute" ⚠️ ✔️
MA0063 Performance Use Where before OrderBy ℹ️ ✔️
MA0064 Design Avoid locking on publicly accessible instance ⚠️ ✔️
MA0065 Performance Default ValueType.Equals or HashCode is used for struct equality ⚠️ ✔️
MA0066 Performance Hash table unfriendly type is used in a hash table ⚠️ ✔️
MA0067 Design Use Guid.Empty ℹ️ ✔️ ✔️
MA0068 Design Invalid parameter name for nullable attribute ⚠️ ✔️
MA0069 Design Non-constant static fields should not be visible ⚠️ ✔️
MA0070 Design Obsolete attributes should include explanations ⚠️ ✔️
MA0071 Style Avoid using redundant else ℹ️ ✔️ ✔️
MA0072 Design Do not throw from a finally block ⚠️ ✔️
MA0073 Style Avoid comparison with bool constant ℹ️ ✔️ ✔️
MA0074 Usage Avoid implicit culture-sensitive methods ⚠️ ✔️ ✔️
MA0075 Design Do not use implicit culture-sensitive ToString ℹ️ ✔️
MA0076 Design Do not use implicit culture-sensitive ToString in interpolated strings ℹ️ ✔️
MA0077 Design A class that provides Equals(T) should implement IEquatable<T> ⚠️ ✔️ ✔️
MA0078 Performance Use 'Cast' instead of 'Select' to cast ℹ️ ✔️ ✔️
MA0079 Usage Forward the CancellationToken using .WithCancellation() ℹ️ ✔️ ✔️
MA0080 Usage Use a cancellation token using .WithCancellation() ℹ️
MA0081 Design Method overrides should not omit params keyword ⚠️ ✔️ ✔️
MA0082 Design NaN should not be used in comparisons ⚠️ ✔️
MA0083 Design ConstructorArgument parameters should exist in constructors ⚠️ ✔️
MA0084 Design Local variables should not hide other symbols ⚠️ ✔️
MA0085 Usage Anonymous delegates should not be used to unsubscribe from Events ⚠️ ✔️
MA0086 Design Do not throw from a finalizer ⚠️ ✔️
MA0087 Design Parameters with [DefaultParameterValue] attributes should also be marked [Optional] ⚠️ ✔️
MA0088 Design Use [DefaultParameterValue] instead of [DefaultValue] ⚠️ ✔️
MA0089 Performance Optimize string method usage ℹ️ ✔️ ✔️
MA0090 Design Remove empty else/finally block ℹ️ ✔️
MA0091 Usage Sender should be 'this' for instance events ⚠️ ✔️ ✔️
MA0092 Usage Sender should be 'null' for static events ⚠️ ✔️
MA0093 Usage EventArgs should not be null ⚠️ ✔️ ✔️
MA0094 Design A class that provides CompareTo(T) should implement IComparable<T> ⚠️ ✔️
MA0095 Design A class that implements IEquatable<T> should override Equals(object) ⚠️ ✔️
MA0096 Design A class that implements IComparable<T> should also implement IEquatable<T> ⚠️ ✔️
MA0097 Design A class that implements IComparable<T> or IComparable should override comparison operators ⚠️ ✔️
MA0098 Performance Use indexer instead of LINQ methods ℹ️ ✔️ ✔️
MA0099 Usage Use Explicit enum value instead of 0 ⚠️ ✔️
MA0100 Usage Await task before disposing of resources ⚠️ ✔️
MA0101 Usage String contains an implicit end of line character 👻 ✔️ ✔️
MA0102 Design Make member readonly ℹ️ ✔️ ✔️
MA0103 Usage Use SequenceEqual instead of equality operator ⚠️ ✔️ ✔️
MA0104 Design Do not create a type with a name from the BCL ⚠️
MA0105 Performance Use the lambda parameters instead of using a closure ℹ️ ✔️
MA0106 Performance Avoid closure by using an overload with the 'factoryArgument' parameter ℹ️ ✔️
MA0107 Design Do not use culture-sensitive object.ToString ℹ️
MA0108 Usage Remove redundant argument value ℹ️ ✔️ ✔️
MA0109 Design Consider adding an overload with a Span<T> or Memory<T> ℹ️
MA0110 Performance Use the Regex source generator ℹ️ ✔️ ✔️
MA0111 Performance Use string.Create instead of FormattableString ℹ️ ✔️ ✔️
MA0112 Performance Use 'Count > 0' instead of 'Any()' ℹ️
MA0113 Design Use DateTime.UnixEpoch ℹ️ ✔️ ✔️
MA0114 Design Use DateTimeOffset.UnixEpoch ℹ️ ✔️ ✔️
MA0115 Usage Unknown component parameter ⚠️ ✔️
MA0116 Design Parameters with [SupplyParameterFromQuery] attributes should also be marked as [Parameter] ⚠️ ✔️ ✔️
MA0117 Design Parameters with [EditorRequired] attributes should also be marked as [Parameter] ⚠️ ✔️ ✔️
MA0118 Design [JSInvokable] methods must be public ⚠️ ✔️
MA0119 Design JSRuntime must not be used in OnInitialized or OnInitializedAsync ⚠️ ✔️
MA0120 Performance Use InvokeVoidAsync when the returned value is not used ℹ️ ✔️ ✔️
MA0121 Design Do not overwrite parameter value ℹ️
MA0122 Design Parameters with [SupplyParameterFromQuery] attributes are only valid in routable components (@page) ℹ️ ✔️
MA0123 Design Sequence number must be a constant ⚠️ ✔️
MA0124 Design Log parameter type is not valid ⚠️ ✔️
MA0125 Design The list of log parameter types contains an invalid type ⚠️ ✔️
MA0126 Design The list of log parameter types contains a duplicate ⚠️ ✔️
MA0127 Usage Use String.Equals instead of is pattern ⚠️
MA0128 Usage Use 'is' operator instead of SequenceEqual ℹ️ ✔️ ✔️
MA0129 Usage Await task in using statement ⚠️ ✔️
MA0130 Usage GetType() should not be used on System.Type instances ⚠️ ✔️
MA0131 Usage ArgumentNullException.ThrowIfNull should not be used with non-nullable types ⚠️ ✔️
MA0132 Design Do not convert implicitly to DateTimeOffset ⚠️ ✔️
MA0133 Design Use DateTimeOffset instead of relying on the implicit conversion ℹ️ ✔️
MA0134 Usage Observe result of async calls ⚠️ ✔️
MA0135 Design The log parameter has no configured type ⚠️
MA0136 Usage Raw String contains an implicit end of line character 👻 ✔️
MA0137 Design Use 'Async' suffix when a method returns an awaitable type ⚠️
MA0138 Design Do not use 'Async' suffix when a method does not return an awaitable type ⚠️
MA0139 Design Log parameter type is not valid ⚠️ ✔️
MA0140 Design Both if and else branch have identical code ⚠️ ✔️
MA0141 Usage Use pattern matching instead of inequality operators for null check ℹ️ ✔️
MA0142 Usage Use pattern matching instead of equality operators for null check ℹ️ ✔️
MA0143 Design Primary constructor parameters should be readonly ⚠️ ✔️
MA0144 Performance Use System.OperatingSystem to check the current OS ⚠️ ✔️
MA0145 Usage Signature for [UnsafeAccessorAttribute] method is not valid ⚠️ ✔️
MA0146 Usage Name must be set explicitly on local functions ⚠️ ✔️
MA0147 Usage Avoid async void method for delegate ⚠️ ✔️
MA0148 Usage Use pattern matching instead of equality operators for discrete value ℹ️ ✔️
MA0149 Usage Use pattern matching instead of inequality operators for discrete value ℹ️ ✔️
MA0150 Design Do not call the default object.ToString explicitly ⚠️ ✔️
MA0151 Usage DebuggerDisplay must contain valid members ⚠️ ✔️
MA0152 Performance Use Unwrap instead of using await twice ℹ️ ✔️
MA0153 Design Do not log symbols decorated with DataClassificationAttribute directly ⚠️ ✔️
MA0154 Design Use langword in XML comment ℹ️ ✔️ ✔️
MA0155 Design Do not use async void methods ⚠️
MA0156 Design Use 'Async' suffix when a method returns IAsyncEnumerable<T> ⚠️
MA0157 Design Do not use 'Async' suffix when a method does not return IAsyncEnumerable<T> ⚠️
MA0158 Performance Use System.Threading.Lock ⚠️ ✔️
MA0159 Performance Use 'Order' instead of 'OrderBy' ℹ️ ✔️ ✔️
MA0160 Performance Use ContainsKey instead of TryGetValue ℹ️ ✔️

Suppressions

Id Suppressed rule Justification
MAS0001 CA1822 Suppress CA1822 on methods decorated with BenchmarkDotNet attributes.
MAS0002 CA1822 Suppress CA1822 on methods decorated with a System.Text.Json attribute such as [JsonPropertyName] or [JsonInclude].

meziantou.analyzer's People

Contributors

abatishchev avatar davidelettieri avatar dependabot-preview[bot] avatar dependabot[bot] avatar domasm 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

meziantou.analyzer's Issues

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)-----'.
--

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

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.