Home

Awesome

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

<!-- rules -->
IdCategoryDescriptionSeverityIs enabledCode fix
MA0001UsageStringComparison is missingℹ️✔️✔️
MA0002UsageIEqualityComparer<string> or IComparer<string> is missing⚠️✔️✔️
MA0003StyleAdd parameter name to improve readabilityℹ️✔️✔️
MA0004UsageUse Task.ConfigureAwait⚠️✔️✔️
MA0005PerformanceUse Array.Empty<T>()⚠️✔️✔️
MA0006UsageUse String.Equals instead of equality operator⚠️✔️✔️
MA0007StyleAdd a comma after the last valueℹ️✔️✔️
MA0008PerformanceAdd StructLayoutAttribute⚠️✔️✔️
MA0009SecurityAdd regex evaluation timeout⚠️✔️
MA0010DesignMark attributes with AttributeUsageAttribute⚠️✔️✔️
MA0011UsageIFormatProvider is missing⚠️✔️
MA0012DesignDo not raise reserved exception type⚠️✔️
MA0013DesignTypes should not extend System.ApplicationException⚠️✔️
MA0014DesignDo not raise System.ApplicationException type⚠️✔️
MA0015UsageSpecify the parameter name in ArgumentException⚠️✔️
MA0016DesignPrefer using collection abstraction instead of implementation⚠️✔️
MA0017DesignAbstract types should not have public or internal constructors⚠️✔️✔️
MA0018DesignDo not declare static members on generic types (deprecated; use CA1000 instead)ℹ️✔️
MA0019UsageUse EventArgs.Empty⚠️✔️✔️
MA0020PerformanceUse direct methods instead of LINQ methodsℹ️✔️✔️
MA0021UsageUse StringComparer.GetHashCode instead of string.GetHashCode⚠️✔️✔️
MA0022DesignReturn Task.FromResult instead of returning null⚠️✔️✔️
MA0023PerformanceAdd RegexOptions.ExplicitCapture⚠️✔️
MA0024UsageUse an explicit StringComparer when possible⚠️✔️✔️
MA0025DesignImplement the functionality instead of throwing NotImplementedException⚠️✔️
MA0026DesignFix TODO comment⚠️✔️
MA0027UsagePrefer rethrowing an exception implicitly⚠️✔️✔️
MA0028PerformanceOptimize StringBuilder usageℹ️✔️✔️
MA0029PerformanceCombine LINQ methodsℹ️✔️✔️
MA0030PerformanceRemove useless OrderBy call⚠️✔️✔️
MA0031PerformanceOptimize Enumerable.Count() usageℹ️✔️✔️
MA0032UsageUse an overload with a CancellationToken argumentℹ️
MA0033DesignDo not tag instance fields with ThreadStaticAttribute⚠️✔️
MA0035UsageDo not use dangerous threading methods⚠️✔️
MA0036DesignMake class staticℹ️✔️✔️
MA0037UsageRemove empty statement✔️✔️
MA0038DesignMake method static (deprecated, use CA1822 instead)ℹ️✔️✔️
MA0039SecurityDo not write your own certificate validation method✔️
MA0040UsageForward the CancellationToken parameter to methods that take oneℹ️✔️✔️
MA0041DesignMake property static (deprecated, use CA1822 instead)ℹ️✔️✔️
MA0042DesignDo not use blocking calls in an async methodℹ️✔️✔️
MA0043UsageUse nameof operator in ArgumentExceptionℹ️✔️✔️
MA0044PerformanceRemove useless ToString callℹ️✔️✔️
MA0045DesignDo not use blocking calls in a sync method (need to make calling method async)ℹ️✔️
MA0046DesignUse EventHandler<T> to declare events⚠️✔️
MA0047DesignDeclare types in namespaces⚠️✔️
MA0048DesignFile name must match type name⚠️✔️
MA0049DesignType name should not match containing namespace✔️
MA0050DesignValidate arguments correctly in iterator methodsℹ️✔️✔️
MA0051DesignMethod is too long⚠️✔️
MA0052PerformanceReplace constant Enum.ToString with nameofℹ️✔️✔️
MA0053DesignMake class sealedℹ️✔️✔️
MA0054DesignEmbed the caught exception as innerException⚠️✔️
MA0055DesignDo not use finalizer⚠️✔️
MA0056DesignDo not call overridable members in constructor⚠️✔️
MA0057NamingClass name should end with 'Attribute'ℹ️✔️
MA0058NamingClass name should end with 'Exception'ℹ️✔️
MA0059NamingClass name should end with 'EventArgs'ℹ️✔️
MA0060DesignThe value returned by Stream.Read/Stream.ReadAsync is not used⚠️✔️
MA0061DesignMethod overrides should not change default values⚠️✔️✔️
MA0062DesignNon-flags enums should not be marked with "FlagsAttribute"⚠️✔️
MA0063PerformanceUse Where before OrderByℹ️✔️
MA0064DesignAvoid locking on publicly accessible instance⚠️✔️
MA0065PerformanceDefault ValueType.Equals or HashCode is used for struct equality⚠️✔️
MA0066PerformanceHash table unfriendly type is used in a hash table⚠️✔️
MA0067DesignUse Guid.Emptyℹ️✔️✔️
MA0068DesignInvalid parameter name for nullable attribute⚠️✔️
MA0069DesignNon-constant static fields should not be visible⚠️✔️
MA0070DesignObsolete attributes should include explanations⚠️✔️
MA0071StyleAvoid using redundant elseℹ️✔️✔️
MA0072DesignDo not throw from a finally block⚠️✔️
MA0073StyleAvoid comparison with bool constantℹ️✔️✔️
MA0074UsageAvoid implicit culture-sensitive methods⚠️✔️✔️
MA0075DesignDo not use implicit culture-sensitive ToStringℹ️✔️
MA0076DesignDo not use implicit culture-sensitive ToString in interpolated stringsℹ️✔️
MA0077DesignA class that provides Equals(T) should implement IEquatable<T>⚠️✔️✔️
MA0078PerformanceUse 'Cast' instead of 'Select' to castℹ️✔️✔️
MA0079UsageForward the CancellationToken using .WithCancellation()ℹ️✔️✔️
MA0080UsageUse a cancellation token using .WithCancellation()ℹ️
MA0081DesignMethod overrides should not omit params keyword⚠️✔️✔️
MA0082DesignNaN should not be used in comparisons⚠️✔️
MA0083DesignConstructorArgument parameters should exist in constructors⚠️✔️
MA0084DesignLocal variables should not hide other symbols⚠️✔️
MA0085UsageAnonymous delegates should not be used to unsubscribe from Events⚠️✔️
MA0086DesignDo not throw from a finalizer⚠️✔️
MA0087DesignParameters with [DefaultParameterValue] attributes should also be marked [Optional]⚠️✔️
MA0088DesignUse [DefaultParameterValue] instead of [DefaultValue]⚠️✔️
MA0089PerformanceOptimize string method usageℹ️✔️✔️
MA0090DesignRemove empty else/finally blockℹ️✔️
MA0091UsageSender should be 'this' for instance events⚠️✔️✔️
MA0092UsageSender should be 'null' for static events⚠️✔️
MA0093UsageEventArgs should not be null⚠️✔️✔️
MA0094DesignA class that provides CompareTo(T) should implement IComparable<T>⚠️✔️
MA0095DesignA class that implements IEquatable<T> should override Equals(object)⚠️✔️
MA0096DesignA class that implements IComparable<T> should also implement IEquatable<T>⚠️✔️
MA0097DesignA class that implements IComparable<T> or IComparable should override comparison operators⚠️✔️
MA0098PerformanceUse indexer instead of LINQ methodsℹ️✔️✔️
MA0099UsageUse Explicit enum value instead of 0⚠️✔️
MA0100UsageAwait task before disposing of resources⚠️✔️
MA0101UsageString contains an implicit end of line character👻✔️✔️
MA0102DesignMake member readonlyℹ️✔️✔️
MA0103UsageUse SequenceEqual instead of equality operator⚠️✔️✔️
MA0104DesignDo not create a type with a name from the BCL⚠️
MA0105PerformanceUse the lambda parameters instead of using a closureℹ️✔️
MA0106PerformanceAvoid closure by using an overload with the 'factoryArgument' parameterℹ️✔️
MA0107DesignDo not use culture-sensitive object.ToStringℹ️
MA0108UsageRemove redundant argument valueℹ️✔️✔️
MA0109DesignConsider adding an overload with a Span<T> or Memory<T>ℹ️
MA0110PerformanceUse the Regex source generatorℹ️✔️✔️
MA0111PerformanceUse string.Create instead of FormattableStringℹ️✔️✔️
MA0112PerformanceUse 'Count > 0' instead of 'Any()'ℹ️
MA0113DesignUse DateTime.UnixEpochℹ️✔️✔️
MA0114DesignUse DateTimeOffset.UnixEpochℹ️✔️✔️
MA0115UsageUnknown component parameter⚠️✔️
MA0116DesignParameters with [SupplyParameterFromQuery] attributes should also be marked as [Parameter]⚠️✔️✔️
MA0117DesignParameters with [EditorRequired] attributes should also be marked as [Parameter]⚠️✔️✔️
MA0118Design[JSInvokable] methods must be public⚠️✔️
MA0119DesignJSRuntime must not be used in OnInitialized or OnInitializedAsync⚠️✔️
MA0120PerformanceUse InvokeVoidAsync when the returned value is not usedℹ️✔️✔️
MA0121DesignDo not overwrite parameter valueℹ️
MA0122DesignParameters with [SupplyParameterFromQuery] attributes are only valid in routable components (@page)ℹ️✔️
MA0123DesignSequence number must be a constant⚠️✔️
MA0124DesignLog parameter type is not valid⚠️✔️
MA0125DesignThe list of log parameter types contains an invalid type⚠️✔️
MA0126DesignThe list of log parameter types contains a duplicate⚠️✔️
MA0127UsageUse String.Equals instead of is pattern⚠️
MA0128UsageUse 'is' operator instead of SequenceEqualℹ️✔️✔️
MA0129UsageAwait task in using statement⚠️✔️
MA0130UsageGetType() should not be used on System.Type instances⚠️✔️
MA0131UsageArgumentNullException.ThrowIfNull should not be used with non-nullable types⚠️✔️
MA0132DesignDo not convert implicitly to DateTimeOffset⚠️✔️
MA0133DesignUse DateTimeOffset instead of relying on the implicit conversionℹ️✔️
MA0134UsageObserve result of async calls⚠️✔️
MA0135DesignThe log parameter has no configured type⚠️
MA0136UsageRaw String contains an implicit end of line character👻✔️
MA0137DesignUse 'Async' suffix when a method returns an awaitable type⚠️
MA0138DesignDo not use 'Async' suffix when a method does not return an awaitable type⚠️
MA0139DesignLog parameter type is not valid⚠️✔️
MA0140DesignBoth if and else branch have identical code⚠️✔️
MA0141UsageUse pattern matching instead of inequality operators for null checkℹ️✔️
MA0142UsageUse pattern matching instead of equality operators for null checkℹ️✔️
MA0143DesignPrimary constructor parameters should be readonly⚠️✔️
MA0144PerformanceUse System.OperatingSystem to check the current OS⚠️✔️
MA0145UsageSignature for [UnsafeAccessorAttribute] method is not valid⚠️✔️
MA0146UsageName must be set explicitly on local functions⚠️✔️
MA0147UsageAvoid async void method for delegate⚠️✔️
MA0148UsageUse pattern matching instead of equality operators for discrete valueℹ️✔️
MA0149UsageUse pattern matching instead of inequality operators for discrete valueℹ️✔️
MA0150DesignDo not call the default object.ToString explicitly⚠️✔️
MA0151UsageDebuggerDisplay must contain valid members⚠️✔️
MA0152PerformanceUse Unwrap instead of using await twiceℹ️✔️
MA0153DesignDo not log symbols decorated with DataClassificationAttribute directly⚠️✔️
MA0154DesignUse langword in XML commentℹ️✔️✔️
MA0155DesignDo not use async void methods⚠️
MA0156DesignUse 'Async' suffix when a method returns IAsyncEnumerable<T>⚠️
MA0157DesignDo not use 'Async' suffix when a method does not return IAsyncEnumerable<T>⚠️
MA0158PerformanceUse System.Threading.Lock⚠️✔️
MA0159PerformanceUse 'Order' instead of 'OrderBy'ℹ️✔️✔️
MA0160PerformanceUse ContainsKey instead of TryGetValueℹ️✔️
MA0161UsageUseShellExecute must be explicitly setℹ️
MA0162UsageUse Process.Start overload with ProcessStartInfoℹ️
MA0163UsageUseShellExecute must be false when redirecting standard input or output⚠️✔️
MA0164StyleUse parentheses to make not pattern clearer⚠️✔️✔️
MA0165UsageMake interpolated string👻✔️✔️
<!-- rules -->

Suppressions

<!-- suppressions -->
IdSuppressed ruleJustification
MAS0001CA1822Suppress CA1822 on methods decorated with BenchmarkDotNet attributes.
MAS0002CA1822Suppress CA1822 on methods decorated with a System.Text.Json attribute such as [JsonPropertyName] or [JsonInclude].
MAS0003IDE0058Suppress IDE0058 on well-known types
<!-- suppressions -->