Add CultureInsensitiveAttribute to annotate culture-insensitive values - #1318
Merged
Merged
Conversation
`CultureInsensitiveTypeAttribute` can only mark a whole type, so there was no way to tell the analyzers that a specific value is culture insensitive while its type is culture sensitive. Add `Meziantou.Analyzer.Annotations.CultureInsensitiveAttribute`, which can be applied to a method (its return value), a property, a field, or a parameter. Annotated values are culture insensitive for MA0011, MA0075, MA0076 and MA0185. Members of another assembly can be annotated at the assembly level using their XML documentation id, like `DoNotIgnoreAttribute` does. When the id has no parameter list, all the overloads of the method are annotated. A parameter is culture insensitive in both directions: reading it in the method is not reported, and MA0075/MA0076 are not reported for the arguments provided by the callers, which covers a method formatting its argument with a fixed culture such as a wrapper around an interpolated string handler. Only the closest argument is considered, so a value nested in another invocation is still reported. The arguments of the `AppendFormatted` calls generated by the compiler are skipped during that walk, so an interpolated string handler parameter behaves like any other parameter. Related to #1316
…ters Applying the attribute on a method was equivalent to applying it on its return value, which is ambiguous: on a method, the attribute could as well mean that the method does not depend on the culture. The attribute marks a value, so it is now only valid on a property, a field, or a parameter, and the assembly-level annotation only accepts the documentation id of a property or a field.
This was referenced Aug 21, 2026
Closed
Closed
Bump Meziantou.Analyzer from 3.0.139 to 3.0.177
Analogy-LogViewer/Analogy.LogViewer.NLog.Targets#553
Closed
Merged
Closed
Bump Meziantou.Analyzer from 3.0.139 to 3.0.177
Analogy-LogViewer/Analogy.AspNetCore.LogProvider#540
Closed
Closed
Closed
Closed
Closed
Closed
Closed
Closed
This was referenced Sep 21, 2026
Closed
Closed
Bump Meziantou.Analyzer from 3.0.139 to 3.0.270
Analogy-LogViewer/Analogy.LogViewer.NLog.Targets#581
Closed
Closed
Bump Meziantou.Analyzer from 3.0.139 to 3.0.270
Analogy-LogViewer/Analogy.AspNetCore.LogProvider#562
Closed
Closed
Closed
Closed
Closed
Closed
Closed
Closed
chore(deps): Bump the nuget-minor-and-patch group with 2 updates
donislawdev/BetterWindowsServices#3
Merged
Closed
Closed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Add
Meziantou.Analyzer.Annotations.CultureInsensitiveAttribute. It marks the value of a property, a field, or a parameter as culture insensitive, even when its type is culture sensitive.Methods and return values are deliberately not valid targets: the attribute marks a value, not the way a method computes it, and
[CultureInsensitive]on a method would be ambiguous with "this method does not depend on the culture".Properties and fields of another assembly are annotated at the assembly level with their XML documentation id, like
DoNotIgnoreAttributedoes.A parameter is culture insensitive in both directions: reading it in the method is not reported, and MA0075/MA0076 are not reported for the arguments provided by the callers. This covers a method that formats its argument with a fixed culture, such as a wrapper around an interpolated string handler.
Why
CultureInsensitiveTypeAttributecan only mark a whole type, so there was no way to opt out for a single member whose type is culture sensitive, nor for a wrapper method that formats its arguments with a fixed culture (#1316).Notes for the reviewer
CultureSensitiveFormattingContext, so it applies to MA0011, MA0075, MA0076 and MA0185. For MA0185 it enables the simplification, as the annotated value counts as invariant.GetCultureSensitivity(IOperation)and are handled separately: interpolation holes andAppendFormattedcalls. The instance of aToString()call is handled as well, soinvariantProperty.ToString()is not reported.Log(double.Parse(text))is still reported.IsInCultureInsensitiveParameterContextstops at the closest enclosing argument, so annotating a parameter never silences a value nested in another invocation. The arguments of the compiler-generatedAppendFormattedcalls are walked through, so a handler parameter behaves like any other parameter.