Skip to content

Add CultureInsensitiveAttribute to annotate culture-insensitive values - #1318

Merged
meziantou merged 2 commits into
mainfrom
feature/meziantou-analyzer-1316-d6ad76
Aug 21, 2026
Merged

meziantou merged 2 commits into
mainfrom
feature/meziantou-analyzer-1316-d6ad76

Conversation

@meziantou

@meziantou meziantou commented Aug 20, 2026 •

Copy link
Copy Markdown
Owner

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.

class Sample
{
    [CultureInsensitive]
    public static double InvariantValue => 0;
}

_ = "value: " + Sample.InvariantValue;   // no MA0075
_ = $"{Sample.InvariantValue}";          // no MA0076
_ = Sample.InvariantValue.ToString();    // no MA0011

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 DoNotIgnoreAttribute does.

[assembly: CultureInsensitive("P:Sample.StringHelper.InvariantValue")]
[assembly: CultureInsensitive("F:Sample.StringHelper.InvariantField")]

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.

static void Log([CultureInsensitive] string message) { }

Log($"Value: {1.5}");  // no MA0076
Log(Format($"{1.5}")); // MA0076, the interpolated string is an argument of Format

Why

CultureInsensitiveTypeAttribute can 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

  • The annotation is resolved in CultureSensitiveFormattingContext, so it applies to MA0011, MA0075, MA0076 and MA0185. For MA0185 it enables the simplification, as the annotated value counts as invariant.
  • Two paths lose the symbol before reaching GetCultureSensitivity(IOperation) and are handled separately: interpolation holes and AppendFormatted calls. The instance of a ToString() call is handled as well, so invariantProperty.ToString() is not reported.
  • The call-site direction of a parameter annotation deliberately affects MA0075/MA0076 only. MA0011 flags how the argument was produced, which a parameter annotation cannot change, so Log(double.Parse(text)) is still reported.
  • IsInCultureInsensitiveParameterContext stops at the closest enclosing argument, so annotating a parameter never silences a value nested in another invocation. The arguments of the compiler-generated AppendFormatted calls are walked through, so a handler parameter behaves like any other parameter.
  • The constructor taking a documentation id is for assembly-level annotations only: an attribute with constructor arguments is ignored when applied on a member.

`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.
@meziantou
meziantou merged commit 22602be into main Aug 21, 2026
12 checks passed
@meziantou
meziantou deleted the feature/meziantou-analyzer-1316-d6ad76 branch August 21, 2026 03:39
This was referenced Aug 21, 2026
This was referenced Sep 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant