Meziantou.Analyzer

July 8, 2026 · View on GitHub

Meziantou.Analyzer on NuGet Meziantou.Analyzer on NuGet

Meziantou.Analyzer is a powerful C# Roslyn analyzer that uses static analysis to detect bugs, surface security issues, and enforce best practices. It helps you catch real-world problems early—before they reach production—by analyzing your code as you write it and providing automated fixes when possible. Designed for both human developers and AI-assisted workflows, it acts as a safety net to keep your code reliable, consistent, and maintainable.

Installation

Install the NuGet package Meziantou.Analyzer

dotnet package add Meziantou.Analyzer

Rules

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

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, even when no token is available in scopeℹ️
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 when the calling method is asyncℹ️✔️✔️
MA0043UsageUse nameof operator in ArgumentExceptionℹ️✔️✔️
MA0044PerformanceRemove useless ToString callℹ️✔️✔️
MA0045DesignDo not use blocking calls, even when the calling method must become 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 or record 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 when raising an event⚠️✔️✔️
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 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⚠️✔️
MA0124DesignMicrosoft.Extensions.Logging 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⚠️✔️
MA0139DesignSerilog parameter type is not valid⚠️✔️
MA0140DesignBoth if and else branch have identical code⚠️✔️
MA0141UsageUse 'is not null' instead of '!= null'ℹ️✔️
MA0142UsageUse 'is null' instead of '== null'ℹ️✔️
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 'is' patterns instead of '==' for constant valuesℹ️✔️
MA0149UsageUse 'is not' patterns instead of '!=' for constant valuesℹ️✔️
MA0150DesignDo not call ToString() when the type falls back to object.ToString()⚠️✔️
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 returns 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⚠️✔️✔️
MA0166UsageForward the TimeProvider to methods that take oneℹ️✔️✔️
MA0167UsageUse an overload with a TimeProvider argumentℹ️
MA0168PerformanceUse readonly struct for in or ref readonly parameterℹ️
MA0169DesignUse Equals method instead of operator⚠️✔️✔️
MA0170DesignType cannot be used as an attribute argument⚠️
MA0171UsageUse 'is null' / 'is not null' instead of Nullable<T>.HasValueℹ️✔️
MA0172UsageBoth sides of the logical operation are identical⚠️
MA0173DesignUse LazyInitializer.EnsureInitializeℹ️✔️✔️
MA0174StyleRecord should use explicit 'class' keywordℹ️✔️
MA0175StyleRecord should not use explicit 'class' keywordℹ️✔️
MA0176PerformanceOptimize guid creationℹ️✔️✔️
MA0177StyleUse single-line XML comment syntax when possibleℹ️✔️
MA0178DesignUse TimeSpan.Zero instead of TimeSpan.FromXXX(0)ℹ️✔️✔️
MA0179PerformanceUse Attribute.IsDefined instead of GetCustomAttribute(s)ℹ️✔️✔️
MA0180DesignILogger type parameter should match containing type⚠️✔️
MA0181StyleDo not use castℹ️
MA0182DesignAvoid unused internal typesℹ️✔️✔️
MA0183UsageThe format string should use placeholders⚠️✔️
MA0184StyleDo not use interpolated string without parameters👻✔️✔️
MA0185PerformanceSimplify string.Create when all parameters are culture invariantℹ️✔️✔️
MA0186DesignEquals method should use [NotNullWhen(true)] on the parameterℹ️✔️
MA0187DesignUse constructor injection instead of [Inject] attributeℹ️✔️
MA0188DesignUse System.TimeProvider instead of a custom time abstractionℹ️✔️
MA0189DesignUse InlineArray instead of fixed-size buffersℹ️✔️✔️
MA0190DesignUse partial property instead of partial method for GeneratedRegexℹ️✔️✔️
MA0191DesignDo not use the null-forgiving operator⚠️
MA0192UsageUse HasFlag instead of bitwise checksℹ️✔️✔️
MA0193UsageUse an overload with a MidpointRounding argumentℹ️✔️✔️
MA0194UsageMerge is expressions on the same valueℹ️✔️✔️
MA0195UsageDo not use static fields before they are initialized⚠️✔️
MA0196DesignDo not use inheritdoc on non-inheriting members⚠️✔️
MA0197DesignAdd dedicated documentation on typesℹ️✔️
MA0198DesignSpecify cref for ambiguous inheritdoc on types⚠️✔️✔️
MA0199DesignDo not use inheritdoc on types without inheritance source⚠️✔️
MA0200UsageDo not use empty property patterns with non-nullable value typesℹ️✔️✔️
MA0201UsageDo not use zero-valued enum flags in flag checks⚠️✔️
MA0202DesignConditional compilation branches have identical code⚠️✔️✔️
MA0203DesignDo not use return tag for void method⚠️✔️
MA0204DesignRemove unnecessary partial modifierℹ️✔️✔️
MA0205StyleUse exclusive or operatorℹ️✔️✔️
MA0206StyleRemove unnecessary braces in type declarationℹ️✔️✔️
MA0207Usage[FixedAddressValueType] fields must be static⚠️✔️
MA0208Usage[FixedAddressValueType] fields must be value types⚠️✔️
MA0209PerformanceUse in keyword for in parameterℹ️✔️
MA0210PerformanceUse in keyword to call the in overloadℹ️✔️
MA0211StyleUse multi-line syntax for XML summary commentsℹ️✔️

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
MAS0004CA1507Suppress CA1507 on methods decorated with a [Newtonsoft.Json.JsonPropertyAttribute].

Refactorings

Name
ConvertToStringFormat
MakeInterpolatedString

Configuration

You can set the <MeziantouAnalysisMode> MSBuild property to configure the default severity of the rules. Supported values are:

  • Default: Uses each rule's default severity
  • None: Disables all rules by default
  • all-suggestions: Sets all rules to suggestion
  • all-warnings: Sets all rules to warning
  • all-errors: Sets all rules to error
<Project>
  <PropertyGroup>
    <MeziantouAnalysisMode>all-warnings</MeziantouAnalysisMode>
  </PropertyGroup>
</Project>